home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / gifheader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-12  |  2.6 KB  |  94 lines

  1. /*
  2.     gifHeader.c
  3.  
  4.     gifheader.c, gifbmap.c, and gif.h are based on "giftoppm"
  5.     of David Koblas.
  6.     They are modified by T. Ogihara. (1995)
  7. */
  8.  
  9. /* +-------------------------------------------------------------------+ */
  10. /* | Copyright 1990, David Koblas.                                     | */
  11. /* |   Permission to use, copy, modify, and distribute this software   | */
  12. /* |   and its documentation for any purpose and without fee is hereby | */
  13. /* |   granted, provided that the above copyright notice appear in all | */
  14. /* |   copies and that both that copyright notice and this permission  | */
  15. /* |   notice appear in supporting documentation.  This software is    | */
  16. /* |   provided "as is" without express or implied warranty.           | */
  17. /* +-------------------------------------------------------------------+ */
  18.  
  19. #include  <stdio.h>
  20. #include  "gif.h"
  21.  
  22. gifHeader *loadGifHeader(FILE *fd, int *errcode)
  23. {
  24.     unsigned char    buf[16];
  25.     gifHeader *gh;
  26.     int cc;
  27.  
  28.     if (! ReadOK(fd,buf,6)) {
  29.         *errcode = Err_FORMAT;
  30.         return NULL;
  31.     }
  32.     buf[6] = 0;
  33.     if (strcmp(buf,"GIF87a") != 0 && strcmp(buf,"GIF89a") != 0) {
  34.         *errcode = Err_FORMAT;
  35.         return NULL;
  36.     }
  37.     if ((gh = (gifHeader *)malloc(sizeof(gifHeader))) == NULL) {
  38.         *errcode = Err_MEMORY;
  39.         return NULL;
  40.     }
  41.     gh->width    = get_short(fd);
  42.     gh->height    = get_short(fd);
  43.     gh->colors    = 2 << ((cc = getc(fd)) & 0x07);
  44.     gh->Resolution    = ((cc & 0x70) >> 3) + 1;
  45.     gh->colormap    = BitSet(cc, LOCALCOLORMAP);
  46.     gh->interlace    = BitSet(cc, INTERLACE);
  47.     gh->Background    = getc(fd);
  48.     gh->AspectRatio    = getc(fd);
  49.     strcpy(gh->ver, buf+3);
  50.     gh->palette    = NULL;
  51.     gh->memo    = NULL;
  52.     return gh;
  53. }
  54.  
  55. void freeGifHeader(gifHeader *gh)
  56. {
  57.     if (gh) {
  58.         if (gh->palette) free((void *)gh->palette);
  59.         if (gh->memo) free((void *)gh->memo);
  60.         free((void *)gh);
  61.     }
  62. }
  63.  
  64. commonInfo *gifInfo(gifHeader *gh, int bits, BOOL mono)
  65.      /* Convert gif-header into commonInfo */
  66. {
  67.     commonInfo *cinf;
  68.     int i;
  69.  
  70.     if (gh == NULL) return NULL;
  71.     cinf = (commonInfo *)malloc(sizeof(commonInfo));
  72.     cinf->width  = gh->width;
  73.     cinf->height = gh->height;
  74.     cinf->type   = Type_gif;
  75.     cinf->bits   = bits;
  76.     cinf->alpha  = NO;
  77.     cinf->isplanar = YES;
  78.     cinf->numcolors = mono ? 1 : 3;
  79.     cinf->cspace = mono ? NX_OneIsWhiteColorSpace : NX_RGBColorSpace;
  80.     cinf->xbytes = byte_length(bits, gh->width);
  81.     cinf->palette = gh->palette;
  82.     gh->palette = NULL;
  83.     cinf->palsteps = gh->colors;
  84.     sprintf(cinf->memo, "%d x %d,  GIF%s%s  %d%s",
  85.         gh->width, gh->height, gh->ver, gh->interlace?":interlace":"",
  86.         gh->colors, mono?"steps":"colors");
  87.     if (gh->memo) {
  88.         strcat(cinf->memo, "  ");
  89.         i = strlen(cinf->memo);
  90.         strncat(cinf->memo, gh->memo, MAX_COMMENT - i - 2);
  91.     }
  92.     return cinf;
  93. }
  94.