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 2013/02/13 19:40:53 UTC

svn commit: r1445822 [1/3] - in /commons/proper/imaging/trunk/src: main/java/org/apache/commons/imaging/common/ main/java/org/apache/commons/imaging/common/bytesource/ main/java/org/apache/commons/imaging/common/mylzw/ main/java/org/apache/commons/imag...

Author: damjan
Date: Wed Feb 13 18:40:52 2013
New Revision: 1445822

URL: http://svn.apache.org/r1445822
Log:
Extensive refactoring and cleanup.

Encapsulated, documented and simplified TiffField variables, and
made only a single value variable instead of both an offset and value.
Initial work on use of long for offsets, so that files
> 2 GB can be read. Cleanup of excessive
interface inheritance that was used to pull in constants.
Generification of the LZW compressor.

The API and ABI will come apart at the seams :-/, but in the long run,
it's for the better.


Added:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffFieldFactory.java   (with props)
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoDirectory.java   (with props)
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoLongOrIFD.java   (with props)
Removed:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffValueOutsideFileBoundsException.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/TiffFieldTypeConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeUnknown.java
Modified:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryFileParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryInputStream.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/ByteConversions.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSource.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceArray.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceFile.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceInputStream.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/JpegImageData.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffContents.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffDirectory.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffElement.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffHeader.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageData.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageMetadata.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/AdobePageMaker6TagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/AliasSketchbookProTagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/DcfTagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/DngTagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/ExifTagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/GpsTagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/HylaFaxTagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/MicrosoftHdPhotoTagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/MolecularDynamicsGelTagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/Rfc2301TagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/TiffConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/TiffEpTagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/constants/TiffTagConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldType.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeAscii.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeByte.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeDouble.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeFloat.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeLong.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeRational.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeShort.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfo.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAny.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAscii.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAsciiOrByte.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAsciiOrRational.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoByte.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoByteOrShort.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoDouble.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoFloat.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoGpsText.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoLong.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoRational.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoSByte.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoSLong.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoSRational.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoSShort.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoShort.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoShortOrLong.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoShortOrLongOrRational.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoShortOrRational.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoUndefined.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoUnknown.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoXpString.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterLossless.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputDirectory.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputField.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputItem.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputSummary.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/jpeg/exif/AsciiFieldTest.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriteTest.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/jpeg/exif/MakerNoteFieldTest.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/jpeg/exif/TextFieldTest.java

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryFileParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryFileParser.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryFileParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryFileParser.java Wed Feb 13 18:40:52 2013
@@ -125,8 +125,13 @@ public class BinaryFileParser {
             throws IOException {
         BinaryFunctions.skipBytes(is, length);
     }
-    
-    protected final void skipBytes(final InputStream is, final int length, final String exception)
+
+    protected final void skipBytes(final InputStream is, final long length)
+            throws IOException {
+        BinaryFunctions.skipBytes(is, length);
+    }
+
+    protected final void skipBytes(final InputStream is, final long length, final String exception)
             throws IOException {
         BinaryFunctions.skipBytes(is, length, exception);
     }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java Wed Feb 13 18:40:52 2013
@@ -130,7 +130,7 @@ public class BinaryFunctions {
         }
     }
 
