Search code examples
xmplibexif

keep/copy XMP with libexif


I try to add a thumbnail to a JPEG picture using libexif. For now I'm borrowing the code from exif (the command line tool that is shipped by the libexif team). However I noticed the XMP tags get deleted from the metadata. There is an old bugreport here.

I tried to see how to achieve this anyway with libexif but I don't really understand how to get the XMP from input file and put it in the output file. I just want to copy all XMP data, I don't need to extract anything of it.

I saw there is a TAG EXIF_TAG_XML_PACKET in exif_tag.h but couldn't figure out how to read/write this tag.

A related solution is in this SO answer but it looks complicated. I'm not familiar coding in C.

Is it actually possible to keep all XMP when using only libexif API? Have things changed in recent years on that? How would you write this in code?

Thanks


Solution

  • I believe it should be somewhat straightforward. XMP fields are described in the ISO/Adobe standard. Regular Kotlin/Java/Android file I/O and some string manipulation should be all that is required.

    I would start out by becoming intimately familiar with ISO 16684-1:2019. Then, write a method for your jpeg file class that grabs all the XMP fields. Store those fields in a temp file (to prevent difficult to recover data loss in the event of your code or libexif crashing). Hand the file off to libexif. Generate the thumbnail. Finally, when that's done you can restore the XMP fields. If the thumbnail is stored in an XMP field as well (and it sounds like it is), it may be easier to concatenate that field with the other ones which were already grabbed, updating the temp file so that it contains EVERY XMP field, before adding all of the XMP fields back to the jpeg.

    Unfortunately, I do not currently have the time to read a 50 page ISO standard, synthesize the information, and then write the code to implement the solution. Here's a link to the standard at least, to get you started.

    https://www.iso.org/obp/ui/#iso:std:iso:16684:-1:ed-2:v1:en