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 ******************************************************************************/
21 #define PROGRAM_NAME "GIFLIB"
24 static void DGifGetWord(GifFileType *GifFile, int *Word);
25 static void DGifSetupDecompress(GifFileType *GifFile);
26 static void DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line,
28 static int DGifGetPrefixChar(unsigned int *Prefix, int Code, int ClearCode);
29 static void DGifDecompressInput(GifFileType *GifFile, int *Code);
30 static void DGifBufferedInput(GifFileType *GifFile, GifByteType *NextByte);
32 /******************************************************************************
33 * Open a new gif file for read, given by its name. *
34 * Returns GifFileType pointer dynamically allocated which serves as the gif *
36 ******************************************************************************/
37 void DGifOpenFileName(GifFileType *GifFile, const char *FileName)
41 if ((f = fopen(FileName,
46 #endif /* WIN32_NATIVE */
48 GifInternError(GifFile, D_GIF_ERR_OPEN_FAILED);
50 GifStdIOInit(GifFile, f, -1);
51 DGifInitRead(GifFile);
54 /******************************************************************************
55 * Update a new gif file, given its file handle. *
56 * Returns GifFileType pointer dynamically allocated which serves as the gif *
58 ******************************************************************************/
59 void DGifOpenFileHandle(GifFileType *GifFile, int FileHandle)
64 setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */
65 f = fdopen(FileHandle, "rb"); /* Make it into a stream: */
66 setvbuf(f, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE);/* And inc. stream buffer.*/
68 f = fdopen(FileHandle, "r"); /* Make it into a stream: */
69 #endif /* WIN32_NATIVE */
71 GifStdIOInit(GifFile, f, -1);
72 DGifInitRead(GifFile);
75 /******************************************************************************
76 * Update a new gif file, given its file handle. *
77 * Returns GifFileType pointer dynamically allocated which serves as the gif *
78 * info record. _GifError is cleared if succesfull. *
79 ******************************************************************************/
80 void DGifInitRead(GifFileType *GifFile)
82 GifByteType Buf[GIF_STAMP_LEN+1];
83 GifFilePrivateType *Private;
85 if ((Private = (GifFilePrivateType *) malloc(sizeof(GifFilePrivateType)))
87 GifInternError(GifFile, D_GIF_ERR_NOT_ENOUGH_MEM);
89 memset(Private, '\0', sizeof(GifFilePrivateType));
90 GifFile->Private = (VoidPtr) Private;
92 Private->FileState = 0; /* Make sure bit 0 = 0 (File open for read). */
94 /* Lets see if this is a GIF file: */
95 GifRead(Buf, GIF_STAMP_LEN, GifFile);
97 /* The GIF Version number is ignored at this time. Maybe we should do */
98 /* something more useful with it. */
99 Buf[GIF_STAMP_LEN] = 0;
100 if (strncmp(GIF_STAMP, (const char *) Buf, GIF_VERSION_POS) != 0) {
101 GifInternError(GifFile, D_GIF_ERR_NOT_GIF_FILE);
104 DGifGetScreenDesc(GifFile);
107 /******************************************************************************
108 * This routine should be called before any other DGif calls. Note that *
109 * this routine is called automatically from DGif file open routines. *
110 ******************************************************************************/
111 void DGifGetScreenDesc(GifFileType *GifFile)
115 GifFilePrivateType *Private = (GifFilePrivateType*) GifFile->Private;
117 if (!IS_READABLE(Private)) {
118 /* This file was NOT open for reading: */
119 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
122 /* Put the screen descriptor into the file: */
123 DGifGetWord(GifFile, &GifFile->SWidth);
124 DGifGetWord(GifFile, &GifFile->SHeight);
126 GifRead(Buf, 3, GifFile);
127 GifFile->SColorResolution = (((Buf[0] & 0x70) + 1) >> 4) + 1;
128 BitsPerPixel = (Buf[0] & 0x07) + 1;
129 GifFile->SBackGroundColor = Buf[1];
130 if (Buf[0] & 0x80) { /* Do we have global color map? */
132 GifFile->SColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
134 /* Get the global color map: */
135 for (i = 0; i < GifFile->SColorMap->ColorCount; i++) {
136 GifRead(Buf, 3, GifFile);
137 GifFile->SColorMap->Colors[i].Red = Buf[0];
138 GifFile->SColorMap->Colors[i].Green = Buf[1];
139 GifFile->SColorMap->Colors[i].Blue = Buf[2];
142 /* We should always have a colormap */
143 GifFile->SColorMap = MakeMapObject(2, NULL);
144 GifFile->SColorMap->Colors[0].Red = 0;
145 GifFile->SColorMap->Colors[0].Green = 0;
146 GifFile->SColorMap->Colors[0].Blue = 0;
147 GifFile->SColorMap->Colors[1].Red = 0xff;
148 GifFile->SColorMap->Colors[1].Green = 0xff;
149 GifFile->SColorMap->Colors[1].Blue = 0xff;
153 /******************************************************************************
154 * This routine should be called before any attemp to read an image. *
155 ******************************************************************************/
156 void DGifGetRecordType(GifFileType *GifFile, GifRecordType *Type)
159 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
161 if (!IS_READABLE(Private)) {
162 /* This file was NOT open for reading: */
163 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
166 GifRead(&Buf, 1, GifFile);
170 *Type = IMAGE_DESC_RECORD_TYPE;
173 *Type = EXTENSION_RECORD_TYPE;
176 *Type = TERMINATE_RECORD_TYPE;
179 *Type = UNDEFINED_RECORD_TYPE;
180 GifInternError(GifFile, D_GIF_ERR_WRONG_RECORD);
184 /******************************************************************************
185 * This routine should be called before any attemp to read an image. *
186 * Note it is assumed the Image desc. header (',') has been read. *
187 ******************************************************************************/
188 void DGifGetImageDesc(GifFileType *GifFile)
192 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
194 if (!IS_READABLE(Private)) {
195 /* This file was NOT open for reading: */
196 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
199 DGifGetWord(GifFile, &GifFile->Image.Left);
200 DGifGetWord(GifFile, &GifFile->Image.Top);
201 DGifGetWord(GifFile, &GifFile->Image.Width);
202 DGifGetWord(GifFile, &GifFile->Image.Height);
204 GifRead(Buf, 1, GifFile);
205 BitsPerPixel = (Buf[0] & 0x07) + 1;
206 GifFile->Image.Interlace = (Buf[0] & 0x40);
207 if (Buf[0] & 0x80) { /* Does this image have local color map? */
209 if (GifFile->Image.ColorMap && GifFile->SavedImages == NULL)
210 FreeMapObject(GifFile->Image.ColorMap);
212 GifFile->Image.ColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
214 /* Get the image local color map: */
215 for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) {
216 GifRead(Buf, 3, GifFile);
217 GifFile->Image.ColorMap->Colors[i].Red = Buf[0];
218 GifFile->Image.ColorMap->Colors[i].Green = Buf[1];
219 GifFile->Image.ColorMap->Colors[i].Blue = Buf[2];
223 if (GifFile->SavedImages) {
226 if ((GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages,
227 sizeof(SavedImage) * (GifFile->ImageCount + 1))) == NULL) {
228 GifInternError(GifFile, D_GIF_ERR_NOT_ENOUGH_MEM);
231 sp = &GifFile->SavedImages[GifFile->ImageCount];
232 memcpy(&sp->ImageDesc, &GifFile->Image, sizeof(GifImageDesc));
233 if (GifFile->Image.ColorMap)
235 sp->ImageDesc.ColorMap =
236 MakeMapObject (GifFile->Image.ColorMap->ColorCount,
237 GifFile->Image.ColorMap->Colors);
239 sp->RasterBits = NULL;
240 sp->ExtensionBlockCount = 0;
241 sp->ExtensionBlocks = (ExtensionBlock *)NULL;
244 GifFile->ImageCount++;
246 Private->PixelCount = (long) GifFile->Image.Width *
247 (long) GifFile->Image.Height;
249 DGifSetupDecompress(GifFile); /* Reset decompress algorithm parameters. */
252 /******************************************************************************
253 * Get one full scanned line (Line) of length LineLen from GIF file. *
254 ******************************************************************************/
255 void DGifGetLine(GifFileType *GifFile, GifPixelType *Line, int LineLen)
258 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
260 if (!IS_READABLE(Private)) {
261 /* This file was NOT open for reading: */
262 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
265 if (!LineLen) LineLen = GifFile->Image.Width;
267 #if defined(WIN32_NATIVE) || defined(__GNUC__)
268 if ((Private->PixelCount -= LineLen) > 0xffff0000UL)
270 if ((Private->PixelCount -= LineLen) > 0xffff0000)
271 #endif /* WIN32_NATIVE */
273 GifInternError(GifFile, D_GIF_ERR_DATA_TOO_BIG);
276 DGifDecompressLine(GifFile, Line, LineLen);
277 if (Private->PixelCount == 0) {
278 /* We probably would not be called any more, so lets clean */
279 /* everything before we return: need to flush out all rest of */
280 /* image until empty block (size 0) detected. We use GetCodeNext.*/
282 DGifGetCodeNext(GifFile, &Dummy);
283 while (Dummy != NULL);
287 /******************************************************************************
288 * Put one pixel (Pixel) into GIF file. *
289 ******************************************************************************/
290 void DGifGetPixel(GifFileType *GifFile, GifPixelType Pixel)
293 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
295 if (!IS_READABLE(Private)) {
296 /* This file was NOT open for reading: */
297 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
300 #if defined(WIN32_NATIVE) || defined(__GNUC__)
301 if (--Private->PixelCount > 0xffff0000UL)
303 if (--Private->PixelCount > 0xffff0000)
304 #endif /* WIN32_NATIVE */
306 GifInternError(GifFile, D_GIF_ERR_DATA_TOO_BIG);
309 DGifDecompressLine(GifFile, &Pixel, 1);
310 if (Private->PixelCount == 0) {
311 /* We probably would not be called any more, so lets clean */
312 /* everything before we return: need to flush out all rest of */
313 /* image until empty block (size 0) detected. We use GetCodeNext.*/
315 DGifGetCodeNext(GifFile, &Dummy);
316 while (Dummy != NULL);
320 /******************************************************************************
321 * Get an extension block (see GIF manual) from gif file. This routine only *
322 * returns the first data block, and DGifGetExtensionNext shouldbe called *
323 * after this one until NULL extension is returned. *
324 * The Extension should NOT be freed by the user (not dynamically allocated).*
325 * Note it is assumed the Extension desc. header ('!') has been read. *
326 ******************************************************************************/
327 void DGifGetExtension(GifFileType *GifFile, int *ExtCode,
328 GifByteType **Extension)
331 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
333 if (!IS_READABLE(Private)) {
334 /* This file was NOT open for reading: */
335 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
338 GifRead(&Buf, 1, GifFile);
341 DGifGetExtensionNext(GifFile, Extension);
344 /******************************************************************************
345 * Get a following extension block (see GIF manual) from gif file. This *
346 * routine sould be called until NULL Extension is returned. *
347 * The Extension should NOT be freed by the user (not dynamically allocated).*
348 ******************************************************************************/
349 void DGifGetExtensionNext(GifFileType *GifFile, GifByteType **Extension)
352 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
354 GifRead(&Buf, 1, GifFile);
356 *Extension = Private->Buf; /* Use private unused buffer. */
357 (*Extension)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
358 GifRead(&((*Extension)[1]), Buf, GifFile);
364 /******************************************************************************
365 * This routine should be called second to last, to close the GIF file. *
366 ******************************************************************************/
367 int DGifCloseFile(GifFileType *GifFile)
369 GifFilePrivateType *Private;
371 if (GifFile == NULL) return -1;
373 Private = (GifFilePrivateType *)GifFile->Private;
374 if (!IS_READABLE(Private))
376 /* This file was NOT open for reading: */
377 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
380 if (GifClose (GifFile))
382 GifInternError(GifFile, D_GIF_ERR_CLOSE_FAILED);
387 /******************************************************************************
388 * Get 2 bytes (word) from the given file: *
389 ******************************************************************************/
390 static void DGifGetWord(GifFileType *GifFile, int *Word)
394 GifRead(c, 2, GifFile);
396 *Word = (((unsigned int) c[1]) << 8) + c[0];
399 /******************************************************************************
400 * Get the image code in compressed form. his routine can be called if the *
401 * information needed to be piped out as is. Obviously this is much faster *
402 * than decoding and encoding again. This routine should be followed by calls *
403 * to DGifGetCodeNext, until NULL block is returned. *
404 * The block should NOT be freed by the user (not dynamically allocated). *
405 ******************************************************************************/
406 void DGifGetCode(GifFileType *GifFile, int *CodeSize, GifByteType **CodeBlock)
408 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
410 if (!IS_READABLE(Private)) {
411 /* This file was NOT open for reading: */
412 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
415 *CodeSize = Private->BitsPerPixel;
417 DGifGetCodeNext(GifFile, CodeBlock);
420 /******************************************************************************
421 * Continue to get the image code in compressed form. This routine should be *
422 * called until NULL block is returned. *
423 * The block should NOT be freed by the user (not dynamically allocated). *
424 ******************************************************************************/
425 void DGifGetCodeNext(GifFileType *GifFile, GifByteType **CodeBlock)
428 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
430 GifRead(&Buf, 1, GifFile);
433 *CodeBlock = Private->Buf; /* Use private unused buffer. */
434 (*CodeBlock)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
435 GifRead(&((*CodeBlock)[1]), Buf, GifFile);
439 Private->Buf[0] = 0; /* Make sure the buffer is empty! */
440 Private->PixelCount = 0; /* And local info. indicate image read. */
445 /******************************************************************************
446 * Setup the LZ decompression for this image: *
447 ******************************************************************************/
448 static void DGifSetupDecompress(GifFileType *GifFile)
451 GifByteType CodeSize;
452 unsigned int *Prefix;
453 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
455 GifRead(&CodeSize, 1, GifFile); /* Read Code size from file. */
456 BitsPerPixel = CodeSize;
458 Private->Buf[0] = 0; /* Input Buffer empty. */
459 Private->BitsPerPixel = BitsPerPixel;
460 Private->ClearCode = (1 << BitsPerPixel);
461 Private->EOFCode = Private->ClearCode + 1;
462 Private->RunningCode = Private->EOFCode + 1;
463 Private->RunningBits = BitsPerPixel + 1; /* Number of bits per code. */
464 Private->MaxCode1 = 1 << Private->RunningBits; /* Max. code + 1. */
465 Private->StackPtr = 0; /* No pixels on the pixel stack. */
466 Private->LastCode = NO_SUCH_CODE;
467 Private->CrntShiftState = 0; /* No information in CrntShiftDWord. */
468 Private->CrntShiftDWord = 0;
470 Prefix = Private->Prefix;
471 for (i = 0; i <= LZ_MAX_CODE; i++) Prefix[i] = NO_SUCH_CODE;
474 /******************************************************************************
475 * The LZ decompression routine: *
476 * This version decompress the given gif file into Line of length LineLen. *
477 * This routine can be called few times (one per scan line, for example), in *
478 * order the complete the whole image. *
479 ******************************************************************************/
480 static void DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line,
483 int i = 0, j, CrntCode, EOFCode, ClearCode, CrntPrefix, LastCode, StackPtr;
484 GifByteType *Stack, *Suffix;
485 unsigned int *Prefix;
486 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
488 StackPtr = Private->StackPtr;
489 Prefix = Private->Prefix;
490 Suffix = Private->Suffix;
491 Stack = Private->Stack;
492 EOFCode = Private->EOFCode;
493 ClearCode = Private->ClearCode;
494 LastCode = Private->LastCode;
498 /* Let pop the stack off before continueing to read the gif file: */
499 while (StackPtr != 0 && i < LineLen) Line[i++] = Stack[--StackPtr];
502 while (i < LineLen) { /* Decode LineLen items. */
503 DGifDecompressInput(GifFile, &CrntCode);
505 if (CrntCode == EOFCode) {
506 /* Note however that usually we will not be here as we will stop */
507 /* decoding as soon as we got all the pixel, or EOF code will */
508 /* not be read at all, and DGifGetLine/Pixel clean everything. */
509 if (i != LineLen - 1 || Private->PixelCount != 0) {
510 GifInternError(GifFile, D_GIF_ERR_EOF_TOO_SOON);
514 else if (CrntCode == ClearCode) {
515 /* We need to start over again: */
516 for (j = 0; j <= LZ_MAX_CODE; j++) Prefix[j] = NO_SUCH_CODE;
517 Private->RunningCode = Private->EOFCode + 1;
518 Private->RunningBits = Private->BitsPerPixel + 1;
519 Private->MaxCode1 = 1 << Private->RunningBits;
520 LastCode = Private->LastCode = NO_SUCH_CODE;
523 /* Its regular code - if in pixel range simply add it to output */
524 /* stream, otherwise trace to codes linked list until the prefix */
525 /* is in pixel range: */
526 if (CrntCode < ClearCode) {
527 /* This is simple - its pixel scalar, so add it to output: */
528 Line[i++] = CrntCode;
531 /* Its a code to needed to be traced: trace the linked list */
532 /* until the prefix is a pixel, while pushing the suffix */
533 /* pixels on our stack. If we done, pop the stack in reverse */
534 /* (thats what stack is good for!) order to output. */
535 if (Prefix[CrntCode] == NO_SUCH_CODE) {
536 /* Only allowed if CrntCode is exactly the running code: */
537 /* In that case CrntCode = XXXCode, CrntCode or the */
538 /* prefix code is last code and the suffix char is */
539 /* exactly the prefix of last code! */
540 if (CrntCode == Private->RunningCode - 2) {
541 CrntPrefix = LastCode;
542 Suffix[Private->RunningCode - 2] =
543 Stack[StackPtr++] = DGifGetPrefixChar(Prefix,
544 LastCode, ClearCode);
547 GifInternError(GifFile, D_GIF_ERR_IMAGE_DEFECT);
551 CrntPrefix = CrntCode;
553 /* Now (if image is O.K.) we should not get an NO_SUCH_CODE */
554 /* During the trace. As we might loop forever, in case of */
555 /* defective image, we count the number of loops we trace */
556 /* and stop if we got LZ_MAX_CODE. obviously we can not */
557 /* loop more than that. */
559 while (j++ <= LZ_MAX_CODE &&
560 CrntPrefix > ClearCode &&
561 CrntPrefix <= LZ_MAX_CODE) {
562 Stack[StackPtr++] = Suffix[CrntPrefix];
563 CrntPrefix = Prefix[CrntPrefix];
565 if (j >= LZ_MAX_CODE || CrntPrefix > LZ_MAX_CODE) {
566 GifInternError(GifFile, D_GIF_ERR_IMAGE_DEFECT);
568 /* Push the last character on stack: */
569 Stack[StackPtr++] = CrntPrefix;
571 /* Now lets pop all the stack into output: */
572 while (StackPtr != 0 && i < LineLen)
573 Line[i++] = Stack[--StackPtr];
575 if (LastCode != NO_SUCH_CODE) {
576 Prefix[Private->RunningCode - 2] = LastCode;
578 if (CrntCode == Private->RunningCode - 2) {
579 /* Only allowed if CrntCode is exactly the running code: */
580 /* In that case CrntCode = XXXCode, CrntCode or the */
581 /* prefix code is last code and the suffix char is */
582 /* exactly the prefix of last code! */
583 Suffix[Private->RunningCode - 2] =
584 DGifGetPrefixChar(Prefix, LastCode, ClearCode);
587 Suffix[Private->RunningCode - 2] =
588 DGifGetPrefixChar(Prefix, CrntCode, ClearCode);
595 Private->LastCode = LastCode;
596 Private->StackPtr = StackPtr;
599 /******************************************************************************
600 * Routine to trace the Prefixes linked list until we get a prefix which is *
601 * not code, but a pixel value (less than ClearCode). Returns that pixel value.*
602 * If image is defective, we might loop here forever, so we limit the loops to *
603 * the maximum possible if image O.k. - LZ_MAX_CODE times. *
604 ******************************************************************************/
605 static int DGifGetPrefixChar(unsigned int *Prefix, int Code, int ClearCode)
609 while (Code > ClearCode && i++ <= LZ_MAX_CODE) Code = Prefix[Code];
613 /******************************************************************************
614 * Interface for accessing the LZ codes directly. Set Code to the real code *
615 * (12bits), or to -1 if EOF code is returned. *
616 ******************************************************************************/
617 void DGifGetLZCodes(GifFileType *GifFile, int *Code)
619 GifByteType *CodeBlock;
620 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
622 if (!IS_READABLE(Private)) {
623 /* This file was NOT open for reading: */
624 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
627 DGifDecompressInput(GifFile, Code);
629 if (*Code == Private->EOFCode) {
630 /* Skip rest of codes (hopefully only NULL terminating block): */
632 DGifGetCodeNext(GifFile, &CodeBlock);
633 while (CodeBlock != NULL);
637 else if (*Code == Private->ClearCode) {
638 /* We need to start over again: */
639 Private->RunningCode = Private->EOFCode + 1;
640 Private->RunningBits = Private->BitsPerPixel + 1;
641 Private->MaxCode1 = 1 << Private->RunningBits;
645 /******************************************************************************
646 * The LZ decompression input routine: *
647 * This routine is responsable for the decompression of the bit stream from *
648 * 8 bits (bytes) packets, into the real codes. *
649 * Returns GIF_OK if read succesfully. *
650 ******************************************************************************/
651 static void DGifDecompressInput(GifFileType *GifFile, int *Code)
653 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
654 GifByteType NextByte;
655 static unsigned int CodeMasks[] = {
656 0x0000, 0x0001, 0x0003, 0x0007,
657 0x000f, 0x001f, 0x003f, 0x007f,
658 0x00ff, 0x01ff, 0x03ff, 0x07ff,
662 while (Private->CrntShiftState < Private->RunningBits) {
663 /* Needs to get more bytes from input stream for next code: */
664 DGifBufferedInput(GifFile, &NextByte);
665 Private->CrntShiftDWord |=
666 ((unsigned long) NextByte) << Private->CrntShiftState;
667 Private->CrntShiftState += 8;
669 *Code = Private->CrntShiftDWord & CodeMasks[Private->RunningBits];
671 Private->CrntShiftDWord >>= Private->RunningBits;
672 Private->CrntShiftState -= Private->RunningBits;
674 /* If code cannt fit into RunningBits bits, must raise its size. Note */
675 /* however that codes above 4095 are used for special signaling. */
676 if (++Private->RunningCode > Private->MaxCode1 &&
677 Private->RunningBits < LZ_BITS) {
678 Private->MaxCode1 <<= 1;
679 Private->RunningBits++;
683 /******************************************************************************
684 * This routines read one gif data block at a time and buffers it internally *
685 * so that the decompression routine could access it. *
686 * The routine returns the next byte from its internal buffer (or read next *
687 * block in if buffer empty) *
688 ******************************************************************************/
689 static void DGifBufferedInput(GifFileType *GifFile, GifByteType *NextByte)
691 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
692 GifByteType *Buf = Private->Buf;
695 /* Needs to read the next buffer - this one is empty: */
696 GifRead(Buf, 1, GifFile);
697 GifRead((Buf + 1), Buf[0], GifFile);
699 Buf[1] = 2; /* We use now the second place as last char read! */
703 *NextByte = Buf[Buf[1]++];
708 /******************************************************************************
709 * This routine reads an entire GIF into core, hanging all its state info off *
710 * the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle() *
711 * first to initialize I/O. Its inverse is EGifSpew(). *
712 ******************************************************************************/
713 void DGifSlurp(GifFileType *GifFile)
716 GifRecordType RecordType;
718 GifByteType *ExtData;
720 /* Some versions of malloc dislike 0-length requests */
721 GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
722 memset(GifFile->SavedImages, 0, sizeof(SavedImage));
723 sp = &GifFile->SavedImages[0];
726 DGifGetRecordType(GifFile, &RecordType);
728 switch (RecordType) {
729 case IMAGE_DESC_RECORD_TYPE:
730 DGifGetImageDesc(GifFile);
732 sp = &GifFile->SavedImages[GifFile->ImageCount-1];
733 ImageSize = sp->ImageDesc.Width * sp->ImageDesc.Height;
736 = (GifPixelType*) malloc (ImageSize * sizeof(GifPixelType));
738 DGifGetLine(GifFile, sp->RasterBits, ImageSize);
741 case EXTENSION_RECORD_TYPE:
742 DGifGetExtension(GifFile,&sp->Function,&ExtData);
744 while (ExtData != NULL) {
745 if (AddExtensionBlock(sp, ExtData[0], ExtData+1) == GIF_ERROR)
746 GifInternError(GifFile, D_GIF_ERR_NOT_ENOUGH_MEM);
747 DGifGetExtensionNext(GifFile, &ExtData);
751 case TERMINATE_RECORD_TYPE:
754 default: /* Should be trapped by DGifGetRecordType */
757 } while (RecordType != TERMINATE_RECORD_TYPE);
760 /******************************************************************************
761 * Extension record functions *
762 ******************************************************************************/
764 void MakeExtension(SavedImage *New, int Function)
766 New->Function = Function;
768 * Someday we might have to deal with multiple extensions.
772 int AddExtensionBlock(SavedImage *New, int Len, GifByteType *data)
777 if (New->ExtensionBlocks == NULL)
778 New->ExtensionBlocks = (ExtensionBlock *)malloc(sizeof(ExtensionBlock));
780 New->ExtensionBlocks =
781 (ExtensionBlock *)realloc(New->ExtensionBlocks,
782 sizeof(ExtensionBlock) * (New->ExtensionBlockCount + 1));
784 if (New->ExtensionBlocks == NULL)
787 ep = &New->ExtensionBlocks[New->ExtensionBlockCount++];
789 size = Len * sizeof(GifByteType);
790 ep->Bytes = (GifByteType *)malloc(size);
791 memcpy(ep->Bytes, data, size);
795 void FreeExtension(SavedImage *Image)
799 for (ep = Image->ExtensionBlocks;
800 ep < Image->ExtensionBlocks + Image->ExtensionBlockCount;
802 (void) free((char *)ep->Bytes);
803 free((char *)Image->ExtensionBlocks);
804 Image->ExtensionBlocks = NULL;
807 /******************************************************************************
808 * Image block allocation functions *
809 ******************************************************************************/
810 SavedImage *MakeSavedImage(GifFileType *GifFile, SavedImage *CopyFrom)
812 * Append an image block to the SavedImages array
817 if (GifFile->SavedImages == NULL)
818 GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
820 GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages,
821 sizeof(SavedImage) * (GifFile->ImageCount+1));
823 if (GifFile->SavedImages == NULL)
824 return((SavedImage *)NULL);
827 sp = &GifFile->SavedImages[GifFile->ImageCount++];
828 memset((char *)sp, '\0', sizeof(SavedImage));
832 memcpy((char *)sp, CopyFrom, sizeof(SavedImage));
835 * Make our own allocated copies of the heap fields in the
836 * copied record. This guards against potential aliasing
840 /* first, the local color map */
841 if (sp->ImageDesc.ColorMap)
842 sp->ImageDesc.ColorMap =
843 MakeMapObject(CopyFrom->ImageDesc.ColorMap->ColorCount,
844 CopyFrom->ImageDesc.ColorMap->Colors);
846 /* next, the raster */
847 sp->RasterBits = (GifPixelType *) malloc(sizeof(GifPixelType)
848 * CopyFrom->ImageDesc.Height
849 * CopyFrom->ImageDesc.Width);
850 memcpy(sp->RasterBits,
851 CopyFrom->RasterBits,
853 * CopyFrom->ImageDesc.Height
854 * CopyFrom->ImageDesc.Width);
856 /* finally, the extension blocks */
857 if (sp->ExtensionBlocks)
860 = (ExtensionBlock*)malloc(sizeof(ExtensionBlock)
861 * CopyFrom->ExtensionBlockCount);
862 memcpy(sp->ExtensionBlocks,
863 CopyFrom->ExtensionBlocks,
864 sizeof(ExtensionBlock)
865 * CopyFrom->ExtensionBlockCount);
868 * For the moment, the actual blocks can take their
869 * chances with free(). We'll fix this later.
878 void FreeSavedImages(GifFileType *GifFile)
882 for (sp = GifFile->SavedImages;
883 sp < GifFile->SavedImages + GifFile->ImageCount;
886 if (sp->ImageDesc.ColorMap)
887 FreeMapObject(sp->ImageDesc.ColorMap);
890 free((char *)sp->RasterBits);
892 if (sp->ExtensionBlocks)
895 free((char *) GifFile->SavedImages);
898 /******************************************************************************
899 * Miscellaneous utility functions *
900 ******************************************************************************/
902 static int BitSize(int n)
903 /* return smallest bitfield size n will fit in */
907 for (i = 1; i <= 8; i++)
913 /******************************************************************************
914 * Color map object functions *
915 ******************************************************************************/
917 ColorMapObject *MakeMapObject(int ColorCount, GifColorType *ColorMap)
919 * Allocate a color map of given size; initialize with contents of
920 * ColorMap if that pointer is non-NULL.
923 ColorMapObject *Object;
925 if (ColorCount != (1 << BitSize(ColorCount)))
926 return((ColorMapObject *)NULL);
928 Object = (ColorMapObject *)malloc(sizeof(ColorMapObject));
929 if (Object == (ColorMapObject *)NULL)
930 return((ColorMapObject *)NULL);
932 Object->Colors = (GifColorType *)calloc(ColorCount, sizeof(GifColorType));
933 if (Object->Colors == (GifColorType *)NULL)
936 return((ColorMapObject *)NULL);
939 Object->ColorCount = ColorCount;
940 Object->BitsPerPixel = BitSize(ColorCount);
943 memcpy((char *)Object->Colors,
944 (char *)ColorMap, ColorCount * sizeof(GifColorType));
949 void FreeMapObject(ColorMapObject *Object)
951 * Free a color map object
954 free(Object->Colors);