Page 1 of 1

Cannot load png: Incorrect bKGD chunk length

Posted: 2014-03-13T07:17:20-07:00
by Lastique
Hi,

We're using ImageMagick through its C++ interface in our project. We're trying to load a png image by calling Magick::Image::read with a Magick::Blob argument and get a Magick::Warning exception that says:

Code: Select all

Incorrect bKGD chunk length `' @ warning/png.c/MagickPNGWarningHandler/1830
The image we're trying to load is here: https://www.dropbox.com/s/g6dro4hpu4v6igk/bad.png

As I understand, the image may not be completely valid, but all viewers I tried display it correctly (I tried Okular and Chrome), although Okular also displays the libpng warning on the console. The image was produced by MS Paint by loading an image with alpha channel, editing it and saving without the alpha channel.

I tried to set the warning handler by calling MagickCore::SetWarningHandler prior to loading the image but it doesn't have any effect (my handler does not get called). Basically, I cannot ignore this warning and get this image loaded.

Is it possible to make this warning non-fatal and load this image even with it?

We're using ImageMagick 6.8.7-7 and 6.8.8-8 on Debian Squeeze/Wheezy and Kubuntu 13.10 x86_64.

Re: Cannot load png: Incorrect bKGD chunk length

Posted: 2014-03-13T07:19:52-07:00
by dlemstra
Magick++ throws two type of exceptions Error and Warning you can just catch the 'Magick::Warning' and ignore it.

Re: Cannot load png: Incorrect bKGD chunk length

Posted: 2014-03-13T08:16:49-07:00
by Lastique
Yes, I tried that during my experiments, but in that case the loaded Magick::Image is corrupted. I figured that the loading process is not completed normally when the exception is thrown.

Re: Cannot load png: Incorrect bKGD chunk length

Posted: 2014-03-13T09:23:47-07:00
by dlemstra
The exception is thrown after the image has been read. But you have to make sure you use the 'read' method and not the constructor to read the image. Did you try this with the latest version of ImageMagick?

Re: Cannot load png: Incorrect bKGD chunk length

Posted: 2014-03-13T16:19:50-07:00
by glennrp
Reading it with the commandline "convert" and writing it out again works for me.

Code: Select all

$ convert bad.png good.png
convert: bKGD: invalid `bad.png' @ warning/png.c/MagickPNGWarningHandler/1830.
$ convert good.png best.png
You should then be able to read "good.png" with your (or any) application.

Re: Cannot load png: Incorrect bKGD chunk length

Posted: 2014-03-14T02:41:50-07:00
by Lastique
Thanks for the information. It seems the corruption happens somewhere later in our code.

Re: Cannot load png: Incorrect bKGD chunk length

Posted: 2014-03-14T09:04:58-07:00
by glennrp
Lastique wrote:Thanks for the information. It seems the corruption happens somewhere later in our code.
It seems likely that at some point in your workflow the image was an indexed PNG, for which a 1-byte bKGD chunk
is correct. Then later it was converted to a 32-bit RGBA but the bKGD chunk was just copied rather than converted.

Re: Cannot load png: Incorrect bKGD chunk length

Posted: 2014-03-14T11:20:06-07:00
by Lastique
As I described, the image was created by Microsoft Paint. I don't know the details on what kind of png it was before being processed by Paint, but I would expect it to be a valid png afterwards, so it seems there is a bug in Paint. Nonetheless, we'll have to tolerate it in our application because there is possibility to to encounter these kind of broken images in the wild.