Cannot load png: Incorrect bKGD chunk length

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Lastique
Posts: 4
Joined: 2014-03-13T06:50:46-07:00
Authentication code: 6789

Cannot load png: Incorrect bKGD chunk length

Post 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.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Cannot load png: Incorrect bKGD chunk length

Post by dlemstra »

Magick++ throws two type of exceptions Error and Warning you can just catch the 'Magick::Warning' and ignore it.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Lastique
Posts: 4
Joined: 2014-03-13T06:50:46-07:00
Authentication code: 6789

Re: Cannot load png: Incorrect bKGD chunk length

Post 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.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Cannot load png: Incorrect bKGD chunk length

Post 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?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Cannot load png: Incorrect bKGD chunk length

Post 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.
Lastique
Posts: 4
Joined: 2014-03-13T06:50:46-07:00
Authentication code: 6789

Re: Cannot load png: Incorrect bKGD chunk length

Post by Lastique »

Thanks for the information. It seems the corruption happens somewhere later in our code.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Cannot load png: Incorrect bKGD chunk length

Post 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.
Lastique
Posts: 4
Joined: 2014-03-13T06:50:46-07:00
Authentication code: 6789

Re: Cannot load png: Incorrect bKGD chunk length

Post 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.
Post Reply