"Reprint" VC + + Image type conversion--using the open source Cximage class Library

Source: Internet
Author: User

Introduction of Cximage Class Library

This is just a partial introduction and simple use of the Cximage Open Source Project homepage.

Cximage Class Library is an excellent image manipulation class library. It can quickly access, display, transform a variety of images. Some readers may say that there are so many good graphics libraries, such as openil,freeimage,paintlib and so on, they are powerful, complete, no need to use other libraries. But I would say that these libraries are basically not free, and using these libraries, you have to be bound by such a license agreement. At this point, the Cximage class library is completely free. In addition, you will encounter a lot of trouble when using the above class library. Because they are mostly platform-independent and written in C, some of the basic C + + wrapper and stacks of German compiler options are required for you to deal with. and the Cximage class library is doing very well in this respect. And what makes me most optimistic is that the author has completely disclosed the source code. This allows us to learn more about the various codec technologies, rather than the surface of the various technologies, compared to the packaged graphics libraries and GDI +.

Cximage is a C + + class that can be used for MFC, can open, save, display, convert various formats of image files, such as BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K and other format files. You can implement conversions such as Bmp<->jpg,png <>tiff format, and so on.

Can realize the type conversion of the image file, can also realize the type conversion of the memory image data, and it is convenient to use.

Its author is: Davide Pizzolato, Home: http://www.xdp.it/

First, download its source file and demo example to http://www.codeproject.com/bitmap/CXImage.asp.

Note: In CodeProject download this class, you have to register first, because this kind of gold is relatively high, download volume is larger, of course, you will also be very happy to become a member of CodeProject, she does not charge you. Authorization:

License

The class is free CxImage ; As for the TIFF, JPEG, PNG, and ZLIB libraries: "If You use this source code in a product , acknowledgment is isn't required but would be appreciated. "

CxImage is open source and licensed under the Zlib license. In a nutshell, this means so can use the code however you wish, as long as you don't claim it as your own.

Since many people are codeproject, you can also go to the author of this class library--davide Pizzolato's homepage to download

His homepage also has other source code, interested can also see.

Second, compile the Cximage class library

The author has provided the entire class library configuration of the project file cximglib.dsw (vc++6.0), just open it for compilation. It is important to note that the entire Cximage class library is very large. If you only need to be able to handle several of these formats and compile the class library, you can find some compile switch options in the configuration header file ximcfg.h to close some of the image libraries. Each library in JPG, PNG, and Tiff adds about 100KB of content to the final program. The Cximage class library is only about 60KB compressed. So, you need to carefully pick out some of the class libraries you really need.

//Class Library configuration file: Ximacfg.h#defineCximage_support_jpg 1//if you want to implement Bmp->jpg, you must open#defineCximage_support_bmp 1#defineCximage_support_gif 1#defineCximage_support_jpg 1//The above is necessary to open, the following casually ... ..#defineCximage_support_png 0//do not use it#defineCXIMAGE_SUPPORT_MNG 0#defineCximage_support_ico 1#defineCximage_support_tif 0//#defineCXIMAGE_SUPPORT_TGA 0//#defineCXIMAGE_SUPPORT_PCX 0//#defineCximage_support_wbmp 0//#defineCXIMAGE_SUPPORT_WMF 0//#defineCXIMAGE_SUPPORT_J2K 0//Beta, use JP2#defineCXIMAGE_SUPPORT_JBG 0//.............

The function of the project corresponds to the following:

  • 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

    Compiling these projects takes a few minutes (intermediate files up to 60MB).

    Third, the application of Cximage class library in the program for image type conversion

    Use this class library in your VC project to set up the project as follows (Project Settings):

    |-C + +
    | |-Code Generation
    |   | |-use Run-time library:multithreaded DLL (must is the same for
    |   |  | All the linked libraries)//should be as long as the multi-threaded DLL can, debug also line
    |   | |-Struct member Alignment:must is the same for all the linked libraries
    | |-precompiled Headers:not Using precompiled headers
    | |-Preprocessor
    | |-Additional Include directories:.. /cximage (where the. h and. cpp files in the Cximage are copied and imported into the project's folder, the compiler looks for the folder when it is filled in the project, so the include file does not need a path)
    |-Link
    |-General
    |-Object/library Modules:png.lib
    Jpeg.lib
    Zlib.lib
    Tiff.lib
    Jasper.lib
    Cximage.lib (Copy the required lib file from the cximage to the directory where the Lib file in the project resides)

    And from the Cximage will Xfile.h, Ximacfg.h, Ximadef.h, Ximage.cpp, Ximage.h, Xiofile.h, Xmemfile.cpp, Xmemfile.h Copy to the project folder and add the CxImage.h file to the project. You can also set the "include" Path in VC6 's "tools".

    Here's how to apply it for image type conversions:

    1. Converting from one image file type to another file type (convert from a format to another)

    Cximage image;//define a Cximage object//convert from BMP file to JPG file (BMP---jpg)Image. Load ("image.bmp", cximage_format_bmp);//to load the BMP file first, you need to specify the file type//determines whether the loaded BMP file exists. if(image. IsValid ()) ... {       //Returns True if the image has a colors and a linear of grey scale palette.    if(!image. Isgrayscale ()) image. INCREASEBPP ( -);//param Nbit:4, 8,Image. Setjpegquality ( About);//set the compression quality parameters for the image (from 0 to 100, the higher the value, the greater the quality)Image. Save ("image.jpg", cximage_format_jpg);//Save the compressed image as a JPG file type. }

  • // convert from a PNG file to a TIF file (PNG-, TIF)image. Load ("image.png", cximage_format_png); if (image. IsValid ()) {    image. Save ("image.tif", Cximage_format_tif);}

    2. Loader resource image (load an image resource)

    The Cximage object is constructed from the program's resource image in the following ways:

    // Load The resource idr_png1 from the PNG resource type New cximage (); newimage-LoadResource (FindResource (Null,makeintresource (idr_png1),                       "  PNG"), cximage_format_png);

    Or

    // Load The resource idr_jpg1 from DLL 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

    /= :: LoadBitmap (AfxGetInstanceHandle (),                              new  cximage (); NewImage Createfromhbitmap (bitmap);

    3. Image type conversions in memory buffering

    (1) Decoding the data in the memory buffer into an image object (decode an image from memory)

    There are several ways to do this:

    Cximage image ((byte*) buffer,size,image_type); // constructs the data in buffer buffers into an image object // or: cxmemfile memfile ((BYTE//  explicitly uses Cxmemfile object cximage image (&memfile,image_type); // or: cxmemfile memfile ((BYTE*) buffer,size); CximageNew  cximage (); Image->decode (&memfile,type);
    2) storing the image encoding in memory buffer (encode an image)
    LongSize=0;//Get image sizebyte* buffer=0;//buffer for storing image dataImage. Encode (Buffer,size,image_type);//Copy the image from the images object to buffer with type data... Free(buffer);: Cxmemfile memfile; //explicitly using Cxmemfile objectsMemfile. Open (); image. Encode (&Memfile,image_type); BYTE* Buffer =Memfile. GetBuffer ();LongSize =Memfile. Size (); .... Free(buffer);
    4. Processing the image in the system pasteboard (Copy/paste an image)
    //copy (to Pasteboard)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 (paste out from the Pasteboard)HANDLE hbitmap=NULL; Cximage*newima =Newcximage ();if(OpenClipboard ()) hbitmap=GetClipboardData (cf_dib);if(HBITMAP) newima->Createfromhandle (HBITMAP); CloseClipboard ();

    5. Display a PNG-formatted file in the picture box

    Hbitmap m_bitmap = NULL; Cximage image ("myfile.png"= image.) Makebitmap (m_picture. GetDC ()m_hdc); m_picture. SetBitmap (M_BITMAP); if (M_bitmap) DeleteObject (M_BITMAP);

    Iv. Other

    A Cximage object is an extended bitmap. The author simply adds some member variables that store information on the bitmap structure. A Cximage object (at the same time) is also a set of layers. Each layer allocates the appropriate buffers only when it is needed. Cximage: The:p DIB represents the background image, Cximage::p Alpha represents the transparent layer, Cximage: The:p selection represents the selected layer and is used to create an area of interest to the user when the image is processed. On the basis of these three special layers, you can add additional layers that can be stored in Cximage::p layers. Generally, a layer is a complete cximage object. Therefore, you can construct very complex nesting layers. Here are some of the member variables of Cximage:

    classcximage{ ...protected:void* PDIB;//include header, palette, etc.Bitmapinfoheader Head;//standard file Header (bitmap)Cximageinfo info;//expanded InformationByte* pselection;//User-selected areaByte* Palpha;//alpha channelcximage** pLayers;//Universal Layer}typedefstructTagcximageinfo {DWORD dweffwidth; //DWORD Scan line widthByte* Pimage;//number of image bitsvoid* PGHOST;//If this was a ghost, PGhost point to the bodyDWORD dwtype;//the format of the original imageCharszlasterror[ the];//error MessageLongnprogress;//number of monitoring loopsLongNescape;//Jump SignLongNbkgndindex;//GIF, PNG, MNG format useRgbquad Nbkgndcolor;//RGB Tri-Primary color transparencyBYTE nquality;//JPEG format usingLongNframe;//TIF, GIF, mng use: Actual number of framesLongNnumframes;//TIF, GIF, MNG use: Total framesDWORD Dwframedelay;//GIF, MNG useLongxdpi;//Horizontal ResolutionLongydpi;//Vertical ResolutionRECT Rselectionbox;//the selected rectangular areaBYTE Nalphamax;//maximum opacity of a shadowBOOLbalphapaletteenabled;//true if there is an alpha channel in the paletteBOOLbenabled;//open a drawing functionLongXoffset;LongYoffset;dword dwencodeoption; //Some encoding optionsRgbquad Last_c;//Some optimization optionsBYTE Last_c_index;BOOLLast_c_isvalid;LongNnumlayers;dword DwFlags;} Cximageinfo;

    About the Cximage class library Davide Pizzolato, an electronic engineer. Programming began in 1984, and no longer cared what programming language was used to develop software. He is currently working in the Electronic Research and development Department of Askoll.

    For more information about the Cximage class library, please visit its website or the author's homepage to learn more.

    Official website: http://www.codeproject.com/bitmap/cximage.asp

    http://www.xdp.it/

    Original address: http://blog.csdn.net/hejianhua/article/details/3934704

    "Reprint" VC + + Image type conversion--using the open source Cximage class Library

    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.