1 /******************************************************************************
2 * "Gif-Lib" - Yet another gif library. *
4 * Written by: Gershon Elber IBM PC Ver 1.1, Aug. 1990 *
5 *******************************************************************************
6 * The kernel of the GIF Decoding process can be found here. *
7 *******************************************************************************
9 * 16 Jun 89 - Version 1.0 by Gershon Elber. *
10 * 3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names). *
11 * 19 Feb 98 - Version 1.2 by Jareth Hein (Support for user specified I/O) *
12 ******************************************************************************/
20 #include <sys/types.h>
22 #endif /* __MSDOS__ */
34 #define PROGRAM_NAME "GIFLIB"
37 static void DGifGetWord(GifFileType *GifFile, int *Word);
38 static void DGifSetupDecompress(GifFileType *GifFile);
39 static void DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line,
41 static int DGifGetPrefixChar(unsigned int *Prefix, int Code, int ClearCode);
42 static void DGifDecompressInput(GifFileType *GifFile, int *Code);
43 static void DGifBufferedInput(GifFileType *GifFile, GifByteType *NextByte);
45 /******************************************************************************
46 * Open a new gif file for read, given by its name. *
47 * Returns GifFileType pointer dynamically allocated which serves as the gif *
49 ******************************************************************************/
50 void DGifOpenFileName(GifFileType *GifFile, const char *FileName)
54 if ((f = fopen(FileName,
59 #endif /* __MSDOS__ */
61 GifInternError(GifFile, D_GIF_ERR_OPEN_FAILED);
63 GifStdIOInit(GifFile, f, -1);
64 DGifInitRead(GifFile);
67 /******************************************************************************
68 * Update a new gif file, given its file handle. *
69 * Returns GifFileType pointer dynamically allocated which serves as the gif *
71 ******************************************************************************/
72 void DGifOpenFileHandle(GifFileType *GifFile, int FileHandle)
77 setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */
78 f = fdopen(FileHandle, "rb"); /* Make it into a stream: */
79 setvbuf(f, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE);/* And inc. stream buffer.*/
81 f = fdopen(FileHandle, "r"); /* Make it into a stream: */
82 #endif /* __MSDOS__ */
84 GifStdIOInit(GifFile, f, -1);
85 DGifInitRead(GifFile);
88 /******************************************************************************
89 * Update a new gif file, given its file handle. *
90 * Returns GifFileType pointer dynamically allocated which serves as the gif *
91 * info record. _GifError is cleared if succesfull. *
92 ******************************************************************************/
93 void DGifInitRead(GifFileType *GifFile)
95 GifByteType Buf[GIF_STAMP_LEN+1];
96 GifFilePrivateType *Private;
98 if ((Private = (GifFilePrivateType *) malloc(sizeof(GifFilePrivateType)))
100 GifInternError(GifFile, D_GIF_ERR_NOT_ENOUGH_MEM);
102 memset(Private, '\0', sizeof(GifFilePrivateType));
103 GifFile->Private = (VoidPtr) Private;
105 Private->FileState = 0; /* Make sure bit 0 = 0 (File open for read). */
107 /* Lets see if this is a GIF file: */
108 GifRead(Buf, GIF_STAMP_LEN, GifFile);
110 /* The GIF Version number is ignored at this time. Maybe we should do */
111 /* something more useful with it. */
112 Buf[GIF_STAMP_LEN] = 0;
113 if (strncmp(GIF_STAMP, (const char *) Buf, GIF_VERSION_POS) != 0) {
114 GifInternError(GifFile, D_GIF_ERR_NOT_GIF_FILE);
117 DGifGetScreenDesc(GifFile);
120 /******************************************************************************
121 * This routine should be called before any other DGif calls. Note that *
122 * this routine is called automatically from DGif file open routines. *
123 ******************************************************************************/
124 void DGifGetScreenDesc(GifFileType *GifFile)
128 GifFilePrivateType *Private = (GifFilePrivateType*) GifFile->Private;
130 if (!IS_READABLE(Private)) {
131 /* This file was NOT open for reading: */
132 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
135 /* Put the screen descriptor into the file: */
136 DGifGetWord(GifFile, &GifFile->SWidth);
137 DGifGetWord(GifFile, &GifFile->SHeight);
139 GifRead(Buf, 3, GifFile);
140 GifFile->SColorResolution = (((Buf[0] & 0x70) + 1) >> 4) + 1;
141 BitsPerPixel = (Buf[0] & 0x07) + 1;
142 GifFile->SBackGroundColor = Buf[1];
143 if (Buf[0] & 0x80) { /* Do we have global color map? */
145 GifFile->SColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
147 /* Get the global color map: */
148 for (i = 0; i < GifFile->SColorMap->ColorCount; i++) {
149 GifRead(Buf, 3, GifFile);
150 GifFile->SColorMap->Colors[i].Red = Buf[0];
151 GifFile->SColorMap->Colors[i].Green = Buf[1];
152 GifFile->SColorMap->Colors[i].Blue = Buf[2];
155 /* We should always have a colormap */
156 GifFile->SColorMap = MakeMapObject(2, NULL);
157 GifFile->SColorMap->Colors[0].Red = 0;
158 GifFile->SColorMap->Colors[0].Green = 0;
159 GifFile->SColorMap->Colors[0].Blue = 0;
160 GifFile->SColorMap->Colors[1].Red = 0xff;
161 GifFile->SColorMap->Colors[1].Green = 0xff;
162 GifFile->SColorMap->Colors[1].Blue = 0xff;
166 /******************************************************************************
167 * This routine should be called before any attemp to read an image. *
168 ******************************************************************************/
169 void DGifGetRecordType(GifFileType *GifFile, GifRecordType *Type)
172 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
174 if (!IS_READABLE(Private)) {
175 /* This file was NOT open for reading: */
176 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
179 GifRead(&Buf, 1, GifFile);
183 *Type = IMAGE_DESC_RECORD_TYPE;
186 *Type = EXTENSION_RECORD_TYPE;
189 *Type = TERMINATE_RECORD_TYPE;
192 *Type = UNDEFINED_RECORD_TYPE;
193 GifInternError(GifFile, D_GIF_ERR_WRONG_RECORD);
197 /******************************************************************************
198 * This routine should be called before any attemp to read an image. *
199 * Note it is assumed the Image desc. header (',') has been read. *
200 ******************************************************************************/
201 void DGifGetImageDesc(GifFileType *GifFile)
205 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
207 if (!IS_READABLE(Private)) {
208 /* This file was NOT open for reading: */
209 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
212 DGifGetWord(GifFile, &GifFile->Image.Left);
213 DGifGetWord(GifFile, &GifFile->Image.Top);
214 DGifGetWord(GifFile, &GifFile->Image.Width);
215 DGifGetWord(GifFile, &GifFile->Image.Height);
217 GifRead(Buf, 1, GifFile);
218 BitsPerPixel = (Buf[0] & 0x07) + 1;
219 GifFile->Image.Interlace = (Buf[0] & 0x40);
220 if (Buf[0] & 0x80) { /* Does this image have local color map? */
222 if (GifFile->Image.ColorMap && GifFile->SavedImages == NULL)
223 FreeMapObject(GifFile->Image.ColorMap);
225 GifFile->Image.ColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
227 /* Get the image local color map: */
228 for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) {
229 GifRead(Buf, 3, GifFile);
230 GifFile->Image.ColorMap->Colors[i].Red = Buf[0];
231 GifFile->Image.ColorMap->Colors[i].Green = Buf[1];
232 GifFile->Image.ColorMap->Colors[i].Blue = Buf[2];
236 if (GifFile->SavedImages) {
239 if ((GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages,
240 sizeof(SavedImage) * (GifFile->ImageCount + 1))) == NULL) {
241 GifInternError(GifFile, D_GIF_ERR_NOT_ENOUGH_MEM);
244 sp = &GifFile->SavedImages[GifFile->ImageCount];
245 memcpy(&sp->ImageDesc, &GifFile->Image, sizeof(GifImageDesc));
246 if (GifFile->Image.ColorMap)
248 sp->ImageDesc.ColorMap =
249 MakeMapObject (GifFile->Image.ColorMap->ColorCount,
250 GifFile->Image.ColorMap->Colors);
252 sp->RasterBits = NULL;
253 sp->ExtensionBlockCount = 0;
254 sp->ExtensionBlocks = (ExtensionBlock *)NULL;
257 GifFile->ImageCount++;
259 Private->PixelCount = (long) GifFile->Image.Width *
260 (long) GifFile->Image.Height;
262 DGifSetupDecompress(GifFile); /* Reset decompress algorithm parameters. */
265 /******************************************************************************
266 * Get one full scanned line (Line) of length LineLen from GIF file. *
267 ******************************************************************************/
268 void DGifGetLine(GifFileType *GifFile, GifPixelType *Line, int LineLen)
271 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
273 if (!IS_READABLE(Private)) {
274 /* This file was NOT open for reading: */
275 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
278 if (!LineLen) LineLen = GifFile->Image.Width;
280 #if defined(__MSDOS__) || defined(__GNUC__)
281 if ((Private->PixelCount -= LineLen) > 0xffff0000UL)
283 if ((Private->PixelCount -= LineLen) > 0xffff0000)
284 #endif /* __MSDOS__ */
286 GifInternError(GifFile, D_GIF_ERR_DATA_TOO_BIG);
289 DGifDecompressLine(GifFile, Line, LineLen);
290 if (Private->PixelCount == 0) {
291 /* We probably would not be called any more, so lets clean */
292 /* everything before we return: need to flush out all rest of */
293 /* image until empty block (size 0) detected. We use GetCodeNext.*/
295 DGifGetCodeNext(GifFile, &Dummy);
296 while (Dummy != NULL);
300 /******************************************************************************
301 * Put one pixel (Pixel) into GIF file. *
302 ******************************************************************************/
303 void DGifGetPixel(GifFileType *GifFile, GifPixelType Pixel)
306 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
308 if (!IS_READABLE(Private)) {
309 /* This file was NOT open for reading: */
310 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
313 #if defined(__MSDOS__) || defined(__GNUC__)
314 if (--Private->PixelCount > 0xffff0000UL)
316 if (--Private->PixelCount > 0xffff0000)
317 #endif /* __MSDOS__ */
319 GifInternError(GifFile, D_GIF_ERR_DATA_TOO_BIG);
322 DGifDecompressLine(GifFile, &Pixel, 1);
323 if (Private->PixelCount == 0) {
324 /* We probably would not be called any more, so lets clean */
325 /* everything before we return: need to flush out all rest of */
326 /* image until empty block (size 0) detected. We use GetCodeNext.*/
328 DGifGetCodeNext(GifFile, &Dummy);
329 while (Dummy != NULL);
333 /******************************************************************************
334 * Get an extension block (see GIF manual) from gif file. This routine only *
335 * returns the first data block, and DGifGetExtensionNext shouldbe called *
336 * after this one until NULL extension is returned. *
337 * The Extension should NOT be freed by the user (not dynamically allocated).*
338 * Note it is assumed the Extension desc. header ('!') has been read. *
339 ******************************************************************************/
340 void DGifGetExtension(GifFileType *GifFile, int *ExtCode,
341 GifByteType **Extension)
344 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
346 if (!IS_READABLE(Private)) {
347 /* This file was NOT open for reading: */
348 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
351 GifRead(&Buf, 1, GifFile);
354 DGifGetExtensionNext(GifFile, Extension);
357 /******************************************************************************
358 * Get a following extension block (see GIF manual) from gif file. This *
359 * routine sould be called until NULL Extension is returned. *
360 * The Extension should NOT be freed by the user (not dynamically allocated).*
361 ******************************************************************************/
362 void DGifGetExtensionNext(GifFileType *GifFile, GifByteType **Extension)
365 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
367 GifRead(&Buf, 1, GifFile);
369 *Extension = Private->Buf; /* Use private unused buffer. */
370 (*Extension)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
371 GifRead(&((*Extension)[1]), Buf, GifFile);
377 /******************************************************************************
378 * This routine should be called second to last, to close the GIF file. *
379 ******************************************************************************/
380 int DGifCloseFile(GifFileType *GifFile)
382 GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
384 if (GifFile == NULL) return -1;
386 if (!IS_READABLE(Private))
388 /* This file was NOT open for reading: */
389 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
392 if (GifClose (GifFile))
394 GifInternError(GifFile, D_GIF_ERR_CLOSE_FAILED);
399 /******************************************************************************
400 * Get 2 bytes (word) from the given file: *
401 ******************************************************************************/
402 static void DGifGetWord(GifFileType *GifFile, int *Word)
406 GifRead(c, 2, GifFile);
408 *Word = (((unsigned int) c[1]) << 8) + c[0];
411 /******************************************************************************
412 * Get the image code in compressed form. his routine can be called if the *
413 * information needed to be piped out as is. Obviously this is much faster *
414 * than decoding and encoding again. This routine should be followed by calls *
415 * to DGifGetCodeNext, until NULL block is returned. *
416 * The block should NOT be freed by the user (not dynamically allocated). *
417 ******************************************************************************/
418 void DGifGetCode(GifFileType *GifFile, int *CodeSize, GifByteType **CodeBlock)
420 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
422 if (!IS_READABLE(Private)) {
423 /* This file was NOT open for reading: */
424 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
427 *CodeSize = Private->BitsPerPixel;
429 DGifGetCodeNext(GifFile, CodeBlock);
432 /******************************************************************************
433 * Continue to get the image code in compressed form. This routine should be *
434 * called until NULL block is returned. *
435 * The block should NOT be freed by the user (not dynamically allocated). *
436 ******************************************************************************/
437 void DGifGetCodeNext(GifFileType *GifFile, GifByteType **CodeBlock)
440 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
442 GifRead(&Buf, 1, GifFile);
445 *CodeBlock = Private->Buf; /* Use private unused buffer. */
446 (*CodeBlock)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
447 GifRead(&((*CodeBlock)[1]), Buf, GifFile);
451 Private->Buf[0] = 0; /* Make sure the buffer is empty! */
452 Private->PixelCount = 0; /* And local info. indicate image read. */
457 /******************************************************************************
458 * Setup the LZ decompression for this image: *
459 ******************************************************************************/
460 static void DGifSetupDecompress(GifFileType *GifFile)
463 GifByteType CodeSize;
464 unsigned int *Prefix;
465 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
467 GifRead(&CodeSize, 1, GifFile); /* Read Code size from file. */
468 BitsPerPixel = CodeSize;
470 Private->Buf[0] = 0; /* Input Buffer empty. */
471 Private->BitsPerPixel = BitsPerPixel;
472 Private->ClearCode = (1 << BitsPerPixel);
473 Private->EOFCode = Private->ClearCode + 1;
474 Private->RunningCode = Private->EOFCode + 1;
475 Private->RunningBits = BitsPerPixel + 1; /* Number of bits per code. */
476 Private->MaxCode1 = 1 << Private->RunningBits; /* Max. code + 1. */
477 Private->StackPtr = 0; /* No pixels on the pixel stack. */
478 Private->LastCode = NO_SUCH_CODE;
479 Private->CrntShiftState = 0; /* No information in CrntShiftDWord. */
480 Private->CrntShiftDWord = 0;
482 Prefix = Private->Prefix;
483 for (i = 0; i <= LZ_MAX_CODE; i++) Prefix[i] = NO_SUCH_CODE;
486 /******************************************************************************
487 * The LZ decompression routine: *
488 * This version decompress the given gif file into Line of length LineLen. *
489 * This routine can be called few times (one per scan line, for example), in *
490 * order the complete the whole image. *
491 ******************************************************************************/
492 static void DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line,
495 int i = 0, j, CrntCode, EOFCode, ClearCode, CrntPrefix, LastCode, StackPtr;
496 GifByteType *Stack, *Suffix;
497 unsigned int *Prefix;
498 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
500 StackPtr = Private->StackPtr;
501 Prefix = Private->Prefix;
502 Suffix = Private->Suffix;
503 Stack = Private->Stack;
504 EOFCode = Private->EOFCode;
505 ClearCode = Private->ClearCode;
506 LastCode = Private->LastCode;
510 /* Let pop the stack off before continueing to read the gif file: */
511 while (StackPtr != 0 && i < LineLen) Line[i++] = Stack[--StackPtr];
514 while (i < LineLen) { /* Decode LineLen items. */
515 DGifDecompressInput(GifFile, &CrntCode);
517 if (CrntCode == EOFCode) {
518 /* Note however that usually we will not be here as we will stop */
519 /* decoding as soon as we got all the pixel, or EOF code will */
520 /* not be read at all, and DGifGetLine/Pixel clean everything. */
521 if (i != LineLen - 1 || Private->PixelCount != 0) {
522 GifInternError(GifFile, D_GIF_ERR_EOF_TOO_SOON);
526 else if (CrntCode == ClearCode) {
527 /* We need to start over again: */
528 for (j = 0; j <= LZ_MAX_CODE; j++) Prefix[j] = NO_SUCH_CODE;
529 Private->RunningCode = Private->EOFCode + 1;
530 Private->RunningBits = Private->BitsPerPixel + 1;
531 Private->MaxCode1 = 1 << Private->RunningBits;
532 LastCode = Private->LastCode = NO_SUCH_CODE;
535 /* Its regular code - if in pixel range simply add it to output */
536 /* stream, otherwise trace to codes linked list until the prefix */
537 /* is in pixel range: */
538 if (CrntCode < ClearCode) {
539 /* This is simple - its pixel scalar, so add it to output: */
540 Line[i++] = CrntCode;
543 /* Its a code to needed to be traced: trace the linked list */
544 /* until the prefix is a pixel, while pushing the suffix */
545 /* pixels on our stack. If we done, pop the stack in reverse */
546 /* (thats what stack is good for!) order to output. */
547 if (Prefix[CrntCode] == NO_SUCH_CODE) {
548 /* Only allowed if CrntCode is exactly the running code: */
549 /* In that case CrntCode = XXXCode, CrntCode or the */
550 /* prefix code is last code and the suffix char is */
551 /* exactly the prefix of last code! */
552 if (CrntCode == Private->RunningCode - 2) {
553 CrntPrefix = LastCode;
554 Suffix[Private->RunningCode - 2] =
555 Stack[StackPtr++] = DGifGetPrefixChar(Prefix,
556 LastCode, ClearCode);
559 GifInternError(GifFile, D_GIF_ERR_IMAGE_DEFECT);
563 CrntPrefix = CrntCode;
565 /* Now (if image is O.K.) we should not get an NO_SUCH_CODE */
566 /* During the trace. As we might loop forever, in case of */
567 /* defective image, we count the number of loops we trace */
568 /* and stop if we got LZ_MAX_CODE. obviously we can not */
569 /* loop more than that. */
571 while (j++ <= LZ_MAX_CODE &&
572 CrntPrefix > ClearCode &&
573 CrntPrefix <= LZ_MAX_CODE) {
574 Stack[StackPtr++] = Suffix[CrntPrefix];
575 CrntPrefix = Prefix[CrntPrefix];
577 if (j >= LZ_MAX_CODE || CrntPrefix > LZ_MAX_CODE) {
578 GifInternError(GifFile, D_GIF_ERR_IMAGE_DEFECT);
580 /* Push the last character on stack: */
581 Stack[StackPtr++] = CrntPrefix;
583 /* Now lets pop all the stack into output: */
584 while (StackPtr != 0 && i < LineLen)
585 Line[i++] = Stack[--StackPtr];
587 if (LastCode != NO_SUCH_CODE) {
588 Prefix[Private->RunningCode - 2] = LastCode;
590 if (CrntCode == Private->RunningCode - 2) {
591 /* Only allowed if CrntCode is exactly the running code: */
592 /* In that case CrntCode = XXXCode, CrntCode or the */
593 /* prefix code is last code and the suffix char is */
594 /* exactly the prefix of last code! */
595 Suffix[Private->RunningCode - 2] =
596 DGifGetPrefixChar(Prefix, LastCode, ClearCode);
599 Suffix[Private->RunningCode - 2] =
600 DGifGetPrefixChar(Prefix, CrntCode, ClearCode);
607 Private->LastCode = LastCode;
608 Private->StackPtr = StackPtr;
611 /******************************************************************************
612 * Routine to trace the Prefixes linked list until we get a prefix which is *
613 * not code, but a pixel value (less than ClearCode). Returns that pixel value.*
614 * If image is defective, we might loop here forever, so we limit the loops to *
615 * the maximum possible if image O.k. - LZ_MAX_CODE times. *
616 ******************************************************************************/
617 static int DGifGetPrefixChar(unsigned int *Prefix, int Code, int ClearCode)
621 while (Code > ClearCode && i++ <= LZ_MAX_CODE) Code = Prefix[Code];
625 /******************************************************************************
626 * Interface for accessing the LZ codes directly. Set Code to the real code *
627 * (12bits), or to -1 if EOF code is returned. *
628 ******************************************************************************/
629 void DGifGetLZCodes(GifFileType *GifFile, int *Code)
631 GifByteType *CodeBlock;
632 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
634 if (!IS_READABLE(Private)) {
635 /* This file was NOT open for reading: */
636 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
639 DGifDecompressInput(GifFile, Code);
641 if (*Code == Private->EOFCode) {
642 /* Skip rest of codes (hopefully only NULL terminating block): */
644 DGifGetCodeNext(GifFile, &CodeBlock);
645 while (CodeBlock != NULL);
649 else if (*Code == Private->ClearCode) {
650 /* We need to start over again: */
651 Private->RunningCode = Private->EOFCode + 1;
652 Private->RunningBits = Private->BitsPerPixel + 1;
653 Private->MaxCode1 = 1 << Private->RunningBits;
657 /******************************************************************************
658 * The LZ decompression input routine: *
659 * This routine is responsable for the decompression of the bit stream from *
660 * 8 bits (bytes) packets, into the real codes. *
661 * Returns GIF_OK if read succesfully. *
662 ******************************************************************************/
663 static void DGifDecompressInput(GifFileType *GifFile, int *Code)
665 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
666 GifByteType NextByte;
667 static unsigned int CodeMasks[] = {
668 0x0000, 0x0001, 0x0003, 0x0007,
669 0x000f, 0x001f, 0x003f, 0x007f,
670 0x00ff, 0x01ff, 0x03ff, 0x07ff,
674 while (Private->CrntShiftState < Private->RunningBits) {
675 /* Needs to get more bytes from input stream for next code: */
676 DGifBufferedInput(GifFile, &NextByte);
677 Private->CrntShiftDWord |=
678 ((unsigned long) NextByte) << Private->CrntShiftState;
679 Private->CrntShiftState += 8;
681 *Code = Private->CrntShiftDWord & CodeMasks[Private->RunningBits];
683 Private->CrntShiftDWord >>= Private->RunningBits;
684 Private->CrntShiftState -= Private->RunningBits;
686 /* If code cannt fit into RunningBits bits, must raise its size. Note */
687 /* however that codes above 4095 are used for special signaling. */
688 if (++Private->RunningCode > Private->MaxCode1 &&
689 Private->RunningBits < LZ_BITS) {
690 Private->MaxCode1 <<= 1;
691 Private->RunningBits++;
695 /******************************************************************************
696 * This routines read one gif data block at a time and buffers it internally *
697 * so that the decompression routine could access it. *
698 * The routine returns the next byte from its internal buffer (or read next *
699 * block in if buffer empty) *
700 ******************************************************************************/
701 static void DGifBufferedInput(GifFileType *GifFile, GifByteType *NextByte)
703 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
704 GifByteType *Buf = Private->Buf;
707 /* Needs to read the next buffer - this one is empty: */
708 GifRead(Buf, 1, GifFile);
709 GifRead((Buf + 1), Buf[0], GifFile);
711 Buf[1] = 2; /* We use now the second place as last char read! */
715 *NextByte = Buf[Buf[1]++];
720 /******************************************************************************
721 * This routine reads an entire GIF into core, hanging all its state info off *
722 * the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle() *
723 * first to initialize I/O. Its inverse is EGifSpew(). *
724 ******************************************************************************/
725 void DGifSlurp(GifFileType *GifFile)
728 GifRecordType RecordType;
730 GifByteType *ExtData;
732 /* Some versions of malloc dislike 0-length requests */
733 GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
734 memset(GifFile->SavedImages, 0, sizeof(SavedImage));
735 sp = &GifFile->SavedImages[0];
738 DGifGetRecordType(GifFile, &RecordType);
740 switch (RecordType) {
741 case IMAGE_DESC_RECORD_TYPE:
742 DGifGetImageDesc(GifFile);
744 sp = &GifFile->SavedImages[GifFile->ImageCount-1];
745 ImageSize = sp->ImageDesc.Width * sp->ImageDesc.Height;
748 = (GifPixelType*) malloc (ImageSize * sizeof(GifPixelType));
750 DGifGetLine(GifFile, sp->RasterBits, ImageSize);
753 case EXTENSION_RECORD_TYPE:
754 DGifGetExtension(GifFile,&sp->Function,&ExtData);
757 if (AddExtensionBlock(sp, ExtData[0], ExtData+1) == GIF_ERROR)
758 GifInternError(GifFile, D_GIF_ERR_NOT_ENOUGH_MEM);
759 DGifGetExtensionNext(GifFile, &ExtData);
760 } while (ExtData != NULL);
763 case TERMINATE_RECORD_TYPE:
766 default: /* Should be trapped by DGifGetRecordType */
769 } while (RecordType != TERMINATE_RECORD_TYPE);
772 /******************************************************************************
773 * Extension record functions *
774 ******************************************************************************/
776 void MakeExtension(SavedImage *New, int Function)
778 New->Function = Function;
780 * Someday we might have to deal with multiple extensions.
784 int AddExtensionBlock(SavedImage *New, int Len, GifByteType *data)
789 if (New->ExtensionBlocks == NULL)
790 New->ExtensionBlocks = (ExtensionBlock *)malloc(sizeof(ExtensionBlock));
792 New->ExtensionBlocks =
793 (ExtensionBlock *)realloc(New->ExtensionBlocks,
794 sizeof(ExtensionBlock) * (New->ExtensionBlockCount + 1));
796 if (New->ExtensionBlocks == NULL)
799 ep = &New->ExtensionBlocks[New->ExtensionBlockCount++];
801 size = Len * sizeof(GifByteType);
802 ep->Bytes = (GifByteType *)malloc(size);
803 memcpy(ep->Bytes, data, size);
807 void FreeExtension(SavedImage *Image)
811 for (ep = Image->ExtensionBlocks;
812 ep < Image->ExtensionBlocks + Image->ExtensionBlockCount;
814 (void) free((char *)ep->Bytes);
815 free((char *)Image->ExtensionBlocks);
816 Image->ExtensionBlocks = NULL;
819 /******************************************************************************
820 * Image block allocation functions *
821 ******************************************************************************/
822 SavedImage *MakeSavedImage(GifFileType *GifFile, SavedImage *CopyFrom)
824 * Append an image block to the SavedImages array
829 if (GifFile->SavedImages == NULL)
830 GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
832 GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages,
833 sizeof(SavedImage) * (GifFile->ImageCount+1));
835 if (GifFile->SavedImages == NULL)
836 return((SavedImage *)NULL);
839 sp = &GifFile->SavedImages[GifFile->ImageCount++];
840 memset((char *)sp, '\0', sizeof(SavedImage));
844 memcpy((char *)sp, CopyFrom, sizeof(SavedImage));
847 * Make our own allocated copies of the heap fields in the
848 * copied record. This guards against potential aliasing
852 /* first, the local color map */
853 if (sp->ImageDesc.ColorMap)
854 sp->ImageDesc.ColorMap =
855 MakeMapObject(CopyFrom->ImageDesc.ColorMap->ColorCount,
856 CopyFrom->ImageDesc.ColorMap->Colors);
858 /* next, the raster */
859 sp->RasterBits = (GifPixelType *) malloc(sizeof(GifPixelType)
860 * CopyFrom->ImageDesc.Height
861 * CopyFrom->ImageDesc.Width);
862 memcpy(sp->RasterBits,
863 CopyFrom->RasterBits,
865 * CopyFrom->ImageDesc.Height
866 * CopyFrom->ImageDesc.Width);
868 /* finally, the extension blocks */
869 if (sp->ExtensionBlocks)
872 = (ExtensionBlock*)malloc(sizeof(ExtensionBlock)
873 * CopyFrom->ExtensionBlockCount);
874 memcpy(sp->ExtensionBlocks,
875 CopyFrom->ExtensionBlocks,
876 sizeof(ExtensionBlock)
877 * CopyFrom->ExtensionBlockCount);
880 * For the moment, the actual blocks can take their
881 * chances with free(). We'll fix this later.
890 void FreeSavedImages(GifFileType *GifFile)
894 for (sp = GifFile->SavedImages;
895 sp < GifFile->SavedImages + GifFile->ImageCount;
898 if (sp->ImageDesc.ColorMap)
899 FreeMapObject(sp->ImageDesc.ColorMap);
902 free((char *)sp->RasterBits);
904 if (sp->ExtensionBlocks)
907 free((char *) GifFile->SavedImages);
910 /******************************************************************************
911 * Miscellaneous utility functions *
912 ******************************************************************************/
914 static int BitSize(int n)
915 /* return smallest bitfield size n will fit in */
919 for (i = 1; i <= 8; i++)
925 /******************************************************************************
926 * Color map object functions *
927 ******************************************************************************/
929 ColorMapObject *MakeMapObject(int ColorCount, GifColorType *ColorMap)
931 * Allocate a color map of given size; initialize with contents of
932 * ColorMap if that pointer is non-NULL.
935 ColorMapObject *Object;
937 if (ColorCount != (1 << BitSize(ColorCount)))
938 return((ColorMapObject *)NULL);
940 Object = (ColorMapObject *)malloc(sizeof(ColorMapObject));
941 if (Object == (ColorMapObject *)NULL)
942 return((ColorMapObject *)NULL);
944 Object->Colors = (GifColorType *)calloc(ColorCount, sizeof(GifColorType));
945 if (Object->Colors == (GifColorType *)NULL)
946 return((ColorMapObject *)NULL);
948 Object->ColorCount = ColorCount;
949 Object->BitsPerPixel = BitSize(ColorCount);
952 memcpy((char *)Object->Colors,
953 (char *)ColorMap, ColorCount * sizeof(GifColorType));
958 void FreeMapObject(ColorMapObject *Object)
960 * Free a color map object
963 free(Object->Colors);