When freeimage is drawn to the handle, the Windows version of GCC can directly use the VC format lib of freeimage. [maybe it is only for the dll version lib of vc6? To be determined]
Add freeimage to the link of CB. Be sure not to include a suffix like freeimage. Lib.
/*************************************** ************************
* Name: free_imgmain.cpp
* Purpose: code for application frame
* Author :()
* Created: 2012-02-03
* Copyright :()
* License:
**************************************** **********************/
# Include "free_imgmain.h"
# Include <wx/msgdlg. h>
# Include "windows. H"
# Include "freeimage. H"
// (* Internalheaders (free_imgdialog)
# Include <wx/intl. h>
# Include <wx/string. h>
//*)
// Helper functions
Enum wxbuildinfoformat {
Short_f, long_f };
Wxstring wxbuildinfo (wxbuildinfoformat format)
{
Wxstring wxbuild (wxversion_string );
If (format = long_f)
{
# If defined (_ wxmsw __)
Wxbuild <_ T ("-Windows ");
# Elif defined (_ UNIX __)
Wxbuild <_ T ("-Linux ");
# Endif
# If wxuse_unicode
Wxbuild <_ T ("-Unicode build ");
# Else
Wxbuild <_ T ("-ANSI build ");
# Endif // wxuse_unicode
}
Return wxbuild;
}
// (* Idinit (free_imgdialog)
Const long free_imgdialog: id_button1 = wxnewid ();
Const long free_imgdialog: id_button2 = wxnewid ();
//*)
Begin_event_table (free_imgdialog, wxdialog)
// (* Eventtable (free_imgdialog)
//*)
End_event_table ()
Free_imgdialog: free_imgdialog (wxwindow * parent, wxwindowid ID)
{
// (* Initialize (free_imgdialog)
Create (parent, ID, _ ("wxWidgets app"), wxdefaultposition, wxdefaultsize, region | wxresize_border | wxmaximize_box | wxminimize_box, _ T ("ID "));
Button1 = new wxbutton (this, id_button1, _ ("label"), wxpoint (64,40), wxdefaultsize, 0, wxdefavalivalidator, _ T ("id_button1 "));
Button2 = new wxbutton (this, id_button2, _ ("label"), wxpoint (88,152), wxdefaultsize, 0, wxdefavalivalidator, _ T ("id_button2 "));
Center ();
Connect (id_button1, wxevt_command_button_clicked, (wxobjecteventfunction) & free_imgdialog: onbutton1click );
//*)
}
Free_imgdialog ::~ Free_imgdialog ()
{
// (* Destroy (free_imgdialog)
//*)
}
Void free_imgdialog: onquit (wxcommandevent & event)
{
Close ();
}
Void free_imgdialog: onabout (wxcommandevent & event)
{
Wxstring MSG = wxbuildinfo (long_f );
Wxmessagebox (MSG, _ ("Welcome ..."));
}
//----------------------------------------------------------
/**
Freeimage error handler
@ Param fif format/plugin responsible for the error
@ Param message error message
*/
Void freeimageerrorhandler (free_image_format fif, const char * message ){
Printf ("\ n ***");
If (FIF! = Cmd_unknown ){
Printf ("% s format \ n", freeimage_getformatfromfif (FIF ));
}
Printf (Message );
Printf ("*** \ n ");
}
//----------------------------------------------------------
Unsigned dll_callconv
Myreadproc (void * buffer, unsigned size, unsigned count, fi_handle handle ){
Return fread (buffer, size, Count, (File *) handle );
}
Unsigned dll_callconv
Mywriteproc (void * buffer, unsigned size, unsigned count, fi_handle handle ){
Return fwrite (buffer, size, Count, (File *) handle );
}
Int dll_callconv
Myseekproc (fi_handle handle, long offset, int origin ){
Return fseek (File *) handle, offset, origin );
}
Long dll_callconv
Mytellproc (fi_handle handle ){
Return ftell (File *) handle );
}
//----------------------------------------------------------
// Example from freeimage
Int main2 (INT argc, char * argv []) {
// Call this only when linking with freeimage as a static library
# Ifdef freeimage_lib
Freeimage_initialise ();
# Endif // freeimage_lib
// Initialize your own freeimage error handler
Freeimage_setoutputmessage (freeimageerrorhandler );
// Print version & Copyright Infos
Printf (freeimage_getversion ());
Printf ("\ n ");
Printf (freeimage_getcopyrightmessage ());
Printf ("\ n ");
If (argc! = 2 ){
Printf ("Usage: loadfromhandle <Input File Name> \ n ");
Return 0;
}
// Initialize your own Io Functions
Freeimageio;
Io. read_proc = myreadproc;
Io. write_proc = mywriteproc;
Io. seek_proc = myseekproc;
Io. tell_proc = mytellproc;
File * file = fopen (argv [1], "rb ");
If (file! = NULL ){
// Find the buffer format
Free_image_format fif = freeimage_getfiletypefromhandle (& Io, (fi_handle) file, 0 );
If (FIF! = Cmd_unknown ){
// Load from the file handle
Fibitmap * Dib = freeimage_loadfromhandle (FIF, & Io, (fi_handle) file, 0 );
// Save the bitmap as a PNG...
Const char * output_filename = "test.png ";
// First, check the output format from the file name or file extension
Free_image_format out_fif = freeimage_get1_fromfilename (output_filename );
If (out_fif! = Cmd_unknown ){
// Then save the file
Freeimage_save (out_fif, Dib, output_filename, 0 );
}
// Free the loaded Fiber itmap
Freeimage_unload (DIB );
}
Fclose (File );
}
// Call this only when linking with freeimage as a static library
# Ifdef freeimage_lib
Freeimage_deinitialise ();
# Endif // freeimage_lib
Return 0;
}
//--------------------------------------------------
Void draw2 (HDC, fibitmap * PFI)
{
// Fibitmap * PFI; // =; // pdoc-> m_pfi;
If (PFI ){
Byte * pbits = freeimage_getbits (PFI );
Bitmapinfo * PIF = freeimage_getinfo (PFI );
Int W = freeimage_getwidth (PFI );
Int H = freeimage_getheight (PFI );
// Crect rectclient; getclientrect (rectclient );
// Int dstw = rectclient. Width ();
// Int d…… = rectclient. Height ();
: Setstretchbltmode (HDC, coloroncolor );
: Setdibitstodevice (HDC, 0, 0, W, H, 0, 0, H, pbits, PIF, dib_rgb_colors );
}
}//
//----------------------------------------------------------
Int draw (HDC, char * fN ){
// Call this only when linking with freeimage as a static library
# Ifdef freeimage_lib
Freeimage_initialise ();
# Endif // freeimage_lib
// Initialize your own freeimage error handler
Freeimage_setoutputmessage (freeimageerrorhandler );
// Print version & Copyright Infos
Printf (freeimage_getversion ());
Printf ("\ n ");
Printf (freeimage_getcopyrightmessage ());
Printf ("\ n ");
// Initialize your own Io Functions
Freeimageio;
Io. read_proc = myreadproc;
Io. write_proc = mywriteproc;
Io. seek_proc = myseekproc;
Io. tell_proc = mytellproc;
File * file = fopen (FN, "rb ");
If (file! = NULL ){
// Find the buffer format
Free_image_format fif = freeimage_getfiletypefromhandle (& Io, (fi_handle) file, 0 );
If (FIF! = Cmd_unknown ){
// Load from the file handle
Fibitmap * Dib = freeimage_loadfromhandle (FIF, & Io, (fi_handle) file, 0 );
Draw2 (HDC, DIB );
// Save the bitmap as a PNG...
Const char * output_filename = "test.png ";
// First, check the output format from the file name or file extension
Free_image_format out_fif = freeimage_get1_fromfilename (output_filename );
If (out_fif! = Cmd_unknown ){
// Then save the file
Freeimage_save (out_fif, Dib, output_filename, 0 );
}
// Free the loaded Fiber itmap
Freeimage_unload (DIB );
}
Fclose (File );
}
// Call this only when linking with freeimage as a static library
# Ifdef freeimage_lib
Freeimage_deinitialise ();
# Endif // freeimage_lib
Return 0;
}
Void free_imgdialog: onbutton1click (wxcommandevent & event)
{
Char * argv [] = {"", "C: // 2.jpg "};
Main2 (2, argv );
// Freeimage _
HDC =: getdc (hwnd) This-> gethwnd ());
Draw (HDC, "C: // 1.txt ");
}
--------------------------------------------------
Several questions:
1. In fact, there are freeimage_load () functions that can be directly loaded without writing so many callbacks.
2. How to draw a transparent PNG Image? You may need to refer to cximage.
3. Is libpng faster than cximage? Cximage scaling is slow, far slower than the image browser that comes with Microsoft XP.