From 5d0ee89132bd85d681d7b0840dcb5f01042237cc Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Tue, 28 Aug 2018 14:54:10 +0200 Subject: [PATCH] Fix reading 16bpc grayscale PNGs They were incorrectly attempted to be read as Indexed8, instead use the RGBA64 formats to read them with full accuracy. Task-number: QTBUG-45858 Change-Id: I14fc3cb0d59fa2fc0e68fd870f3b32352c34161f Reviewed-by: Eirik Aavitsland --- src/gui/image/qpnghandler.cpp | 37 ++++++++++-------- .../image/qimagereader/images/basn0g16.png | Bin 0 -> 167 bytes .../image/qimagereader/images/basn2c16.png | Bin 0 -> 302 bytes .../image/qimagereader/images/basn4a16.png | Bin 0 -> 2206 bytes .../image/qimagereader/images/basn6a16.png | Bin 0 -> 3435 bytes .../image/qimagereader/images/tbwn0g16.png | Bin 0 -> 1313 bytes .../image/qimagereader/tst_qimagereader.cpp | 5 +++ 7 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 tests/auto/gui/image/qimagereader/images/basn0g16.png create mode 100644 tests/auto/gui/image/qimagereader/images/basn2c16.png create mode 100644 tests/auto/gui/image/qimagereader/images/basn4a16.png create mode 100644 tests/auto/gui/image/qimagereader/images/basn6a16.png create mode 100644 tests/auto/gui/image/qimagereader/images/tbwn0g16.png diff --git a/src/gui/image/qpnghandler.cpp b/src/gui/image/qpnghandler.cpp index 94479b4f8e..242d8cd63b 100644 --- a/src/gui/image/qpnghandler.cpp +++ b/src/gui/image/qpnghandler.cpp @@ -245,7 +245,7 @@ void setup_qt(QImage& image, png_structp png_ptr, png_infop info_ptr, QSize scal png_set_interlace_handling(png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY) { - // Black & White or 8-bit grayscale + // Black & White or grayscale if (bit_depth == 1 && png_get_channels(png_ptr, info_ptr) == 1) { png_set_invert_mono(png_ptr); png_read_update_info(png_ptr, info_ptr); @@ -266,19 +266,22 @@ void setup_qt(QImage& image, png_structp png_ptr, png_infop info_ptr, QSize scal else if (g == 1) image.setColor(0, qRgba(255, 255, 255, 0)); } - } else if (bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { - png_set_expand(png_ptr); - png_set_strip_16(png_ptr); + } else if (bit_depth == 16) { + bool hasMask = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS); + if (!hasMask) + png_set_filler(png_ptr, 0xffff, PNG_FILLER_AFTER); + else + png_set_expand(png_ptr); png_set_gray_to_rgb(png_ptr); - if (image.size() != QSize(width, height) || image.format() != QImage::Format_ARGB32) { - image = QImage(width, height, QImage::Format_ARGB32); + QImage::Format format = hasMask ? QImage::Format_RGBA64 : QImage::Format_RGBX64; + if (image.size() != QSize(width, height) || image.format() != format) { + image = QImage(width, height, format); if (image.isNull()) return; } - if (QSysInfo::ByteOrder == QSysInfo::BigEndian) - png_set_swap_alpha(png_ptr); - png_read_update_info(png_ptr, info_ptr); + if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) + png_set_swap(png_ptr); } else if (bit_depth == 8 && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { png_set_expand(png_ptr); if (image.size() != QSize(width, height) || image.format() != QImage::Format_Grayscale8) { @@ -289,9 +292,7 @@ void setup_qt(QImage& image, png_structp png_ptr, png_infop info_ptr, QSize scal png_read_update_info(png_ptr, info_ptr); } else { - if (bit_depth == 16) - png_set_strip_16(png_ptr); - else if (bit_depth < 8) + if (bit_depth < 8) png_set_packing(png_ptr); int ncols = bit_depth < 8 ? 1 << bit_depth : 256; png_read_update_info(png_ptr, info_ptr); @@ -356,12 +357,14 @@ void setup_qt(QImage& image, png_structp png_ptr, png_infop info_ptr, QSize scal if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) { png_set_bgr(png_ptr); } - } else if (bit_depth == 16 && (color_type & PNG_COLOR_MASK_COLOR)) { + } else if (bit_depth == 16 && !(color_type & PNG_COLOR_MASK_PALETTE)) { QImage::Format format = QImage::Format_RGBA64; if (!(color_type & PNG_COLOR_MASK_ALPHA) && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { png_set_filler(png_ptr, 0xffff, PNG_FILLER_AFTER); format = QImage::Format_RGBX64; } + if (!(color_type & PNG_COLOR_MASK_COLOR)) + png_set_gray_to_rgb(png_ptr); if (image.size() != QSize(width, height) || image.format() != format) { image = QImage(width, height, format); if (image.isNull()) @@ -680,11 +683,11 @@ QImage::Format QPngHandlerPrivate::readImageFormat() int num_palette; png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0); if (color_type == PNG_COLOR_TYPE_GRAY) { - // Black & White or 8-bit grayscale + // Black & White or grayscale if (bit_depth == 1 && png_get_channels(png_ptr, info_ptr) == 1) { format = QImage::Format_Mono; - } else if (bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { - format = QImage::Format_ARGB32; + } else if (bit_depth == 16) { + format = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ? QImage::Format_RGBA64 : QImage::Format_RGBX64; } else if (bit_depth == 8 && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { format = QImage::Format_Grayscale8; } else { @@ -696,7 +699,7 @@ QImage::Format QPngHandlerPrivate::readImageFormat() { // 1-bit and 8-bit color format = bit_depth == 1 ? QImage::Format_Mono : QImage::Format_Indexed8; - } else if (bit_depth == 16 && (color_type & PNG_COLOR_MASK_COLOR)) { + } else if (bit_depth == 16 && !(color_type & PNG_COLOR_MASK_PALETTE)) { format = QImage::Format_RGBA64; if (!(color_type & PNG_COLOR_MASK_ALPHA) && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) format = QImage::Format_RGBX64; diff --git a/tests/auto/gui/image/qimagereader/images/basn0g16.png b/tests/auto/gui/image/qimagereader/images/basn0g16.png new file mode 100644 index 0000000000000000000000000000000000000000..318ebcadf4fd0b64fb9b824e81c0cc8e3dfd2d82 GIT binary patch literal 167 zcmeAS@N?(olHy`uVBq!ia0vp^3Lq>1BpBEle`W(ImUKs7M+U~W1%@xC#RK_qo-U3d z9-UV&8FC#k;BgMLjDBB#?9PJ82Q;dUu4=I?zS(bY(Xf0+xUg{Y0;{G3S&o?ucbmRB zoML$)c8ASL&0+fj+X+UDyI8k3PT;(to58=tC&50S{f++@NsIIK3}QFDZ^ZA8J8=o* N0#8>zmvv4FO#l#XIHLdn literal 0 HcmV?d00001 diff --git a/tests/auto/gui/image/qimagereader/images/basn2c16.png b/tests/auto/gui/image/qimagereader/images/basn2c16.png new file mode 100644 index 0000000000000000000000000000000000000000..1bd4a4d0e2c2c6dd8a2e63a63b26f166b8ef5353 GIT binary patch literal 302 zcmV+}0nz@6P)NTxus~-YE?|ew94KIo9tHTv?hhRR zwrA%J^h9UxCeRmyPjW#d?oxNFL9(uFDZ1gBle+D$rIj`J+5;}Xa zfF63WfGT3xy1iYa$zve>zUQQss#Q*>R07*qoM6N<$g1`fR ASpWb4 literal 0 HcmV?d00001 diff --git a/tests/auto/gui/image/qimagereader/images/basn4a16.png b/tests/auto/gui/image/qimagereader/images/basn4a16.png new file mode 100644 index 0000000000000000000000000000000000000000..6dbee9fbdb93f3535be23326a52fb4d3ac60711e GIT binary patch literal 2206 zcmV;P2x0e$P)kEEkx>-b1@Qb$y-ohUek5*oTfx-A#6y^c`Q zE+sS)yU66wLTO83iFXm5i(N>*n3Idp^rFd-7Fe(v0)u8Hg(Rko(t3o_;+1WoY3fR? z;u%**k?mvsIhJ?vei@;3*EJZKPsh^xKJW8B&+`FbWnox{J*QyQf)~!hu>m;#N5~Gr z*+0X@UGUyz_~;4v=U?IOURb{_zjyb_*+);vGZ%Ns|73^cGmZ_&XO3F(9mBeOCxB*U zp=MjTt{LzZp}PWF5dym*a0v7!gx-ecb!ZpC(_mu+I?n=c$cJ>2a^`8!UXW*-DOjHb z_YkaIf>i_R6{x46R)AUxtZXgQHLOFeVC9@tBpY?%Ar~H6|39xka0c+50pEqb_xeL; z>a}V>WX7?rbSB#R07d+ApMY-yAH>T3O$tpzVWT4D`KV9D#5e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_=B_E7dnQy&=%m3* zDCd0zS}ADFD+E>zs2dWUs|O`O4x}A>>t~W=B5CF4X>qmStD@~)iUV3+_B@mqwwnc+ zd9xs$chb^OXB50E(5^x|39aW80`&^qb0xPgtMuBRkPK&4ggm`H%Fo9{P`Y=EwuAib zKPY=1x*=>g=Va#1fOOs&mBuk$A(S{Hf;FuuV#)d7sbeoV@Fcz z#`3Pp?K3L97A3<4ijriy)kU_eC)4|}U(UH||907*qoM6N<$f-(OD1poj5 literal 0 HcmV?d00001 diff --git a/tests/auto/gui/image/qimagereader/images/basn6a16.png b/tests/auto/gui/image/qimagereader/images/basn6a16.png new file mode 100644 index 0000000000000000000000000000000000000000..a9bf3cb461231de29905eaa02f3a8a66f9fc5477 GIT binary patch literal 3435 zcmZu!c{tPy7yiwHA^TDknhJAO5;JmTFJcJU8M34%B>R?S{LDp`Fk}h0vV{=c>|`0F zY}t2XhO#pw*@@4+-}ija_s93gIp?4AJnwVP`=0Zh6Lm*li-YYn8vpunKbAURdar&!F_@9SoS%FU_nL5v4bhxdo;qYV}(%9Z85QFC@Q_IDD58Vx)&SPd#1 z&_z21b`BlfZb@Z16}%Yv2Xg?CHRMe7qkJ)iJY&NQ*3GZeU+ur74iHv1P36E6yN%N5 zpV$8~7z8Rae`?A#-PAcaas4$5zYproUQo8+k=bWG?@3hw>tAT`15|X+#pzBR*EFaZTfujS2W+R%rAX5GszY`8KG_1tm1wxz^bYuH^_iu1 z#`8Dkd|UXn0~66IH5Y)%1^wg@FJA3z9$T7YRh1FE(o z&iHoiEcavc!3({fssSC{c=<uH$6|lJ`Cw^#7+?9Oeh5*sk;TrZA_sxq|m5T=dEMGV0i z^PYDhxn{(1ZN~3SMiq)&y^i@u;6U{BWs8VI#ZOVtSIoPu#Uy>G@F^}60slpM% z4fg4f0@c(&o=lnNB5(wh%7VCL!-EVW2Z|!o{aAn*D%&65S@(Uw9aQtxwD)dfkbS0D zN4fECZ9%?vJvB6eQZ4WGA92VxeEjiXK}uR*cR55g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