说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

ImageStone图像处理C++类库

HTML文档下载 WORD文档下载 PDF文档下载
ImageStone是一个功能强大的图像处理C++类库。它采用纯C++编写,接口简单。功能包括:加载、保存(JPG格式,BMP,GIF,PNG,TIFF,ICO图标,TGA,PCX,PSD)、显示,直方图,撤销(undo)/重做(redo),超过100个图像特效。


作者:crazybit

 

介绍

ImageStone是一个功能强大的图像处理C++类库。它采用纯C++编写,接口简单。功能包括:加载、保存(JPG格式,BMP,GIF,PNG,TIFF,ICO图标,TGA,PCX,PSD)、显示,直方图,撤销(undo)/重做(redo),超过100个图像特效。

 

许可

 

ImageStone是免费的。使用代码无论用在什么地方都行(商业的也可以),只要你不声称它是你自己的。(如果你将它用在你的产品中,我希望你能通知我。)

 

使用ImageStone

这是非常容易的。所有你需要做的就是在您的源代码的开头添加#include "ImageStone.h"。如果您使用的是一个MFC项目ImageStone,只需要在StdAfx.h里添加该行。

 

最基本和最重要的类是FCObjImage。了解如何使用它。

 

在任意操作系统中加载、保存图像文件

 

// for Windows : ImageStone uses GDI+ to load/save image,//               Jpg/Png/Gif/Bmp/Tga/Tif be supported// for Linux : Only Bmp/Tga are supported, but you can set//             FreeImage handler to support more.FCObjImage   img ;img.Load ("test.jpg") ;if (!img.IsValidImage()){   assert(false) ;   return false ;} // print image's information : width, height, bit per pixelprintf ("image's width : %d",  img.Width()) ;printf ("image's height : %d", img.Height()) ;printf ("image's bpp : %d",    img.ColorBits()) ; // Load/Save function determine image's format by file's ext name// save image as jpeg file, its quality set 90 (ranges from 1 to 99)img.Save ("save.jpg", 90) ;img.Save ("save.png") ;img.Save ("save.tif") ; // Another way to set qualityFCImageProperty   prop ;prop.SetPropertyValue (IMAGE_TAG_JPEG_QUALITY, "90") ;img.Save ("save.jpg", prop) ;

在任意操作系统中从内存加载图像

 

// Load image into memorychar   * p = 0 ;int    n = 0 ;FCOXOHelper::LoadFileToBuffer ("test.jpg", p, n) ; FCObjImage   img ;img.Load (p, n, IMG_JPG) ; delete[] p ; // this line demonstrates how to determine the image's format by// the file's ext nameIMAGE_TYPE  t = FCObjImage::GetImageHandleFactory()->   QueryImageFileType("test.jpg");

在任意操作系统中从DIB流中加载图像

 

// Load image into memorychar   * p = 0 ;int    n = 0 ;FCOXOHelper::LoadFileToBuffer ("test.bmp", p, n) ;p += sizeof(BITMAPFILEHEADER) ; // now p points to a DIB streamFCObjImage   img ;img.LoadDIBStream (p, n) ; delete[] p ;

Windows中从资源加载图像

 

// Load image from local exe fileFCObjImage   img ;FCWin32::LoadImageRes (img, MAKEINTRESOURCE(nID), TEXT("JPG"),                       IMG_JPG) ; // Load image from DLL's resourceHMODULE   hDll = LoadLibrary (TEXT("ResDll.dll")) ;FCWin32::LoadImageRes (img, MAKEINTRESOURCE(nID), TEXT("JPG"),                       IMG_JPG, hDll) ; // Load image from standard BITMAP resourceFCWin32::LoadImageBitmapRes (img, MAKEINTRESOURCE(nID));

通过FreeImage库加载/保存图像

 

