8181#ifndef RGIF_H
8282#define RGIF_H
8383
84- #include <stdio.h> // Required for: FILE
84+ #include <stdio.h> // Required for: FILE
8585
8686//#define RGIF_STATIC
8787#ifdef RGIF_STATIC
@@ -117,16 +117,6 @@ RGIFDEF bool GifEnd();
117117#include <stdio.h> // Required for: FILE, fopen(), fclose()
118118#include <string.h> // Required for: memcpy()
119119
120- // Define these macros to hook into a custom memory allocator.
121- // RGIF_TEMP_MALLOC and RGIF_TEMP_FREE will only be called in stack fashion - frees in the reverse order of mallocs
122- // and any temp memory allocated by a function will be freed before it exits.
123- #if !defined(RGIF_TEMP_MALLOC )
124- #include <stdlib.h>
125-
126- #define RGIF_TEMP_MALLOC malloc
127- #define RGIF_TEMP_FREE free
128- #endif
129-
130120// Check if custom malloc/free functions defined, if not, using standard ones
131121// RGIF_MALLOC and RGIF_FREE are used only by GifBegin and GifEnd respectively,
132122// to allocate a buffer the size of the image, which is used to find changed pixels for delta-encoding.
@@ -185,7 +175,7 @@ typedef struct GifLzwNode {
185175//----------------------------------------------------------------------------------
186176const int gifTransparentIndex = 0 ; // Transparent color index
187177
188- static FILE * gifFile ;
178+ static FILE * gifFile = NULL ;
189179unsigned char * gifFrame ;
190180
191181//----------------------------------------------------------------------------------
@@ -201,9 +191,10 @@ static void GifMakePalette(const unsigned char *lastFrame, const unsigned char *
201191static void GifDitherImage (const unsigned char * lastFrame , const unsigned char * nextFrame , unsigned char * outFrame , unsigned int width , unsigned int height , GifPalette * pPal );
202192static void GifThresholdImage (const unsigned char * lastFrame , const unsigned char * nextFrame , unsigned char * outFrame , unsigned int width , unsigned int height , GifPalette * pPal );
203193static void GifWriteBit (GifBitStatus * stat , unsigned int bit );
194+
204195static void GifWriteChunk (FILE * f , GifBitStatus * stat );
196+ static void GifWritePalette (FILE * f , const GifPalette * pPal );
205197static void GifWriteCode (FILE * f , GifBitStatus * stat , unsigned int code , unsigned int length );
206- static void GifWritePalette (const GifPalette * pPal , FILE * f );
207198static void GifWriteLzwImage (FILE * f , unsigned char * image , unsigned int left , unsigned int top , unsigned int width , unsigned int height , unsigned int delay , GifPalette * pPal );
208199
209200//----------------------------------------------------------------------------------
@@ -591,7 +582,7 @@ static void GifMakePalette(const unsigned char *lastFrame, const unsigned char *
591582 // SplitPalette is destructive (it sorts the pixels by color) so
592583 // we must create a copy of the image for it to destroy
593584 int imageSize = width * height * 4 * sizeof (unsigned char );
594- unsigned char * destroyableImage = (unsigned char * )RGIF_TEMP_MALLOC (imageSize );
585+ unsigned char * destroyableImage = (unsigned char * )RGIF_MALLOC (imageSize );
595586 memcpy (destroyableImage , nextFrame , imageSize );
596587
597588 int numPixels = width * height ;
@@ -604,7 +595,7 @@ static void GifMakePalette(const unsigned char *lastFrame, const unsigned char *
604595
605596 GifSplitPalette (destroyableImage , numPixels , 1 , lastElt , splitElt , splitDist , 1 , buildForDither , pPal );
606597
607- RGIF_TEMP_FREE (destroyableImage );
598+ RGIF_FREE (destroyableImage );
608599
609600 // add the bottom node for the transparency index
610601 pPal -> treeSplit [1 << (bitDepth - 1 )] = 0 ;
@@ -621,7 +612,7 @@ static void GifDitherImage(const unsigned char *lastFrame, const unsigned char *
621612 // quantPixels initially holds color*256 for all pixels
622613 // The extra 8 bits of precision allow for sub-single-color error values
623614 // to be propagated
624- int * quantPixels = (int * )RGIF_TEMP_MALLOC (sizeof (int )* numPixels * 4 );
615+ int * quantPixels = (int * )RGIF_MALLOC (sizeof (int )* numPixels * 4 );
625616
626617 for (int ii = 0 ; ii < numPixels * 4 ; ++ ii )
627618 {
@@ -719,7 +710,7 @@ static void GifDitherImage(const unsigned char *lastFrame, const unsigned char *
719710 outFrame [ii ] = quantPixels [ii ];
720711 }
721712
722- RGIF_TEMP_FREE (quantPixels );
713+ RGIF_FREE (quantPixels );
723714}
724715
725716// Picks palette colors for the image using simple thresholding, no dithering
@@ -805,7 +796,7 @@ static void GifWriteCode(FILE *f, GifBitStatus *stat, unsigned int code, unsigne
805796}
806797
807798// write a 256-color (8-bit) image palette to the file
808- static void GifWritePalette (const GifPalette * pPal , FILE * f )
799+ static void GifWritePalette (FILE * f , const GifPalette * pPal )
809800{
810801 fputc (0 , f ); // first color: transparency
811802 fputc (0 , f );
@@ -852,14 +843,14 @@ static void GifWriteLzwImage(FILE *f, unsigned char *image, unsigned int left, u
852843 //fputc(0x80, f); // no local color table, but transparency
853844
854845 fputc (0x80 + pPal -> bitDepth - 1 , f ); // local color table present, 2 ^ bitDepth entries
855- GifWritePalette (pPal , f );
846+ GifWritePalette (f , pPal );
856847
857848 const int minCodeSize = pPal -> bitDepth ;
858849 const unsigned int clearCode = 1 << pPal -> bitDepth ;
859850
860851 fputc (minCodeSize , f ); // min code size 8 bits
861852
862- GifLzwNode * codetree = (GifLzwNode * )RGIF_TEMP_MALLOC (sizeof (GifLzwNode )* 4096 );
853+ GifLzwNode * codetree = (GifLzwNode * )RGIF_MALLOC (sizeof (GifLzwNode )* 4096 );
863854
864855 memset (codetree , 0 , sizeof (GifLzwNode )* 4096 );
865856 int curCode = -1 ;
@@ -933,7 +924,7 @@ static void GifWriteLzwImage(FILE *f, unsigned char *image, unsigned int left, u
933924
934925 fputc (0 , f ); // image block terminator
935926
936- RGIF_TEMP_FREE (codetree );
927+ RGIF_FREE (codetree );
937928}
938929
939930#endif // RGIF_IMPLEMENTATION
0 commit comments