Added some more methods for getting typed values from TIFF fields.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/sanselan/trunk@1326069 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffDirectory.java b/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffDirectory.java
index 9a28c2d..976699d 100644
--- a/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffDirectory.java
+++ b/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffDirectory.java
@@ -202,148 +202,332 @@
return field.getValue();
}
- public byte[] getFieldValue(TagInfoByte tag) throws ImageReadException {
+ public byte getSingleFieldValue(TagInfoByte tag) throws ImageReadException {
+ byte[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public String getSingleFieldValue(TagInfoAscii tag) throws ImageReadException {
+ String[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public short getSingleFieldValue(TagInfoShort tag) throws ImageReadException {
+ short[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public int getSingleFieldValue(TagInfoLong tag) throws ImageReadException {
+ int[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public RationalNumber getSingleFieldValue(TagInfoRational tag) throws ImageReadException {
+ RationalNumber[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public byte getSingleFieldValue(TagInfoSByte tag) throws ImageReadException {
+ byte[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public short getSingleFieldValue(TagInfoSShort tag) throws ImageReadException {
+ short[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public int getSingleFieldValue(TagInfoSLong tag) throws ImageReadException {
+ int[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public RationalNumber getSingleFieldValue(TagInfoSRational tag) throws ImageReadException {
+ RationalNumber[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public float getSingleFieldValue(TagInfoFloat tag) throws ImageReadException {
+ float[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public double getSingleFieldValue(TagInfoDouble tag) throws ImageReadException {
+ double[] result = getFieldValue(tag, true);
+ if (result.length != 1) {
+ throw new ImageReadException("Field \"" + tag.name + "\" has incorrect length " + result.length);
+ }
+ return result[0];
+ }
+
+ public byte[] getFieldValue(TagInfoByte tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
return field.fieldType.getRawBytes(field);
}
- public String[] getFieldValue(TagInfoAscii tag) throws ImageReadException {
+ public String[] getFieldValue(TagInfoAscii tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
byte[] bytes = field.fieldType.getRawBytes(field);
return tag.getValue(field.byteOrder, bytes);
}
- public short[] getFieldValue(TagInfoShort tag) throws ImageReadException {
+ public short[] getFieldValue(TagInfoShort tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
byte[] bytes = field.fieldType.getRawBytes(field);
return tag.getValue(field.byteOrder, bytes);
}
- public int[] getFieldValue(TagInfoLong tag) throws ImageReadException {
+ public int[] getFieldValue(TagInfoLong tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
byte[] bytes = field.fieldType.getRawBytes(field);
return tag.getValue(field.byteOrder, bytes);
}
- public RationalNumber[] getFieldValue(TagInfoRational tag) throws ImageReadException {
+ public RationalNumber[] getFieldValue(TagInfoRational tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
byte[] bytes = field.fieldType.getRawBytes(field);
return tag.getValue(field.byteOrder, bytes);
}
- public byte[] getFieldValue(TagInfoSByte tag) throws ImageReadException {
+ public byte[] getFieldValue(TagInfoSByte tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
return field.fieldType.getRawBytes(field);
}
- public short[] getFieldValue(TagInfoSShort tag) throws ImageReadException {
+ public short[] getFieldValue(TagInfoSShort tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
byte[] bytes = field.fieldType.getRawBytes(field);
return tag.getValue(field.byteOrder, bytes);
}
- public int[] getFieldValue(TagInfoSLong tag) throws ImageReadException {
+ public int[] getFieldValue(TagInfoSLong tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
byte[] bytes = field.fieldType.getRawBytes(field);
return tag.getValue(field.byteOrder, bytes);
}
- public RationalNumber[] getFieldValue(TagInfoSRational tag) throws ImageReadException {
+ public RationalNumber[] getFieldValue(TagInfoSRational tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
byte[] bytes = field.fieldType.getRawBytes(field);
return tag.getValue(field.byteOrder, bytes);
}
- public float[] getFieldValue(TagInfoFloat tag) throws ImageReadException {
+ public float[] getFieldValue(TagInfoFloat tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
byte[] bytes = field.fieldType.getRawBytes(field);
return tag.getValue(field.byteOrder, bytes);
}
- public double[] getFieldValue(TagInfoDouble tag) throws ImageReadException {
+ public double[] getFieldValue(TagInfoDouble tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
if (!tag.dataTypes.contains(field.fieldType)) {
- return null;
+ if (mustExist){
+ throw new ImageReadException("Required field \"" + tag.name + "\" has incorrect type " + field.fieldType.name);
+ } else {
+ return null;
+ }
}
byte[] bytes = field.fieldType.getRawBytes(field);
return tag.getValue(field.byteOrder, bytes);
}
- public String getFieldValue(TagInfoGpsText tag) throws ImageReadException {
+ public String getFieldValue(TagInfoGpsText tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
return tag.getValue(field);
}
- public String getFieldValue(TagInfoXpString tag) throws ImageReadException {
+ public String getFieldValue(TagInfoXpString tag, boolean mustExist) throws ImageReadException {
TiffField field = findField(tag);
if (field == null) {
- return null;
+ if (mustExist) {
+ throw new ImageReadException("Required field \"" + tag.name + "\" is missing");
+ } else {
+ return null;
+ }
}
return tag.getValue(field);
}
diff --git a/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffImageParser.java b/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffImageParser.java
index 9611e30..2b0eefa 100644
--- a/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffImageParser.java
+++ b/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffImageParser.java
@@ -96,7 +96,7 @@
.readFirstDirectory(byteSource, params, false, formatCompliance);
TiffDirectory directory = contents.directories.get(0);
- return directory.getFieldValue(AllTagConstants.EXIF_TAG_ICC_PROFILE);
+ return directory.getFieldValue(AllTagConstants.EXIF_TAG_ICC_PROFILE, false);
}
@Override
@@ -278,8 +278,7 @@
int colorType = ImageInfo.COLOR_TYPE_RGB;
- int compression = directory.findField(TiffTagConstants.TIFF_TAG_COMPRESSION)
- .getIntValue();
+ int compression = 0xffff & directory.getSingleFieldValue(TiffTagConstants.TIFF_TAG_COMPRESSION);
String compressionAlgorithm;
switch (compression)
@@ -331,7 +330,7 @@
.readDirectories(byteSource, false, formatCompliance);
TiffDirectory directory = contents.directories.get(0);
- byte bytes[] = directory.getFieldValue(TiffTagConstants.TIFF_TAG_XMP);
+ byte bytes[] = directory.getFieldValue(TiffTagConstants.TIFF_TAG_XMP, false);
if (bytes == null) {
return null;
}
@@ -492,10 +491,9 @@
if (entries == null)
throw new ImageReadException("TIFF missing entries");
- int photometricInterpretation = directory.findField(
- TiffTagConstants.TIFF_TAG_PHOTOMETRIC_INTERPRETATION, true).getIntValue();
- int compression = directory.findField(TiffTagConstants.TIFF_TAG_COMPRESSION, true)
- .getIntValue();
+ int photometricInterpretation = 0xffff & directory.getSingleFieldValue(
+ TiffTagConstants.TIFF_TAG_PHOTOMETRIC_INTERPRETATION);
+ int compression = 0xffff & directory.getSingleFieldValue(TiffTagConstants.TIFF_TAG_COMPRESSION);
int width = directory.findField(TiffTagConstants.TIFF_TAG_IMAGE_WIDTH, true)
.getIntValue();
int height = directory.findField(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH, true)