Write an arbitrary file to the iTXt chunk of PNG?

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
Johan foreva
Posts: 2
Joined: 2011-06-07T03:29:28-07:00
Authentication code: 8675308

Write an arbitrary file to the iTXt chunk of PNG?

Post by Johan foreva »

Tell me please, how to write an arbitrary file to the iTXt chunk of PNG file, like it does Adobe Fireworks, writing all project data in the PNG in addition to the image.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Write an arbitrary file to the iTXt chunk of PNG?

Post by glennrp »

The feature for writing arbitrary PNG chunks is documented as a comment
in coders/png.c but doesn't seem to be in the manual:
  • %
    % It is possible to request that the PNG encoder write previously-formatted
    % ancillary chunks in the output PNG file, using the "-profile" commandline
    % option as shown below or by setting the profile via a programming
    % interface:
    %
    % -profile PNG-chunk-x:<file>
    %
    % where x is a location flag and <file> is a file containing the chunk
    % name in the first 4 bytes, then a colon (":"), followed by the chunk data.
    % This encoder will compute the chunk length and CRC, so those must not
    % be included in the file.
    %
    % "x" can be "b" (before PLTE), "m" (middle, i.e., between PLTE and IDAT),
    % or "e" (end, i.e., after IDAT). If you want to write multiple chunks
    % of the same type, then add a short unique string after the "x" to prevent
    % subsequent profiles from overwriting the preceding ones, e.g.,
    %
    % -profile PNG-chunk-b01:file01 -profile PNG-chunk-b02:file02
    %
In your case, put "iTXt:Project Data\0\0\0\0\0" at the beginning of your file, then use

Code: Select all

 -profile PNG-chunk-e:filename
The five binary zeroes convey the iTXt keyword terminator, compression flag,
compression method, language tag terminator, and translated keyword terminator.

If you do further operations on the file with ImageMagick, this new
chunk will probably get lost or maybe converted to a zTXt chunk
because IM doesn't recognize iTXt.

Glenn
Johan foreva
Posts: 2
Joined: 2011-06-07T03:29:28-07:00
Authentication code: 8675308

Re: Write an arbitrary file to the iTXt chunk of PNG?

Post by Johan foreva »

Will be the file corrupted, if written to another type of chunk?
(Excuse me for my bad English, it's not my native language ;-)
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Write an arbitrary file to the iTXt chunk of PNG?

Post by glennrp »

If it contains only symbols from the Latin-1 character set you
can write it as tEXt or zTXt and it won't be corrupted.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Write an arbitrary file to the iTXt chunk of PNG?

Post by glennrp »

The method for injecting arbitrary PNG chunks via the "-profile" option
is not working in recent versions of IM. We are working on a fix.

Meanwhile, using "-set profile PNG-chunk-x:<file>" works.
Post Reply