Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/FreeImage/PluginPNG.cpp view on Meta::CPAN
palette[i].rgbGreen = png_palette[i].green;
palette[i].rgbBlue = png_palette[i].blue;
}
}
break;
case PNG_COLOR_TYPE_GRAY:
dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
if(dib && (pixel_depth <= 8)) {
RGBQUAD *palette = FreeImage_GetPalette(dib);
const int palette_entries = 1 << pixel_depth;
for(int i = 0; i < palette_entries; i++) {
palette[i].rgbRed =
palette[i].rgbGreen =
palette[i].rgbBlue = (BYTE)((i * 255) / (palette_entries - 1));
}
}
break;
default:
throw FI_MSG_ERROR_UNSUPPORTED_FORMAT;
}
if(!dib) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
// store the transparency table
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
// array of alpha (transparency) entries for palette
png_bytep trans_alpha = NULL;
// number of transparent entries
int num_trans = 0;
// graylevel or color sample values of the single transparent color for non-paletted images
png_color_16p trans_color = NULL;
png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans, &trans_color);
if((color_type == PNG_COLOR_TYPE_GRAY) && trans_color) {
// single transparent color
if (trans_color->gray < 256) {
BYTE table[256];
memset(table, 0xFF, 256);
table[trans_color->gray] = 0;
FreeImage_SetTransparencyTable(dib, table, 256);
}
// check for a full transparency table, too
else if ((trans_alpha) && (pixel_depth <= 8)) {
FreeImage_SetTransparencyTable(dib, (BYTE *)trans_alpha, num_trans);
}
} else if((color_type == PNG_COLOR_TYPE_PALETTE) && trans_alpha) {
// transparency table
FreeImage_SetTransparencyTable(dib, (BYTE *)trans_alpha, num_trans);
}
}
// store the background color (only supported for FIT_BITMAP types)
if ((image_type == FIT_BITMAP) && png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD)) {
// Get the background color to draw transparent and alpha images over.
// Note that even if the PNG file supplies a background, you are not required to
// use it - you should use the (solid) application background if it has one.
png_color_16p image_background = NULL;
RGBQUAD rgbBkColor;
if (png_get_bKGD(png_ptr, info_ptr, &image_background)) {
rgbBkColor.rgbRed = (BYTE)image_background->red;
rgbBkColor.rgbGreen = (BYTE)image_background->green;
rgbBkColor.rgbBlue = (BYTE)image_background->blue;
rgbBkColor.rgbReserved = 0;
FreeImage_SetBackgroundColor(dib, &rgbBkColor);
}
}
// get physical resolution
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_pHYs)) {
png_uint_32 res_x, res_y;
// we'll overload this var and use 0 to mean no phys data,
// since if it's not in meters we can't use it anyway
int res_unit_type = PNG_RESOLUTION_UNKNOWN;
png_get_pHYs(png_ptr,info_ptr, &res_x, &res_y, &res_unit_type);
if (res_unit_type == PNG_RESOLUTION_METER) {
FreeImage_SetDotsPerMeterX(dib, res_x);
FreeImage_SetDotsPerMeterY(dib, res_y);
}
}
// get possible ICC profile
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_iCCP)) {
png_charp profile_name = NULL;
png_bytep profile_data = NULL;
png_uint_32 profile_length = 0;
int compression_type;
png_get_iCCP(png_ptr, info_ptr, &profile_name, &compression_type, &profile_data, &profile_length);
// copy ICC profile data (must be done after FreeImage_AllocateHeader)
FreeImage_CreateICCProfile(dib, profile_data, profile_length);
}
// --- header only mode => clean-up and return
if (header_only) {
// get possible metadata (it can be located both before and after the image data)
ReadMetadata(png_ptr, info_ptr, dib);
if (png_ptr) {
// clean up after the read, and free any memory allocated - REQUIRED
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
}
return dib;
}
// set the individual row_pointers to point at the correct offsets
row_pointers = (png_bytepp)malloc(height * sizeof(png_bytep));
if (!row_pointers) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
FreeImage_Unload(dib);
return NULL;
}
src/Source/FreeImage/PluginPNG.cpp view on Meta::CPAN
// You must not free palette here, because png_set_PLTE only makes a link to
// the palette that you malloced. Wait until you are about to destroy
// the png structure.
break;
}
case FIC_RGBALPHA :
has_alpha_channel = TRUE;
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
PNG_COLOR_TYPE_RGBA, interlace_type,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
// flip BGR pixels to RGB
if(image_type == FIT_BITMAP) {
png_set_bgr(png_ptr);
}
#endif
break;
case FIC_RGB:
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
PNG_COLOR_TYPE_RGB, interlace_type,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
// flip BGR pixels to RGB
if(image_type == FIT_BITMAP) {
png_set_bgr(png_ptr);
}
#endif
break;
case FIC_CMYK:
break;
}
// write possible ICC profile
FIICCPROFILE *iccProfile = FreeImage_GetICCProfile(dib);
if (iccProfile->size && iccProfile->data) {
png_set_iCCP(png_ptr, info_ptr, "Embedded Profile", 0, (png_const_bytep)iccProfile->data, iccProfile->size);
}
// write metadata
WriteMetadata(png_ptr, info_ptr, dib);
// Optional gamma chunk is strongly suggested if you have any guess
// as to the correct gamma of the image.
// png_set_gAMA(png_ptr, info_ptr, gamma);
// set the transparency table
if (bIsTransparent) {
png_set_tRNS(png_ptr, info_ptr, FreeImage_GetTransparencyTable(dib), FreeImage_GetTransparencyCount(dib), NULL);
}
// set the background color
if(FreeImage_HasBackgroundColor(dib)) {
png_color_16 image_background;
RGBQUAD rgbBkColor;
FreeImage_GetBackgroundColor(dib, &rgbBkColor);
memset(&image_background, 0, sizeof(png_color_16));
image_background.blue = rgbBkColor.rgbBlue;
image_background.green = rgbBkColor.rgbGreen;
image_background.red = rgbBkColor.rgbRed;
image_background.index = rgbBkColor.rgbReserved;
png_set_bKGD(png_ptr, info_ptr, &image_background);
}
// Write the file header information.
png_write_info(png_ptr, info_ptr);
// write out the image data
#ifndef FREEIMAGE_BIGENDIAN
if (bit_depth == 16) {
// turn on 16 bit byte swapping
png_set_swap(png_ptr);
}
#endif
int number_passes = 1;
if (bInterlaced) {
number_passes = png_set_interlace_handling(png_ptr);
}
if ((pixel_depth == 32) && (!has_alpha_channel)) {
BYTE *buffer = (BYTE *)malloc(width * 3);
// transparent conversion to 24-bit
// the number of passes is either 1 for non-interlaced images, or 7 for interlaced images
for (int pass = 0; pass < number_passes; pass++) {
for (png_uint_32 k = 0; k < height; k++) {
FreeImage_ConvertLine32To24(buffer, FreeImage_GetScanLine(dib, height - k - 1), width);
png_write_row(png_ptr, buffer);
}
}
free(buffer);
} else {
// the number of passes is either 1 for non-interlaced images, or 7 for interlaced images
for (int pass = 0; pass < number_passes; pass++) {
for (png_uint_32 k = 0; k < height; k++) {
png_write_row(png_ptr, FreeImage_GetScanLine(dib, height - k - 1));
}
}
}
// It is REQUIRED to call this to finish writing the rest of the file
// Bug with png_flush
png_write_end(png_ptr, info_ptr);
// clean up after the write, and free any memory allocated
if (palette) {
png_free(png_ptr, palette);
}
png_destroy_write_struct(&png_ptr, &info_ptr);
return TRUE;
} catch (const char *text) {
if(png_ptr) {
png_destroy_write_struct(&png_ptr, &info_ptr);
}
FreeImage_OutputMessageProc(s_format_id, text);
( run in 2.161 seconds using v1.01-cache-2.11-cpan-0d23b851a93 )