Retrieving image data from memory
I'm using the FreeImage Library to handle loading images and passing them to OpenGL. Currently, I have support for reading an image from a file. However, I'd like to extend this to being able to read from a variable for the purpose of game content packages. Basically, in short, I have the entire file, header and all, written to an unsigned char *. I want to take this buffer and do something similar to this with it (assume variable fif is declared):
FIBITMAP * dib = FreeImage_Load(fif, "someImage.png");
but instead of someImage.png, I want to specify the unsigned char * (lets call it buffer for the sake of the question). Is there开发者_运维百科 a method in the library that can handle such a thing?
EDIT: I should be a bit more specific considering "someImage.png" can be considered an unsigned char *.
To clear confusion up, if any, the value of unsigned char * buffer would be determined by something like this psuedo code:
fileStream = openFile "someImage.png"
unsigned char * buffer = fileStream.ReadToEnd
It sounds like you want to use FreeImage_ConvertToRawBits. Here is an example from the documentation:
// this code assumes there is a bitmap loaded and
// present in a variable called ‘dib’
// convert a bitmap to a 32-bit raw buffer (top-left pixel first)
// --------------------------------------------------------------
FIBITMAP *src = FreeImage_ConvertTo32Bits(dib);
FreeImage_Unload(dib);
// Allocate a raw buffer
int width = FreeImage_GetWidth(src);
int height = FreeImage_GetHeight(src);
int scan_width = FreeImage_GetPitch(src);
BYTE *bits = (BYTE*)malloc(height * scan_width);
// convert the bitmap to raw bits (top-left pixel first)
FreeImage_ConvertToRawBits(bits, src, scan_width, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE);
FreeImage_Unload(src);
// convert a 32-bit raw buffer (top-left pixel first) to a FIBITMAP
// ----------------------------------------------------------------
FIBITMAP *dst = FreeImage_ConvertFromRawBits(bits, width, height, scan_width, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
Use FreeImage_LoadFromHandle
. You'll need to create 3 functions that can access the buffer, and put the addresses of those functions in a FreeImageIO structure.
精彩评论