这是一段将几个RGB24的图像编码成一个gif文件的测试/Demo源码。使用的是libgif。
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <gif_lib.h>
-
- #define PROGRAM_NAME "gifenc"
-
- static int ColorFlag = FALSE;
- static int ExpNumOfColors = 8;
- static int HelpFlag = FALSE;
- static int ColorMapSize = 256;
-
- static void QuitGifError(GifFileType *GifFile)
- {
- PrintGifError();
- if (GifFile != NULL)
- EGifCloseFile(GifFile);
- exit(EXIT_FAILURE);
- }
-
- static void LoadRGB(char *FileName,
- GifByteType **RedBuffer,
- GifByteType **GreenBuffer,
- GifByteType **BlueBuffer,
- int Width, int Height)
- {
- int i, j;
- unsigned long Size;
- GifByteType *RedP, *GreenP, *BlueP;
- FILE *f[3];
-
- Size = ((long) Width) * Height * sizeof(GifByteType);
-
- if ((*RedBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL ||
- (*GreenBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL ||
- (*BlueBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL)
- GIF_EXIT("Failed to allocate memory required, aborted.");
-
- RedP = *RedBuffer;
- GreenP = *GreenBuffer;
- BlueP = *BlueBuffer;
-
- if ((f[0] = fopen(FileName, "rb")) == NULL)
- GIF_EXIT("Can't open input file name.");
-
- GifByteType *Buffer, *BufferP;
-
- if ((Buffer = (GifByteType *) malloc(Width * 3)) == NULL)
- GIF_EXIT("Failed to allocate memory required, aborted.");
-
- for (i = 0; i < Height; i++)
- {
- GifQprintf("\b\b\b\b%-4d", i);
- if (fread(Buffer, Width * 3, 1, f[0]) != 1)
- GIF_EXIT("Input file(s) terminated prematurly.");
- for (j = 0, BufferP = Buffer; j < Width; j++)
- {
- *RedP++ = *BufferP++;
- *GreenP++ = *BufferP++;
- *BlueP++ = *BufferP++;
- }
- }
-
- free((char *) Buffer);
- fclose(f[0]);
- }
-
- static void SaveGif(GifByteType *OutputBuffer,
- ColorMapObject *OutputColorMap,
- int ExpColorMapSize, int Width, int Height)
- {
- int i;
- GifFileType *GifFile;
- GifByteType *Ptr = OutputBuffer;
-
-
- if ((GifFile = EGifOpenFileName("1.gif", TRUE)) == NULL)
- QuitGifError(GifFile);
-
- if (EGifPutScreenDesc(GifFile, Width, Height, ExpColorMapSize, 0, OutputColorMap)
- == GIF_ERROR ||
- EGifPutImageDesc(GifFile, 0, 0, Width, Height, FALSE, NULL) == GIF_ERROR)
- {
- QuitGifError(GifFile);
- }
- GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]: ",
- PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top,
- GifFile->Image.Width, GifFile->Image.Height);
-
- for (i = 0; i < Height; i++)
- {
- if (EGifPutLine(GifFile, Ptr, Width) == GIF_ERROR)
- QuitGifError(GifFile);
- GifQprintf("\b\b\b\b%-4d", Height - i - 1);
-
- Ptr += Width;
- }
-
- if (EGifCloseFile(GifFile) == GIF_ERROR)
- QuitGifError(GifFile);
- }
-
-
-
- int main(int argc, char **argv)
- {
- int Width = 2048, Height= 1536;
- char *FileName = "1.rgb";
- GifByteType *RedBuffer = NULL, *GreenBuffer = NULL, *BlueBuffer = NULL,
- *OutputBuffer = NULL;
- ColorMapObject *OutputColorMap = NULL;
-
- ColorMapSize = 1 << ExpNumOfColors;
-
- LoadRGB(FileName, &RedBuffer, &GreenBuffer, &BlueBuffer, Width, Height);
-
- if ((OutputColorMap = MakeMapObject(ColorMapSize, NULL)) == NULL ||
- (OutputBuffer = (GifByteType *) malloc(Width * Height * sizeof(GifByteType))) == NULL)
- GIF_EXIT("Failed to allocate memory required, aborted.");
-
- if (QuantizeBuffer(Width, Height, &ColorMapSize,RedBuffer, GreenBuffer, BlueBuffer
- , OutputBuffer, OutputColorMap->Colors) == GIF_ERROR)
- QuitGifError(NULL);
- free((char *) RedBuffer);
- free((char *) GreenBuffer);
- free((char *) BlueBuffer);
-
- SaveGif(OutputBuffer, OutputColorMap, ExpNumOfColors, Width, Height);
-
- return 0;
- }
(carlsonlee_freec) |