利用CxImage实现编解码Gif图像代码举例
2015-02-05 21:59
549 查看
Gif(GraphicsInterchange Format,图形交换格式)是由CompuServe公司在1987年开发的图像文件格式,分为87a和89a两种版本。Gif是基于LZW算法的无损压缩算法。Gif图像是基于颜色表的,最多只支持8位(256色)。Gif减少了图像调色板中的色彩数量,从而在存储时达到减少图像文件大小的目的。Gif分为静态Gif和动画Gif两种,扩展名为.gif,是一种压缩位图格式,支持透明背景图像,适用于多种操作系统。
下面利用CxImage开源库,实现对Gif图像进行编解码,主要包括3个文件:
1. funset.h :
2. funset.cpp:
利用CxImage实现起来简单吧!!!^_^
下面利用CxImage开源库,实现对Gif图像进行编解码,主要包括3个文件:
1. funset.h :
#ifndef _FUNSET_H_ #define _FUNSET_H_ #include <string> using namespace std; void decoding_gif(string strGifName, string strSavePath); void encoding_gif(string strImgPath, string strGifName); #endif //_FUNSET_H_
2. funset.cpp:
#include "stdafx.h" #include "funset.h" #include <iostream> #include "../CxImage/ximagif.h" #include <io.h> using namespace std; void decoding_gif(string strGifName, string strSavePath) { CxImage img; img.Load(strGifName.c_str(), CXIMAGE_FORMAT_GIF); int iNumFrames = img.GetNumFrames(); cout<<"frames num = "<<iNumFrames<<endl; CxImage* newImage = new CxImage(); for (int i = 0; i < iNumFrames; i++) { newImage->SetFrame(i); newImage->Load(strGifName.c_str(), CXIMAGE_FORMAT_GIF); char tmp[64]; sprintf(tmp, "%d", i); string tmp1; tmp1 = tmp1.insert(0, tmp); tmp1 = strSavePath + tmp1 + ".png"; newImage->Save(tmp1.c_str(), CXIMAGE_FORMAT_PNG); } if (newImage) delete newImage; } int TraverseFolder(const string strFilePath, string strImageNameSets[]) { int iImageCount=0; _finddata_t fileInfo; long handle = _findfirst(strFilePath.c_str(), &fileInfo); if (handle == -1L) { cerr << "failed to transfer files" << endl; return -1; } do { //cout << fileInfo.name <<endl; strImageNameSets[iImageCount] = (string)fileInfo.name; iImageCount ++; } while (_findnext(handle, &fileInfo) == 0); return iImageCount; } void encoding_gif(string strImgPath, string strGifName) { string strImgSets[100] = {}; int iImgCount = TraverseFolder(strImgPath, strImgSets); string strTmp = strImgPath.substr(0, strImgPath.find_last_of("/") +1); CxImage** img = new CxImage*[iImgCount]; if (img == NULL) { cout<<"new Cximage error!"<<endl; return; } for (int i = 0; i < iImgCount; i++) { string tmp1; tmp1 = strTmp + strImgSets[i]; img[i] = new CxImage; img[i]->Load(tmp1.c_str(), CXIMAGE_FORMAT_PNG); } CxIOFile hFile; hFile.Open(strGifName.c_str(), "wb"); CxImageGIF multiimage; multiimage.SetLoops(3); multiimage.SetDisposalMethod(2); multiimage.Encode(&hFile, img, iImgCount, false, false); hFile.Close(); delete [] img; }3. main.cpp:
#include "stdafx.h" #include <iostream> #include "funset.h" using namespace std; int main(int argc, char* argv[]) { string strGifName = "../../Data/fire.gif"; string strSavaPath = "../../Data/"; decoding_gif(strGifName, strSavaPath); string strImgPath = "../../Data/*.png"; strGifName = "../../Data/tmp.gif"; encoding_gif(strImgPath, strGifName); cout<<"ok!!!"<<endl; return 0; }
利用CxImage实现起来简单吧!!!^_^
相关文章推荐
- 利用CxImage实现编解码Gif图像代码举例
- TensorFlow实战5:利用卷积神经网络对图像分类(初阶:MNIST手写数字)代码实现
- 利用均值漂移实现图像分割的原理和OpenCV代码
- 利用OpenCV实现图像拼接的代码!
- 利用GDI+实现gif图像背景透明
- 利用IPicture接口实现显示BMP/JPG/GIF图像
- 利用OpenCV实现旋转文本图像矫正的原理及OpenCV代码
- 利用最大熵进行阈值分割从而实现灰度图像的二值化的原理概要及OpenCV代码
- 在 Django 的 View 中利用 function decorator 可实现一定程度的代码重用
- 利用 FxCop 实现代码更正
- 利用WinInet实现网页代码截取
- 在 Django 的 View 中利用 function decorator 可实现一定程度的代码重用
- asp.net下利用JS实现对后台CS代码的调用方法
- 转载:利用MS SQL实现异构数据库的分布式查询的t-sql代码
- 使用vb.net代码实现图像水印技术
- 从gif动画图像中导出桢的方法实现
- 利用newxy(新坐标),不写一行java代码实现多项查询
- 利用setTimeout()函数实现函数代码的延迟执行
- 利用ADODB写文件——恶意文件young.gif代码的分析心得1
- 利用USB2.0接口的芯片CY7C68013和单片机实现全数字图像的实时采集