Cximage class library

Source: Internet
Author: User
Recently, we are planning to implement the bip protocol for Bluetooth, which requires the function to generate thumbnail, so we can find the relevant open source code on the Internet.
The cximage class library is said to be quite good, and is based on zlib license and completely free.
Article on codeproject: http://www.codeproject.com/KB/graphics/cximage.aspx
Another article on thumbnail viewer implementation: http://www.codeproject.com/KB/combobox/ThumbsViewer.aspx

Cximage author Home: http://www.xdp.it/cximage/

Repost some online articles as follows:

The cximage class library is an excellent image operation class library. It can quickly access, display, and convert various images. Some readers may say that there are so many excellent graphics libraries, such as openil, freeimage, and paintlib, which are highly functional and complete, and there is no need to use other class libraries. However, I would like to say that these class libraries are basically not free of charge. To use these class libraries, you must be bound by such a license agreement. At this point, the cximage class library is free of charge. In addition, you may encounter a lot of trouble when using the above class libraries. Because most of them are platform-independent and are written in C language, some are also mixed with the basic C ++ wrapper and heap German compilation option declarations that need to be processed by you. The cximage class library is doing well in this regard. What makes me most optimistic is that the source code is completely disclosed by the author. Compared with those encapsulated graphics libraries and GDI +, this allows us to further study various coding and decoding technologies, instead of floating on the surface of various technologies. Cximage class library structure:

A cximage object is an extended bitmap. The author adds some member variables that store information in the bitmap structure. A cximage object (also) is a set of layers. Each layer is allocated with a buffer only when needed. Cximage: pdib indicates the background image, cximage: palpha indicates the transparent layer, and cximage: pselection indicates the selected layer, it is used to create areas of interest to users during image processing. On the basis of these three special layers, you can add some additional layers that can be stored in cximage: players. Generally, a layer is a complete cximage object. Therefore, you can construct a complex nested layer. The following are some member variables of cximage:

Class cximage
{
...
Protected:
Void * pdib; // contains the file header, palette, and so on.
Bitmapinfoheader head; // standard file header (Bitmap)
Cximageinfo Info; // extended information
Byte * pselection; // region selected by the user
Byte * palpha; // alpha channel
Cximage ** players; // common Layer
}
Typedef struct tagcximageinfo {
DWORD dweffwidth; // DWORD scan width
Byte * pimage; // Number of image digits
Void * pghost; // if this is a ghost, pghost point to the body
DWORD dwtype; // format of the original image
Char szlasterror [256]; // error message
Long nprogress; // number of monitoring cycles
Long nescape; // exit flag
Long nbkgndindex; // GIF, PNG, or MNG format
Rgbquad nbkgndcolor; // RGB three-color transparency
Byte nquality; // JPEG format
Long nframe; // TIF, GIF, and MNG usage: actual number of frames
Long nnumframes; // TIF, GIF, and MNG usage: Total number of frames
DWORD dwframedelay; // GIF, used by MNG
Long xdpi; // horizontal resolution
Long ydpi; // vertical resolution
Rect rselectionbox; // The selected rectangle
Byte nalphamax; // specifies the maximum opacity of the shadow.
Bool balphapaletteenabled; // true if there is an alpha channel in the palette
Bool benabled; // open the plotting 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 PNG file in picture box, you only need:
Cximage image ("myfile.png", cximage_format_png );
Hbitmap m_bitmap = image. makebitmap (m_picture.getdc ()-> m_hdc );
M_picture.setbitmap (m_bitmap );
Other formats, and so on.
Examples: how...
... 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. set0000quality (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 );
}
... Load 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 = 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 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 ();

Note that the entire cximage class library is very large. If you only need to be able to process several formats, you can find some switch options in the main header file ximage. h to close some image libraries. Each library in JPG, PNG, And Tiff will add about KB of content to the final program. The cximage class library is compressed to about 60 kb. Therefore, you need to carefully select the class libraries you actually need. After compiling the sample project provided by the author, you will find the following files: · cximage: cximage. lib-static library · cximagecrtdll: cximagecrt. DLL-DLL not using MFC · cximagemfcdll: 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 it takes several minutes to build these projects (the intermediate file can reach 60 MB ). The following are some 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/Debug/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) about the cximage class library by Davide pizzolato, an electronic engineer. Programming started in 1984 and does not care what programming language is used for software development. He is now working at askoll's electronics R & D department.

 

Another similar C ++ class cimg: http://cimg.sourceforge.net/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.