FreeImage例子

举报
风吹稻花香 发表于 2021/06/05 00:06:56 2021/06/05
【摘要】 http://www.pudn.com/downloads525/sourcecode/windows/detail2176862.html #include "OpenJPEG.h" #include "unistd.h" OpenJPEG::OpenJPEG(void) { } OpenJPEG::~OpenJPEG(void) { } FIBITMAP* Open...

http://www.pudn.com/downloads525/sourcecode/windows/detail2176862.html

#include "OpenJPEG.h"
#include "unistd.h"
OpenJPEG::OpenJPEG(void)
{
}
OpenJPEG::~OpenJPEG(void)
{
}
FIBITMAP* OpenJPEG::Openjpg()
{
 char *strPath="lena.jpg";
 m_pFibitmap = LoadDIB(strPath, 0);
 return m_pFibitmap;
}
//通用图像加载函数,支持的图像有bmp,jpg,tif,png,gif,psd,pgm等等
FIBITMAP* OpenJPEG::LoadDIB(char* lpszPathName, int flag)
{
 m_lpszPathName = lpszPathName;
 m_fif = FreeImage_GetFileType(lpszPathName); //获取Image类型
 //如果没有文件签名,试着从文件扩展名判断文件类型
 if(m_fif == FIF_UNKNOWN)
 m_fif = FreeImage_GetFIFFromFilename(lpszPathName);
 //检查插件是否具有读取该格式的能力
 if((m_fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(m_fif))
 {
 FIBITMAP *dib = FreeImage_Load(m_fif, lpszPathName, flag);
 return dib;
// FITAG *tagMake = NULL;
// FreeImage_GetMetadata(FIMD_EXIF_EXIF,dib,"Make",&tagMake);
// if (tagMake!=NULL)
// {
// //SetDlgItemText(IDC_EDIT1,tagMake);
// }
 }
 return NULL;
}
bool OpenJPEG::Savejpg(FIBITMAP *dib,char* lpszPath, int flag)
{
 FREE_IMAGE_FORMAT fif =FIF_UNKNOWN;
 //
 BOOL bSuccess = FALSE;
 //
 fif=FreeImage_GetFIFFromFilename(lpszPath);
 //判断能否进行保存
 if(fif != FIF_UNKNOWN )
 {
 BOOL bCanSave;
 FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
 if(image_type == FIT_BITMAP)
 {
 WORD bpp = FreeImage_GetBPP(dib);
 bCanSave=(FreeImage_FIFSupportsWriting(fif) && FreeImage_FIFSupportsExportBPP(fif, bpp));
 }
 else
 {
 bCanSave=FreeImage_FIFSupportsExportType(fif, image_type);
 }
 if (bCanSave)
 {
 bSuccess = true;
 }
 FIBITMAP* bitmap = FreeImage_ConvertFromRawBits(dib->data,width,height,pitch,24,FI_RGBA_BLUE_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_RED_MASK,FALSE);
 FreeImage_Save(fif,bitmap,lpszPath,flag);
 }
 return true;
}
int OpenJPEG::RescalePic(const char *srcPicPath,int rscWidth,int rscHeight,const char *rscPicPath)
{
 if(access(srcPicPath,0) != 0)
 return 499; //no such file ,or access deny
 if(rscWidth<=0 || rscHeight<=0)
 return 423; //you input a wrong size, the rscWidth/rscHeight must be bigger than zero
 FIBITMAP *sourcePic = NULL,*rescalePic = NULL,*finalPic = NULL;
 FreeImage_Initialise();
 FREE_IMAGE_FORMAT picType = FIF_UNKNOWN;
 picType = FreeImage_GetFileType (srcPicPath,0);
 if(picType == FIF_UNKNOWN)
 {
// picType = FreeImage_GetFIFFromFilename(srcPicPath);
// if(picType == FIF_UNKNOWN)
// {
 FreeImage_DeInitialise();
 return 421; //can not get picture type
// }
 }
 bool typeSupport = FreeImage_FIFSupportsReading(picType);
 if(typeSupport == FALSE)
 {
 FreeImage_DeInitialise();
 return 422; //not support image type
 }
 sourcePic = FreeImage_Load(picType,srcPicPath,NULL);
 if(!sourcePic)
 {
 FreeImage_DeInitialise();
 return 499;
 }
 double originalWidth,originalHeight;
 unsigned int finalWidth,finalHeight;
 originalWidth  = FreeImage_GetWidth(sourcePic);
 originalHeight = FreeImage_GetHeight(sourcePic);
 if(originalWidth*originalHeight == 0)
 {
 FreeImage_Unload(sourcePic);
 sourcePic = NULL;
 FreeImage_DeInitialise();
 return 425;
 }
// finalWidth  = rscWidth;
// finalHeight = rscHeight;
 if(originalWidth<=rscWidth && originalHeight<=rscHeight)
 {
 finalWidth = originalWidth;
 finalHeight = originalHeight;
 }
 else
 {
 if (rscWidth * (originalHeight / originalWidth) > rscHeight)
 {
 finalWidth = rscHeight * (originalWidth / originalHeight);
 finalHeight = rscHeight;
 }
 else
 {
 finalWidth = rscWidth;
 finalHeight = rscWidth * (originalHeight / originalWidth);
 }
 }
// rsc = FreeImage_ConvertToType(src,FIT_BITMAP,true);
 rescalePic = FreeImage_Rescale(sourcePic,finalWidth,finalHeight,FILTER_BOX);//rescale size: 165x130
 if(!rescalePic)
 {
 FreeImage_Unload(sourcePic);
 sourcePic = NULL;
 FreeImage_DeInitialise();
 return 499;//
 }
 finalPic = FreeImage_ConvertTo24Bits(rescalePic); //this is important,if no this,the rescaled gif cannot flash
 if(!finalPic)
 {
 FreeImage_Unload(sourcePic);
 FreeImage_Unload(rescalePic);
 sourcePic = NULL;
 rescalePic = NULL;
 FreeImage_DeInitialise();
 return 499;//
 }
 int returnValue = 0;
 if(!FreeImage_Save(FIF_JPEG,finalPic,rscPicPath,JPEG_DEFAULT)) //save rescaled picture
 returnValue = 499;
 FreeImage_Unload(sourcePic);
 FreeImage_Unload(rescalePic);
 FreeImage_Unload(finalPic);
 sourcePic = NULL;
 rescalePic = NULL;
 finalPic = NULL;
 FreeImage_DeInitialise();
 return returnValue;
}
IplImage* OpenJPEG::loadipl(const char* filename)
{
 FreeImage_Initialise();
 FIBITMAP* dib = GenericLoader(filename);
 if(!dib)
 return NULL;
 int nClrUsed = FreeImage_GetColorsUsed(dib);
 int nBpp = FreeImage_GetBPP(dib);
 int height = FreeImage_GetHeight(dib);
 int width = FreeImage_GetWidth(dib);
 RGBQUAD* pPalette = FreeImage_GetPalette(dib);
 int nChannel=3;
 if(!nClrUsed && !pPalette) //无调色板图像处理
 {
 IplImage* iplImg = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,nChannel);
 iplImg->origin = 1;
 for(int y=0;y<height;y++)
 {
 BYTE* pLine = (BYTE*)iplImg->imageData + y*iplImg->widthStep;
 BYTE* psrcLine = (BYTE*)FreeImage_GetScanLine(dib,y);
 for (int x=0;x<nChannel*width;x++)
 {
 *pLine++ = *psrcLine++;
 }
 }
 FreeImage_Unload(dib);
 FreeImage_DeInitialise();
 return iplImg;
 }
 else if(pPalette)//索引图像处理
 {
 IplImage* iplImg = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,nChannel);
 iplImg->origin = 1;
 BYTE intensity;
 BYTE* pIntensity = &intensity;
 for(int y=0;y<height;y++)
 {
 BYTE* pLine = (BYTE*)iplImg->imageData + y*iplImg->widthStep;
 for (int x=0;x<width;x++)
 {
 FreeImage_GetPixelIndex(dib,x,y,pIntensity);
 pLine[x*3] = pPalette[intensity].rgbBlue;
 pLine[x*3+1] = pPalette[intensity].rgbGreen;
 pLine[x*3+2] = pPalette[intensity].rgbRed;
 }
 }
 FreeImage_Unload(dib);
 FreeImage_DeInitialise();
 return iplImg;
 }
 else
 {
 FreeImage_Unload(dib);
 FreeImage_DeInitialise();
 return NULL;
 }
}



文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/60144757

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。