void PNGImageDecoder::headerAvailable()
{
png_structp png = m_reader->pngPtr();
png_infop info = m_reader->infoPtr();
png_uint_32 width = png_get_image_width(png, info);
png_uint_32 height = png_get_image_height(png, info);
// Protect against large PNGs. See http://bugzil.la/251381 for more details.
const unsigned long maxPNGSize = 1000000UL;
if (width > maxPNGSize || height > maxPNGSize) {
longjmp(JMPBUF(png), 1);
return;
}
// Set the image size now that the image header is available.
if (!setSize(width, height)) {
longjmp(JMPBUF(png), 1);
return;
}
int bitDepth, colorType, interlaceType, compressionType, filterType, channels;
png_get_IHDR(png, info, &width, &height, &bitDepth, &colorType, &interlaceType, &compressionType, &filterType);
// The options we set here match what Mozilla does.
// Expand to ensure we use 24-bit for RGB and 32-bit for RGBA.
if (colorType == PNG_COLOR_TYPE_PALETTE || (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8))
png_set_expand(png);
png_bytep trns = 0;
int trnsCount = 0;
if (png_get_valid(png, info, PNG_INFO_tRNS)) {
png_get_tRNS(png, info, &trns, &trnsCount, 0);
png_set_expand(png);
}
if (bitDepth == 16)
png_set_strip_16(png);
if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png);
#if USE(QCMSLIB)
if ((colorType & PNG_COLOR_MASK_COLOR) && !m_ignoreGammaAndColorProfile) {
// We only support color profiles for color PALETTE and RGB[A] PNG. Supporting
// color profiles for gray-scale images is slightly tricky, at least using the
// CoreGraphics ICC library, because we expand gray-scale images to RGB but we
// do not similarly transform the color profile. We'd either need to transform
// the color profile or we'd need to decode into a gray-scale image buffer and
// hand that to CoreGraphics.
bool sRGB = false;
ColorProfile colorProfile;
getColorProfile(png, info, colorProfile, sRGB);
bool imageHasAlpha = (colorType & PNG_COLOR_MASK_ALPHA) || trnsCount;
m_reader->createColorTransform(colorProfile, imageHasAlpha, sRGB);
m_hasColorProfile = !!m_reader->colorTransform();
}
#endif
if (!m_hasColorProfile) {
// Deal with gamma and keep it under our control.
const double inverseGamma = 0.45455;
const double defaultGamma = 2.2;
double gamma;
if (!m_ignoreGammaAndColorProfile && png_get_gAMA(png, info, &gamma)) {
const double maxGamma = 21474.83;
if ((gamma <= 0.0) || (gamma > maxGamma)) {
gamma = inverseGamma;
png_set_gAMA(png, info, gamma);
}
png_set_gamma(png, defaultGamma, gamma);
} else {
png_set_gamma(png, defaultGamma, inverseGamma);
}
}
// Tell libpng to send us rows for interlaced pngs.
if (interlaceType == PNG_INTERLACE_ADAM7)
png_set_interlace_handling(png);
// Update our info now.
png_read_update_info(png, info);
channels = png_get_channels(png, info);
ASSERT(channels == 3 || channels == 4);
m_reader->setHasAlpha(channels == 4);
if (m_reader->decodingSizeOnly()) {
// If we only needed the size, halt the reader.
#if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 5)
// '0' argument to png_process_data_pause means: Do not cache unprocessed data.
m_reader->setReadOffset(m_reader->currentBufferSize() - png_process_data_pause(png, 0));
#else
m_reader->setReadOffset(m_reader->currentBufferSize() - png->buffer_size);
png->buffer_size = 0;
#endif
}
}
//.........这里部分代码省略.........
if (pixel_depth == 2) {
png_set_expand_gray_1_2_4_to_8(png_ptr);
pixel_depth = 8;
}
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
// expand 8-bit greyscale + 8-bit alpha to 32-bit
png_set_gray_to_rgb(png_ptr);
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
// flip the RGBA pixels to BGRA
png_set_bgr(png_ptr);
#endif
pixel_depth = 32;
break;
default:
throw FI_MSG_ERROR_UNSUPPORTED_FORMAT;
}
// 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);
// color type may have changed, due to our transformations
color_type = png_get_color_type(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:
png_set_invert_alpha(png_ptr);
if(image_type == FIT_BITMAP) {
dib = FreeImage_AllocateHeader(header_only, width, height, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
} else {
dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth);
}
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
if(image_type == FIT_BITMAP) {
dib = FreeImage_AllocateHeader(header_only, width, height, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
} else {
dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth);
}
break;
//.........这里部分代码省略.........
&BitDepth, &ColorType, NULL, NULL, NULL);
Width=w;
Height=h;
}
// Convert palette color to true color
if (ColorType==PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
// Convert low bit colors to 8 bit colors
if (BitDepth < 8)
{
if (ColorType==PNG_COLOR_TYPE_GRAY || ColorType==PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_expand_gray_1_2_4_to_8(png_ptr);
else
png_set_packing(png_ptr);
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
// Convert high bit colors to 8 bit colors
if (BitDepth == 16)
png_set_strip_16(png_ptr);
// Convert gray color to true color
if (ColorType==PNG_COLOR_TYPE_GRAY || ColorType==PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
int intent;
const double screen_gamma = 2.2;
if (png_get_sRGB(png_ptr, info_ptr, &intent))
png_set_gamma(png_ptr, screen_gamma, 0.45455);
else
{
double image_gamma;
if (png_get_gAMA(png_ptr, info_ptr, &image_gamma))
png_set_gamma(png_ptr, screen_gamma, image_gamma);
else
png_set_gamma(png_ptr, screen_gamma, 0.45455);
}
// Update the changes in between, as we need to get the new color type
// for proper processing of the RGBA type
png_read_update_info(png_ptr, info_ptr);
{
// Use temporary variables to avoid passing casted pointers
png_uint_32 w,h;
// Extract info
png_get_IHDR(png_ptr, info_ptr,
&w, &h,
&BitDepth, &ColorType, NULL, NULL, NULL);
Width=w;
Height=h;
}
// Convert RGBA to BGRA
if (ColorType==PNG_COLOR_TYPE_RGB_ALPHA)
{
png_set_bgr(png_ptr);
}
// Create the image structure to be filled by png data
Picture* pic = GfxEngine::instance().createPicture( Size( Width, Height ) );
GfxEngine::instance().loadPicture( *pic );
int ReadPngStream(FILE *file,
std::vector<unsigned char> * ptr,
int * w,
int * h,
int * depth) {
// first check the eight byte PNG signature
png_byte pbSig[8];
size_t readcnt = fread(pbSig, 1, 8, file);
(void) readcnt;
if (png_sig_cmp(pbSig, 0, 8))
{
return 0;
}
// create the two png(-info) structures
png_structp png_ptr = nullptr;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
(png_error_ptr)nullptr, (png_error_ptr)nullptr);
if (!png_ptr)
{
return 0;
}
png_infop info_ptr = nullptr;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, nullptr, nullptr);
return 0;
}
// initialize the png structure
png_init_io(png_ptr, file);
png_set_sig_bytes(png_ptr, 8);
// read all PNG info up to image data
png_read_info(png_ptr, info_ptr);
// get width, height, bit-depth and color-type
png_uint_32 wPNG, hPNG;
int iBitDepth;
int iColorType;
png_get_IHDR(png_ptr, info_ptr, &wPNG, &hPNG, &iBitDepth,
&iColorType, nullptr, nullptr, nullptr);
// expand images of all color-type to 8-bit
if (iColorType == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
if (iBitDepth < 8)
png_set_expand(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand(png_ptr);
if (iBitDepth == 16) // convert 16-bit to 8-bit on the fly
png_set_strip_16(png_ptr);
double dGamma;
// if required set gamma conversion
if (png_get_gAMA(png_ptr, info_ptr, &dGamma))
png_set_gamma(png_ptr, (double) 2.2, dGamma);
// after the transformations are registered, update info_ptr data
png_read_update_info(png_ptr, info_ptr);
// get again width, height and the new bit-depth and color-type
png_get_IHDR(png_ptr, info_ptr, &wPNG, &hPNG, &iBitDepth,
&iColorType, nullptr, nullptr, nullptr);
// Get number of byte along a tow
png_uint_32 ulRowBytes;
ulRowBytes = png_get_rowbytes(png_ptr, info_ptr);
// and allocate memory for an array of row-pointers
png_byte **ppbRowPointers = nullptr;
if ((ppbRowPointers = (png_bytepp) malloc(hPNG
* sizeof(png_bytep))) == nullptr)
{
std::cerr << "PNG: out of memory" << std::endl;
return 0;
}
*w = wPNG;
*h = hPNG;
*depth = png_get_channels(png_ptr, info_ptr);
// now we can allocate memory to store the image
ptr->resize((*h)*(*w)*(*depth));
// set the individual row-pointers to point at the correct offsets
for (png_uint_32 i = 0; i < hPNG; i++)
ppbRowPointers[i] = &((*ptr)[0]) + i * ulRowBytes;
// now we can go ahead and just read the whole image
png_read_image(png_ptr, ppbRowPointers);
// read the additional chunks in the PNG file (not really needed)
png_read_end(png_ptr, nullptr);
//.........这里部分代码省略.........
请发表评论