imgdecode strip the alpha channel on some png images (Bug #3075)


Added by Leszek Hanusz almost 12 years ago. Updated over 11 years ago.


Status:Done Start date:2013-06-06
Priority:Normal Due date:
Assignee:Vadim Pisarevsky % Done:

100%

Category:highgui-images
Target version:2.4.7
Affected version:branch '2.4' Operating System:Any
Difficulty: HW Platform:Any
Pull request:https://github.com/Itseez/opencv/pull/1311

Description

When decoding the transparent image in attachment (test image made with gimp) to a Mat, the alpha channel is stripped.

The image is coming from a resource on android and is decoded with

Mat m = Utils.loadResource(c,R.drawable.rgbt, Highgui.CV_LOAD_IMAGE_UNCHANGED);
==> m.channels = 3

After more analyse:

Utils.loadResource is calling imgdecode with -1 as flags
imgdecode is calling the png decoder in the grfmt_png.cpp file
In the PngDecoder::readHeader function, the image is detected as having a color_type of PNG_COLOR_TYPE_PALETTE
and then m_type = CV_8UC3

Remarks:

- If I modify the code so that m_type = CV_8UC4 in case of a PNG_COLOR_TYPE_PALETTE, the alpha channel is not stripped and the mat returned is correct

- using pnginfo on the image we have :

Image Width: 10 Image Length: 10
Bitdepth (Bits/Sample): 8
Channels (Samples/Pixel): 4
Pixel depth (Pixel Depth): 32
Colour Type (Photometric Interpretation): RGB with alpha channel
Image filter: Single row per byte filter
Interlacing: No interlacing
Compression Scheme: Deflate method 8, 32k window
Resolution: 2835, 2835 (pixels per meter)
FillOrder: msb-to-lsb
Byte Order: Network (Big Endian)
Number of text strings: 1 of 9
Comment (xTXt deflate compressed): Created with GIMP


trame_rgbt.png (194 Bytes) Leszek Hanusz, 2013-06-06 06:19 pm


Associated revisions

Revision 8fd61c9a
Added by Alexander Alekhin over 10 years ago

Merge pull request #3075 from akarsakov:ipp_imgproc_fix

History

Updated by Leszek Hanusz almost 12 years ago

More information:

Apparently PNG files in resources on android could be optimized automatically to a palette color type :

"Note: Bitmap files may be automatically optimized with lossless image compression by the aapt tool during the build process. For example, a true-color PNG that does not require more than 256 colors may be converted to an 8-bit PNG with a color palette. This will result in an image of equal quality but which requires less memory. So be aware that the image binaries placed in this directory can change during the build. If you plan on reading an image as a bit stream in order to convert it to a bitmap, put your images in the res/raw/ folder instead, where they will not be optimized."
See http://developer.android.com/guide/topics/resources/drawable-resource.html#Bitmap

Updated by Kirill Kornyakov over 11 years ago

This is great that you prepared the fix, but why don't you send a pull request. I can see this one: https://github.com/Itseez/opencv_extra/pull/58, but you should also send your fix (https://github.com/leszekhanusz/opencv/commit/81bcc1309be5c7e386e5f0608db0c29d9cf0fa86) to the opencv repository as another pull request. But please note that you should target your fix to the 2.4 branch instead of master like you do. This fix is useful for 2.4.x releases as well, so please cherry-pick your commit to the 2.4 branch, and send a pull request to the opencv repository.

Thanks!!

  • Description changed from When decoding the transparent image in attachment (test image made with gimp... to When decoding the transparent image in attachment (test image made with gimp)... More
  • HW Platform set to Any
  • Operating System set to Any
  • Affected version changed from branch 'master' (2.4.9) to branch '2.4'
  • Target version set to 2.4.7

Updated by Leszek Hanusz over 11 years ago

A new pull request has been made from a new branch (png_palette_alpha_support) to the 2.4 branch.

I made a mistake with the last pull request and send an older version of the code (wich does not work correctly if the png does not contain a alpha channel)

The current pull request should be correct. New tests have been added to be sure.

Thank for your help.

Updated by Leszek Hanusz over 11 years ago

Hmm buildbot is not happy. Apparently still not correct because of the location of pnginfo.h ...

Updated by Leszek Hanusz over 11 years ago

Using png_get_tRNS instead of the pnginfo struct + Squashing the oops commits into one and new pull request to the 2.4 branch

Updated by Alexander Smorkalov over 11 years ago

  • Pull request set to https://github.com/Itseez/opencv/pull/1311

Updated by Leszek Hanusz over 11 years ago

  • % Done changed from 0 to 100
  • Status changed from Open to Done

Also available in: Atom PDF