Back to index

im-sdk  12.3.91
Defines | Functions
gifalloc.c File Reference
#include <stdio.h>
#include "gif_lib.h"

Go to the source code of this file.

Defines

#define MAX(x, y)   (((x) > (y)) ? (x) : (y))

Functions

int BitSize (int n)
ColorMapObjectMakeMapObject (int ColorCount, GifColorType *ColorMap)
void FreeMapObject (ColorMapObject *Object)
ColorMapObjectUnionColorMap (ColorMapObject *ColorIn1, ColorMapObject *ColorIn2, GifPixelType ColorTransIn2[])
void ApplyTranslation (SavedImage *Image, GifPixelType Translation[])
void MakeExtension (SavedImage *New, int Function)
int AddExtensionBlock (SavedImage *New, int Len, char ExtData[])
void FreeExtension (SavedImage *Image)
SavedImageMakeSavedImage (GifFileType *GifFile, SavedImage *CopyFrom)
void FreeSavedImages (GifFileType *GifFile)

Define Documentation

#define MAX (   x,
 
)    (((x) > (y)) ? (x) : (y))

Definition at line 15 of file gifalloc.c.


Function Documentation

int AddExtensionBlock ( SavedImage New,
int  Len,
char  ExtData[] 
)

Definition at line 212 of file gifalloc.c.

{
    ExtensionBlock   *ep;

    if (New->ExtensionBlocks == NULL)
       New->ExtensionBlocks = (ExtensionBlock *)malloc(sizeof(ExtensionBlock));
    else
       New->ExtensionBlocks =
           (ExtensionBlock *)realloc(New->ExtensionBlocks,
                    sizeof(ExtensionBlock) * (New->ExtensionBlockCount + 1));

    if (New->ExtensionBlocks == NULL)
       return(GIF_ERROR);

    ep = &New->ExtensionBlocks[New->ExtensionBlockCount++];

    if ((ep->Bytes = (char *)malloc(ep->ByteCount = Len)) == NULL)
       return(GIF_ERROR);

    if (ExtData)
       memcpy(ep->Bytes, ExtData, Len);

    return(GIF_OK);
}
void ApplyTranslation ( SavedImage Image,
GifPixelType  Translation[] 
)

Definition at line 188 of file gifalloc.c.

{
    register int i;
    register int RasterSize = Image->ImageDesc.Height * Image->ImageDesc.Width;

    for (i = 0; i < RasterSize; i++)
       Image->RasterBits[i] = Translation[Image->RasterBits[i]];
}
int BitSize ( int  n)

Definition at line 21 of file gifalloc.c.

{
    register  i;

    for (i = 1; i <= 8; i++)
       if ((1 << i) >= n)
           break;
    return(i);
}

Here is the caller graph for this function:

void FreeExtension ( SavedImage Image)

Definition at line 237 of file gifalloc.c.

{
    ExtensionBlock   *ep;

    for (ep = Image->ExtensionBlocks;
        ep < Image->ExtensionBlocks + Image->ExtensionBlockCount;
        ep++)
       (void) free((char *)ep->Bytes);
    free((char *)Image->ExtensionBlocks);
    Image->ExtensionBlocks = NULL;
}

Here is the caller graph for this function:

void FreeMapObject ( ColorMapObject Object)

Definition at line 66 of file gifalloc.c.

{
    free(Object->Colors);
    free(Object);
}

Here is the caller graph for this function:

void FreeSavedImages ( GifFileType GifFile)

Definition at line 320 of file gifalloc.c.