// change to FreeImage library to load/save image// for more detail, refer to example 005FCObjImage::SetImageHandleFactory (new FCImageHandleFactory_FreeImage) ;img.Load ("test.jpg") ; // change to GDI+ load/save imageFCObjImage::SetImageHandleFactory (new FCImageHandleFactory_Gdiplus) ;img.Load ("test.jpg") ;

结合ImageHandleFactory

 

// use FreeImage to load/save PSD/PCX imageclass CMyImageFactory : public FCImageHandleFactory{protected:   virtual FCImageHandleBase* CreateImageHandle (IMAGE_TYPE imgType)   {      switch (imgType)      {         case IMG_BMP : return new FCImageHandle_Bmp ;         case IMG_TGA : return new FCImageHandle_Tga ;         case IMG_JPG : return new FCImageHandle_Gdiplus ;         case IMG_GIF : return new FCImageHandle_Gdiplus ;         case IMG_TIF : return new FCImageHandle_Gdiplus ;         case IMG_PNG : return new FCImageHandle_Gdiplus ;         case IMG_PCX : return new FCImageHandle_FreeImage ;         case IMG_PSD : return new FCImageHandle_FreeImage ;      }      return 0 ;   }   // protected avoid user delete object.   virtual ~CMyImageFactory() {}}; // use our custom factory to read/write imageFCObjImage::SetImageHandleFactory (new CMyImageFactory) ;FCObjImage   img ;img.Load ("test.jpg") ;

加载多帧GIF

 

FCObjMultiFrame   img ;img.Load ("test.gif") ;img.GetFrame(0)->Save ("001.jpg") ;img.GetFrame(1)->Save ("001.jpg") ;

得到jpeg的EXIF信息

 

FCObjImage        img ;FCImageProperty   prop ;img.Load ("test.jpg", &prop) ; // get camera's ISO speedstd::string   m = prop.QueryPropertyValue (IMAGE_TAG_EXIF_ISOSpeed) ;// get camera's equip modelstd::string   n = prop.QueryPropertyValue (IMAGE_TAG_EquipModel) ;

在Windows环境下绘制图像对象

 

FCObjImage   img ;// capture current screenRECT         rc = {0, 0, GetSystemMetrics(SM_CXSCREEN),                         GetSystemMetrics(SM_CYSCREEN)} ;FCWin32::CaptureScreen (img, rc) ; // Draw image (no stretch) where top-left at (0,0) of hdcFCWin32::DrawImage (img, hdc, 0, 0) ; // Stretch image on region of hdcRECT     rcOnDC = {100, 100, 200, 200} ;FCWin32::DrawImage (img, hdc, rcOnDC) ; // Stretch image on central of hdc's region and keep image's aspectFCWin32::DrawImageAspect (img, hdc, rcOnDC) ; // Stretch region of image on region of hdcRECT     rcImg = {20, 20, 50, 50} ;FCWin32::DrawImage (img, hdc, rcOnDC, rcImg) ;

复制/粘贴图像到剪切板

 

FCObjImage   img ;img.Load ("test.jpg") ; // copy image to ClipboardFCWin32::CopyToClipboard (img) ; // get image on ClipboardFCWin32::GetClipboardImage (img) ;

GDI HBITMAP和FCObjImage之间的转换

 

// create HBITMAP from FCObjImage objectFCObjImage   img ;img.Load ("test.jpg") ;HBITMAP   h = FCWin32::CreateDDBHandle (img) ; // create FCObjImage object from HBITMAPFCWin32::CreateImageFromDDB (h, img) ;

GDI+ 位图和FCObjImage之间的转换

 

// create GDI+ Bitmap from FCObjImage objectFCObjImage   img ;img.Load ("test.jpg") ;Gdiplus::Bitmap   * pBmp = FCWin32::GDIPlus_CreateBitmap(img) ;delete pBmp ; // create FCObjImage object from GDI+ BitmapFCWin32::GDIPlus_LoadBitmap (*pBmp, img) ;

图像处理

 

