TIFFImageMetadata: ImageOrientation in mergeTree (#667)

TIFFImageMetadata: ImageOrientation in mergeTree
(cherry picked from commit b67d687761328f3a395ae8ba561131bb15660efa)
This commit is contained in:
Oliver Schmidtmer 2022-02-28 15:53:49 +01:00 committed by Harald Kuhr
parent 46bfdd93d8
commit a12a1f73b5
2 changed files with 43 additions and 1 deletions

View File

@ -1004,12 +1004,12 @@ public final class TIFFImageMetadata extends AbstractMetadata {
// - If set, set res unit to pixels per cm as this better reflects values?
// - Or, convert to DPI, if we already had values in DPI??
// Also, if we have only aspect, set these values, and use unknown as unit?
// TODO: ImageOrientation => Orientation
NodeList children = dimensionNode.getChildNodes();
Float aspect = null;
Float xRes = null;
Float yRes = null;
Integer orientation = null;
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
@ -1024,6 +1024,9 @@ public final class TIFFImageMetadata extends AbstractMetadata {
else if ("VerticalPixelSize".equals(nodeName)) {
yRes = Float.parseFloat(getAttribute(child, "value"));
}
else if ("ImageOrientation".equals(nodeName)) {
orientation = toTIFFOrientation(getAttribute(child, "value"));
}
}
// If we have one size compute the other
@ -1070,6 +1073,11 @@ public final class TIFFImageMetadata extends AbstractMetadata {
new TIFFEntry(TIFF.TAG_RESOLUTION_UNIT, TIFF.TYPE_SHORT, TIFFBaseline.RESOLUTION_UNIT_NONE));
}
// Else give up...
if (orientation != null) {
entries.put(TIFF.TAG_ORIENTATION,
new TIFFEntry(TIFF.TAG_ORIENTATION, TIFF.TYPE_SHORT, orientation.shortValue()));
}
}
private void mergeFromStandardDocumentNode(final Node documentNode, final Map<Integer, Entry> entries) {
@ -1195,6 +1203,34 @@ public final class TIFFImageMetadata extends AbstractMetadata {
}
}
private Integer toTIFFOrientation(String imageOrientation) {
if (imageOrientation == null) {
// malformed, empty or not readable value
return null;
}
switch (imageOrientation.toLowerCase()) {
case "normal":
return TIFFBaseline.ORIENTATION_TOPLEFT;
case "fliph":
return TIFFExtension.ORIENTATION_TOPRIGHT;
case "rotate180":
return TIFFExtension.ORIENTATION_BOTRIGHT;
case "flipv":
return TIFFExtension.ORIENTATION_BOTLEFT;
case "fliphrotate90":
return TIFFExtension.ORIENTATION_LEFTTOP;
case "rotate270":
return TIFFExtension.ORIENTATION_RIGHTTOP;
case "flipvrotate90":
return TIFFExtension.ORIENTATION_RIGHTBOT;
case "rotate90":
return TIFFExtension.ORIENTATION_LEFTBOT;
default:
// malformed, invalid value
return null;
}
}
private short getTIFFType(final Node node) throws IIOInvalidTreeException {
Node containerNode = node.getFirstChild();
if (containerNode == null) {

View File

@ -324,6 +324,10 @@ public class TIFFImageMetadataTest {
dimensionNode.appendChild(verticalPixelSize);
verticalPixelSize.setAttribute("value", String.valueOf(300 / 25.4));
IIOMetadataNode orientation = new IIOMetadataNode("ImageOrientation");
dimensionNode.appendChild(orientation);
orientation.setAttribute("value", "FlipV");
metadata.mergeTree(standardFormat, newTree);
Directory ifd = metadata.getIFD();
@ -332,6 +336,8 @@ public class TIFFImageMetadataTest {
assertEquals(new Rational(300), ifd.getEntryById(TIFF.TAG_X_RESOLUTION).getValue());
assertNotNull(ifd.getEntryById(TIFF.TAG_Y_RESOLUTION));
assertEquals(new Rational(300), ifd.getEntryById(TIFF.TAG_Y_RESOLUTION).getValue());
assertNotNull(ifd.getEntryById(TIFF.TAG_ORIENTATION));
assertEquals(TIFFExtension.ORIENTATION_BOTLEFT, ((Number) ifd.getEntryById(TIFF.TAG_ORIENTATION).getValue()).intValue());
// Should keep DPI as unit
assertNotNull(ifd.getEntryById(TIFF.TAG_RESOLUTION_UNIT));