Game Development Reference
In-Depth Information
void* lfind( const void * key, const void * base, size_t num,
size_t width, int (*fncomparison)(const void *, const void * ) )
{
char* Ptr = (char*)base;
for ( size_t i = 0; i != num; i++, Ptr+=width )
{
if ( fncomparison( key, Ptr ) == 0 ) return Ptr;
}
return NULL;
}
3.
Now, a single command will do the job:
>ndk-build
How it works...
An image is a 2D array represented as a collection of raw pixel data, but there are too many
ways to store this array: there might be some compression applied, there might be some
non-RGB color spaces involved, or non-trivial pixel layouts. To avoid dealing with all these
complexities, we suggest using the FreeImage library by Herve Drolon.
We need to be able to deal with image ile data as a memory block and FreeImage supports
this kind of input. Suppose, we have a ile named
1.jpg
and we read it with an
fread()
or
ifstream::read()
calls into an array
char Buffer[]
. The size of the array is stored in
the
Size
variable. Then, we can create the
FIBITMAP
structure and use the
FreeImage_
OpenMemory()
API call to load the buffer into this
FIBITMAP
structure. The
FIBITMAP
structure is almost the 2D array we are looking for, with some extra information on the
pixels' layout and image size. To convert it to the 2D array, FreeImage provides the function
FreeImage_GetRowPtr()
that returns a pointer to the raw RGB data of the
i
th pixels row.
And vice versa, our frame buffer or any other 2D RGB image can be encoded into a memory
block with
FreeImage_SaveMemory()
and saved to a ile using a single
fwrite()
or
ofstream::write()
call.
Here is the code that loads any picture format supported by FreeImage, for example, JPEG,
TIFF, or PNG, and converts it into a 24-bit RGB image. Any other supported pixel formats, such
as RGBA or loating point EXR, will be automatically converted to a 24-bit color format. For the
sake of brevity, we do not handle errors in this code.
Let us declare a structure that will hold the image dimensions and pixel data:
struct sBitmap
{
int Width;
int Height;
void* RGBPixels;
};