![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cximage介绍 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-9-12 ![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
正在装载数据…… cximage介绍
PrefaceCxImage grew together with the CodeProject site, since 2001, and reached this level thanks to the CodeProject community. Thanks a lot for the suggestions, the contributions and for the critiques. However, if I look at the other image processing libraries, it is clear that nowadays, the CxImage project is quite inadequate from many points of view. For this reason, this one will be last release in this form. Of course the development will not stop: it is just going to follow new directions. Introduction
Why another image library? Around there are many good libraries (OpenIL, FreeImage, PaintLib ...), these are powerful, complete, and constantly updated. However if you ever try to use them, you may find some difficulties; because these libraries are mainly platform independent, written in C, sometimes with a basic C++ wrapper and with tons of compiler switch declarations. Now with the new GDI+ classes on the scene, maybe CxImage isn't so useful, but at least here you have the source code. It is not a MFC library, altogether it's a windows library, because of some particular constructors and the painting functions, but the backbone is platform independent. LicenseThe class
What's new in version 5.99cBugfixes
CxImage structureIn the vertical hierarchy of the library, The glue to connect all the modules and the C libraries is
A class CxImage { ... protected: void* pDib; //contains the header, the palette, the pixels BITMAPINFOHEADER head; //standard header CXIMAGEINFO info; //extended information BYTE* pSelection; //selected region BYTE* pAlpha; //alpha channel CxImage** pLayers; //generic layers }
typedef struct tagCxImageInfo { DWORD dwEffWidth; //DWORD aligned scan line width BYTE* pImage; //THE IMAGE BITS void* pGhost; //if this is a ghost, pGhost point to the body DWORD dwType; //original image format char szLastError[256]; //debugging long nProgress; //monitor long nEscape; //escape long nBkgndIndex; //used for GIF, PNG, MNG RGBQUAD nBkgndColor; //used for RGB transparency BYTE nQuality; //used for JPEG long nFrame; //used for TIF, GIF, MNG : actual frame long nNumFrames; //used for TIF, GIF, MNG : total number of //frames DWORD dwFrameDelay; //used for GIF, MNG long xDPI; //horizontal resolution long yDPI; //vertical resolution RECT rSelectionBox; //bounding rectangle BYTE nAlphaMax; //max opacity (fade) bool bAlphaPaletteEnabled; //true if alpha values in the palette are // enabled. bool bEnabled; //enables the painting functions long xOffset; long yOffset; DWORD dwEncodeOption; //for GIF, TIF : 0=def.1=unc,2=fax3,3=fax4, // 4=pack,5=jpg RGBQUAD last_c; //for GetNearestIndex optimization BYTE last_c_index; bool last_c_isvalid; long nNumLayers; DWORD dwFlags;} CXIMAGEINFO;
CxImage Class Members & OperationsCxImage 5.99c is documented using Doxygen , however for historical reasons, many uncommon features are still undocumented. The class members reference, together with release history, and license, can be found here Supported formats and optionsThe whole library is quite big, in the main header file ximcfg.h you'll find the switches to enable or disable a specific graphic format or feature. Each JPG, PNG and TIFF library adds about 100KB to the final application, while the
Using CxImage in your projects
To use CxImage in your project, you must edit these settings: Project Settings |- C/C++ | |- Code Generation | | |- Use run-time library : Multithreaded DLL (must be the same for | | | all the linked libraries) | | |- Struct member alignment : must be the same for all the linked | | | libraries | |- Precompiled headers : not using divcompiled headers | |- Preprocessor | |- Additional Include Directories: ..\cximage |- Link |- General |- Object/library modules: ../png/Debug/png.lib ../jpeg/Debug/jpeg.lib ../zlib/Debug/zlib.lib ../tiff/Debug/tiff.lib ../cximage/Debug/cximage.lib ...
Adding your custom functions in CxImageWriting a new function for image processing is not so hard with bool CxImage::Jitter(long radius){ // check if the image is valid, this should be always the first line in // the function if (!pDib) return false; // local variables long nx,ny; // temporary image to store the partial results of the algorithm CxImage tmp(*this,pSelection!=0,true,true); // limit the effects of the functions only in the smallest rectangle that // holds the selected region (defined with the Selection...() functions ), // this will speed up the loops. long xmin,xmax,ymin,ymax; if (pSelection){ xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right; ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top; } else { xmin = ymin = 0; xmax = head.biWidth; ymax=head.biHeight; } // main loop : scan the image in vertical direction for(long y=ymin; y <ymax; y++){ // monitor the progress of the loops info.nProgress = (long)(100*y/head.biHeight); // let the application a way to exit quickly if (info.nEscape) break; // main loop : scan the image in horizontal direction for(long x=xmin; x<xmax; x++){ // if the feature is enabled, process only the pixels inside the // selected region#if CXIMAGE_SUPPORT_SELECTION if (SelectionIsInside(x,y))#endif //CXIMAGE_SUPPORT_SELECTION { // main algorithm nx=x+(long)((rand()/(float)RAND_MAX - 0.5)*(radius*2)); ny=y+(long)((rand()/(float)RAND_MAX - 0.5)*(radius*2)); if (!IsInside(nx,ny)) { nx=x; ny=y; } // save the result in the temporary image. // if you can, use PixelColor only for 24 bpp images, // and PixelIndex for 8, 4 and 1 bpp images : it's faster if (head.biClrUsed==0){ tmp.SetPixelColor(x,y,GetPixelColor(nx,ny)); } else { tmp.SetPixelIndex(x,y,GetPixelIndex(nx,ny)); } // if the feature is enabled, process also the pixels // in the alpha layer#if CXIMAGE_SUPPORT_ALPHA tmp.AlphaSet(x,y,AlphaGet(nx,ny));#endif //CXIMAGE_SUPPORT_ALPHA } } } // save the result and exit Transfer(tmp); return true;} Examples: how to ...... convert from a format to anotherCxImage image;// bmp -> jpgimage.Load("image.bmp", CXIMAGE_FORMAT_BMP);if (image.IsValid()){ if(!image.IsGrayScale()) image.IncreaseBpp(24); image.SetJpegQuality(99); image.Save("image.jpg",CXIMAGE_FORMAT_JPG);}// png -> tifimage.Load("image.png", CXIMAGE_FORMAT_PNG);if (image.IsValid()){ image.Save("image.tif",CXIMAGE_FORMAT_TIF);} ... load an image resource//Load the resource IDR_PNG1 from the PNG resource typeCxImage* newImage = new CxImage();newImage->LoadResource(FindResource(NULL,MAKEINTRESOURCE(IDR_PNG1), "PNG"),CXIMAGE_FORMAT_PNG); or //Load the resource IDR_JPG1 from DLLCxImage* newImage = new CxImage();HINSTANCE hdll=LoadLibrary("imagelib.dll");if (hdll){ HRSRC hres=FindResource(hdll,MAKEINTRESOURCE(IDR_JPG1),"JPG"); newImage->LoadResource(hres,CXIMAGE_FORMAT_JPG,hdll); FreeLibrary(hdll);} or //Load a bitmap resource;HBITMAP bitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1)));CxImage *newImage = new CxImage();newImage->CreateFromHBITMAP(bitmap); ... decode an image from memoryCxImage image((BYTE*)buffer,size,image_type); or CxMemFile memfile((BYTE*)buffer,size);CxImage image(&memfile,image_type); or CxMemFile memfile((BYTE*)buffer,size);CxImage* image = new CxImage();image->Decode(&memfile,type); ... encode an image in memorylong size=0;BYTE* buffer=0;image.Encode(buffer,size,image_type);...free(buffer); or CxMemFile memfile;memfile.Open();image.Encode(&memfile,image_type);BYTE* buffer = memfile.GetBuffer();long size = memfile.Size();...free(buffer); ... create a multipage TIFFCxImage *pimage[3];pimage[0]=&image1;pimage[1]=&image2;pimage[2]=&image3;FILE* hFile;hFile = fopen("multipage.tif","w+b");CxImageTIF multiimage;multiimage.Encode(hFile,pimage,3);fclose(hFile); or FILE* hFile;hFile = fopen("c:\\multi.tif","w+b");CxImageTIF image;image.Load("c:\\1.tif",CXIMAGE_FORMAT_TIF);image.Encode(hFile,true);image.Load("c:\\2.bmp",CXIMAGE_FORMAT_BMP);image.Encode(hFile,true);image.Load("c:\\3.png",CXIMAGE_FORMAT_PNG);image.Encode(hFile);fclose(hFile); ... copy/paste an image//copyHANDLE hDIB = image->CopyToHandle();if (::OpenClipboard(AfxGetApp()->m_pMainWnd->GetSafeHwnd())) { if(::EmptyClipboard()) { if (::SetClipboardData(CF_DIB,hDIB) == NULL ) { AfxMessageBox( "Unable to set Clipboard data" );} } }CloseClipboard();//pasteHANDLE hBitmap=NULL;CxImage *newima = new CxImage();if (OpenClipboard()) hBitmap=GetClipboardData(CF_DIB);if (hBitmap) newima->CreateFromHANDLE(hBitmap);CloseClipboard(); ... display a file in a picture boxHBITMAP m_bitmap = NULL; CxImage image("myfile.png", CXIMAGE_FORMAT_PNG);...m_bitmap = image.MakeBitmap(m_picture.GetDC()->m_hDC); m_picture.SetBitmap(m_bitmap); ... if (m_bitmap) DeleteObject(m_bitmap); History and credits.Starting form my With
More specific credits and disclaimers are in every header file of each library 本文来源:http://blog.csdn.net/edison0663/archive/2007/08/28/1761405.aspx
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 文章录入:admin 责任编辑:admin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|