{
    SavedImage       *sp;

    for (sp = GifFile->SavedImages;
        sp < GifFile->SavedImages + GifFile->ImageCount;
        sp++)
    {
       if (sp->ImageDesc.ColorMap)
           FreeMapObject(sp->ImageDesc.ColorMap);

       if (sp->RasterBits)
           free((char *)sp->RasterBits);

       if (sp->ExtensionBlocks)
           FreeExtension(sp);
    }
    free((char *) GifFile->SavedImages);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void MakeExtension ( SavedImage New,
int  Function 
)

Definition at line 204 of file gifalloc.c.

{
    New->Function = Function;
    /*
     * Someday we might have to deal with multiple extensions.
     */
}
ColorMapObject* MakeMapObject ( int  ColorCount,
GifColorType ColorMap 
)

Definition at line 37 of file gifalloc.c.

{
    ColorMapObject *Object;

    if (ColorCount != (1 << BitSize(ColorCount)))
       return((ColorMapObject *)NULL);

    Object = (ColorMapObject *)malloc(sizeof(ColorMapObject));
    if (Object == (ColorMapObject *)NULL)
       return((ColorMapObject *)NULL);

    Object->Colors = (GifColorType *)calloc(ColorCount, sizeof(GifColorType));
    if (Object->Colors == (GifColorType *)NULL)
       return((ColorMapObject *)NULL);

    Object->ColorCount = ColorCount;
    Object->BitsPerPixel = BitSize(ColorCount);

    if (ColorMap)
       memcpy((char *)Object->Colors,
              (char *)ColorMap, ColorCount * sizeof(GifColorType));

    return(Object);
}

Here is the call graph for this function:

Here is the caller graph for this function:

SavedImage* MakeSavedImage ( GifFileType GifFile,
SavedImage CopyFrom 
)

Definition at line 252 of file gifalloc.c.

{
    SavedImage       *sp;

    if (GifFile->SavedImages == NULL)
       GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
    else
       GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages,
                            sizeof(SavedImage) * (GifFile->ImageCount+1));

    if (GifFile->SavedImages == NULL)
       return((SavedImage *)NULL);
    else
    {
       sp = &GifFile->SavedImages[GifFile->ImageCount++];
       memset((char *)sp, '\0', sizeof(SavedImage));

       if (CopyFrom)
       {
           memcpy((char *)sp, CopyFrom, sizeof(SavedImage));

           /*
            * Make our own allocated copies of the heap fields in the
            * copied record.  This guards against potential aliasing
            * problems.
            */

           /* first, the local color map */
           if (sp->ImageDesc.ColorMap)
              sp->ImageDesc.ColorMap =
                  MakeMapObject(CopyFrom->ImageDesc.ColorMap->ColorCount,
                              CopyFrom->ImageDesc.ColorMap->Colors);

           /* next, the raster */
           sp->RasterBits = (char *)malloc(sizeof(GifPixelType)
                            * CopyFrom->ImageDesc.Height
                            * CopyFrom->ImageDesc.Width);
           memcpy(sp->RasterBits,
                 CopyFrom->RasterBits,
                 sizeof(GifPixelType)
                     * CopyFrom->ImageDesc.Height
                     * CopyFrom->ImageDesc.Width);

           /* finally, the extension blocks */
           if (sp->ExtensionBlocks)
           {
              sp->ExtensionBlocks
                  = (ExtensionBlock*)malloc(sizeof(ExtensionBlock)
                                         * CopyFrom->ExtensionBlockCount);
              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. 
               */
           }
       }

       return(sp);
    }
}

Here is the call graph for this function:

ColorMapObject* UnionColorMap ( ColorMapObject ColorIn1,
ColorMapObject ColorIn2,
GifPixelType  ColorTransIn2[] 
)

Definition at line 96 of file gifalloc.c.

{
    int i, j, CrntSlot, RoundUpTo, NewBitSize;
    ColorMapObject *ColorUnion;

    /*
     * Allocate table which will hold the result for sure.
     */
    ColorUnion
       = MakeMapObject(MAX(ColorIn1->ColorCount,ColorIn2->ColorCount)*2,NULL);

    if (ColorUnion == NULL)
       return(NULL);

    /* Copy ColorIn1 to ColorUnionSize; */
    for (i = 0; i < ColorIn1->ColorCount; i++)
       ColorUnion->Colors[i] = ColorIn1->Colors[i];
    CrntSlot = ColorIn1->ColorCount;

    /*
     * Potentially obnoxious hack:
     *
     * Back CrntSlot down past all contiguous {0, 0, 0} slots at the end
     * of table 1.  This is very useful if your display is limited to
     * 16 colors.
     */
    while (ColorIn1->Colors[CrntSlot-1].Red == 0
          && ColorIn1->Colors[CrntSlot-1].Green == 0
          && ColorIn1->Colors[CrntSlot-1].Red == 0)
       CrntSlot--;

    /* Copy ColorIn2 to ColorUnionSize (use old colors if they exist): */
    for (i = 0; i < ColorIn2->ColorCount && CrntSlot<=256; i++)
    {
       /* Let's see if this color already exists: */
       for (j = 0; j < ColorIn1->ColorCount; j++)
           if (memcmp(&ColorIn1->Colors[j], &ColorIn2->Colors[i], sizeof(GifColorType)) == 0)
              break;

       if (j < ColorIn1->ColorCount)
           ColorTransIn2[i] = j;   /* color exists in Color1 */
       else
       {
           /* Color is new - copy it to a new slot: */
           ColorUnion->Colors[CrntSlot] = ColorIn2->Colors[i];
           ColorTransIn2[i] = CrntSlot++;
       }
    }

    if (CrntSlot > 256)
    {
       FreeMapObject(ColorUnion);
       return((ColorMapObject *)NULL);
    }

    NewBitSize = BitSize(CrntSlot);
    RoundUpTo = (1 << NewBitSize);

    if (RoundUpTo != ColorUnion->ColorCount)
    {
       register GifColorType       *Map = ColorUnion->Colors;

       /*
        * Zero out slots up to next power of 2.
        * We know these slots exist because of the way ColorUnion's
        * start dimension was computed.
        */
       for (j = CrntSlot; j < RoundUpTo; j++)
           Map[j].Red = Map[j].Green = Map[j].Blue = 0;

       /* perhaps we can shrink the map? */
       if (RoundUpTo < ColorUnion->ColorCount)
           ColorUnion->Colors 
              = (GifColorType *)realloc(Map, sizeof(GifColorType)*RoundUpTo);
    }

    ColorUnion->ColorCount = RoundUpTo;
    ColorUnion->BitsPerPixel = NewBitSize;

    return(ColorUnion);
}

Here is the call graph for this function: