You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by da...@apache.org on 2012/04/14 10:08:05 UTC

svn commit: r1326069 - in /commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff: TiffDirectory.java TiffImageParser.java

Author: damjan
Date: Sat Apr 14 08:08:05 2012
New Revision: 1326069

URL: http://svn.apache.org/viewvc?rev=1326069&view=rev
Log:
Added some more methods for getting typed values from TIFF fields.


Modified:
    commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffDirectory.java
    commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffImageParser.java

Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffDirectory.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffDirectory.java?rev=1326069&r1=1326068&r2=1326069&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffDirectory.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffDirectory.java Sat Apr 14 08:08:05 2012
@@ -202,148 +202,332 @@ public class TiffDirectory extends TiffE
         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);
     }

Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffImageParser.java?rev=1326069&r1=1326068&r2=1326069&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffImageParser.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/tiff/TiffImageParser.java Sat Apr 14 08:08:05 2012
@@ -96,7 +96,7 @@ public class TiffImageParser extends Ima
                 .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 @@ public class TiffImageParser extends Ima
 
         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 @@ public class TiffImageParser extends Ima
                 .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 @@ public class TiffImageParser extends Ima
         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)