Losing custom PNG chunk during Mogrify? (solved)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
exwizard
Posts: 3
Joined: 2011-01-29T06:11:25-07:00
Authentication code: 8675308

Losing custom PNG chunk during Mogrify? (solved)

Post by exwizard »

Hi folks,

I'm trying to batch trim a number of PNG files that have extra transparent space. These PNGs have a custom chunk (grAb) described here: http://zdoom.org/wiki/GrAb

This is the command I am entering; it successfully trims the image but loses the grAb chunk. I know the grAb chunk determines an offset from the top left of the image and even if it was kept it would be wrong after a trim but, after I get over this obstacle, I'm hoping to be able to modify the grAb chunk to adjust for the trimmed space as a second step (any ideas on how to do this would also be appreciated).

Code: Select all

Mogrify -trim *.png
Can anyone offer any assistance? Here is a sample PNG with the grAb chunk: http://www.megaupload.com/?d=S9XLAO7V I've also posed this question on the zDoom forums (the game that uses the grAb chunk: http://forum.zdoom.org/viewtopic.php?f= ... 43#p544643)

Thanks in advance for any help you may be able to offer.
Last edited by exwizard on 2011-01-31T07:25:58-07:00, edited 1 time in total.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Losing custom PNG chunk during Mogrify?

Post by glennrp »

-deleted-
Last edited by glennrp on 2011-01-29T08:40:31-07:00, edited 1 time in total.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Losing custom PNG chunk during Mogrify?

Post by glennrp »

I'll look into it. The PNG spec says chunks with the last byte of the chunkname lower-case are safe to copy, so the grAb chunk should have been preserved.
But as you note, the chunk contents depend on the contents of IHDR (width and height) so it isn't really safe-to-copy and should have been called grAB.

ImageMagick deliberately ignores all unknown chunks because typical operations (such as -trim, -crop, etc.) will likely render them invalid despite their
being "safe-to-copy" according to the PNG spec.

One solution would be to add grAb to the chunks known by IM and handle it like vpAg or oFFs. Another would be for the application to extract the grAb chunk
data with another application before mogrifying and then adjust and reinsert it afterwards.

BTW why not use oFFs which conveys the same information? Hmm, by the same logic as above, oFFs should really have been oFFS, but that's water
under the bridge now. ImageMaqick recognizes oFFs and does adjust it (using image->page data) when writing.
exwizard
Posts: 3
Joined: 2011-01-29T06:11:25-07:00
Authentication code: 8675308

Re: Losing custom PNG chunk during Mogrify?

Post by exwizard »

I think grAb is being used for historical reasons; switching to oFFs would break a lot of existing resources (something the maintainers of the engine are very much against even if a better implementation exists).
One solution would be to add grAb to the chunks known by IM and handle it like vpAg or oFFs. Another would be for the application to extract the grAb chunk
data with another application before mogrifying and then adjust and reinsert it afterwards.
This looks like the best way to do it at the moment; thanks! I'm going to have to see if the command line programs available for modifying the grAb chunk can help me do this otherwise I may have to write something up myself.
Last edited by exwizard on 2011-01-29T09:07:43-07:00, edited 2 times in total.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Losing custom PNG chunk during Mogrify?

Post by glennrp »

Ignore my comment about oFFs and gRAb having the same content. As I understand it now, oFFs conveys the offset of the image, while grAb conveys an offset within the image.
exwizard
Posts: 3
Joined: 2011-01-29T06:11:25-07:00
Authentication code: 8675308

Re: Losing custom PNG chunk during Mogrify?

Post by exwizard »

I've written a program that extracts the grAb chunk and then uses the oFFs chunk of a processed file in conjunction with the previously extracted grAb chunk to write a new grAb chunk (in the form of grAb xxxx - oFFs xxxx).

Thanks glennrp, pointing me at oFFs let me develop this workaround. Too bad imagemagick doesn't follow the PNG specification for ancillary chunks; would save me a number of steps.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Losing custom PNG chunk during Mogrify? (solved)

Post by glennrp »

Just to be clear, ImageMagick does follow the PNG spec. A spec-compliant PNG decoder is allowed to delete any or all ancillary chunks, whether it recognizes them or not, and according to the rules it must delete all copy-unsafe chunks. A spec-compliant PNG editor is supposed to preserve unknown chunks when it can, but ImageMagick does not claim to be a PNG editor, only a general-purpose image editor. I think that with a modest amount of work, ImageMagick could be made into a spec-compliant PNG editor, but it would never preserve copy-unsafe chunks because we can't guarantee that it writes the same IDAT chunks that it found in the input. Any difference in the input IDAT and output IDAT means that it would only be allowed to preserve the copy-safe chunks.

Also, the grAb chunk happens to be mis-named as though it were copy-safe although it isn't, because changing the image dimensions in the IHDR chunk invalidates its contents.
Post Reply