Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/FreeImage/PluginBMP.cpp view on Meta::CPAN
return dib;
}
// seek to the actual pixel data
io->seek_proc(handle, bitmap_bits_offset, SEEK_SET);
// load pixel data and swap as needed if OS is Big Endian
LoadPixelData(io, handle, dib, height, pitch, bit_count);
return dib;
}
break; // 16-bit
case 24 :
case 32 :
{
int use_bitfields = 0;
if (bih.biCompression == BI_BITFIELDS) use_bitfields = 3;
else if (bih.biCompression == BI_ALPHABITFIELDS) use_bitfields = 4;
else if (type == 52) use_bitfields = 3;
else if (type >= 56) use_bitfields = 4;
if (use_bitfields > 0) {
DWORD bitfields[4];
io->read_proc(bitfields, use_bitfields * sizeof(DWORD), 1, handle);
dib = FreeImage_AllocateHeader(header_only, width, height, bit_count, bitfields[0], bitfields[1], bitfields[2]);
} else {
if( bit_count == 32 ) {
dib = FreeImage_AllocateHeader(header_only, width, height, bit_count, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
} else {
dib = FreeImage_AllocateHeader(header_only, width, height, bit_count, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
}
}
if (dib == NULL) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
// set resolution information
FreeImage_SetDotsPerMeterX(dib, bih.biXPelsPerMeter);
FreeImage_SetDotsPerMeterY(dib, bih.biYPelsPerMeter);
if(header_only) {
// header only mode
return dib;
}
// Skip over the optional palette
// A 24 or 32 bit DIB may contain a palette for faster color reduction
// i.e. you can have (FreeImage_GetColorsUsed(dib) > 0)
// seek to the actual pixel data
io->seek_proc(handle, bitmap_bits_offset, SEEK_SET);
// read in the bitmap bits
// load pixel data and swap as needed if OS is Big Endian
LoadPixelData(io, handle, dib, height, pitch, bit_count);
// check if the bitmap contains transparency, if so enable it in the header
FreeImage_SetTransparent(dib, (FreeImage_GetColorType(dib) == FIC_RGBALPHA));
return dib;
}
break; // 24-, 32-bit
}
} catch(const char *message) {
if(dib) {
FreeImage_Unload(dib);
}
if(message) {
FreeImage_OutputMessageProc(s_format_id, message);
}
}
return NULL;
}
// --------------------------------------------------------------------------
static FIBITMAP *
LoadOS22XBMP(FreeImageIO *io, fi_handle handle, int flags, unsigned bitmap_bits_offset) {
FIBITMAP *dib = NULL;
try {
BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;
// load the info header
BITMAPINFOHEADER bih;
io->read_proc(&bih, sizeof(BITMAPINFOHEADER), 1, handle);
#ifdef FREEIMAGE_BIGENDIAN
SwapInfoHeader(&bih);
#endif
// keep some general information about the bitmap
unsigned used_colors = bih.biClrUsed;
int width = bih.biWidth;
int height = bih.biHeight; // WARNING: height can be < 0 => check each read_proc using 'height' as a parameter
unsigned bit_count = bih.biBitCount;
unsigned compression = bih.biCompression;
unsigned pitch = CalculatePitch(CalculateLine(width, bit_count));
switch (bit_count) {
case 1 :
case 4 :
case 8 :
{
if ((used_colors == 0) || (used_colors > CalculateUsedPaletteEntries(bit_count)))
used_colors = CalculateUsedPaletteEntries(bit_count);
// allocate enough memory to hold the bitmap (header, palette, pixels) and read the palette
dib = FreeImage_AllocateHeader(header_only, width, height, bit_count);
if (dib == NULL) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
src/Source/FreeImage/PluginBMP.cpp view on Meta::CPAN
}
if (dib == NULL) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
// set resolution information
FreeImage_SetDotsPerMeterX(dib, bih.biXPelsPerMeter);
FreeImage_SetDotsPerMeterY(dib, bih.biYPelsPerMeter);
if(header_only) {
// header only mode
return dib;
}
if (bitmap_bits_offset > (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (used_colors * 3))) {
io->seek_proc(handle, bitmap_bits_offset, SEEK_SET);
}
// load pixel data and swap as needed if OS is Big Endian
LoadPixelData(io, handle, dib, height, pitch, bit_count);
return dib;
}
case 24 :
case 32 :
{
if( bit_count == 32 ) {
dib = FreeImage_AllocateHeader(header_only, width, height, bit_count, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
} else {
dib = FreeImage_AllocateHeader(header_only, width, height, bit_count, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
}
if (dib == NULL) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
// set resolution information
FreeImage_SetDotsPerMeterX(dib, bih.biXPelsPerMeter);
FreeImage_SetDotsPerMeterY(dib, bih.biYPelsPerMeter);
if(header_only) {
// header only mode
return dib;
}
// Skip over the optional palette
// A 24 or 32 bit DIB may contain a palette for faster color reduction
if (bitmap_bits_offset > (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (used_colors * 3))) {
io->seek_proc(handle, bitmap_bits_offset, SEEK_SET);
}
// read in the bitmap bits
// load pixel data and swap as needed if OS is Big Endian
LoadPixelData(io, handle, dib, height, pitch, bit_count);
// check if the bitmap contains transparency, if so enable it in the header
FreeImage_SetTransparent(dib, (FreeImage_GetColorType(dib) == FIC_RGBALPHA));
return dib;
}
}
} catch(const char *message) {
if(dib)
FreeImage_Unload(dib);
FreeImage_OutputMessageProc(s_format_id, message);
}
return NULL;
}
// --------------------------------------------------------------------------
static FIBITMAP *
LoadOS21XBMP(FreeImageIO *io, fi_handle handle, int flags, unsigned bitmap_bits_offset) {
FIBITMAP *dib = NULL;
try {
BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;
BITMAPINFOOS2_1X_HEADER bios2_1x;
io->read_proc(&bios2_1x, sizeof(BITMAPINFOOS2_1X_HEADER), 1, handle);
#ifdef FREEIMAGE_BIGENDIAN
SwapOS21XHeader(&bios2_1x);
#endif
// keep some general information about the bitmap
unsigned used_colors = 0;
unsigned width = bios2_1x.biWidth;
unsigned height = bios2_1x.biHeight; // WARNING: height can be < 0 => check each read_proc using 'height' as a parameter
unsigned bit_count = bios2_1x.biBitCount;
unsigned pitch = CalculatePitch(CalculateLine(width, bit_count));
switch (bit_count) {
case 1 :
case 4 :
case 8 :
{
used_colors = CalculateUsedPaletteEntries(bit_count);
// allocate enough memory to hold the bitmap (header, palette, pixels) and read the palette
dib = FreeImage_AllocateHeader(header_only, width, height, bit_count);
if (dib == NULL) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
// set resolution information to default values (72 dpi in english units)
FreeImage_SetDotsPerMeterX(dib, 2835);
FreeImage_SetDotsPerMeterY(dib, 2835);
// load the palette
RGBQUAD *pal = FreeImage_GetPalette(dib);
src/Source/FreeImage/PluginBMP.cpp view on Meta::CPAN
// load pixel data
LoadPixelData(io, handle, dib, height, pitch, bit_count);
return dib;
}
case 16 :
{
dib = FreeImage_AllocateHeader(header_only, width, height, bit_count, FI16_555_RED_MASK, FI16_555_GREEN_MASK, FI16_555_BLUE_MASK);
if (dib == NULL) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
// set resolution information to default values (72 dpi in english units)
FreeImage_SetDotsPerMeterX(dib, 2835);
FreeImage_SetDotsPerMeterY(dib, 2835);
if(header_only) {
// header only mode
return dib;
}
// load pixel data and swap as needed if OS is Big Endian
LoadPixelData(io, handle, dib, height, pitch, bit_count);
return dib;
}
case 24 :
case 32 :
{
if( bit_count == 32 ) {
dib = FreeImage_AllocateHeader(header_only, width, height, bit_count, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
} else {
dib = FreeImage_AllocateHeader(header_only, width, height, bit_count, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
}
if (dib == NULL) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
// set resolution information to default values (72 dpi in english units)
FreeImage_SetDotsPerMeterX(dib, 2835);
FreeImage_SetDotsPerMeterY(dib, 2835);
if(header_only) {
// header only mode
return dib;
}
// Skip over the optional palette
// A 24 or 32 bit DIB may contain a palette for faster color reduction
// load pixel data and swap as needed if OS is Big Endian
LoadPixelData(io, handle, dib, height, pitch, bit_count);
// check if the bitmap contains transparency, if so enable it in the header
FreeImage_SetTransparent(dib, (FreeImage_GetColorType(dib) == FIC_RGBALPHA));
return dib;
}
}
} catch(const char *message) {
if(dib)
FreeImage_Unload(dib);
FreeImage_OutputMessageProc(s_format_id, message);
}
return NULL;
}
// ==========================================================
// Plugin Implementation
// ==========================================================
static const char * DLL_CALLCONV
Format() {
return "BMP";
}
static const char * DLL_CALLCONV
Description() {
return "Windows or OS/2 Bitmap";
}
static const char * DLL_CALLCONV
Extension() {
return "bmp";
}
static const char * DLL_CALLCONV
RegExpr() {
return "^BM";
}
static const char * DLL_CALLCONV
MimeType() {
return "image/bmp";
}
static BOOL DLL_CALLCONV
Validate(FreeImageIO *io, fi_handle handle) {
BYTE bmp_signature1[] = { 0x42, 0x4D };
BYTE bmp_signature2[] = { 0x42, 0x41 };
BYTE signature[2] = { 0, 0 };
io->read_proc(signature, 1, sizeof(bmp_signature1), handle);
if (memcmp(bmp_signature1, signature, sizeof(bmp_signature1)) == 0)
return TRUE;
if (memcmp(bmp_signature2, signature, sizeof(bmp_signature2)) == 0)
return TRUE;
return FALSE;
}
( run in 1.454 second using v1.01-cache-2.11-cpan-fa01517f264 )