FCObjImage   img ;img.Load ("test.jpg") ; // resize (smooth) imageimg.Stretch (nWidth, nHeight) ;img.Stretch_Smooth (nWidth, nHeight) ; // Use SinglePixelProcessProc interface to process image.// There are over 100 pre-implemented effects;// please refer to class derived from FCInterface_PixelProcessFCPixelRotate   aCmd (37) ;img.SinglePixelProcessProc (aCmd) ; FCPixelBrightness   aCmd (150) ;    // 150%img.SinglePixelProcessProc (aCmd) ; FCPixelMosaic   aCmd(5) ;img.SinglePixelProcessProc (aCmd) ; FCPixelOilPaint   aCmd (3) ;img.SinglePixelProcessProc (aCmd) ;

自定义图像处理

 

// our processor : change pixel's RGB valueclass CMyPixelProcessor : public FCSinglePixelProcessBase{public:   CMyPixelProcessor (int nR, int nG, int nB) : m_R(nR), m_G(nG),                                                m_B(nB) {}private:   virtual void ProcessPixel (FCObjImage* pImg, int x, int y,                              BYTE* pPixel)   {      PCL_B(pPixel) = FClamp0255 (PCL_B(pPixel) + m_B) ;      PCL_G(pPixel) = FClamp0255 (PCL_G(pPixel) + m_G) ;      PCL_R(pPixel) = FClamp0255 (PCL_R(pPixel) + m_R) ;   }   int   m_R, m_G, m_B ;}; // this class has the same function to upper class,// but implements other classclass CMyImageProcessor : public FCPixelWholeImageBase{public:   CMyPixelProcessor (int nR, int nG, int nB) : m_R(nR), m_G(nG),                                                m_B(nB) {}private:   virtual void ProcessWholeImage (FCObjImage* pImg,                                   FCObjProgress* pProgress)   {      for (int y=0 ; y < pImg->Height() ; y++)      {         for (int x=0 ; x < pImg->Width() ; x++)         {            BYTE   * p = pImg->GetBits(x,y) ;            PCL_B(p)   = FClamp0255 (PCL_B(p) + m_B) ;            PCL_G(p)   = FClamp0255 (PCL_G(p) + m_G) ;            PCL_R(p)   = FClamp0255 (PCL_R(p) + m_R) ;         }         if (pProgress)            pProgress->SetProgress (100 * y / pImg->Height()) ;      }   }    int   m_R, m_G, m_B ;}; // use our custom processorFCObjImage   img ;img.Load ("test.jpg") ; CMyPixelProcessor   aCmd (20, 20, 20) ;img.SinglePixelProcessProc (aCmd) ; CMyImageProcessor   aCmd (20, 20, 20) ;img.SinglePixelProcessProc (aCmd) ;

添加文字到图像

 

FCObjImage   img ;img.Load ("c:\\test.jpg") ; // now we create text layerFCObjTextLayer   imgT ;PACK_TextLayer   tp ;tp.m_bAddShadow   = false ;tp.m_bAntiAliased = true ;tp.m_bBold        = true ;tp.m_bItalic      = true ;tp.m_crFont       = PCL_RGBA(0,0,255) ;tp.m_nFontSize    = 128 ;tp.m_strFace      = "Arial" ;tp.m_strText      = "Hello" ;FCWin32::CreateTextLayer_GDIPlus (imgT, tp) ; // Now we have created a text image. Additionaly,// we can add some affect to it, such as gradient colorPOINT                 pt1={0,0}, pt2={0,50} ;FCPixelGradientLine   aCmd (pt1, pt2, PCL_RGBA(0,0,255),                            FCColor::crWhite()) ;imgT.SinglePixelProcessProc (aCmd) ; // blend text layer on imageRECT   rc = {0, 0, imgT.Width(), imgT.Height()} ;img.AlphaBlend (imgT, rc, rc, 100) ;

下载源代码:http://www.okbase.net/file/item/2994

备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