-    public static final void skipBytes(final InputStream is, final int length, final String exception)
+    public static final void skipBytes(final InputStream is, final long length, final String exception)
             throws IOException {
         long total = 0;
         while (length != total) {
@@ -315,7 +315,7 @@ public class BinaryFunctions {
 
     }
 
-    public static final void skipBytes(final InputStream is, final int length) throws IOException {
+    public static final void skipBytes(final InputStream is, final long length) throws IOException {
         skipBytes(is, length, "Couldn't skip bytes");
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryInputStream.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryInputStream.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/BinaryInputStream.java Wed Feb 13 18:40:52 2013
@@ -98,7 +98,7 @@ public class BinaryInputStream extends I
         BinaryFunctions.readAndVerifyBytes(name, is, expected, exception);
     }
 
-    public final void skipBytes(final int length, final String exception)
+    public final void skipBytes(final long length, final String exception)
             throws IOException {
         BinaryFunctions.skipBytes(is, length, exception);
     }
@@ -199,7 +199,7 @@ public class BinaryInputStream extends I
         return BinaryFunctions.getRAFBytes(raf, pos, length, exception);
     }
 
-    public void skipBytes(final int length) throws IOException {
+    public void skipBytes(final long length) throws IOException {
         BinaryFunctions.skipBytes(is, length);
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/ByteConversions.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/ByteConversions.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/ByteConversions.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/ByteConversions.java Wed Feb 13 18:40:52 2013
@@ -355,6 +355,24 @@ public class ByteConversions {
         }
     }
     
+    public static int[] toUInt16s(final byte[] bytes, final ByteOrder byteOrder) {
+        return toUInt16s(bytes, 0, bytes.length, byteOrder);
+    }
+
+    public static int[] toUInt16s(final byte[] bytes, final int length,
+            final ByteOrder byteOrder) {
+        return toUInt16s(bytes, 0, length, byteOrder);
+    }
+
+    public static int[] toUInt16s(final byte[] bytes, final int offset, final int length,
+            final ByteOrder byteOrder) {
+        final int[] result = new int[length / 2];
+        for (int i = 0; i < result.length; i++) {
+            result[i] = toUInt16(bytes, offset + 2 * i, byteOrder);
+        }
+        return result;
+    }
+    
     public static int toInt(final byte[] bytes, final ByteOrder byteOrder) {
         return toInt(bytes, 0, byteOrder);
     }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSource.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSource.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSource.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSource.java Wed Feb 13 18:40:52 2013
@@ -28,7 +28,7 @@ public abstract class ByteSource extends
         this.filename = filename;
     }
 
-    public final InputStream getInputStream(final int start) throws IOException {
+    public final InputStream getInputStream(final long start) throws IOException {
         InputStream is = null;
         boolean succeeded = false;
         try {
@@ -45,7 +45,11 @@ public abstract class ByteSource extends
 
     public abstract InputStream getInputStream() throws IOException;
 
-    public abstract byte[] getBlock(int start, int length) throws IOException;
+    public byte[] getBlock(int start, int length) throws IOException {
+        return getBlock(0xFFFFffffL & start, length);
+    }
+    
+    public abstract byte[] getBlock(long start, int length) throws IOException;
 
     public abstract byte[] getAll() throws IOException;
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceArray.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceArray.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceArray.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceArray.java Wed Feb 13 18:40:52 2013
@@ -39,7 +39,8 @@ public class ByteSourceArray extends Byt
     }
 
     @Override
-    public byte[] getBlock(final int start, final int length) throws IOException {
+    public byte[] getBlock(final long startLong, final int length) throws IOException {
+        final int start = (int) startLong;
         // We include a separate check for int overflow.
         if ((start < 0) || (length < 0) || (start + length < 0)
                 || (start + length > bytes.length)) {

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceFile.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceFile.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceFile.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceFile.java Wed Feb 13 18:40:52 2013
@@ -44,7 +44,7 @@ public class ByteSourceFile extends Byte
     }
 
     @Override
-    public byte[] getBlock(final int start, final int length) throws IOException {
+    public byte[] getBlock(final long start, final int length) throws IOException {
 
         RandomAccessFile raf = null;
         try {

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceInputStream.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceInputStream.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceInputStream.java Wed Feb 13 18:40:52 2013
@@ -206,7 +206,7 @@ public class ByteSourceInputStream exten
     }
 
     @Override
-    public byte[] getBlock(final int blockStart, final int blockLength) throws IOException {
+    public byte[] getBlock(final long blockStart, final int blockLength) throws IOException {
         // We include a separate check for int overflow.
         if ((blockStart < 0) || (blockLength < 0)
                 || (blockStart + blockLength < 0)

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java Wed Feb 13 18:40:52 2013
@@ -57,7 +57,7 @@ public class MyLzwCompressor {
         InitializeStringTable();
     }
 
-    private final Map<Object, Integer> map = new HashMap<Object, Integer>();
+    private final Map<ByteArray, Integer> map = new HashMap<ByteArray, Integer>();
 
     private final void InitializeStringTable() {
         codeSize = initialCodeSize;
@@ -67,7 +67,7 @@ public class MyLzwCompressor {
         map.clear();
         for (codes = 0; codes < intial_entries_count; codes++) {
             if ((codes != clearCode) && (codes != eoiCode)) {
-                final Object key = arrayToKey((byte) codes);
+                final ByteArray key = arrayToKey((byte) codes);
 
                 map.put(key, codes);
             }
@@ -85,7 +85,7 @@ public class MyLzwCompressor {
         }
     }
 
-    private final Object arrayToKey(final byte b) {
+    private final ByteArray arrayToKey(final byte b) {
         return arrayToKey(new byte[] { b, }, 0, 1);
     }
 
@@ -138,7 +138,7 @@ public class MyLzwCompressor {
         }
     }
 
-    private final Object arrayToKey(final byte bytes[], final int start, final int length) {
+    private final ByteArray arrayToKey(final byte bytes[], final int start, final int length) {
         return new ByteArray(bytes, start, length);
     }
 
@@ -170,28 +170,28 @@ public class MyLzwCompressor {
     }
 
     private final boolean isInTable(final byte bytes[], final int start, final int length) {
-        final Object key = arrayToKey(bytes, start, length);
+        final ByteArray key = arrayToKey(bytes, start, length);
 
         return map.containsKey(key);
     }
 
     private final int codeFromString(final byte bytes[], final int start, final int length)
             throws IOException {
-        final Object key = arrayToKey(bytes, start, length);
-        final Object o = map.get(key);
-        if (o == null) {
+        final ByteArray key = arrayToKey(bytes, start, length);
+        final Integer code = map.get(key);
+        if (code == null) {
             throw new IOException("CodeFromString");
         }
-        return ((Integer) o).intValue();
+        return code;
     }
 
     private final boolean addTableEntry(final MyBitOutputStream bos, final byte bytes[],
             final int start, final int length) throws IOException {
-        final Object key = arrayToKey(bytes, start, length);
+        final ByteArray key = arrayToKey(bytes, start, length);
         return addTableEntry(bos, key);
     }
 
-    private final boolean addTableEntry(final MyBitOutputStream bos, final Object key)
+    private final boolean addTableEntry(final MyBitOutputStream bos, final ByteArray key)
             throws IOException {
         boolean cleared = false;
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java Wed Feb 13 18:40:52 2013
@@ -108,9 +108,9 @@ public class DcxImageParser extends Imag
 
         public static final int DCX_ID = 0x3ADE68B1;
         public final int id;
-        public final int[] pageTable;
+        public final long[] pageTable;
 
-        public DcxHeader(final int id, final int[] pageTable) {
+        public DcxHeader(final int id, final long[] pageTable) {
             this.id = id;
             this.pageTable = pageTable;
         }
@@ -129,9 +129,9 @@ public class DcxImageParser extends Imag
         try {
             is = byteSource.getInputStream();
             final int id = read4Bytes("Id", is, "Not a Valid DCX File");
-            final List<Integer> pageTable = new ArrayList<Integer>(1024);
+            final List<Long> pageTable = new ArrayList<Long>(1024);
             for (int i = 0; i < 1024; i++) {
-                final int pageOffset = read4Bytes("PageTable", is,
+                final long pageOffset = 0xFFFFffffL & read4Bytes("PageTable", is,
                         "Not a Valid DCX File");
                 if (pageOffset == 0) {
                     break;
@@ -149,9 +149,9 @@ public class DcxImageParser extends Imag
             }
 
             final Object[] objects = pageTable.toArray();
-            final int[] pages = new int[objects.length];
+            final long[] pages = new long[objects.length];
             for (int i = 0; i < objects.length; i++) {
-                pages[i] = ((Integer) objects[i]).intValue();
+                pages[i] = ((Long) objects[i]);
             }
 
             return new DcxHeader(id, pages);
@@ -189,7 +189,7 @@ public class DcxImageParser extends Imag
         final DcxHeader dcxHeader = readDcxHeader(byteSource);
         final List<BufferedImage> images = new ArrayList<BufferedImage>();
         final PcxImageParser pcxImageParser = new PcxImageParser();
-        for (final int element : dcxHeader.pageTable) {
+        for (final long element : dcxHeader.pageTable) {
             InputStream stream = null;
             try {
                 stream = byteSource.getInputStream(element);

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/JpegImageData.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/JpegImageData.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/JpegImageData.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/JpegImageData.java Wed Feb 13 18:40:52 2013
@@ -18,7 +18,7 @@
 package org.apache.commons.imaging.formats.tiff;
 
 public class JpegImageData extends TiffElement.DataElement {
-    public JpegImageData(final int offset, final int length, final byte data[]) {
+    public JpegImageData(final long offset, final int length, final byte data[]) {
         super(offset, length, data);
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffContents.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffContents.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffContents.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffContents.java Wed Feb 13 18:40:52 2013
@@ -81,7 +81,7 @@ public class TiffContents {
 
         Collections.sort(elements, TiffElement.COMPARATOR);
 
-        int lastEnd = 0;
+        long lastEnd = 0;
         for (int i = 0; i < elements.size(); i++) {
             final TiffElement element = elements.get(i);
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffDirectory.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffDirectory.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffDirectory.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffDirectory.java Wed Feb 13 18:40:52 2013
@@ -23,10 +23,12 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.ByteConversions;
 import org.apache.commons.imaging.common.ByteOrder;
 import org.apache.commons.imaging.common.RationalNumber;
 import org.apache.commons.imaging.formats.tiff.constants.TiffConstants;
 import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
+import org.apache.commons.imaging.formats.tiff.fieldtypes.FieldType;
 import org.apache.commons.imaging.formats.tiff.taginfos.TagInfo;
 import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoAscii;
 import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoByte;
@@ -40,10 +42,25 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoSRational;
 import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoSShort;
 import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoShort;
+import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoShortOrLong;
 import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoXpString;
 
-public class TiffDirectory extends TiffElement implements TiffConstants {
+public class TiffDirectory extends TiffElement {
+    public final int type;
+    public final List<TiffField> entries;
+    public final long nextDirectoryOffset;
+
+    public TiffDirectory(final int type, final List<TiffField> entries, final long offset,
+            final long nextDirectoryOffset) {
+        super(offset, TiffConstants.TIFF_DIRECTORY_HEADER_LENGTH +
+                entries.size() * TiffConstants.TIFF_ENTRY_LENGTH +
+                TiffConstants.TIFF_DIRECTORY_FOOTER_LENGTH);
 
+        this.type = type;
+        this.entries = entries;
+        this.nextDirectoryOffset = nextDirectoryOffset;
+    }
+    
     public String description() {
         return TiffDirectory.description(type);
     }
@@ -54,7 +71,7 @@ public class TiffDirectory extends TiffE
             return "TIFF Directory (" + description() + ")";
         }
 
-        int entryOffset = offset + TIFF_DIRECTORY_HEADER_LENGTH;
+        long entryOffset = offset + TiffConstants.TIFF_DIRECTORY_HEADER_LENGTH;
 
         final StringBuilder result = new StringBuilder();
         for (int i = 0; i < entries.size(); i++) {
@@ -62,16 +79,16 @@ public class TiffDirectory extends TiffE
 
             result.append("\t");
             result.append("[" + entryOffset + "]: ");
-            result.append(entry.tagInfo.name);
-            result.append(" (" + entry.tag + ", 0x"
-                    + Integer.toHexString(entry.tag) + ")");
-            result.append(", " + entry.fieldType.name);
-            result.append(", " + entry.fieldType.getRawBytes(entry).length);
+            result.append(entry.getTagInfo().name);
+            result.append(" (" + entry.getTag() + ", 0x"
+                    + Integer.toHexString(entry.getTag()) + ")");
+            result.append(", " + entry.getFieldType().getName());
+            result.append(", " + entry.getBytesLength());
             result.append(": " + entry.getValueDescription());
 
             result.append("\n");
 
-            entryOffset += TIFF_ENTRY_LENGTH;
+            entryOffset += TiffConstants.TIFF_ENTRY_LENGTH;
             // entry.fillInValue(byteSource);
         }
         return result.toString();
@@ -79,41 +96,27 @@ public class TiffDirectory extends TiffE
 
     public static final String description(final int type) {
         switch (type) {
-        case DIRECTORY_TYPE_UNKNOWN:
+        case TiffConstants.DIRECTORY_TYPE_UNKNOWN:
             return "Unknown";
-        case DIRECTORY_TYPE_ROOT:
+        case TiffConstants.DIRECTORY_TYPE_ROOT:
             return "Root";
-        case DIRECTORY_TYPE_SUB:
+        case TiffConstants.DIRECTORY_TYPE_SUB:
             return "Sub";
-        case DIRECTORY_TYPE_THUMBNAIL:
+        case TiffConstants.DIRECTORY_TYPE_THUMBNAIL:
             return "Thumbnail";
-        case DIRECTORY_TYPE_EXIF:
+        case TiffConstants.DIRECTORY_TYPE_EXIF:
             return "Exif";
-        case DIRECTORY_TYPE_GPS:
+        case TiffConstants.DIRECTORY_TYPE_GPS:
             return "Gps";
-        case DIRECTORY_TYPE_INTEROPERABILITY:
+        case TiffConstants.DIRECTORY_TYPE_INTEROPERABILITY:
             return "Interoperability";
         default:
             return "Bad Type";
         }
     }
 
-    public final int type;
-    public final List<TiffField> entries;
-    // public final int offset;
-    public final int nextDirectoryOffset;
 
-    public TiffDirectory(final int type, final List<TiffField> entries, final int offset,
-            final int nextDirectoryOffset) {
-        super(offset, TIFF_DIRECTORY_HEADER_LENGTH + entries.size()
-                * TIFF_ENTRY_LENGTH + TIFF_DIRECTORY_FOOTER_LENGTH);
-
-        this.type = type;
-        this.entries = entries;
-        this.nextDirectoryOffset = nextDirectoryOffset;
-    }
-
-    public List<TiffField> getDirectoryEntrys() {
+    public List<TiffField> getDirectoryEntries() {
         return new ArrayList<TiffField>(entries);
     }
 
@@ -173,7 +176,7 @@ public class TiffDirectory extends TiffE
 
         for (int i = 0; i < entries.size(); i++) {
             final TiffField field = entries.get(i);
-            if (field.tag == tag.tag) {
+            if (field.getTag() == tag.tag) {
                 return field;
             }
         }
@@ -231,6 +234,15 @@ public class TiffDirectory extends TiffE
         }
         return result[0];
     }
+    
+    public int getSingleFieldValue(final TagInfoShortOrLong tag) throws ImageReadException {
+        final 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(final TagInfoRational tag)
             throws ImageReadException {
@@ -311,15 +323,15 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        return field.fieldType.getRawBytes(field);
+        return field.getByteArrayValue();
     }
 
     public String[] getFieldValue(final TagInfoAscii tag, final boolean mustExist)
@@ -333,16 +345,16 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public short[] getFieldValue(final TagInfoShort tag, final boolean mustExist)
@@ -356,16 +368,16 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public int[] getFieldValue(final TagInfoLong tag, final boolean mustExist)
@@ -379,16 +391,43 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
+            if (mustExist) {
+                throw new ImageReadException("Required field \"" + tag.name
+                        + "\" has incorrect type " + field.getFieldType().getName());
+            } else {
+                return null;
+            }
+        }
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
+    }
+    
+    public int[] getFieldValue(final TagInfoShortOrLong tag, final boolean mustExist)
+            throws ImageReadException {
+        final TiffField field = findField(tag);
+        if (field == null) {
+            if (mustExist) {
+                throw new ImageReadException("Required field \"" + tag.name
+                        + "\" is missing");
+            } else {
+                return null;
+            }
+        }
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        if (field.getFieldType() == FieldType.SHORT) {
+            return ByteConversions.toUInt16s(bytes, field.getByteOrder());
+        } else {
+            return ByteConversions.toInts(bytes, field.getByteOrder());
+        }
     }
 
     public RationalNumber[] getFieldValue(final TagInfoRational tag, final boolean mustExist)
@@ -402,16 +441,16 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public byte[] getFieldValue(final TagInfoSByte tag, final boolean mustExist)
@@ -425,15 +464,15 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        return field.fieldType.getRawBytes(field);
+        return field.getByteArrayValue();
     }
 
     public short[] getFieldValue(final TagInfoSShort tag, final boolean mustExist)
@@ -447,16 +486,16 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public int[] getFieldValue(final TagInfoSLong tag, final boolean mustExist)
@@ -470,16 +509,16 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public RationalNumber[] getFieldValue(final TagInfoSRational tag,
@@ -493,16 +532,16 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public float[] getFieldValue(final TagInfoFloat tag, final boolean mustExist)
@@ -516,16 +555,16 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public double[] getFieldValue(final TagInfoDouble tag, final boolean mustExist)
@@ -539,16 +578,16 @@ public class TiffDirectory extends TiffE
                 return null;
             }
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             if (mustExist) {
                 throw new ImageReadException("Required field \"" + tag.name
-                        + "\" has incorrect type " + field.fieldType.name);
+                        + "\" has incorrect type " + field.getFieldType().getName());
             } else {
                 return null;
             }
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public String getFieldValue(final TagInfoGpsText tag, final boolean mustExist)
@@ -580,7 +619,7 @@ public class TiffDirectory extends TiffE
     }
 
     public static final class ImageDataElement extends TiffElement {
-        public ImageDataElement(final int offset, final int length) {
+        public ImageDataElement(final long offset, final int length) {
             super(offset, length);
         }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffElement.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffElement.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffElement.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffElement.java Wed Feb 13 18:40:52 2013
@@ -19,10 +19,10 @@ package org.apache.commons.imaging.forma
 import java.util.Comparator;
 
 public abstract class TiffElement {
-    public final int offset;
+    public final long offset;
     public final int length;
 
-    public TiffElement(final int offset, final int length) {
+    public TiffElement(final long offset, final int length) {
         this.offset = offset;
         this.length = length;
     }
@@ -35,14 +35,20 @@ public abstract class TiffElement {
 
     public static final Comparator<TiffElement> COMPARATOR = new Comparator<TiffElement>() {
         public int compare(final TiffElement e1, final TiffElement e2) {
-            return e1.offset - e2.offset;
+            if (e1.offset < e2.offset) {
+                return -1;
+            } else if (e1.offset > e2.offset){
+                return 1;
+            } else {
+                return 0;
+            }
         }
     };
 
     public static abstract class DataElement extends TiffElement {
         public final byte data[];
 
-        public DataElement(final int offset, final int length, final byte data[]) {
+        public DataElement(final long offset, final int length, final byte data[]) {
             super(offset, length);
 
             this.data = data;
@@ -55,7 +61,7 @@ public abstract class TiffElement {
     }
 
     public static final class Stub extends TiffElement {
-        public Stub(final int offset, final int length) {
+        public Stub(final long offset, final int length) {
             super(offset, length);
         }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java Wed Feb 13 18:40:52 2013
@@ -16,7 +16,6 @@
  */
 package org.apache.commons.imaging.formats.tiff;
 
-import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.nio.charset.Charset;
@@ -29,8 +28,9 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.BinaryFunctions;
 import org.apache.commons.imaging.common.ByteOrder;
-import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.formats.tiff.constants.AllTagConstants;
 import org.apache.commons.imaging.formats.tiff.constants.TiffConstants;
 import org.apache.commons.imaging.formats.tiff.constants.TiffDirectoryType;
 import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
@@ -38,48 +38,110 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.formats.tiff.taginfos.TagInfo;
 
 /**
- * A TIFF field in a TIFF directory. 
+ * A TIFF field in a TIFF directory. Immutable.
  */
-public class TiffField implements TiffConstants {
-    public final TagInfo tagInfo;
-    public final FieldType fieldType;
-
-    /// The tag, bytes 0-1.
-    public final int tag;
-    public final int directoryType;
-    /// The type, bytes 2-3.
-    public final int type;
-    /// The count, bytes 4-7.
-    public final int length;
-    /// The value offset, bytes 8-11, containing either the value, or its offset in the file.
-    public final int valueOffset;
-    public final byte valueOffsetBytes[];
-
-    public byte oversizeValue[] = null;
-    public final ByteOrder byteOrder;
-    private int sortHint = -1;
-
-    public TiffField(final int tag, final int directoryType, final int type, final int Length,
-            final int ValueOffset, final byte ValueOffsetBytes[], final ByteOrder byteOrder) {
+public class TiffField {
+    private final TagInfo tagInfo;
+    private final int tag;
+    private final int directoryType;
+    private final FieldType fieldType;
+    private final long count;
+    private final long offset;
+    private final byte[] value;
+    private final ByteOrder byteOrder;
+    private final int sortHint;
+
+    public TiffField(final int tag, final int directoryType, final FieldType fieldType,
+            final long count, final long offset, final byte[] value,
+            final ByteOrder byteOrder, final int sortHint) throws ImageReadException{
 
         this.tag = tag;
         this.directoryType = directoryType;
-        this.type = type;
-        this.length = Length;
-        this.valueOffset = ValueOffset;
-        this.valueOffsetBytes = ValueOffsetBytes;
+        this.fieldType = fieldType;
+        this.count = count;
+        this.offset = offset;
+        this.value = value;
         this.byteOrder = byteOrder;
+        this.sortHint = sortHint;
 
-        fieldType = getFieldType(type);
         tagInfo = getTag(directoryType, tag);
     }
+    
+    public int getDirectoryType() {
+        return directoryType;
+    }
+    
+    public TagInfo getTagInfo() {
+        return tagInfo;
+    }
+
+    /**
+     * Returns the field's tag, derived from bytes 0-1.
+     * @return the tag, as an <code>int</code> in which only the lowest 2 bytes are set 
+     */
+    public int getTag() {
+        return tag;
+    }
+    
+    /**
+     * Returns the field's type, derived from bytes 2-3.
+     * @return the field's type, as a {@code FieldType} object.
+     */
+    public FieldType getFieldType() {
+        return fieldType;
+    }
+
+    /**
+     * Returns the field's count, derived from bytes 4-7.
+     * @return the count
+     */
+    public long getCount() {
+        return count;
+    }
+
+    /**
+     * Returns the TIFF field's offset/value field, derived from bytes 8-11.
+     * @return the field's offset in a <code>long</code> of 4 packed bytes,
+     * or its inlined value <= 4 bytes long encoded in the field's byte order.
+     */
+    public int getOffset() {
+        return (int)offset;
+    }
+
+    /**
+     * Returns the field's byte order.
+     * @return the byte order
+     */
+    public ByteOrder getByteOrder() {
+        return byteOrder;
+    }
+
+    public int getSortHint() {
+        return sortHint;
+    }
 
+    /**
+     * Indicates whether the field's value is inlined into the offset field.
+     * @return true if the value is inlined
+     */
     public boolean isLocalValue() {
-        return fieldType.isLocalValue(this);
+        return (count * fieldType.getSize()) <= TiffConstants.TIFF_ENTRY_MAX_VALUE_LENGTH;
     }
 
-    public int getBytesLength() throws ImageReadException {
-        return fieldType.getBytesLength(this);
+    /**
+     * The length of the field's value.
+     * @return the length, in bytes.
+     */
+    public int getBytesLength() {
+        return (int) count * fieldType.getSize();
+    }
+
+    /**
+     * Returns a copy of the raw value of the field. 
+     * @return the value of the field, in the byte order of the field.
+     */
+    public byte[] getByteArrayValue() {
+        return BinaryFunctions.head(value, getBytesLength());
     }
 
     public final class OversizeValueElement extends TiffElement {
@@ -93,32 +155,17 @@ public class TiffField implements TiffCo
                 return null;
             }
 
-            return "OversizeValueElement, tag: " + tagInfo.name
-                    + ", fieldType: " + fieldType.name;
+            return "OversizeValueElement, tag: " + getTagInfo().name
+                    + ", fieldType: " + getFieldType().getName();
         }
     }
 
     public TiffElement getOversizeValueElement() {
-        if (fieldType.isLocalValue(this)) {
+        if (isLocalValue()) {
             return null;
         }
 
-        return new OversizeValueElement(valueOffset, oversizeValue.length);
-    }
-
-    public void setOversizeValue(final byte bytes[]) {
-        this.oversizeValue = bytes;
-    }
-
-    private static FieldType getFieldType(final int value) {
-        for (int i = 0; i < FIELD_TYPES.size(); i++) {
-            final FieldType fieldType = FIELD_TYPES.get(i);
-            if (fieldType.type == value) {
-                return fieldType;
-            }
-        }
-
-        return FIELD_TYPE_UNKNOWN;
+        return new OversizeValueElement(getOffset(), value.length);
     }
 
     private static TagInfo getTag(final int directoryType, final int tag,
@@ -262,42 +309,6 @@ public class TiffField implements TiffCo
         return result;
     }
 
-    private int getValueLengthInBytes() {
-        final int unit_length = fieldType.length;
-        final int valueLength = unit_length * length;
-
-        // Debug.debug("getValueLengthInBytes unit_length", unit_length);
-        // Debug.debug("getValueLengthInBytes length", length);
-
-        return valueLength;
-    }
-
-    public void fillInValue(final ByteSource byteSource) throws IOException,
-            TiffValueOutsideFileBoundsException {
-        if (fieldType.isLocalValue(this)) {
-            return;
-        }
-
-        final int valueLength = getValueLengthInBytes();
-        final long valueLengthLong = 0xffffffffL & valueLength;
-
-        // Debug.debug("fillInValue tag", tag);
-        // Debug.debug("fillInValue tagInfo", tagInfo);
-        // Debug.debug("fillInValue valueOffset", valueOffset);
-        // Debug.debug("fillInValue valueLength", valueLength);
-
-        if (valueOffset < 0
-                || valueOffset + valueLengthLong > byteSource.getLength()) {
-            throw new TiffValueOutsideFileBoundsException(
-                    "Attempt to read byte range starting from " + valueOffset
-                            + " " + "of length " + valueLength + " "
-                            + "which is outside the file's size of "
-                            + byteSource.getLength());
-        }
-        final byte bytes[] = byteSource.getBlock(valueOffset, valueLength);
-        setOversizeValue(bytes);
-    }
-
     public String getValueDescription() {
         try {
             return getValueDescription(getValue());
@@ -509,7 +520,7 @@ public class TiffField implements TiffCo
     }
 
     public String getDescriptionWithoutValue() {
-        return tag + " (0x" + Integer.toHexString(tag) + ": " + tagInfo.name
+        return getTag() + " (0x" + Integer.toHexString(getTag()) + ": " + getTagInfo().name
                 + "): ";
     }
 
@@ -517,30 +528,30 @@ public class TiffField implements TiffCo
     public String toString() {
         final StringBuilder result = new StringBuilder();
 
-        result.append(tag + " (0x" + Integer.toHexString(tag) + ": "
-                + tagInfo.name + "): ");
-        result.append(getValueDescription() + " (" + length + " "
-                + fieldType.name + ")");
+        result.append(getTag() + " (0x" + Integer.toHexString(getTag()) + ": "
+                + getTagInfo().name + "): ");
+        result.append(getValueDescription() + " (" + getCount() + " "
+                + getFieldType().getName() + ")");
 
         return result.toString();
     }
 
     public String getTagName() {
-        if (tagInfo == TiffTagConstants.TIFF_TAG_UNKNOWN) {
-            return tagInfo.name + " (0x" + Integer.toHexString(tag) + ")";
+        if (getTagInfo() == TiffTagConstants.TIFF_TAG_UNKNOWN) {
+            return getTagInfo().name + " (0x" + Integer.toHexString(getTag()) + ")";
         }
-        return tagInfo.name;
+        return getTagInfo().name;
     }
 
     public String getFieldTypeName() {
-        return fieldType.name;
+        return getFieldType().getName();
     }
 
     public static final String Attribute_Tag = "Tag";
 
     public Object getValue() throws ImageReadException {
         // System.out.print("getValue");
-        return tagInfo.getValue(this);
+        return getTagInfo().getValue(this);
     }
 
     public String getStringValue() throws ImageReadException {
@@ -550,7 +561,7 @@ public class TiffField implements TiffCo
         }
         if (!(o instanceof String)) {
             throw new ImageReadException("Expected String value("
-                    + tagInfo.getDescription() + "): " + o);
+                    + getTagInfo().getDescription() + "): " + o);
         }
         return (String) o;
     }
@@ -575,7 +586,7 @@ public class TiffField implements TiffCo
     }
 
     private static final Map<Object, List<TagInfo>> ALL_TAG_MAP = makeTagMap(
-            ALL_TAGS, true, "All");
+            AllTagConstants.ALL_TAGS, true, "All");
 
     public int[] getIntArrayValue() throws ImageReadException {
         final Object o = getValue();
@@ -608,7 +619,7 @@ public class TiffField implements TiffCo
         }
 
         throw new ImageReadException("Unknown value: " + o + " for: "
-                + tagInfo.getDescription());
+                + getTagInfo().getDescription());
         // return null;
     }
 
@@ -657,7 +668,7 @@ public class TiffField implements TiffCo
         }
 
         throw new ImageReadException("Unknown value: " + o + " for: "
-                + tagInfo.getDescription());
+                + getTagInfo().getDescription());
         // return null;
     }
 
@@ -692,7 +703,7 @@ public class TiffField implements TiffCo
         }
 
         throw new ImageReadException("Unknown value: " + o + " for: "
-                + tagInfo.getDescription());
+                + getTagInfo().getDescription());
         // return -1;
     }
 
@@ -700,7 +711,7 @@ public class TiffField implements TiffCo
         final Object o = getValue();
         if (o == null) {
             throw new ImageReadException("Missing value: "
-                    + tagInfo.getDescription());
+                    + getTagInfo().getDescription());
         }
 
         return ((Number) o).intValue();
@@ -710,21 +721,9 @@ public class TiffField implements TiffCo
         final Object o = getValue();
         if (o == null) {
             throw new ImageReadException("Missing value: "
-                    + tagInfo.getDescription());
+                    + getTagInfo().getDescription());
         }
 
         return ((Number) o).doubleValue();
     }
-
-    public byte[] getByteArrayValue() {
-        return fieldType.getRawBytes(this);
-    }
-
-    public int getSortHint() {
-        return sortHint;
-    }
-
-    public void setSortHint(final int sortHint) {
-        this.sortHint = sortHint;
-    }
 }

Added: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffFieldFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffFieldFactory.java?rev=1445822&view=auto
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffFieldFactory.java (added)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffFieldFactory.java Wed Feb 13 18:40:52 2013
@@ -0,0 +1,10 @@
+package org.apache.commons.imaging.formats.tiff;
+
+public class TiffFieldFactory {
+    
+    
+    public static TiffField createField(final int tag, final int type,
+            final long count, final long offset, final byte[] offsetBytes) {
+        return null;
+    }
+}

Propchange: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffFieldFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffHeader.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffHeader.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffHeader.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffHeader.java Wed Feb 13 18:40:52 2013
@@ -22,9 +22,9 @@ import org.apache.commons.imaging.format
 public class TiffHeader extends TiffElement {
     public final ByteOrder byteOrder;
     public final int tiffVersion;
-    public final int offsetToFirstIFD;
+    public final long offsetToFirstIFD;
 
-    public TiffHeader(final ByteOrder byteOrder, final int tiffVersion, final int offsetToFirstIFD) {
+    public TiffHeader(final ByteOrder byteOrder, final int tiffVersion, final long offsetToFirstIFD) {
         super(0, TiffConstants.TIFF_HEADER_SIZE);
 
         this.byteOrder = byteOrder;

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageData.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageData.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageData.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageData.java Wed Feb 13 18:40:52 2013
@@ -141,7 +141,7 @@ public abstract class TiffImageData {
             ImageReadException;
 
     public static class Data extends TiffElement.DataElement {
-        public Data(final int offset, final int length, final byte data[]) {
+        public Data(final long offset, final int length, final byte data[]) {
             super(offset, length, data);
         }
 
@@ -155,7 +155,7 @@ public abstract class TiffImageData {
     public static class ByteSourceData extends Data {
         ByteSourceFile byteSourceFile;
 
-        public ByteSourceData(final int offset, final int length, final ByteSourceFile byteSource) {
+        public ByteSourceData(final long offset, final int length, final ByteSourceFile byteSource) {
             super(offset, length, new byte[0]);
             byteSourceFile = byteSource;
         }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageMetadata.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageMetadata.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageMetadata.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageMetadata.java Wed Feb 13 18:40:52 2013
@@ -110,7 +110,7 @@ public class TiffImageMetadata extends I
         }
 
         public List<TiffField> getAllFields() {
-            return directory.getDirectoryEntrys();
+            return directory.getDirectoryEntries();
         }
 
         public JpegImageData getJpegImageData() {
@@ -138,16 +138,16 @@ public class TiffImageMetadata extends I
                             .get(i);
                     final TiffField srcField = item.getTiffField();
 
-                    if (null != dstDir.findField(srcField.tag)) {
+                    if (null != dstDir.findField(srcField.getTag())) {
                         // ignore duplicate tags in a directory.
                         continue;
-                    } else if (srcField.tagInfo.isOffset()) {
+                    } else if (srcField.getTagInfo().isOffset()) {
                         // ignore offset fields.
                         continue;
                     }
 
-                    final TagInfo tagInfo = srcField.tagInfo;
-                    final FieldType fieldType = srcField.fieldType;
+                    final TagInfo tagInfo = srcField.getTagInfo();
+                    final FieldType fieldType = srcField.getFieldType();
                     // byte bytes[] = srcField.fieldType.getRawBytes(srcField);
 
                     // Debug.debug("tagInfo", tagInfo);
@@ -165,9 +165,9 @@ public class TiffImageMetadata extends I
                     // + Integer.toHexString(srcField.tag)
                     // + ") bytes", bytes);
 
-                    final int count = bytes.length / fieldType.length;
+                    final int count = bytes.length / fieldType.getSize();
                     final TiffOutputField dstField = new TiffOutputField(
-                            srcField.tag, tagInfo, fieldType, count, bytes);
+                            srcField.getTag(), tagInfo, fieldType, count, bytes);
                     dstField.setSortHint(srcField.getSortHint());
                     dstDir.add(dstField);
                 }
@@ -306,10 +306,10 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        return field.fieldType.getRawBytes(field);
+        return field.getByteArrayValue();
     }
 
     public String[] getFieldValue(final TagInfoAscii tag) throws ImageReadException {
@@ -317,11 +317,11 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public short[] getFieldValue(final TagInfoShort tag) throws ImageReadException {
@@ -329,11 +329,11 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public int[] getFieldValue(final TagInfoLong tag) throws ImageReadException {
@@ -341,11 +341,11 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public RationalNumber[] getFieldValue(final TagInfoRational tag)
@@ -354,11 +354,11 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public byte[] getFieldValue(final TagInfoSByte tag) throws ImageReadException {
@@ -366,10 +366,10 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        return field.fieldType.getRawBytes(field);
+        return field.getByteArrayValue();
     }
 
     public short[] getFieldValue(final TagInfoSShort tag) throws ImageReadException {
@@ -377,11 +377,11 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public int[] getFieldValue(final TagInfoSLong tag) throws ImageReadException {
@@ -389,11 +389,11 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public RationalNumber[] getFieldValue(final TagInfoSRational tag)
@@ -402,11 +402,11 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public float[] getFieldValue(final TagInfoFloat tag) throws ImageReadException {
@@ -414,11 +414,11 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public double[] getFieldValue(final TagInfoDouble tag) throws ImageReadException {
@@ -426,11 +426,11 @@ public class TiffImageMetadata extends I
         if (field == null) {
             return null;
         }
-        if (!tag.dataTypes.contains(field.fieldType)) {
+        if (!tag.dataTypes.contains(field.getFieldType())) {
             return null;
         }
-        final byte[] bytes = field.fieldType.getRawBytes(field);
-        return tag.getValue(field.byteOrder, bytes);
+        final byte[] bytes = field.getByteArrayValue();
+        return tag.getValue(field.getByteOrder(), bytes);
     }
 
     public String getFieldValue(final TagInfoGpsText tag) throws ImageReadException {

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageParser.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageParser.java Wed Feb 13 18:40:52 2013
@@ -145,7 +145,7 @@ public class TiffImageParser extends Ima
             final TiffImageMetadata.Directory metadataDirectory = new TiffImageMetadata.Directory(
                     tiffReader.getByteOrder(), dir);
 
-            final List<TiffField> entries = dir.getDirectoryEntrys();
+            final List<TiffField> entries = dir.getDirectoryEntries();
 
             for (int j = 0; j < entries.size(); j++) {
                 final TiffField entry = entries.get(j);
@@ -578,14 +578,14 @@ public class TiffImageParser extends Ima
             throw new ImageReadException("TIFF missing entries");
         }
 
-        final int photometricInterpretation = 0xffff & directory
-                .getSingleFieldValue(TiffTagConstants.TIFF_TAG_PHOTOMETRIC_INTERPRETATION);
-        final int compression = 0xffff & directory
-                .getSingleFieldValue(TiffTagConstants.TIFF_TAG_COMPRESSION);
-        final int width = directory.findField(TiffTagConstants.TIFF_TAG_IMAGE_WIDTH,
-                true).getIntValue();
-        final int height = directory.findField(
-                TiffTagConstants.TIFF_TAG_IMAGE_LENGTH, true).getIntValue();      
+        final int photometricInterpretation = 0xffff & directory.getSingleFieldValue(
+                TiffTagConstants.TIFF_TAG_PHOTOMETRIC_INTERPRETATION);
+        final int compression = 0xffff & directory.getSingleFieldValue(
+                TiffTagConstants.TIFF_TAG_COMPRESSION);
+        final int width = directory.getSingleFieldValue(
+                TiffTagConstants.TIFF_TAG_IMAGE_WIDTH);
+        final int height = directory.getSingleFieldValue(
+                TiffConstants.TIFF_TAG_IMAGE_LENGTH);      
         Rectangle subImage = checkForSubImage(params);
         if(subImage!=null){
             // Check for valid subimage specification. The following checks
@@ -623,15 +623,15 @@ public class TiffImageParser extends Ima
 
         
         int samplesPerPixel = 1;
-        final TiffField samplesPerPixelField = directory
-                .findField(TiffTagConstants.TIFF_TAG_SAMPLES_PER_PIXEL);
+        final TiffField samplesPerPixelField = directory.findField(
+                TiffTagConstants.TIFF_TAG_SAMPLES_PER_PIXEL);
         if (samplesPerPixelField != null) {
             samplesPerPixel = samplesPerPixelField.getIntValue();
         }
         int bitsPerSample[] = { 1 };
         int bitsPerPixel = samplesPerPixel;
-        final TiffField bitsPerSampleField = directory
-                .findField(TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE);
+        final TiffField bitsPerSampleField = directory.findField(
+                TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE);
         if (bitsPerSampleField != null) {
             bitsPerSample = bitsPerSampleField.getIntArrayValue();
             bitsPerPixel = bitsPerSampleField.getIntValueOrArraySum();
@@ -647,8 +647,8 @@ public class TiffImageParser extends Ima
             // dumpOptionalNumberTag(entries, TIFF_TAG_FREE_OFFSETS);
             // dumpOptionalNumberTag(entries, TIFF_TAG_ORIENTATION);
             // dumpOptionalNumberTag(entries, TIFF_TAG_PLANAR_CONFIGURATION);
-            final TiffField predictorField = directory
-                    .findField(TiffTagConstants.TIFF_TAG_PREDICTOR);
+            final TiffField predictorField = directory.findField(
+                    TiffTagConstants.TIFF_TAG_PREDICTOR);
             if (null != predictorField) {
                 predictor = predictorField.getIntValueOrArraySum();
             }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java?rev=1445822&r1=1445821&r2=1445822&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java Wed Feb 13 18:40:52 2013
@@ -31,8 +31,12 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.formats.jpeg.JpegConstants;
 import org.apache.commons.imaging.formats.tiff.TiffDirectory.ImageDataElement;
 import org.apache.commons.imaging.formats.tiff.constants.TiffConstants;
+import org.apache.commons.imaging.formats.tiff.constants.TiffDirectoryConstants;
 import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
+import org.apache.commons.imaging.formats.tiff.fieldtypes.FieldType;
+import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoDirectory;
 import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoLong;
+import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoLongOrIFD;
 import org.apache.commons.imaging.util.Debug;
 
 public class TiffReader extends BinaryFileParser implements TiffConstants {
@@ -88,7 +92,7 @@ public class TiffReader extends BinaryFi
             throw new ImageReadException("Unknown Tiff Version: " + tiffVersion);
         }
 
-        final int offsetToFirstIFD = read4Bytes("offsetToFirstIFD", is,
+        final long offsetToFirstIFD = 0xFFFFffffL & read4Bytes("offsetToFirstIFD", is,
                 "Not a Valid TIFF File");
 
         skipBytes(is, offsetToFirstIFD - 8,
@@ -109,15 +113,15 @@ public class TiffReader extends BinaryFi
             return;
         }
 
-        final int offset = tiffHeader.offsetToFirstIFD;
-        final int dirType = TiffDirectory.DIRECTORY_TYPE_ROOT;
+        final long offset = tiffHeader.offsetToFirstIFD;
+        final int dirType = TiffDirectoryConstants.DIRECTORY_TYPE_ROOT;
 
         final List<Number> visited = new ArrayList<Number>();
         readDirectory(byteSource, offset, dirType, formatCompliance, listener,
                 visited);
     }
 
-    private boolean readDirectory(final ByteSource byteSource, final int offset,
+    private boolean readDirectory(final ByteSource byteSource, final long offset,
             final int dirType, final FormatCompliance formatCompliance, final Listener listener,
             final List<Number> visited) throws ImageReadException, IOException {
         final boolean ignoreNextDirectory = false;
@@ -125,34 +129,24 @@ public class TiffReader extends BinaryFi
                 listener, ignoreNextDirectory, visited);
     }
 
-    private boolean readDirectory(final ByteSource byteSource, final int offset,
+    private boolean readDirectory(final ByteSource byteSource, final long directoryOffset,
             final int dirType, final FormatCompliance formatCompliance, final Listener listener,
             final boolean ignoreNextDirectory, final List<Number> visited)
             throws ImageReadException, IOException {
 
-        // Debug.debug();
-        // Debug.debug("dir offset", offset + " (0x" +
-        // Integer.toHexString(offset)
-        // + ")");
-        // Debug.debug("dir key", key);
-        // Debug.debug("dir visited", visited);
-        // Debug.debug("dirType", dirType);
-        // Debug.debug();
-
-        if (visited.contains(offset)) {
+        if (visited.contains(directoryOffset)) {
             return false;
         }
-        visited.add(offset);
+        visited.add(directoryOffset);
 
         InputStream is = null;
         try {
-            if (offset >= byteSource.getLength()) {
-                // Debug.debug("skipping invalid directory!");
+            if (directoryOffset >= byteSource.getLength()) {
                 return true;
             }
 
             is = byteSource.getInputStream();
-            skipBytes(is, offset);
+            skipBytes(is, directoryOffset);
 
             final List<TiffField> fields = new ArrayList<TiffField>();
 
@@ -168,19 +162,13 @@ public class TiffReader extends BinaryFi
                 }
             }
 
-            // Debug.debug("entryCount", entryCount);
-
             for (int i = 0; i < entryCount; i++) {
                 final int tag = read2Bytes("Tag", is, "Not a Valid TIFF File");
                 final int type = read2Bytes("Type", is, "Not a Valid TIFF File");
-                final int length = read4Bytes("Length", is, "Not a Valid TIFF File");
-
-                // Debug.debug("tag*", tag + " (0x" + Integer.toHexString(tag)
-                // + ")");
-
-                final byte valueOffsetBytes[] = readBytes("ValueOffset", is, 4,
+                final long count = 0xFFFFffffL & read4Bytes("Count", is, "Not a Valid TIFF File");
+                final byte offsetBytes[] = readBytes("Offset", is, 4,
                         "Not a Valid TIFF File");
-                final int valueOffset = toInt(valueOffsetBytes);
+                final long offset = 0xFFFFffffL & toInt(offsetBytes);
 
                 if (tag == 0) {
                     // skip invalid fields.
@@ -189,29 +177,39 @@ public class TiffReader extends BinaryFi
                     // which can cause OOM problems.
                     continue;
                 }
-
-                // if (keepField(tag, tags))
-                // {
-                final TiffField field = new TiffField(tag, dirType, type, length,
-                        valueOffset, valueOffsetBytes, getByteOrder());
-                field.setSortHint(i);
-
-                // Debug.debug("tagInfo", field.tagInfo);
-
+                
+                final FieldType fieldType;
                 try {
-                    field.fillInValue(byteSource);
-                } catch (final TiffValueOutsideFileBoundsException valueOutsideFileException) {
-                    if (strict) {
-                        final IOException ioEx = new IOException();
-                        ioEx.initCause(valueOutsideFileException);
-                        throw ioEx;
-                    } else {
-                        // corrupt field, ignore it
-                        continue;
+                    fieldType = FieldType.getFieldType(type);
+                } catch (ImageReadException imageReadEx) {
+                    // skip over unknown fields types, since we
+                    // can't calculate their size without
+                    // knowing their type
+                    continue;
+                }
+                final long valueLength = count * fieldType.getSize();
+                final byte[] value;
+                if (valueLength > TIFF_ENTRY_MAX_VALUE_LENGTH) {
+                    if ((offset < 0) ||
+                            (offset + valueLength) > byteSource.getLength()) {
+                        if (strict) {
+                            throw new IOException(
+                                    "Attempt to read byte range starting from " + offset + " " +
+                                            "of length " + valueLength + " " +
+                                            "which is outside the file's size of " +
+                                            byteSource.getLength());
+                        } else {
+                            // corrupt field, ignore it
+                            continue;
+                        }
                     }
+                    value = byteSource.getBlock(offset, (int)valueLength);
+                } else {
+                    value = offsetBytes;
                 }
 
-                // Debug.debug("\t" + "value", field.getValueDescription());
+                final TiffField field = new TiffField(tag, dirType, fieldType, count,
+                        offset, value, getByteOrder(), i);
 
                 fields.add(field);
 
@@ -220,12 +218,11 @@ public class TiffReader extends BinaryFi
                 }
             }
 
-            final int nextDirectoryOffset = read4Bytes("nextDirectoryOffset", is,
+            final long nextDirectoryOffset = 0xFFFFffffL & read4Bytes("nextDirectoryOffset", is,
                     "Not a Valid TIFF File");
-            // Debug.debug("nextDirectoryOffset", nextDirectoryOffset);
 
             final TiffDirectory directory = new TiffDirectory(dirType, fields,
-                    offset, nextDirectoryOffset);
+                    directoryOffset, nextDirectoryOffset);
 
             if (listener.readImageData()) {
                 if (directory.hasTiffImageData()) {
@@ -251,15 +248,15 @@ public class TiffReader extends BinaryFi
                         EXIF_TAG_INTEROP_OFFSET
                 };
                 final int[] directoryTypes = {
-                        TiffDirectory.DIRECTORY_TYPE_EXIF,
-                        TiffDirectory.DIRECTORY_TYPE_GPS,
-                        TiffDirectory.DIRECTORY_TYPE_INTEROPERABILITY
+                        TiffDirectoryConstants.DIRECTORY_TYPE_EXIF,
+                        TiffDirectoryConstants.DIRECTORY_TYPE_GPS,
+                        TiffDirectoryConstants.DIRECTORY_TYPE_INTEROPERABILITY
                 };
                 for (int i = 0; i < offsetFields.length; i++) {
                     final TagInfoLong offsetField = offsetFields[i];
                     TiffField field = directory.findField(offsetField);
                     if (field != null) {
-                        int subDirectoryOffset;
+                        long subDirectoryOffset;
                         int subDirectoryType;
                         boolean subDirectoryRead = false;
                         try {
@@ -503,11 +500,11 @@ public class TiffReader extends BinaryFi
     private JpegImageData getJpegRawImageData(final ByteSource byteSource,
             final TiffDirectory directory) throws ImageReadException, IOException {
         final ImageDataElement element = directory.getJpegRawImageDataElement();
-        final int offset = element.offset;
+        final long offset = element.offset;
         int length = element.length;
         // In case the length is not correct, adjust it and check if the last read byte actually is the end of the image
         if (offset + length > byteSource.getLength()) {
-            length = (int) byteSource.getLength() - offset;
+            length = (int) (byteSource.getLength() - offset);
         }
         final byte data[] = byteSource.getBlock(offset, length);
         // check if the last read byte is actually the end of the image data