Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/FreeImage/PluginPNG.cpp view on Meta::CPAN
// PNG Loader and Writer
//
// Design and implementation by
// - Floris van den Berg (flvdberg@wxs.nl)
// - Herve Drolon (drolon@infonie.fr)
// - Detlev Vendt (detlev.vendt@brillit.de)
// - Aaron Shumate (trek@startreker.com)
// - Tanner Helland (tannerhelland@users.sf.net)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
#ifdef _MSC_VER
#pragma warning (disable : 4786) // identifier was truncated to 'number' characters
#endif
#include "FreeImage.h"
#include "Utilities.h"
#include "../Metadata/FreeImageTag.h"
// ----------------------------------------------------------
#define PNG_BYTES_TO_CHECK 8
#undef PNG_Z_DEFAULT_COMPRESSION // already used in ../LibPNG/pnglibconf.h
// ----------------------------------------------------------
#include "../ZLib/zlib.h"
#include "../LibPNG/png.h"
// ----------------------------------------------------------
typedef struct {
FreeImageIO *s_io;
fi_handle s_handle;
} fi_ioStructure, *pfi_ioStructure;
// ==========================================================
// libpng interface
// ==========================================================
static void
_ReadProc(png_structp png_ptr, unsigned char *data, png_size_t size) {
pfi_ioStructure pfio = (pfi_ioStructure)png_get_io_ptr(png_ptr);
unsigned n = pfio->s_io->read_proc(data, (unsigned int)size, 1, pfio->s_handle);
if(size && (n == 0)) {
throw "Read error: invalid or corrupted PNG file";
}
}
static void
_WriteProc(png_structp png_ptr, unsigned char *data, png_size_t size) {
pfi_ioStructure pfio = (pfi_ioStructure)png_get_io_ptr(png_ptr);
pfio->s_io->write_proc(data, (unsigned int)size, 1, pfio->s_handle);
}
static void
_FlushProc(png_structp png_ptr) {
(png_structp)png_ptr;
// empty flush implementation
}
static void
error_handler(png_structp png_ptr, const char *error) {
(png_structp)png_ptr;
throw error;
}
// in FreeImage warnings disabled
static void
warning_handler(png_structp png_ptr, const char *warning) {
(png_structp)png_ptr;
(char*)warning;
}
// ==========================================================
// Metadata routines
// ==========================================================
static BOOL
ReadMetadata(png_structp png_ptr, png_infop info_ptr, FIBITMAP *dib) {
// XMP keyword
const char *g_png_xmp_keyword = "XML:com.adobe.xmp";
FITAG *tag = NULL;
png_textp text_ptr = NULL;
png_timep mod_time = NULL;
int num_text = 0;
// iTXt/tEXt/zTXt chuncks
if(png_get_text(png_ptr, info_ptr, &text_ptr, &num_text) > 0) {
for(int i = 0; i < num_text; i++) {
// create a tag
tag = FreeImage_CreateTag();
if(!tag) return FALSE;
DWORD tag_length = (DWORD) MAX(text_ptr[i].text_length, text_ptr[i].itxt_length);
FreeImage_SetTagLength(tag, tag_length);
FreeImage_SetTagCount(tag, tag_length);
FreeImage_SetTagType(tag, FIDT_ASCII);
FreeImage_SetTagValue(tag, text_ptr[i].text);
if(strcmp(text_ptr[i].key, g_png_xmp_keyword) == 0) {
// store the tag as XMP
FreeImage_SetTagKey(tag, g_TagLib_XMPFieldName);
FreeImage_SetMetadata(FIMD_XMP, dib, FreeImage_GetTagKey(tag), tag);
} else {
// store the tag as a comment
FreeImage_SetTagKey(tag, text_ptr[i].key);
FreeImage_SetMetadata(FIMD_COMMENTS, dib, FreeImage_GetTagKey(tag), tag);
}
// destroy the tag
FreeImage_DeleteTag(tag);
}
}
// timestamp chunk
if(png_get_tIME(png_ptr, info_ptr, &mod_time)) {
char timestamp[32];
// create a tag
tag = FreeImage_CreateTag();
if(!tag) return FALSE;
src/Source/FreeImage/PluginPNG.cpp view on Meta::CPAN
// flip the RGB pixels to BGR (or RGBA to BGRA)
png_set_bgr(png_ptr);
}
#endif
// gamma correction
// unlike the example in the libpng documentation, we have *no* idea where
// this file may have come from--so if it doesn't have a file gamma, don't
// do any correction ("do no harm")
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA)) {
double gamma = 0;
double screen_gamma = 2.2;
if (png_get_gAMA(png_ptr, info_ptr, &gamma) && ( flags & PNG_IGNOREGAMMA ) != PNG_IGNOREGAMMA) {
png_set_gamma(png_ptr, screen_gamma, gamma);
}
}
// all transformations have been registered; now update info_ptr data
png_read_update_info(png_ptr, info_ptr);
// return the output image type
*output_image_type = image_type;
return TRUE;
}
static FIBITMAP * DLL_CALLCONV
Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_uint_32 width, height;
int color_type;
int bit_depth;
int pixel_depth = 0; // pixel_depth = bit_depth * channels
FIBITMAP *dib = NULL;
png_bytepp row_pointers = NULL;
fi_ioStructure fio;
fio.s_handle = handle;
fio.s_io = io;
if (handle) {
BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;
try {
// check to see if the file is in fact a PNG file
BYTE png_check[PNG_BYTES_TO_CHECK];
io->read_proc(png_check, PNG_BYTES_TO_CHECK, 1, handle);
if (png_sig_cmp(png_check, (png_size_t)0, PNG_BYTES_TO_CHECK) != 0) {
return NULL; // Bad signature
}
// create the chunk manage structure
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, error_handler, warning_handler);
if (!png_ptr) {
return NULL;
}
// create the info structure
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
return NULL;
}
// init the IO
png_set_read_fn(png_ptr, &fio, _ReadProc);
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return NULL;
}
// because we have already read the signature...
png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK);
// read the IHDR chunk
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
// configure the decoder
FREE_IMAGE_TYPE image_type = FIT_BITMAP;
if(!ConfigureDecoder(png_ptr, info_ptr, flags, &image_type)) {
throw FI_MSG_ERROR_UNSUPPORTED_FORMAT;
}
// update image info
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
pixel_depth = bit_depth * png_get_channels(png_ptr, info_ptr);
// create a dib and write the bitmap header
// set up the dib palette, if needed
switch (color_type) {
case PNG_COLOR_TYPE_RGB:
case PNG_COLOR_TYPE_RGB_ALPHA:
dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
break;
case PNG_COLOR_TYPE_PALETTE:
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) {
png_colorp png_palette = NULL;
int palette_entries = 0;
src/Source/FreeImage/PluginPNG.cpp view on Meta::CPAN
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;
}
// read in the bitmap bits via the pointer table
// allow loading of PNG with minor errors (such as images with several IDAT chunks)
for (png_uint_32 k = 0; k < height; k++) {
row_pointers[height - 1 - k] = FreeImage_GetScanLine(dib, k);
}
png_set_benign_errors(png_ptr, 1);
png_read_image(png_ptr, row_pointers);
// check if the bitmap contains transparency, if so enable it in the header
if (FreeImage_GetBPP(dib) == 32) {
if (FreeImage_GetColorType(dib) == FIC_RGBALPHA) {
FreeImage_SetTransparent(dib, TRUE);
} else {
FreeImage_SetTransparent(dib, FALSE);
}
}
// cleanup
if (row_pointers) {
free(row_pointers);
row_pointers = NULL;
}
// read the rest of the file, getting any additional chunks in info_ptr
png_read_end(png_ptr, info_ptr);
// 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;
} catch (const char *text) {
if (png_ptr) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
}
if (row_pointers) {
free(row_pointers);
}
if (dib) {
FreeImage_Unload(dib);
}
FreeImage_OutputMessageProc(s_format_id, text);
return NULL;
}
}
return NULL;
}
// --------------------------------------------------------------------------
static BOOL DLL_CALLCONV
Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data) {
png_structp png_ptr;
png_infop info_ptr;
png_colorp palette = NULL;
png_uint_32 width, height;
BOOL has_alpha_channel = FALSE;
RGBQUAD *pal; // pointer to dib palette
int bit_depth, pixel_depth; // pixel_depth = bit_depth * channels
int palette_entries;
int interlace_type;
fi_ioStructure fio;
fio.s_handle = handle;
fio.s_io = io;
if ((dib) && (handle)) {
try {
// create the chunk manage structure
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, error_handler, warning_handler);
if (!png_ptr) {
return FALSE;
}
// allocate/initialize the image information data.
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
return FALSE;
}
// Set error handling. REQUIRED if you aren't supplying your own
// error handling functions in the png_create_write_struct() call.
if (setjmp(png_jmpbuf(png_ptr))) {
// if we get here, we had a problem reading the file
png_destroy_write_struct(&png_ptr, &info_ptr);
return FALSE;
}
// init the IO
png_set_write_fn(png_ptr, &fio, _WriteProc, _FlushProc);
// set physical resolution
png_uint_32 res_x = (png_uint_32)FreeImage_GetDotsPerMeterX(dib);
png_uint_32 res_y = (png_uint_32)FreeImage_GetDotsPerMeterY(dib);
if ((res_x > 0) && (res_y > 0)) {
png_set_pHYs(png_ptr, info_ptr, res_x, res_y, PNG_RESOLUTION_METER);
}
// Set the image information here. Width and height are up to 2^31,
// bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
// the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
// PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
// or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
// PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
// currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
pixel_depth = FreeImage_GetBPP(dib);
BOOL bInterlaced = FALSE;
if( (flags & PNG_INTERLACED) == PNG_INTERLACED) {
interlace_type = PNG_INTERLACE_ADAM7;
bInterlaced = TRUE;
} else {
interlace_type = PNG_INTERLACE_NONE;
}
// set the ZLIB compression level or default to PNG default compression level (ZLIB level = 6)
int zlib_level = flags & 0x0F;
if((zlib_level >= 1) && (zlib_level <= 9)) {
png_set_compression_level(png_ptr, zlib_level);
} else if((flags & PNG_Z_NO_COMPRESSION) == PNG_Z_NO_COMPRESSION) {
png_set_compression_level(png_ptr, Z_NO_COMPRESSION);
}
// filtered strategy works better for high color images
if(pixel_depth >= 16){
png_set_compression_strategy(png_ptr, Z_FILTERED);
png_set_filter(png_ptr, 0, PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_PAETH);
} else {
png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
}
FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
if(image_type == FIT_BITMAP) {
( run in 1.271 second using v1.01-cache-2.11-cpan-5735350b133 )