home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Graphics / ToyViewer-2.6a / src / gifheader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-28  |  3.1 KB  |  114 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. #include  "strfunc.h"
  22.  
  23. gifHeader *loadGifHeader(FILE *fd, int *errcode)
  24. {
  25.     unsigned char    buf[16];
  26.     gifHeader *gh;
  27.     int cc;
  28.  
  29.     if (! ReadOK(fd,buf,6)) {
  30.         *errcode = Err_FORMAT;
  31.         return NULL;
  32.     }
  33.     buf[6] = 0;
  34.     if (strcmp(buf,"GIF87a") != 0 && strcmp(buf,"GIF89a") != 0) {
  35.         *errcode = Err_FORMAT;
  36.         return NULL;
  37.     }
  38.     if ((gh = (gifHeader *)malloc(sizeof(gifHeader))) == NULL) {
  39.         *errcode = Err_MEMORY;
  40.         return NULL;
  41.     }
  42.     gh->width    = get_short(fd);
  43.     gh->height    = get_short(fd);
  44.     gh->colors    = 2 << ((cc = getc(fd)) & 0x07);
  45.     gh->bits    = 8;
  46.     gh->Resolution    = ((cc & 0x70) >> 3) + 1;
  47.     gh->colormap    = BitSet(cc, LOCALCOLORMAP);
  48.     gh->transp    = -1;
  49.     gh->interlace    = BitSet(cc, INTERLACE);
  50.     gh->isgray    = NO;
  51.     gh->Background    = getc(fd);
  52.     gh->AspectRatio    = getc(fd);
  53.     strcpy(gh->ver, buf+3);
  54.     // gh->palette    = ...;
  55.     gh->memo    = NULL;
  56.     return gh;
  57. }
  58.  
  59. void freeGifHeader(gifHeader *gh)
  60. {
  61.     if (gh) {
  62.         if (gh->memo) free((void *)gh->memo);
  63.         free((void *)gh);
  64.     }
  65. }
  66.  
  67. commonInfo *gifInfo(gifHeader *gh)
  68.      /* Convert gif-header into commonInfo */
  69. {
  70.     commonInfo *cinf;
  71.     int i, pnum;
  72.  
  73.     if (gh == NULL) return NULL;
  74.     cinf = (commonInfo *)malloc(sizeof(commonInfo));
  75.     cinf->width  = gh->width;
  76.     cinf->height = gh->height;
  77.     cinf->type   = Type_gif;
  78.     cinf->bits   = gh->bits;
  79.     cinf->alpha  = (gh->transp >= 0);
  80.     cinf->isplanar = YES;
  81.     cinf->numcolors = gh->isgray ? 1 : 3;
  82.     cinf->cspace = gh->isgray ? NX_OneIsWhiteColorSpace : NX_RGBColorSpace;
  83.     cinf->xbytes = byte_length(gh->bits, gh->width);
  84.     pnum = gh->colors + ((gh->transp >= 0) ? 1 : 0);
  85.     if (pnum > 256) pnum = 256;
  86.     cinf->palette = copyPalette(gh->palette, pnum);
  87.  
  88.     /* Make the index of transparence the last entry of palette */
  89.     if (gh->transp >= 0) {
  90.         cinf->palsteps = --pnum;
  91.         if (gh->transp != pnum) {
  92.             unsigned char *p, *q;
  93.             p = gh->palette[pnum];
  94.             q = cinf->palette[gh->transp];
  95.             for (i = 0; i < 3; i++)
  96.                 q[i] = p[i];
  97.             p = cinf->palette[pnum];
  98.             for (i = 0; i < 3; i++)
  99.                 p[i] = 255;
  100.         }
  101.     }else
  102.         cinf->palsteps = gh->colors;
  103.     sprintf(cinf->memo, "%d x %d  GIF%s%s%s  %d%s",
  104.         gh->width, gh->height, gh->ver,
  105.         gh->interlace ? " Interlace" : "",
  106.         (gh->transp >= 0) ? " Alpha" : "",
  107.         gh->colors, gh->isgray?"steps":"colors");
  108.     if (gh->memo) {
  109.         strcat(cinf->memo, " : ");
  110.         comm_cat(cinf->memo, gh->memo);
  111.     }
  112.     return cinf;
  113. }
  114.