question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JPEG metadata always read-only

See original GitHub issue

Greetings. I’m trying to read and modify metadata of JPEG images. Let’s look at the following (heavily simplified) code:

try (final Closeable imageInputStream = ImageIO.createImageInputStream(new File("D:\\testImage.jpg"))) {
  final ImageReader reader = ImageIO.getImageReaders(imageInputStream).next();
  reader.setInput(imageInputStream);

  final IIOImage imageAndMeta = reader.readAll(0, reader.getDefaultReadParam());
  reader.dispose();

  final IIOMetadata metadata = imageAndMeta.getMetadata();

  final Node root = new IIOMetadataNode("javax_imageio_jpeg_image_1.0");
  root.appendChild(new IIOMetadataNode("JPEGvariety"));
  root.appendChild(new IIOMetadataNode("markerSequence"));
  //root.appendChild(...somethingElse...)

  metadata.mergeTree("javax_imageio_jpeg_image_1.0", root);
} catch (final Exception error) {
  error.printStackTrace();
}

It works fine without TwelveMonkeys, but throws an error (only) when TwelveMonkeys is installed:

java.lang.IllegalStateException: Metadata is read-only
	at com.twelvemonkeys.imageio.AbstractMetadata.assertMutable(AbstractMetadata.java:117) ~[imageio-core-3.8.1.jar:3.8.1]
	at com.twelvemonkeys.imageio.AbstractMetadata.mergeTree(AbstractMetadata.java:96) ~[imageio-core-3.8.1.jar:3.8.1]
	...

It seems that the metadata is always read-only and thus mergeTree function does not work. I’ve looked into this with debugger and it seems that in TwelveMonkeys’ JPEGImage10Metadata.java isReadOnly() always returns true while in the original com.sun.imageio.plugins.jpeg.JPEGMetadata it’s always false. Any idea why is this and how could I get around this?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
davidekholmcommented, Oct 11, 2022

Hi Harald. I’ve updated to v3.8.3 and the “Invalid DHT node” error is indeed gone now. Thank you.

0reactions
haraldkcommented, Oct 12, 2022

No, it gives you a mutable JPEG metadata, so you can copy whatever you like (what this issue is about).


Copying only Exif is (probably) possible, but tedious…

To copy only the Exif segment, you need to look at the "markerSequence", get all the "unknown" nodes, and get the ones with "MarkerTag" value "225" (APP1/0xFFE1). There might be other APP1 markers beside Exif, so you probably need to look into the userObject, which for all "unknown" segments will contain a byte array. For Exif, this array will start with the string Exif\0\0 (and be followed by the TIFF structure).

Sadly, there is a bug in the JDK metadata, that inserts multiple "unknown" markers when you merge… 😕 Workaround for this is to merge the nodes first, then use setFromTree. Again, unnecessary hard work for something that should be simple.

I’d love to fix it, but proposing PRs to OpenJDK often takes several times that of a workaround… If only there was more time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Read and Remove Metadata from Your Photos ... - Auth0
The code opens each file as read-only. It reads the file's data in binary format into a file object, which it then uses...
Read more >
How do I make Metadata read-only? - Microsoft Community
After reading about how the creation and modification dates on files can be changed by something as insignificant as a copy operation (a...
Read more >
What Is EXIF Data? 3 Ways to Remove Metadata From Photos
Most cameras embed hidden information ("metadata") in photographs taken. For privacy's sake, here's how to remove that metadata.
Read more >
Making a jpeg Read only for others? - Seven Forums
No way at all. Data can only be controlled as long as trusted people are the only with write access to it. And...
Read more >
JPEG Metadata Format Specification and Usage Notes
JPEG metadata consists of the data contained in marker segments in a JPEG stream. The image metadata object ... Such an image may...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found