Game Development Reference
In-Depth Information
Decoding the image from the memory block to the sBitmap structure is done this way:
void FreeImage_LoadImageFromMemory( unsigned char* Data, unsigned
int Size, sBitmap* OutBitmap )
{
FIMEMORY* Mem = FreeImage_OpenMemory( Data, Size );
FREE_IMAGE_FORMAT FIF=FreeImage_GetFileTypeFromMemory(Mem, 0);
FIBITMAP* Bitmap = FreeImage_LoadFromMemory( FIF, Mem, 0 );
FIBITMAP* ConvBitmap;
FreeImage_CloseMemory( Mem );
ConvBitmap = FreeImage_ConvertTo24Bits( Bitmap );
FreeImage_Unload( Bitmap );
Bitmap = ConvBitmap;
OutBitmap->Width = FreeImage_GetWidth( Bitmap );
OutBitmap->Height = FreeImage_GetHeight( Bitmap );
OutBitmap->RGBPixels = malloc( OutBitmap->Width *
OutBitmap->Height * 3 );
FreeImage_ConvertToRawBits( OutBitmap->RGBPixels, Bitmap,
OutBitmap->Width * 3, 24, 0, 1, 2, false );
FreeImage_Unload( Bitmap );
}
Saving the image is even simpler. Save the array img representing the image with width W ,
height H , and containing BitsPP bits per pixel:
void FreeImage_Save( const char* fname, unsigned char* img, int W,
int H, int BitsPP )
{
// Create the FIBITMAP structure
// using the source image data
FIBITMAP* Bitmap = FI_ConvertFromRawBits(img,
W, H, W * BitsPP / 8,
BitsPP, 0, 1, 2, false);
// save PNG file using the default parameters
FI_Save( FIF_PNG, Bitmap, fname, PNG_DEFAULT );
FI_Unload( Bitmap );
}
 
Search WWH ::




Custom Search