home *** CD-ROM | disk | FTP | other *** search
- /*
- gifHeader.c
-
- gifheader.c, gifbmap.c, and gif.h are based on "giftoppm"
- of David Koblas.
- They are modified by T. Ogihara. (1995)
- */
-
- /* +-------------------------------------------------------------------+ */
- /* | Copyright 1990, David Koblas. | */
- /* | Permission to use, copy, modify, and distribute this software | */
- /* | and its documentation for any purpose and without fee is hereby | */
- /* | granted, provided that the above copyright notice appear in all | */
- /* | copies and that both that copyright notice and this permission | */
- /* | notice appear in supporting documentation. This software is | */
- /* | provided "as is" without express or implied warranty. | */
- /* +-------------------------------------------------------------------+ */
-
- #include <stdio.h>
- #include "gif.h"
- #include "strfunc.h"
-
- gifHeader *loadGifHeader(FILE *fd, int *errcode)
- {
- unsigned char buf[16];
- gifHeader *gh;
- int cc;
-
- if (! ReadOK(fd,buf,6)) {
- *errcode = Err_FORMAT;
- return NULL;
- }
- buf[6] = 0;
- if (strcmp(buf,"GIF87a") != 0 && strcmp(buf,"GIF89a") != 0) {
- *errcode = Err_FORMAT;
- return NULL;
- }
- if ((gh = (gifHeader *)malloc(sizeof(gifHeader))) == NULL) {
- *errcode = Err_MEMORY;
- return NULL;
- }
- gh->width = get_short(fd);
- gh->height = get_short(fd);
- gh->colors = 2 << ((cc = getc(fd)) & 0x07);
- gh->bits = 8;
- gh->Resolution = ((cc & 0x70) >> 3) + 1;
- gh->colormap = BitSet(cc, LOCALCOLORMAP);
- gh->transp = -1;
- gh->interlace = BitSet(cc, INTERLACE);
- gh->isgray = NO;
- gh->Background = getc(fd);
- gh->AspectRatio = getc(fd);
- strcpy(gh->ver, buf+3);
- // gh->palette = ...;
- gh->memo = NULL;
- return gh;
- }
-
- void freeGifHeader(gifHeader *gh)
- {
- if (gh) {
- if (gh->memo) free((void *)gh->memo);
- free((void *)gh);
- }
- }
-
- commonInfo *gifInfo(gifHeader *gh)
- /* Convert gif-header into commonInfo */
- {
- commonInfo *cinf;
- int i, pnum;
-
- if (gh == NULL) return NULL;
- cinf = (commonInfo *)malloc(sizeof(commonInfo));
- cinf->width = gh->width;
- cinf->height = gh->height;
- cinf->type = Type_gif;
- cinf->bits = gh->bits;
- cinf->alpha = (gh->transp >= 0);
- cinf->isplanar = YES;
- cinf->numcolors = gh->isgray ? 1 : 3;
- cinf->cspace = gh->isgray ? NX_OneIsWhiteColorSpace : NX_RGBColorSpace;
- cinf->xbytes = byte_length(gh->bits, gh->width);
- pnum = gh->colors + ((gh->transp >= 0) ? 1 : 0);
- if (pnum > 256) pnum = 256;
- cinf->palette = copyPalette(gh->palette, pnum);
-
- /* Make the index of transparence the last entry of palette */
- if (gh->transp >= 0) {
- cinf->palsteps = --pnum;
- if (gh->transp != pnum) {
- unsigned char *p, *q;
- p = gh->palette[pnum];
- q = cinf->palette[gh->transp];
- for (i = 0; i < 3; i++)
- q[i] = p[i];
- p = cinf->palette[pnum];
- for (i = 0; i < 3; i++)
- p[i] = 255;
- }
- }else
- cinf->palsteps = gh->colors;
- sprintf(cinf->memo, "%d x %d GIF%s%s%s %d%s",
- gh->width, gh->height, gh->ver,
- gh->interlace ? " Interlace" : "",
- (gh->transp >= 0) ? " Alpha" : "",
- gh->colors, gh->isgray?"steps":"colors");
- if (gh->memo) {
- strcat(cinf->memo, " : ");
- comm_cat(cinf->memo, gh->memo);
- }
- return cinf;
- }
-