BMAP *bmap_copy(BMAP *source)
{
if(!source)
return NULL; // Oh look, invalid data...
var width, height, format, bitDepth;
BMAP *result; // The resulting bitmap
width = bmap_width(source); // Get the width
height = bmap_height(source); // and height of the source bitmap
format = bmap_lock(source, 0); // Lock the source bitmap
// Get the bitdepth of the source
// Only supported by the pixel functions is 16, 24 and 32 bit, so we don't care about the rest
switch(format)
{
case 88:
case 565:
bitDepth = 16;
break;
case 888:
bitDepth = 24;
break;
case 8888:
bitDepth = 32;
break;
default:
bmap_unlock(source);
return NULL; // Well, the bitmap isn't in a format we can use
break;
}
// Create a new, void, bitmap with the exact same size and bit depth
result = bmap_createblack(width, height, bitDepth);
if(result)
{
int x, y;
bmap_lock(result, 0); // Lock the new bitmap so that we can write into it
// Copy all pixels by iterating over each source pixel and storing it in the copy
for(x=0; x<width; x++)
{
for(y=0; y<height; y++)
{
var pixel = pixel_for_bmap(source, x, y);
pixel_to_bmap(result, x, y, pixel);
}
}
bmap_unlock(result); // Unlock the previously locked copy
}
bmap_unlock(source); // Unlock the source
return result; // Return the result, this might be NULL if the bmap_createblack() failed
}