Before introducing you to the CXImage class library, we recommend an excellent site to you: http://www.codeproject.com. This site has many articles and source code worth collecting. This article is written according to this site related information.
The CXIMAGE library is an excellent image operation class library. It can be quickly accessed, displayed, and convert various images. Some readers may say that there are so many excellent graphics libraries, such as OpenIL, FreeImage, PaintLib, etc., they can be described as powerful, complete, and there is no need to use other class libraries. But I want to say that these class libraries are basically free, using these class libraries, you have to be bound by such a license agreement. At this point, the CXIMAGE class library is completely free. In addition, when you use the above type libraries, you will encounter more troubles. Because most of them are not related, and use C language written, some also mix the basic C Wrapper and the statement of the completion options you need to deal with. The CXIMAGE class is well done in this regard. There is also the most optimistic, that is, the author fully discloses the source code. This relative to those packaged graphics and GDI , this makes us further learn a variety of codec technology, and no longer float the surface of various technologies. The structure of the CxImage class library:
A CXIMAGE object is an extended bitmap. The author only adds some member variables that store information roles on the bitmap structure. A CXIMAGE object (simultaneous) is also a set of layers. Each layer is only assigned a corresponding buffer when needed. CXIMAGE :: PDIB represents background images, CXIMAGE :: Palpha represents a transparent layer, CXIMAGE:: PSeLection represents the area that is used to create image processing, which is used to create image processing. Based on these three special levels, you can add some extra layers that can be stored in CXIMAGE :: Players. Generally speaking, the layer is a complete CXIMAGE object. Therefore, you can construct a very complex nested layer. Here is some member variables of CXImage:
Class CxImage
{
...
protected:
Void * pdib; // contains file headers, palettes, etc.
BitmapInfoHeader Head; // Standard file header (bitmap)
CXIMAGEINFO INFO; / / Extended information
Byte * pselection; // user selection area
Byte * Palpha; // Alpha channel
CXIMAGE ** Players; // General layer
}
Typedef struct tagcximageInfo {
DWORD DWEFFWIDTH; / / DWORD Scan line width
BYTE * PIMAGE; // Image digits
Void * pghost; // if this is a ghost, pghost point to the body
DWORD DWTYPE; / / The format of the original image
Char szlasterror [256]; // error message
Long nprogress; // Monitor the number of cycles
Long nescape; // jump out of the sign
Long NBKGNDINDEX; // GIF, PNG, MNG format
RGBQUAD NBKGNDCOLOR; // RGB three primary color transparency
Byte nquality; // jpeg format
LONG NFRAME; / / TIF, GIF, MNG Usage: actual frame number
Long nnumframes; // TIF, GIF, MNG Usage: The total number of frames DWRAMEDELAY; / / GIF, MNG use
Long xdpi; // Horizontal resolution
Long YDPI; // Vertical resolution
Rect RselectionBox; // Select the rectangular area
Byte nalphamax; // Shadow's maximum opacity
Bool Balphapaaletteenabled; // If there is an Alpha channel in the palette, it is true.
Bool benabled; // Open the drawing function
Long Xoffset;
Long Yoffset;
DWORD DWENCODEOPTION; // Some encoding options
RGBQUAD LAST_C; // Some Optimization Options
BYTE LAST_C_INDEX;
BOOL LAST_C_ISVALID;
Long nnumlayers;
DWORD DWFLAGS;
CXIMAGEINFO;
To display a file in a PNG format in the Picture Box, just:
CXIMAGE Image ("MyFile.png", CXIMAGE_FORMAT_PNG);
Hbitmap m_bitmap = image.makebitmap (m_picture.getdc () -> m_hdc);
m_picture.setBitmap (m_bitmap);
Other formats are pushed.
Examples: how to ...
... Convert from A Format to Another
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);
}
... loading an image resource
// 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);
OR // load the resource idr_jpg1 from DLL
CXIMAGE * newimage = new cximage ();
Hinstance HDLL = loadingLibrary ("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 Memory
CXIMAGE Image ((byte *) Buffer, size, image_type;
ORCXMEMFILE MEMFILE ((Byte *) Buffer, size);
CXIMAGE Image (& Memfile, Image_Type);
ORCXMEMFILE MEMFILE ((Byte *) Buffer, size);
CXIMAGE * image = new cximage ();
Image-> Decode (& Memfile, Type);
... Encode An Image In Memory
Long size = 0;
BYTE * BUFFER = 0;
Image.Encode (buffer, size, image_type);
...
Free (buffer);
ORCXMEMFILE MEMFILE;
Memfile.open ();
Image.Encode (& Memfile, Image_Type);
BYTE * BUFFER = Memfile.getBuffer ();
Long size = Memfile.size ();
...
Free (buffer);
... CREATE A MULTIPAGE TIFF
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);
ORfile * 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
// 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 ();
You need to pay attention to: The entire CXIMAGE class library is very large. If you only need to process several formats, you can find some switch options in the primary header file XImage.h to close some image libraries. Each library in JPG, PNG, TIFF increases about 100kb content to the final program. The CXImage class is only about 60kb after compression. So, you need to be cautious to pick some class libraries you really need. The sample provided by the author After compiling, you will find some of the following files: CXIMAGE: CXIMAGE.LIB - Static Library CXIMAGEMFCDLL: CXIMAGECRT.DLL - DLL NOT USING MFC · CXIMFCDLL: CXIMAGE.DLL - DLL USING MFC · Demo: demo .exe - program linked with cximage.lib and the C libraries · DemoDll: demodll.exe - program linked with cximagecrt.dll · j2k, jasper, jbig, jpeg, png, tiff, zlib: static C libraries to build these projects takes several Minute time (intermediate file can reach 60MB). The following is some of the parameters that must be set before using the CXImage class library:
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
../cximage.lib ...
Compatibility: - Microsoft Visual C 6.0 (static library, DLL and OCX builds) - Microsoft Visual C .NET 2003 - Borland C Builder version 3 and version 6 - Kdevelop 2.1 with gcc 2.96 (Linux) on CxImage library Author: Davide Pizzolato , An electronic engineer. In 1984, the programming is not cared for using what programming language to develop software. Now, in addition to the Electronic R & D Department of Askoll.