#include "stdafx.h"
#include "CxImageManage.h"
#include "Quantize.h"
#include "ximage_include/ximage.h"
#include "ximage_include/ximagif.h"
// 加载图片格式处理静态库,一定要按照这个顺序导入静态库,不然会有很多错误
#pragma comment(lib, "ximage/ximage_lib/jasper.lib")
#pragma comment(lib, "ximage/ximage_lib/jbig.lib")
#pragma comment(lib, "ximage/ximage_lib/Jpeg.lib")
#pragma comment(lib, "ximage/ximage_lib/mng.lib")
#pragma comment(lib, "ximage/ximage_lib/png.lib")
#pragma comment(lib, "ximage/ximage_lib/Tiff.lib")
#pragma comment(lib, "ximage/ximage_lib/zlib.lib")
#pragma comment(lib, "ximage/ximage_lib/cximagecrtu.lib")
#pragma comment(lib, "ximage/ximage_lib/cximage.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CxImageManage::CxImageManage()
{
GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL); //启用GDI+
}
CxImageManage::~CxImageManage()
{
GdiplusShutdown(m_gdiplusToken); //释放GDI+
}
bool CxImageManage::ZoomGif(CString strGifPath, int nNewX, int nNewY, CString strSavePath)
{
CxImageGIF ci;
if (!ci.Load(strGifPath, CXIMAGE_FORMAT_GIF))
return false;
int nFrameCount = ci.GetNumFrames();//获取gif帧数
CxImage ** arrImage = new CxImage *[nFrameCount];//创建指针数组
for (int i = 0; i < nFrameCount; i++)
{
arrImage[i] = new CxImage();
CxImage im;
im.SetFrame(i);
im.Load(strGifPath, CXIMAGE_FORMAT_GIF);
im.Resample(nNewX, nNewY, 0, arrImage[i]);
// arrImage[i]->SetFrame(i);
// arrImage[i]->Load(strGifPath, CXIMAGE_FORMAT_GIF);
//
//
// CString str;
// str.Format(_T("output\\%d.bmp"), i);
// arrImage[i]->Save(str, CXIMAGE_FORMAT_BMP);
}
CxIOFile hFile;
hFile.Open(strSavePath, _T("wb"));
CxImageGIF multiimage;
multiimage.SetFrameDelay(ci.GetFrameDelay());
multiimage.SetLoops(ci.GetLoops());
multiimage.SetDisposalMethod(ci.GetDisposalMethod());
multiimage.Encode(&hFile, arrImage, nFrameCount, false, false);
hFile.Close();
#ifdef _DEBUG
#else
for (int i = 0; i < nFrameCount; i++)
{
::delete arrImage[i];
}
#endif
delete[] arrImage;
return true;
}
bool CxImageManage::ZoomGifByGDIPlusAndCxImage(CString strGifPath, int nNewX, int nNewY, CString strSavePath)
{
CString strTmpDir = strSavePath;
strTmpDir = strTmpDir.Left(strTmpDir.ReverseFind('\\')) + _T("\\tmp_gif");
if (!PathFileExists(strTmpDir) && !CreateDirectory(strTmpDir, nullptr))
return false;
int nCount = ParseGifToPng(strGifPath, strTmpDir, nNewX, nNewY);
if (nCount < 1)
return false;
bool bSucceed = EncodingGif(strGifPath, strTmpDir, strSavePath, nCount);
#if _DEBUG
#else
DeleteDirectory(strTmpDir);
#endif
return bSucceed;
}
int CxImageManage::GetEncoderClsid(const WCHAR * format, CLSID * pClsid)//GDI+将像素保存为图片
{
UINT num = 0;
UINT size = 0;
ImageCodecInfo * pImageCodeInfo = NULL;
GetImageEncodersSize(&num, &size);
if (size == 0)
return -1;
pImageCodeInfo = (ImageCodecInfo *)(malloc(size));
if (pImageCodeInfo == NULL)
return -1;
GetImageEncoders(num, size, pImageCodeInfo);
for (UINT j = 0; j < num; j++)
{
if (wcscmp(pImageCodeInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodeInfo[j].Clsid;
free(pImageCodeInfo);
return j;
}
}
free(pImageCodeInfo);
return -1;
}
int CxImageManage::ParseGifToPng(CString strGifPath, CString strSaveDir)
{
Image im(strGifPath);
if (im.GetLastStatus() != Ok)
return 0;
GUID Guid;
//获得有多少个维度,对于gif就一个维度
UINT count = im.GetFrameDimensionsCount();
GUID *pDimensionIDs = (GUID*)new GUID[count];
im.GetFrameDimensionsList(pDimensionIDs, count);
WCHAR strGuid[39];
StringFromGUID2(pDimensionIDs[0], strGuid, 39);
int frameCount = im.GetFrameCount(&pDimensionIDs[0]);
delete[] pDimensionIDs;
//获得各帧之间的时间间隔
//先获得有多少个时间间隔,PropertyTagFrameDelay是GDI+中预定义的一个GIG属性ID值,表示标签帧数据的延迟时间
UINT FrameDelayNums = im.GetPropertyItemSize(PropertyTagFrameDelay);
PropertyItem* lpPropertyItem = new PropertyItem[FrameDelayNums];
im.GetPropertyItem(PropertyTagFrameDelay, FrameDelayNums, lpPropertyItem);
Guid = FrameDimensionTime;
GUID pngGUID;
GetEncoderClsid(_T("image/png"), &pngGUID);
for (int i = 0; i < frameCount;i ++)
{
CString strTmp;
strTmp.Format(_T("%s\\%d.png"), strSaveDir, i);
im.SelectActiveFrame(&Guid, i);
im.Save(strTmp, &pngGUID);
}
delete lpPropertyItem;
return frameCount;
}
int CxImageManage::ParseGifToPng(CString strGifPath, CString strSaveDir, int nNewX, int nNewY)
{
Image im(strGifPath);
if (im.GetLastStatus() != Ok)
return 0;
GUID Guid;
//获得有多少个维度,对于gif就一个维度
UINT count = im.GetFrameDimensionsCount();
GUID *pDimensionIDs = (GUID*)new GUID[count];
im.GetFrameDimensionsList(pDimensionIDs, count);
WCHAR strGuid[39];
StringFromGUID2(pDimensionIDs[0], strGuid, 39);
int frameCount = im.GetFrameCount(&pDimensionIDs[0]);
delete[] pDimensionIDs;
//获得各帧之间的时间间隔
//先获得有多少个时间间隔,PropertyTagFrameDelay是GDI+中预定义的一个GIG属性ID值,表示标签帧数据的延迟时间
UINT FrameDelayNums = im.GetPropertyItemSize(PropertyTagFrameDelay);
PropertyItem* lpPropertyItem = new PropertyItem[FrameDelayNums];
im.GetPropertyItem(PropertyTagFrameDelay, FrameDelayNums, lpPropertyItem);
Guid = FrameDimensionTime;
GUID pngGUID;
GetEncoderClsid(_T("image/png"), &pngGUID);
for (int i = 0; i < frameCount;i ++)
{
CString strTmp;
strTmp.Format(_T("%s\\%d.png"), strSaveDir, i);
im.SelectActiveFrame(&Guid, i);
//im.Save(strTmp, &pngGUID);
Bitmap bmp(nNewX, nNewY, PixelFormat32bppARGB);
Rect rt(0, 0, nNewX, nNewY);
Graphics * g = Graphics::FromImage(&bmp);
g->DrawImage(&im, rt);
bmp.Save(strTmp, &pngGUID);
delete g;
}
delete lpPropertyItem;
return frameCount;
}
int CxImageManage::ParseGifToPngByCximage(CString strGifPath, CString strSaveDir)
{
CxImage img;
img.Load(strGifPath, CXIMAGE_FORMAT_GIF);
int iNumFrames = img.GetNumFrames();
CxImage* newImage = new CxImage();
for (int i = 0; i < iNumFrames; i++)
{
newImage->SetFrame(i);
newImage->Load(strGifPath, CXIMAGE_FORMAT_GIF);
CString strTmp;
strTmp.Format(_T("%s\\%d.png"), strSaveDir, i);
newImage->Save(strTmp, CXIMAGE_FORMAT_PNG);
}
#if _DEBUG
#else
if (newImage)
delete newImage;
#endif
return iNumFrames;
}
//当前函数在debug下会有内存泄露,应为delete CxImage会导致程序崩溃,暂时未找出原因。猜测可能是debug模式下CxImage内部有内存泄露
bool CxImageManage::EncodingGif(CString strOldGifPath, CString strSaveDir, CString strNewGifPath, int nCount)
{
bool bSucceed = false;
int nDelay = 20;
int nLoops = -1;
char chMethod = 2;
int nFrameCount = -1;
std::vector<gif_frame_inform> vecInform;
if (!strOldGifPath.IsEmpty())
{
CxImageGIF xImGif;
if (!xImGif.Load(strOldGifPath, CXIMAGE_FORMAT_GIF))
return false;
nLoops = xImGif.GetLoops();
nFrameCount = xImGif.GetNumFrames();
chMethod = /*xImGif.GetDisposalMethod()*/2;
for (int i = 0; i < nFrameCount; i++)
{
gif_frame_inform gfi;
gfi.m_nDelay = xImGif.GetFrameDelay();
gfi.m_ucMethod = /*xImGif.GetDisposalMethod()*/0;
vecInform.push_back(gfi);
}
}
int nRealCount = 0;
std::vector<CString> vecPng;
nRealCount = GetNeedPngPath(strSaveDir, vecPng, nCount);
SortPngPath(vecPng);
if (nRealCount < 1 || nFrameCount != nRealCount)
return false;
CxImage** imgArr = new CxImage*[nRealCount];
if (imgArr == nullptr)
return false;
for (int i = 0; i < nRealCount; i++)
{
imgArr[i] = new CxImage;
imgArr[i]->Load(vecPng[i], CXIMAGE_FORMAT_PNG);
if (imgArr[i]->IsValid())
{
if (imgArr[i]->GetBpp() != 24)
{
imgArr[i]->IncreaseBpp(24);
}
imgArr[i]->DecreaseBpp(8, 0, 0);//保存为gif格式通常要求1、4、8位的色
没有合适的资源?快使用搜索试试~ 我知道了~
CxImage7.02静态库和动态库
共40个文件
h:26个
lib:11个
cpp:2个
需积分: 0 4 下载量 89 浏览量
2023-04-21
15:18:41
上传
评论
收藏 3.4MB RAR 举报
温馨提示
使用vs2015编译出来的Unicode Release x86版本的静态库和动态库。适合MFC程序使用。具体的使用方法请查看我的博客
资源推荐
资源详情
资源评论
收起资源包目录
ximage.rar (40个子文件)
ximage
ximage_lib
libdcr.lib 707KB
Jpeg.lib 1.25MB
Tiff.lib 1.27MB
mng.lib 1.72MB
png.lib 992KB
jasper.lib 1.78MB
zlib.lib 210KB
libpsd.lib 1.03MB
cximage.lib 2.31MB
jbig.lib 146KB
cximagecrtu.lib 151KB
cximagecrtu.dll 2.03MB
CxImageManage.cpp 12KB
CxImageManage.h 2KB
Quantize.h 3KB
Quantize.cpp 8KB
ximage_include
ximajbg.h 1KB
ximapcx.h 2KB
ximage.h 31KB
ximajpg.h 9KB
xmemfile.h 1KB
ximajas.h 3KB
ximapsd.h 3KB
ximagif.h 8KB
ximatga.h 2KB
ximaraw.h 3KB
ximamng.h 2KB
ximacfg.h 2KB
ximaiter.h 7KB
ximath.h 802B
ximadef.h 4KB
ximabmp.h 3KB
ximawbmp.h 1KB
xiofile.h 3KB
ximatif.h 2KB
ximawmf.h 5KB
xfile.h 3KB
ximaska.h 1KB
ximaico.h 2KB
ximapng.h 3KB
共 40 条
- 1
资源评论
天上的猩猩Y
- 粉丝: 116
- 资源: 53
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功