0

I was working with your library with version 3.3.2 everything goes well, but for vulnerabilities topics I decided to upgrade to version 3.8.2, but now, I received this problem:

Unsupported stream metadata format, expected com_sun_media_imageio_plugins_tiff_stream_1.0: [com_sun_media_imageio_plugins_tiff_image_1.0, javax_imageio_1.0]

Looks like the new Object to Metadata is TIFFStreamMetadada instead TIFFImageMetadata, but I don't know how to change from previous version no newer or how to avoid the exception. Can someone support me?

This is my code.

IIOMetadata metadata = getTifMetadata(); writer.prepareWriteSequence(metadata);

private IIOMetadata getTifMetadata() throws IIOInvalidTreeException {
String now = LocalDateTime.now(ZoneId.of("CST", ZoneId.SHORT_IDS)).format(dateTimeFormatter);
// custom tiff attributes
List<Entry> ifd = new ArrayList<>();
ifd.add(new TiffAttribute(TIFF.TAG_X_RESOLUTION, new Rational(RESOLUTION_PERCENT),
        TIFF_RATIONAL_TYPE));
ifd.add(new TiffAttribute(TIFF.TAG_Y_RESOLUTION, new Rational(RESOLUTION_PERCENT),
        TIFF_RATIONAL_TYPE));
ifd.add(new TiffAttribute(TIFF.TAG_RESOLUTION_UNIT, RESOLUTION_UNIT_DPI, TIFF_NUMBER_TYPE));
ifd.add(new TiffAttribute(TIFF.TAG_SOFTWARE, TIFF_SOFTWARE_NAME, TIFF_STRING_TYPE));
ifd.add(new TiffAttribute(TIFF.TAG_DATE_TIME, now, TIFF_STRING_TYPE));
ifd.add(new TiffAttribute(TIFF.TAG_MODEL, TIFF_MODEL, TIFF_STRING_TYPE));

String standardFormat = IIOMetadataFormatImpl.standardMetadataFormatName;
IIOMetadata metadata = new TIFFImageMetadata(ifd);
IIOMetadataNode customMeta = new IIOMetadataNode(standardFormat);
IIOMetadataNode dimension = new IIOMetadataNode("Dimension");
customMeta.appendChild(dimension);

IIOMetadataNode xSize = new IIOMetadataNode("HorizontalPixelSize");
dimension.appendChild(xSize);
xSize.setAttribute("value", String.valueOf(TIF_DPI));

IIOMetadataNode ySize = new IIOMetadataNode("VerticalPixelSize");
dimension.appendChild(ySize);
ySize.setAttribute("value", String.valueOf(TIF_DPI));

metadata.mergeTree(standardFormat, customMeta);
return metadata;
}

Any suggestions? Please.

BrandonRG
  • 111
  • 1
  • 1
  • 9
  • More as a PS, but I don't think you should need to first add the resolution in the "native" way, and then overwrite those with values from the "standard" format via `mergeTree`. Also, the mix of the value `RESOLUTION_PERCENT` as `RESOLUTION_UNIT_DPI` and then using `TIFF_DPI` in the standard format (where the unit is defined to be millimeters) sounds a bit fishy to me... – Harald K Jun 23 '22 at 09:49

1 Answers1

0

If you look at the API docs for prepareWriteSequence, you will find that the parameter is indeed stream metadata (perhaps "global" or "document" metadata would be better names, but that's what it is). And the TIFF format only have metadata associated with each image or IFD [1]. This means what you do didn't work as intended in earlier versions either, it was just silently ignored.

The fix is easy, just pass null as stream metadata:

writer.prepareWriteSequence(null);

To correctly write your TIFF metadata, you need to pass it along with your image (and optional thumbnails) as an IIOImage to the writeToSequence method:

writer.writeToSequence(new IIOImage(image, null, metadata), param);

And finally:

writer.endWriteSequence();

Alternatively, if you only need to write a single image to your TIFF, you can skip all the "sequence" methods, and simply use (again, passing null as stream metadata):

writer.write(null, new IIOImage(image, null, metadata), param);

1: For compatibility with the JAI TIFF plugin, the stream metadata can be used for specifying the byte order of the TIFF also in the TwelveMonkeys TIFF plugin. This is the only "global" setting. But you can configure this setting more easily by just setting the byte order on the stream you write to.

Harald K
  • 26,314
  • 7
  • 65
  • 111