织梦CMS - 轻松建站从此开始!

罗索

encode RGB24 to gif, using libgif

jackyhwei 发布于 2011-05-16 16:29 点击:次 
这是一段将几个RGB24的图像编码成一个gif文件的测试/Demo源码。使用的是libgif。
TAG:

这是一段将几个RGB24的图像编码成一个gif文件的测试/Demo源码。使用的是libgif。

  1. #include <stdlib.h> 
  2. #include <stdio.h> 
  3. #include <ctype.h> 
  4. #include <string.h> 
  5. #include <gif_lib.h> 
  6.  
  7. #define PROGRAM_NAME "gifenc" 
  8.  
  9. static int    ColorFlag = FALSE; 
  10. static int    ExpNumOfColors = 8; 
  11. static int    HelpFlag = FALSE; 
  12. static int    ColorMapSize = 256; 
  13.  
  14. static void QuitGifError(GifFileType *GifFile) 
  15.     PrintGifError(); 
  16.     if (GifFile != NULL) 
  17.         EGifCloseFile(GifFile); 
  18.     exit(EXIT_FAILURE); 
  19.  
  20. static void LoadRGB(char *FileName, 
  21.             GifByteType **RedBuffer, 
  22.             GifByteType **GreenBuffer, 
  23.             GifByteType **BlueBuffer, 
  24.             int Width, int Height) 
  25.     int i, j; 
  26.     unsigned long Size; 
  27.     GifByteType *RedP, *GreenP, *BlueP; 
  28.     FILE *f[3]; 
  29.  
  30.     Size = ((long) Width) * Height * sizeof(GifByteType); 
  31.  
  32.     if ((*RedBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL || 
  33.         (*GreenBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL || 
  34.         (*BlueBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL) 
  35.         GIF_EXIT("Failed to allocate memory required, aborted."); 
  36.  
  37.     RedP = *RedBuffer; 
  38.     GreenP = *GreenBuffer; 
  39.     BlueP = *BlueBuffer; 
  40.  
  41.     if ((f[0] = fopen(FileName, "rb")) == NULL) 
  42.         GIF_EXIT("Can't open input file name."); 
  43.  
  44.     GifByteType *Buffer, *BufferP; 
  45.  
  46.     if ((Buffer = (GifByteType *) malloc(Width * 3)) == NULL) 
  47.         GIF_EXIT("Failed to allocate memory required, aborted."); 
  48.  
  49.     for (i = 0; i < Height; i++) 
  50.     { 
  51.         GifQprintf("\b\b\b\b%-4d", i); 
  52.         if (fread(Buffer, Width * 3, 1, f[0]) != 1) 
  53.             GIF_EXIT("Input file(s) terminated prematurly."); 
  54.         for (j = 0, BufferP = Buffer; j < Width; j++) 
  55.         { 
  56.             *RedP++ = *BufferP++; 
  57.             *GreenP++ = *BufferP++; 
  58.             *BlueP++ = *BufferP++; 
  59.         } 
  60.     } 
  61.  
  62.     free((char *) Buffer); 
  63.     fclose(f[0]); 
  64.  
  65. static void SaveGif(GifByteType *OutputBuffer, 
  66.             ColorMapObject *OutputColorMap, 
  67.             int ExpColorMapSize, int Width, int Height) 
  68.     int i; 
  69.     GifFileType *GifFile; 
  70.     GifByteType *Ptr = OutputBuffer; 
  71.  
  72.     /* Open stdout for the output file: */ 
  73.     if ((GifFile = EGifOpenFileName("1.gif", TRUE)) == NULL) 
  74.         QuitGifError(GifFile); 
  75.  
  76.     if (EGifPutScreenDesc(GifFile, Width, Height, ExpColorMapSize, 0, OutputColorMap)
  77.  == GIF_ERROR || 
  78.         EGifPutImageDesc(GifFile, 0, 0, Width, Height, FALSE, NULL) == GIF_ERROR) 
  79.     { 
  80.         QuitGifError(GifFile); 
  81.     } 
  82.     GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]:     "
  83.            PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top, 
  84.            GifFile->Image.Width, GifFile->Image.Height); 
  85.  
  86.     for (i = 0; i < Height; i++) 
  87.     { 
  88.         if (EGifPutLine(GifFile, Ptr, Width) == GIF_ERROR) 
  89.             QuitGifError(GifFile); 
  90.         GifQprintf("\b\b\b\b%-4d", Height - i - 1); 
  91.  
  92.         Ptr += Width; 
  93.     } 
  94.  
  95.     if (EGifCloseFile(GifFile) == GIF_ERROR) 
  96.         QuitGifError(GifFile); 
  97.  
  98.  
  99.  
  100. int main(int argc, char **argv) 
  101.     int    Width = 2048, Height= 1536; 
  102.     char *FileName = "1.rgb"
  103.     GifByteType *RedBuffer = NULL, *GreenBuffer = NULL, *BlueBuffer = NULL, 
  104.     *OutputBuffer = NULL; 
  105.     ColorMapObject *OutputColorMap = NULL; 
  106.  
  107.     ColorMapSize = 1 << ExpNumOfColors; 
  108.  
  109.     LoadRGB(FileName, &RedBuffer, &GreenBuffer, &BlueBuffer, Width, Height); 
  110.  
  111.     if ((OutputColorMap = MakeMapObject(ColorMapSize, NULL)) == NULL || 
  112.         (OutputBuffer = (GifByteType *) malloc(Width * Height * sizeof(GifByteType))) == NULL) 
  113.         GIF_EXIT("Failed to allocate memory required, aborted."); 
  114.  
  115.     if (QuantizeBuffer(Width, Height, &ColorMapSize,RedBuffer, GreenBuffer, BlueBuffer
  116. , OutputBuffer, OutputColorMap->Colors) == GIF_ERROR) 
  117.         QuitGifError(NULL); 
  118.     free((char *) RedBuffer); 
  119.     free((char *) GreenBuffer); 
  120.     free((char *) BlueBuffer); 
  121.  
  122.     SaveGif(OutputBuffer, OutputColorMap, ExpNumOfColors, Width, Height); 
  123.  
  124.     return 0; 

 

(carlsonlee_freec)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201105/11784.html]
本文出处:百度博客 作者:carlsonlee_freec
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容