diff options
Diffstat (limited to 'gifalloc.c')
-rw-r--r-- | gifalloc.c | 273 |
1 files changed, 116 insertions, 157 deletions
@@ -1,34 +1,25 @@ /***************************************************************************** - * "Gif-Lib" - Yet another gif library. - * - * Written by: Gershon Elber Ver 0.1, Jun. 1989 - * Extensively hacked by: Eric S. Raymond Ver 1.?, Sep 1992 - ***************************************************************************** - * GIF construction tools - ***************************************************************************** - * History: - * 15 Sep 92 - Version 1.0 by Eric Raymond. - ****************************************************************************/ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif + + GIF construction tools + +****************************************************************************/ #include <stdlib.h> #include <stdio.h> #include <string.h> + #include "gif_lib.h" #define MAX(x, y) (((x) > (y)) ? (x) : (y)) /****************************************************************************** - * Miscellaneous utility functions - *****************************************************************************/ + Miscellaneous utility functions +******************************************************************************/ /* return smallest bitfield size n will fit in */ int -BitSize(int n) { - +GifBitSize(int n) +{ register int i; for (i = 1; i <= 8; i++) @@ -38,22 +29,21 @@ BitSize(int n) { } /****************************************************************************** - * Color map object functions - *****************************************************************************/ + Color map object functions +******************************************************************************/ /* * Allocate a color map of given size; initialize with contents of * ColorMap if that pointer is non-NULL. */ ColorMapObject * -MakeMapObject(int ColorCount, - const GifColorType * ColorMap) { - +GifMakeMapObject(int ColorCount, const GifColorType *ColorMap) +{ ColorMapObject *Object; /*** FIXME: Our ColorCount has to be a power of two. Is it necessary to * make the user know that or should we automatically round up instead? */ - if (ColorCount != (1 << BitSize(ColorCount))) { + if (ColorCount != (1 << GifBitSize(ColorCount))) { return ((ColorMapObject *) NULL); } @@ -64,13 +54,14 @@ MakeMapObject(int ColorCount, Object->Colors = (GifColorType *)calloc(ColorCount, sizeof(GifColorType)); if (Object->Colors == (GifColorType *) NULL) { + free(Object); return ((ColorMapObject *) NULL); } Object->ColorCount = ColorCount; - Object->BitsPerPixel = BitSize(ColorCount); + Object->BitsPerPixel = GifBitSize(ColorCount); - if (ColorMap) { + if (ColorMap != NULL) { memcpy((char *)Object->Colors, (char *)ColorMap, ColorCount * sizeof(GifColorType)); } @@ -78,72 +69,70 @@ MakeMapObject(int ColorCount, return (Object); } -/* - * Free a color map object - */ +/******************************************************************************* +Free a color map object +*******************************************************************************/ void -FreeMapObject(ColorMapObject * Object) { - +GifFreeMapObject(ColorMapObject *Object) +{ if (Object != NULL) { - free(Object->Colors); - free(Object); - /*** FIXME: - * When we are willing to break API we need to make this function - * FreeMapObject(ColorMapObject **Object) - * and do this assignment to NULL here: - * *Object = NULL; - */ + (void)free(Object->Colors); + (void)free(Object); } } #ifdef DEBUG void -DumpColorMap(ColorMapObject * Object, - FILE * fp) { - - if (Object) { +DumpColorMap(ColorMapObject *Object, + FILE * fp) +{ + if (Object != NULL) { int i, j, Len = Object->ColorCount; for (i = 0; i < Len; i += 4) { for (j = 0; j < 4 && j < Len; j++) { - fprintf(fp, "%3d: %02x %02x %02x ", i + j, - Object->Colors[i + j].Red, - Object->Colors[i + j].Green, - Object->Colors[i + j].Blue); + (void)fprintf(fp, "%3d: %02x %02x %02x ", i + j, + Object->Colors[i + j].Red, + Object->Colors[i + j].Green, + Object->Colors[i + j].Blue); } - fprintf(fp, "\n"); + (void)fprintf(fp, "\n"); } } } #endif /* DEBUG */ -/* - * Compute the union of two given color maps and return it. If result can't - * fit into 256 colors, NULL is returned, the allocated union otherwise. - * ColorIn1 is copied as is to ColorUnion, while colors from ColorIn2 are - * copied iff they didn't exist before. ColorTransIn2 maps the old - * ColorIn2 into ColorUnion color map table. - */ +/******************************************************************************* + Compute the union of two given color maps and return it. If result can't + fit into 256 colors, NULL is returned, the allocated union otherwise. + ColorIn1 is copied as is to ColorUnion, while colors from ColorIn2 are + copied iff they didn't exist before. ColorTransIn2 maps the old + ColorIn2 into the ColorUnion color map table./ +*******************************************************************************/ ColorMapObject * -UnionColorMap(const ColorMapObject * ColorIn1, - const ColorMapObject * ColorIn2, - GifPixelType ColorTransIn2[]) { - - int i, j, CrntSlot, RoundUpTo, NewBitSize; +GifUnionColorMap(const ColorMapObject *ColorIn1, + const ColorMapObject *ColorIn2, + GifPixelType ColorTransIn2[]) +{ + int i, j, CrntSlot, RoundUpTo, NewGifBitSize; ColorMapObject *ColorUnion; - /* - * Allocate table which will hold the result for sure. + /* + * We don't worry about duplicates within either color map; if + * the caller wants to resolve those, he can perform unions + * with an empty color map. */ - ColorUnion = MakeMapObject(MAX(ColorIn1->ColorCount, + + /* Allocate table which will hold the result for sure. */ + ColorUnion = GifMakeMapObject(MAX(ColorIn1->ColorCount, ColorIn2->ColorCount) * 2, NULL); if (ColorUnion == NULL) return (NULL); - /* Copy ColorIn1 to ColorUnionSize; */ - /*** FIXME: What if there are duplicate entries into the colormap to begin - * with? */ + /* + * Copy ColorIn1 to ColorUnion. + */ for (i = 0; i < ColorIn1->ColorCount; i++) ColorUnion->Colors[i] = ColorIn1->Colors[i]; CrntSlot = ColorIn1->ColorCount; @@ -160,13 +149,9 @@ UnionColorMap(const ColorMapObject * ColorIn1, && ColorIn1->Colors[CrntSlot - 1].Blue == 0) CrntSlot--; - /* Copy ColorIn2 to ColorUnionSize (use old colors if they exist): */ + /* Copy ColorIn2 to ColorUnion (use old colors if they exist): */ for (i = 0; i < ColorIn2->ColorCount && CrntSlot <= 256; i++) { /* Let's see if this color already exists: */ - /*** FIXME: Will it ever occur that ColorIn2 will contain duplicate - * entries? So we should search from 0 to CrntSlot rather than - * ColorIn1->ColorCount? - */ for (j = 0; j < ColorIn1->ColorCount; j++) if (memcmp (&ColorIn1->Colors[j], &ColorIn2->Colors[i], sizeof(GifColorType)) == 0) @@ -182,12 +167,12 @@ UnionColorMap(const ColorMapObject * ColorIn1, } if (CrntSlot > 256) { - FreeMapObject(ColorUnion); + GifFreeMapObject(ColorUnion); return ((ColorMapObject *) NULL); } - NewBitSize = BitSize(CrntSlot); - RoundUpTo = (1 << NewBitSize); + NewGifBitSize = GifBitSize(CrntSlot); + RoundUpTo = (1 << NewGifBitSize); if (RoundUpTo != ColorUnion->ColorCount) { register GifColorType *Map = ColorUnion->Colors; @@ -207,18 +192,17 @@ UnionColorMap(const ColorMapObject * ColorIn1, } ColorUnion->ColorCount = RoundUpTo; - ColorUnion->BitsPerPixel = NewBitSize; + ColorUnion->BitsPerPixel = NewGifBitSize; return (ColorUnion); } -/* - * Apply a given color translation to the raster bits of an image - */ +/******************************************************************************* + Apply a given color translation to the raster bits of an image +*******************************************************************************/ void -ApplyTranslation(SavedImage * Image, - GifPixelType Translation[]) { - +GifApplyTranslation(SavedImage *Image, GifPixelType Translation[]) +{ register int i; register int RasterSize = Image->ImageDesc.Height * Image->ImageDesc.Width; @@ -227,83 +211,70 @@ ApplyTranslation(SavedImage * Image, } /****************************************************************************** - * Extension record functions - *****************************************************************************/ - -void -MakeExtension(SavedImage * New, - int Function) { - - New->Function = Function; - /*** FIXME: - * Someday we might have to deal with multiple extensions. - * ??? Was this a note from Gershon or from me? Does the multiple - * extension blocks solve this or do we need multiple Functions? Or is - * this an obsolete function? (People should use AddExtensionBlock - * instead?) - * Looks like AddExtensionBlock needs to take the int Function argument - * then it can take the place of this function. Right now people have to - * use both. Fix AddExtensionBlock and add this to the deprecation list. - */ -} - + Extension record functions +******************************************************************************/ int -AddExtensionBlock(SavedImage * New, - int Len, - unsigned char ExtData[]) { - +GifAddExtensionBlock(int *ExtensionBlockCount, + ExtensionBlock **ExtensionBlocks, + int Function, + unsigned int Len, + unsigned char ExtData[]) +{ ExtensionBlock *ep; - if (New->ExtensionBlocks == NULL) - New->ExtensionBlocks=(ExtensionBlock *)malloc(sizeof(ExtensionBlock)); + if (*ExtensionBlocks == NULL) + *ExtensionBlocks=(ExtensionBlock *)malloc(sizeof(ExtensionBlock)); else - New->ExtensionBlocks = (ExtensionBlock *)realloc(New->ExtensionBlocks, + *ExtensionBlocks = (ExtensionBlock *)realloc(*ExtensionBlocks, sizeof(ExtensionBlock) * - (New->ExtensionBlockCount + 1)); + (*ExtensionBlockCount + 1)); - if (New->ExtensionBlocks == NULL) + if (*ExtensionBlocks == NULL) return (GIF_ERROR); - ep = &New->ExtensionBlocks[New->ExtensionBlockCount++]; + ep = &(*ExtensionBlocks)[(*ExtensionBlockCount)++]; + ep->Function = Function; ep->ByteCount=Len; - ep->Bytes = (char *)malloc(ep->ByteCount); + ep->Bytes = (GifByteType *)malloc(ep->ByteCount); if (ep->Bytes == NULL) return (GIF_ERROR); - if (ExtData) { + if (ExtData != NULL) { memcpy(ep->Bytes, ExtData, Len); - ep->Function = New->Function; } return (GIF_OK); } void -FreeExtension(SavedImage * Image) +GifFreeExtensions(int *ExtensionBlockCount, + ExtensionBlock **ExtensionBlocks) { ExtensionBlock *ep; - if ((Image == NULL) || (Image->ExtensionBlocks == NULL)) { + if (*ExtensionBlocks == NULL) return; - } - for (ep = Image->ExtensionBlocks; - ep < (Image->ExtensionBlocks + Image->ExtensionBlockCount); ep++) + + for (ep = *ExtensionBlocks; + ep < (*ExtensionBlocks + *ExtensionBlockCount); + ep++) (void)free((char *)ep->Bytes); - free((char *)Image->ExtensionBlocks); - Image->ExtensionBlocks = NULL; + (void)free((char *)*ExtensionBlocks); + *ExtensionBlocks = NULL; + *ExtensionBlockCount = 0; } /****************************************************************************** - * Image block allocation functions + Image block allocation functions ******************************************************************************/ /* Private Function: * Frees the last image in the GifFile->SavedImages array */ void -FreeLastSavedImage(GifFileType *GifFile) { - +FreeLastSavedImage(GifFileType *GifFile) +{ SavedImage *sp; if ((GifFile == NULL) || (GifFile->SavedImages == NULL)) @@ -314,22 +285,21 @@ FreeLastSavedImage(GifFileType *GifFile) { sp = &GifFile->SavedImages[GifFile->ImageCount]; /* Deallocate its Colormap */ - if (sp->ImageDesc.ColorMap) { - FreeMapObject(sp->ImageDesc.ColorMap); + if (sp->ImageDesc.ColorMap != NULL) { + GifFreeMapObject(sp->ImageDesc.ColorMap); sp->ImageDesc.ColorMap = NULL; } /* Deallocate the image data */ - if (sp->RasterBits) + if (sp->RasterBits != NULL) free((char *)sp->RasterBits); /* Deallocate any extensions */ - if (sp->ExtensionBlocks) - FreeExtension(sp); + GifFreeExtensions(&sp->ExtensionBlockCount, &sp->ExtensionBlocks); /*** FIXME: We could realloc the GifFile->SavedImages structure but is * there a point to it? Saves some memory but we'd have to do it every - * time. If this is used in FreeSavedImages then it would be inefficient + * time. If this is used in GifFreeSavedImages then it would be inefficient * (The whole array is going to be deallocated.) If we just use it when * we want to free the last Image it's convenient to do it here. */ @@ -339,9 +309,8 @@ FreeLastSavedImage(GifFileType *GifFile) { * Append an image block to the SavedImages array */ SavedImage * -MakeSavedImage(GifFileType * GifFile, - const SavedImage * CopyFrom) { - +GifMakeSavedImage(GifFileType *GifFile, const SavedImage *CopyFrom) +{ SavedImage *sp; if (GifFile->SavedImages == NULL) @@ -356,7 +325,7 @@ MakeSavedImage(GifFileType * GifFile, sp = &GifFile->SavedImages[GifFile->ImageCount++]; memset((char *)sp, '\0', sizeof(SavedImage)); - if (CopyFrom) { + if (CopyFrom != NULL) { memcpy((char *)sp, CopyFrom, sizeof(SavedImage)); /* @@ -366,8 +335,8 @@ MakeSavedImage(GifFileType * GifFile, */ /* first, the local color map */ - if (sp->ImageDesc.ColorMap) { - sp->ImageDesc.ColorMap = MakeMapObject( + if (sp->ImageDesc.ColorMap != NULL) { + sp->ImageDesc.ColorMap = GifMakeMapObject( CopyFrom->ImageDesc.ColorMap->ColorCount, CopyFrom->ImageDesc.ColorMap->Colors); if (sp->ImageDesc.ColorMap == NULL) { @@ -389,7 +358,7 @@ MakeSavedImage(GifFileType * GifFile, CopyFrom->ImageDesc.Width); /* finally, the extension blocks */ - if (sp->ExtensionBlocks) { + if (sp->ExtensionBlocks != NULL) { sp->ExtensionBlocks = (ExtensionBlock *)malloc( sizeof(ExtensionBlock) * CopyFrom->ExtensionBlockCount); @@ -399,17 +368,6 @@ MakeSavedImage(GifFileType * GifFile, } memcpy(sp->ExtensionBlocks, CopyFrom->ExtensionBlocks, sizeof(ExtensionBlock) * CopyFrom->ExtensionBlockCount); - - /* - * For the moment, the actual blocks can take their - * chances with free(). We'll fix this later. - *** FIXME: [Better check this out... Toshio] - * 2004 May 27: Looks like this was an ESR note. - * It means the blocks are shallow copied from InFile to - * OutFile. However, I don't see that in this code.... - * Did ESR fix it but never remove this note (And other notes - * in gifspnge?) - */ } } @@ -418,8 +376,8 @@ MakeSavedImage(GifFileType * GifFile, } void -FreeSavedImages(GifFileType * GifFile) { - +GifFreeSavedImages(GifFileType *GifFile) +{ SavedImage *sp; if ((GifFile == NULL) || (GifFile->SavedImages == NULL)) { @@ -427,17 +385,18 @@ FreeSavedImages(GifFileType * GifFile) { } for (sp = GifFile->SavedImages; sp < GifFile->SavedImages + GifFile->ImageCount; sp++) { - if (sp->ImageDesc.ColorMap) { - FreeMapObject(sp->ImageDesc.ColorMap); + if (sp->ImageDesc.ColorMap != NULL) { + GifFreeMapObject(sp->ImageDesc.ColorMap); sp->ImageDesc.ColorMap = NULL; } - if (sp->RasterBits) + if (sp->RasterBits != NULL) free((char *)sp->RasterBits); - - if (sp->ExtensionBlocks) - FreeExtension(sp); + + GifFreeExtensions(&sp->ExtensionBlockCount, &sp->ExtensionBlocks); } free((char *)GifFile->SavedImages); - GifFile->SavedImages=NULL; + GifFile->SavedImages = NULL; } + +/* end */ |