Skip to content

Instantly share code, notes, and snippets.

@mdauphin
Created August 6, 2015 19:49
Show Gist options
  • Save mdauphin/1ce306df53da73a24a5c to your computer and use it in GitHub Desktop.
Save mdauphin/1ce306df53da73a24a5c to your computer and use it in GitHub Desktop.
--- build/qt-everywhere-opensource-src-4.6.2/src/gui/image/qpnghandler.cpp.ori 2015-08-06 21:07:32.907967363 +0200
+++ build/qt-everywhere-opensource-src-4.6.2/src/gui/image/qpnghandler.cpp 2015-08-06 21:47:40.071884019 +0200
@@ -163,8 +163,11 @@
int bit_depth;
int color_type;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
-
- if (color_type == PNG_COLOR_TYPE_GRAY) {
+ png_colorp palette;
+ int num_palette;
+ png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
+
+ if (png_get_color_type(png_ptr,info_ptr) == PNG_COLOR_TYPE_GRAY) {
// Black & White or 8-bit grayscale
if (bit_depth == 1 && png_get_channels(png_ptr, info_ptr) == 1) {
png_set_invert_mono(png_ptr);
@@ -208,10 +211,12 @@
image.setColor(i, qRgba(c,c,c,0xff));
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
+ png_bytep trans; int num_trans; png_color_16p trans_values;
+ png_get_tRNS( png_ptr, info_ptr, &trans, &num_trans, &trans_values);
#if PNG_LIBPNG_VER_MAJOR < 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 4)
- const int g = info_ptr->trans_values.gray;
+ const int g = num_trans ;
#else
- const int g = info_ptr->trans_color.gray;
+ const int g = num_trans;
#endif
if (g < ncols) {
image.setColor(g, 0);
@@ -220,42 +225,45 @@
}
} else if (color_type == PNG_COLOR_TYPE_PALETTE
&& png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)
- && info_ptr->num_palette <= 256)
+ && num_palette <= 256)
{
// 1-bit and 8-bit color
if (bit_depth != 1)
png_set_packing(png_ptr);
png_read_update_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
+ png_bytep trans; int num_trans; png_color_16p trans_values;
+ png_get_tRNS( png_ptr, info_ptr, &trans, &num_trans, &trans_values);
+
QImage::Format format = bit_depth == 1 ? QImage::Format_Mono : QImage::Format_Indexed8;
if (image.size() != QSize(width, height) || image.format() != format) {
image = QImage(width, height, format);
if (image.isNull())
return;
}
- image.setColorCount(info_ptr->num_palette);
+ image.setColorCount(num_palette);
int i = 0;
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
- while (i < info_ptr->num_trans) {
+ while (i < num_trans) {
image.setColor(i, qRgba(
- info_ptr->palette[i].red,
- info_ptr->palette[i].green,
- info_ptr->palette[i].blue,
+ palette[i].red,
+ palette[i].green,
+ palette[i].blue,
#if PNG_LIBPNG_VER_MAJOR < 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 4)
info_ptr->trans[i]
#else
- info_ptr->trans_alpha[i]
+ trans[i]
#endif
)
);
i++;
}
}
- while (i < info_ptr->num_palette) {
+ while (i < num_palette) {
image.setColor(i, qRgba(
- info_ptr->palette[i].red,
- info_ptr->palette[i].green,
- info_ptr->palette[i].blue,
+ palette[i].red,
+ palette[i].green,
+ palette[i].blue,
0xff
)
);
@@ -531,33 +539,36 @@
QImage::Format format = QImage::Format_Invalid;
png_uint_32 width, height;
int bit_depth, color_type;
- if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY) {
+ png_colorp palette;
+ int num_palette;
+ png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
+ if (png_get_color_type(png_ptr,info_ptr) == PNG_COLOR_TYPE_GRAY) {
// Black & White or 8-bit grayscale
- if (info_ptr->bit_depth == 1 && info_ptr->channels == 1) {
+ if (png_get_bit_depth(png_ptr, info_ptr) == 1 && png_get_channels(png_ptr, info_ptr) == 1) {
format = QImage::Format_Mono;
- } else if (info_ptr->bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
+ } else if (png_get_bit_depth(png_ptr, info_ptr) == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
format = QImage::Format_ARGB32;
} else {
format = QImage::Format_Indexed8;
}
- } else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE
+ } else if ((png_get_color_type(png_ptr,info_ptr) == PNG_COLOR_TYPE_PALETTE)
&& png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)
- && info_ptr->num_palette <= 256)
+ && num_palette <= 256)
{
// 1-bit and 8-bit color
- if (info_ptr->bit_depth != 1)
+ if (png_get_bit_depth(png_ptr, info_ptr) != 1)
png_set_packing(png_ptr);
png_read_update_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
format = bit_depth == 1 ? QImage::Format_Mono : QImage::Format_Indexed8;
} else {
// 32-bit
- if (info_ptr->bit_depth == 16)
+ if (png_get_bit_depth(png_ptr, info_ptr) == 16)
png_set_strip_16(png_ptr);
format = QImage::Format_ARGB32;
// Only add filler if no alpha, or we can get 5 channel data.
- if (!(info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
+ if (!((png_get_color_type(png_ptr,info_ptr) & PNG_COLOR_MASK_ALPHA))
&& !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
// We want 4 bytes, but it isn't an alpha channel
format = QImage::Format_RGB32;
@@ -741,10 +752,10 @@
png_set_write_fn(png_ptr, (void*)this, qpiw_write_fn, qpiw_flush_fn);
- info_ptr->channels =
+ /*png_get_channels(png_ptr, info_ptr) =
(image.depth() == 32)
? (image.format() == QImage::Format_RGB32 ? 3 : 4)
- : 1;
+ : 1;*/
png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(),
image.depth() == 1 ? 1 : 8 /* per channel */,
@@ -756,9 +767,12 @@
//png_set_sBIT(png_ptr, info_ptr, 8);
- info_ptr->sig_bit.red = 8;
- info_ptr->sig_bit.green = 8;
- info_ptr->sig_bit.blue = 8;
+ png_color_8 sig_bit;
+ sig_bit.red = sig_bit.green = sig_bit.blue = 8 ;
+ png_set_sBIT(png_ptr, info_ptr, &sig_bit);
+ //info_ptr->sig_bit.red = 8;
+ //info_ptr->sig_bit.green = 8;
+ //info_ptr->sig_bit.blue = 8;
if (image.format() == QImage::Format_MonoLSB)
png_set_packswap(png_ptr);
@@ -774,9 +788,9 @@
int num_trans = 0;
for (int i=0; i<num_palette; i++) {
QRgb rgb=image.color(i);
- info_ptr->palette[i].red = qRed(rgb);
- info_ptr->palette[i].green = qGreen(rgb);
- info_ptr->palette[i].blue = qBlue(rgb);
+ //info_ptr->palette[i].red = qRed(rgb);
+ //info_ptr->palette[i].green = qGreen(rgb);
+ //info_ptr->palette[i].blue = qBlue(rgb);
trans[i] = rgb >> 24;
if (trans[i] < 255) {
num_trans = i+1;
@@ -792,7 +806,9 @@
}
if (image.format() != QImage::Format_RGB32) {
- info_ptr->sig_bit.alpha = 8;
+ png_color_8 sig_bit ; sig_bit.alpha = 8 ;
+ png_set_shift(png_ptr, &sig_bit);
+ //info_ptr->sig_bit.alpha = 8;
}
// Swap ARGB to RGBA (normal PNG format) before saving on
@@ -958,7 +974,7 @@
else if (option == Description)
return d->description;
else if (option == Size)
- return QSize(d->info_ptr->width, d->info_ptr->height);
+ return QSize(png_get_image_width(d->png_ptr,d->info_ptr), png_get_image_height(d->png_ptr,d->info_ptr));
else if (option == ImageFormat)
return d->readImageFormat();
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment