📄 cximage.htm
字号:
<TD><B>CxImgLib.dsw</B><BR>
<IMG height=193 src="tree.png"
width=151></TD>
</TR></TBODY></TABLE>
<P>To use CxImage in your project, you must edit these settings: </P>
<PRE>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 precompiled 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
../jasper/Debug/jasper.lib
../cximage/Debug/cximage.lib ...</PRE>
<P>
<H2>Adding your custom functions in CxImage<A name=custom></A></H2>
<P>Writing a new function for image processing is not so hard with CxImage. Here
I'm going to describe <CODE>CxImage::Jitter</CODE>, it's very simple but it
shows many aspects to take care when you work inside CxImage. The first thing,
of course, is the declaration : <CODE>bool Jitter(long radius=2);</CODE> in the
CXIMAGE_SUPPORT_DSP section of ximage.h, you can declare the function everywhere
in the <CODE>public</CODE> scope of the class. And now the definition: </P><PRE>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;
}
</PRE>
<P><BR>
<H2>Examples: how to ...<A name=examples></A></H2>
<H2>... convert from a format to another</H2><PRE>CxImage image;
// bmp -> jpg
image.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 -> tif
image.Load("image.png", CXIMAGE_FORMAT_PNG);
if (image.IsValid()){
image.Save("image.tif",CXIMAGE_FORMAT_TIF);
}
</PRE>
<H2>... load an image resource</H2><PRE>//Load the resource IDR_PNG1 from the PNG resource type
CxImage* newImage = new CxImage();
newImage->LoadResource(FindResource(NULL,MAKEINTRESOURCE(IDR_PNG1),
"PNG"),CXIMAGE_FORMAT_PNG);</PRE>or<PRE>//Load the resource IDR_JPG1 from DLL
CxImage* 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);
}</PRE>or<PRE>//Load a bitmap resource;
HBITMAP bitmap = ::LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP1)));
CxImage *newImage = new CxImage();
newImage->CreateFromHBITMAP(bitmap);</PRE>
<H2>... decode an image from memory</H2><PRE>CxImage image((BYTE*)buffer,size,image_type);</PRE>or<PRE>CxMemFile memfile((BYTE*)buffer,size);
CxImage image(&memfile,image_type);</PRE>or<PRE>CxMemFile memfile((BYTE*)buffer,size);
CxImage* image = new CxImage();
image->Decode(&memfile,type);</PRE>
<H2>... encode an image in memory</H2><PRE>long size=0;
BYTE* buffer=0;
image.Encode(buffer,size,image_type);
...
free(buffer);</PRE>or<PRE>CxMemFile memfile;
memfile.Open();
image.Encode(&memfile,image_type);
BYTE* buffer = memfile.GetBuffer();
long size = memfile.Size();
...
free(buffer);</PRE>
<H2>... create a multipage TIFF</H2><PRE>CxImage *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);</PRE>or<PRE>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);
</PRE>
<H2>... copy/paste an image</H2><PRE>//copy
HANDLE hDIB = image->CopyToHandle();
if (::OpenClipboard(AfxGetApp()->m_pMainWnd->GetSafeHwnd())) {
if(::EmptyClipboard()) {
if (::SetClipboardData(CF_DIB,hDIB) == NULL ) {
AfxMessageBox( "Unable to set Clipboard data" );
} } }
CloseClipboard();
//paste
HANDLE hBitmap=NULL;
CxImage *newima = new CxImage();
if (OpenClipboard()) hBitmap=GetClipboardData(CF_DIB);
if (hBitmap) newima->CreateFromHANDLE(hBitmap);
CloseClipboard();</PRE>
<H2>... display a file in a picture box</H2>
<PRE>HBITMAP m_bitmap = NULL;<br>CxImage image("myfile.png", CXIMAGE_FORMAT_PNG);
...
if (m_bitmap) DeleteObject(m_bitmap);<br>m_bitmap = image.MakeBitmap(m_picture.GetDC()->m_hDC);<br>m_picture.SetBitmap(m_bitmap);</PRE>
<H2>History and credits.<A name=history></A></H2>
<P>Starting form my <CODE>CxDib</CODE> class, that implements memory DIBs only,
I tried to add some members to read images from files. Looking for a solution,
I found a nice MFC class named <CODE>CImage</CODE> on the net, release 1.4 (1998).
<CODE>CImage</CODE> supports BMP, GIF, PNG and JPG, but suffers many little
bugs and uses a complex class structure, so I decided to strip it to the base
and merge <CODE>CxDib</CODE> with the <CODE>CImage</CODE> philosophy, to obtain
the new <CODE>CxImage</CODE> class. Also I updated the libraries for JPG, PNG
and ZLIB.<BR>
With <CODE>CxImage</CODE> is very easy to add new image types, so I added the
TIFF library (rev. 6) and a minimal support for <CODE>ICON</CODE>s, MNG, TGA
and PCX. Finally I added some specific functions to obtain an image from global
<CODE>HANDLE</CODE>s (windows clipboard) and objects (windows resources).<BR>
With the release 5, CxImage has now a good support for memory files, new methods
and file formats, and it is more portable.</P>
<ul>
<li>CImage
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -