(Toby Thain) wrote in message news:…
"Bob" …
I’m looking for Java code that writes out keywords to JPEG images in PhotoShop format. I’m looking for the same functionality that PhotoShop has in its fileinfo menu.
I’ve searched for this in Google and I’ve seen that this question has been asked several times before but there have been no answers. Is is possible to do this in Java?
Some time ago I e-mailed you C code which builds the IPTC record in Photoshop’s required format (see below). If you translate this to Java, and add code to insert the IPTC into the JPEG, it’s done.
Let’s get specific. You need to add an application specific marker to a JFIF file. The JFIF format spec can be found here:
http://www.ijg.org/files/ The JFIF spec does not mention the APP13 (0xED) marker used by Adobe, but paraphrasing that document, the Photoshop image resources (metadata) marker appears to have the following format:
X’FF’, X’ED’ [=M_APP13], length, identifier, image resources data
length (2 bytes) Total field byte count, including the byte count value (2 bytes), but excluding the
marker itself
identifier This zero terminated string ("Photoshop 3.0") uniquely identifies this marker.
= 50 68 6F 74 6F 73 68 6F 70 20 33 2E 30 00
The content of this marker is the Photoshop image resource data, which is described in the Photoshop SDK ("Photoshop File Formats.pdf", chapter 2).
The IPTC-NAA record (File Info information) is stored as an image resource with ID = 0x0404 (1028). The format of this data is also documented in the SDK ("iimv4.pdf"). Note that Photoshop uses only a small subset of the datasets defined in that document, and uses an application record (2:xx) only; it does not write the object envelope record (1:xx). It uses a tag marker = 0x1c (which I can’t find documented).
Within the IPTC data, several datasets comprise the File Info. The C code posted earlier – which was derived from the IPTC spec – should be sufficient to construct this data.
Note that Photoshop stores many other image resources for different non-pixel parts of the document (paths, for example) – these should be preserved.
In summary, the resulting JPG consists of several nested data formats like the layers of an onion:
JFIF (.JPG):
JFIF APP0 marker
APP13 marker, containing Photoshop image resource data:
image resource ID=1028 (File Info), in IPTC-NAA format:
Application record (2), for example:
Version dataset 2:00 (this is mandatory in the IPTC spec) Keyword dataset(s) 2:25
Caption dataset(s) 2:120
…more datasets…
…more image resources…
…more markers, including JPEG data…
(end)
You may find the IJG Group’s tools and source code (link above) useful, e.g.
djpeg -v test.jpg -outfile /dev/null
gives a rough breakdown of the JFIF contents.
T