The FreeImage Project

Frequently Asked Questions

 

Developer's Corner

Documentation

FAQ

Mailing List

Changes Log

Source Code

How do I draw a FreeImage image to the screen in Windows ?

FIBITMAP *dib = FreeImage_Load(FIF_PNG, "test.png", PNG_DEFAULT);
// ...
SetStretchBltMode(hDC, COLORONCOLOR);
StretchDIBits(hDC, rcDest.left, rcDest.top,

rcDest.right-rcDest.left, rcDest.bottom-rcDest.top,
0, 0, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib),
FreeImage_GetBits(dib), FreeImage_GetInfo(dib), DIB_RGB_COLORS, SRCCOPY);

// ...
FreeImage_Unload(dib);

 

 

How do I convert a FreeImage image to a HBITMAP ?

FIBITMAP *dib = FreeImage_Load(FIF_PNG, "test.png", PNG_DEFAULT);
// ...
HBITMAP bitmap = CreateDIBitmap(hDC, FreeImage_GetInfoHeader(dib),

CBM_INIT, FreeImage_GetBits(dib), FreeImage_GetInfo(dib), DIB_RGB_COLORS);

// ...
FreeImage_Unload(dib);

 

 

How do I convert a HBITMAP to a FreeImage image ?

HBITMAP hbmp;
// ...
// the following code assumes that you have a valid HBITMAP loaded into the memory
FIBITMAP *dib = NULL;
if(hbmp) {

BITMAP bm;
GetObject(hbmp, sizeof(BITMAP), (LPSTR) &bm);
dib = FreeImage_Allocate(bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);
// The GetDIBits function clears the biClrUsed and biClrImportant BITMAPINFO members (dont't know why)
// So we save these infos below. This is needed for palettized images only.
int nColors = FreeImage_GetColorsUsed(dib);
HDC dc = GetDC(NULL);
int Success = GetDIBits(dc, hbmp, 0, FreeImage_GetHeight(dib),
FreeImage_GetBits(dib), FreeImage_GetInfo(dib), DIB_RGB_COLORS);
ReleaseDC(NULL, dc);
// restore BITMAPINFO members
FreeImage_GetInfoHeader(dib)->biClrUsed = nColors;
FreeImage_GetInfoHeader(dib)->biClrImportant = nColors;

}
// ...
// don't forget to call FreeImage_Unload when you no longer need the dib
FreeImage_Unload(dib);

 

 

How to use FreeImage as a static library instead of as a DLL (Visual C++ 6) ?

Using FreeImage as a static library is not so easy, but it's not more complicated than using any other library. Here is a step by step method to do this :

1. Compile the FreeImage library in debug and release modes and close your projects. You won't need to use the FreeImage source code anymore.
Note: Do not compile the FreeImage DLL (project named FreeImage) but the project named FreeImageLib. This should produce a huge file named FreeImage.lib (in release mode) or FreeImaged.lib (in debug mode) in the Dist\ directory.

 

2. Copy FreeImage.lib/FreeImaged.lib into your lib\ directory or in a directory where the linker can find them (e.g. your project directory). You can use "Menu->Tools->Options->Directories->Library files" for this.

 

3. Create a new project and add your code in it.
Add a call to FreeImage_Initialise() at the beginning of you main function and a call to FreeImage_DeInitialise() at the end of this function.

 

4. Edit the compiler options (Menu -> Project -> Settings)

1. tab C/C++ : Category "Preprocessor"
* Add FREEIMAGE_LIB to the preprocessor definitions
2. tab C/C++ : Category "Code Generation"
* Use the Multithreaded run-time library (in release mode)
* Use the Debug Multithreaded run-time library (in debug mode)

5. Edit linker options (Menu -> Project -> Settings)

1. tab Link : Category Input
* Add FreeImage.lib to the list of object/library modules (release mode)
* Add FreeImaged.lib to the list of object/library modules (debug mode)
2. tab Link : Category Input
* Add LIBCMT to the Ignore library list (it helps to avoid a warning)

6. Compile and link your program.

 

Why can't I save this image as JPEG ?

First, make sure to use FreeImage_SetOutputMessage to see what's going on inside the JPEG plugin.

Next, the JPEG specification only allows 8-bit greyscale, 24-bit RGB and 32-bit CMYK images to be saved as JPEG. Thus, you cannot save a 32-bit RGBA image to JPEG, nor a 1- or 4-bit palettized images. As for 8-bit palettized images, if you try to save one of them as JPEG, it will be transparently converted to 24-bit by FreeImage.

 

 

Why FreeImage loads image in BGR format ?

FreeImage actually doesn't neccessarily use BGR(A) ordering. Section "Pixel access functions" in the FreeImage documentation contains the following statement :

However, the pixel layout used by this model is OS dependant. Using a byte by byte memory order to label the pixel layout, then FreeImage uses a BGR[A] pixel layout under a Little Endian processor (Windows, Linux) and uses a RGB[A] pixel layout under a Big Endian processor (Mac OS X or any Big Endian Linux / Unix). This choice was made to ease the use of FreeImage with graphics API. This subtle difference is however transparent to the user. In order to make pixel access OS independent, FreeImage defines a set of macros used to set or get individual color components in a 24- or 32-bit DIB.


However, these macros will not help you when you want to use FreeImage's raw pixel data as source for OpenGL. OpenGL will accept BGR(A) as well as RGB(A) as image data. And actually, FreeImage's choice, as BGR(A) is most likely the native data format on such platforms, so that using this format represents the most efficient way to transfer image data.

FreeImage can be built to always load in RGB order independent of the architecture.
Use

#define FREEIMAGE_COLORORDER=FREEIMAGE_COLORORDER_RGB

If you're using Android.mk to build FreeImage, add the following line to it:

LOCAL_CPPFLAGS += -DFREEIMAGE_COLORORDER=FREEIMAGE_COLORORDER_RGB

Documentation for this can be found in Source/FreeImage.h if you search for FREEIMAGE_COLORORDER:

Color-Order: The specified order of color components red, green and blue affects 24- and 32-bit images of type FIT_BITMAP as well as the colors that are part of a color palette. All other images always use RGB order. By default, color order is coupled to endianness: little-endian -> BGR big-endian -> RGB

However, you can always define FREEIMAGE_COLORORDER to any of the known orders FREEIMAGE_COLORORDER_BGR (0) and FREEIMAGE_COLORORDER_RGB (1) to specify your preferred color order.

 

 

 

 


© 2003-2023 by FreeImage
Page maintained by Hervé Drolon

 

 

Website Design by Web World Expert

Licensed under Creative Commons License