使用libgif将图片转换为gif文件

伪代码

 1 void func()
 2 {
 3     /* 创建gif文件 strFilePath:文件保存路径*/
 4     GifFileType *pGifFile = EGifOpenFileName(strFilePath, FALSE, &iError);
 5 
 6     /* 版本使用89a支持动画 */
 7     EGifSetGifVersion(pGifFile, TRUE);
 8 
 9     /* iWidth, iHeight, gif文件分辨率;不使用全局颜色列表 */
10     EGifPutScreenDesc(pGifFile, iWidth, iHeight, 1, 0, NULL)
11 
12     /* 处理每一帧 */
13     for (size_t i = 0; i < count; i++)
14     {
15         /* 初始化图像控制帧参数,控制当前帧的播放 */
16         GraphicsControlBlock stGCB = {0};
17         stGCB.DisposalMode = DISPOSAL_UNSPECIFIED;    /*  */
18         stGCB.DelayTime = iDelayTime;    /* 当前帧播放时间 */
19         stGCB.UserInputFlag = FALSE;
20         stGCB.TransparentColor = NO_TRANSPARENT_COLOR;
21 
22         /* 图像控制帧序列化 */
23         GifByteType aGifExtension[4] = {0};
24         EGifGCBToExtension(&stGCB, aGifExtension);
25 
26         /* 写入图像控制帧 */
27         EGifPutExtension(pGifFile, GRAPHICS_EXT_FUNC_CODE, sizeof(aGifExtension), &aGifExtension)
28 
29         /* 将源图片转为RGB图,并分裂出R、G、B,分别存储在pRedBuffer, pGreenBuffer, pBlueBuffer,*/
30         /* 
31         ...........
32          */
33         /* 生成每帧图像的颜色列表与基于颜色列表的数据 */
34         GifByteType         *pOutputBuffer;          /* 每帧图像的颜色列表 */
35         ColorMapObject      *pOutputColorMap;        /* 基于颜色列表的图像数据 */
36         int iColorMapSize = 256;/* 颜色列表大小,0-256 */
37         /* iWidth, iHeight, gif文件分辨率 */
38         GifQuantizeBuffer(iWidth, iHeight, iColorMapSize,
39                               pRedBuffer, pGreenBuffer, pBlueBuffer,
40                               pOutputBuffer, pOutputColorMap->Colors)
41 
42         /* 写入每帧图像的图像标识符,与颜色列表 */
43         EGifPutImageDesc(pGifFile, 0, 0, iWidth, iHeight, FALSE, pOutputColorMap)
44 
45         /* 写入每帧图像的图像数据 */
46         pTmp = pOutputBuffer;
47         for (iIndex = 0; iIndex < iHeight; iIndex++)
48         {
49             if (GIF_ERROR == EGifPutLine(pGifFile, pTmp, iWidth))
50             {
51                 GIF_ERRORS("EGifPutLine failed \n");
52                 return ERROR;
53             }
54             pTmp += iWidth;
55         }
56         sync();
57     }
58 
59     /* 关闭文件写入终止符 */
60     EGifCloseFile(pGifFile, NULL)
61     
62 }

 

 

参考文献:https://www.cnblogs.com/stronghorse/p/16002888.html

posted @ 2022-11-01 09:10  墨尔基阿德斯  阅读(111)  评论(0编辑  收藏  举报