Index: trunk/src/com/drew/imaging/tiff/TiffReader.java
===================================================================
--- trunk/src/com/drew/imaging/tiff/TiffReader.java	(revision 8132)
+++ trunk/src/com/drew/imaging/tiff/TiffReader.java	(revision 10862)
@@ -1,4 +1,4 @@
 /*
- * Copyright 2002-2015 Drew Noakes
+ * Copyright 2002-2016 Drew Noakes
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
@@ -110,4 +110,5 @@
                                   final int tiffHeaderOffset) throws IOException
     {
+        Boolean resetByteOrder = null;
         try {
             // check for directories we've already visited to avoid stack overflows when recursive/cyclic directory structures exist
@@ -126,4 +127,14 @@
             // First two bytes in the IFD are the number of tags in this directory
             int dirTagCount = reader.getUInt16(ifdOffset);
+
+            // Some software modifies the byte order of the file, but misses some IFDs (such as makernotes).
+            // The entire test image repository doesn't contain a single IFD with more than 255 entries.
+            // Here we detect switched bytes that suggest this problem, and temporarily swap the byte order.
+            // This was discussed in GitHub issue #136.
+            if (dirTagCount > 0xFF && (dirTagCount & 0xFF) == 0) {
+                resetByteOrder = reader.isMotorolaByteOrder();
+                dirTagCount >>= 8;
+                reader.setMotorolaByteOrder(!reader.isMotorolaByteOrder());
+            }
 
             int dirLength = (2 + (12 * dirTagCount) + 4);
@@ -147,29 +158,30 @@
                 final TiffDataFormat format = TiffDataFormat.fromTiffFormatCode(formatCode);
 
+                // 4 bytes dictate the number of components in this tag's data
+                final long componentCount = reader.getUInt32(tagOffset + 4);
+
+                final long byteCount;
                 if (format == null) {
-                    // This error suggests that we are processing at an incorrect index and will generate
-                    // rubbish until we go out of bounds (which may be a while).  Exit now.
-                    handler.error("Invalid TIFF tag format code: " + formatCode);
-                    // TODO specify threshold as a parameter, or provide some other external control over this behaviour
-                    if (++invalidTiffFormatCodeCount > 5) {
-                        handler.error("Stopping processing as too many errors seen in TIFF IFD");
-                        return;
+                    Long byteCountOverride = handler.tryCustomProcessFormat(tagId, formatCode, componentCount);
+                    if (byteCountOverride == null) {
+                        // This error suggests that we are processing at an incorrect index and will generate
+                        // rubbish until we go out of bounds (which may be a while).  Exit now.
+                        handler.error(String.format("Invalid TIFF tag format code %d for tag 0x%04X", formatCode, tagId));
+                        // TODO specify threshold as a parameter, or provide some other external control over this behaviour
+                        if (++invalidTiffFormatCodeCount > 5) {
+                            handler.error("Stopping processing as too many errors seen in TIFF IFD");
+                            return;
+                        }
+                        continue;
                     }
-                    continue;
-                }
-
-                // 4 bytes dictate the number of components in this tag's data
-                final int componentCount = reader.getInt32(tagOffset + 4);
-                if (componentCount < 0) {
-                    handler.error("Negative TIFF tag component count");
-                    continue;
-                }
-
-                final int byteCount = componentCount * format.getComponentSizeBytes();
-
-                final int tagValueOffset;
+                    byteCount = byteCountOverride;
+                } else {
+                    byteCount = componentCount * format.getComponentSizeBytes();
+                }
+
+                final long tagValueOffset;
                 if (byteCount > 4) {
                     // If it's bigger than 4 bytes, the dir entry contains an offset.
-                    final int offsetVal = reader.getInt32(tagOffset + 8);
+                    final long offsetVal = reader.getUInt32(tagOffset + 8);
                     if (offsetVal + byteCount > reader.getLength()) {
                         // Bogus pointer offset and / or byteCount value
@@ -195,14 +207,20 @@
                 }
 
-                //
-                // Special handling for tags that point to other IFDs
-                //
-                if (byteCount == 4 && handler.isTagIfdPointer(tagId)) {
-                    final int subDirOffset = tiffHeaderOffset + reader.getInt32(tagValueOffset);
-                    processIfd(handler, reader, processedIfdOffsets, subDirOffset, tiffHeaderOffset);
-                } else {
-                    if (!handler.customProcessTag(tagValueOffset, processedIfdOffsets, tiffHeaderOffset, reader, tagId, byteCount)) {
-                        processTag(handler, tagId, tagValueOffset, componentCount, formatCode, reader);
+                // Some tags point to one or more additional IFDs to process
+                boolean isIfdPointer = false;
+                if (byteCount == 4 * componentCount) {
+                    for (int i = 0; i < componentCount; i++) {
+                        if (handler.tryEnterSubIfd(tagId)) {
+                            isIfdPointer = true;
+                            int subDirOffset = tiffHeaderOffset + reader.getInt32((int) (tagValueOffset + i * 4));
+                            processIfd(handler, reader, processedIfdOffsets, subDirOffset, tiffHeaderOffset);
+                        }
                     }
+                }
+
+                // If it wasn't an IFD pointer, allow custom tag processing to occur
+                if (!isIfdPointer && !handler.customProcessTag((int) tagValueOffset, processedIfdOffsets, tiffHeaderOffset, reader, tagId, (int) byteCount)) {
+                    // If no custom processing occurred, process the tag in the standard fashion
+                    processTag(handler, tagId, (int) tagValueOffset, (int) componentCount, formatCode, reader);
                 }
             }
@@ -229,4 +247,6 @@
         } finally {
             handler.endingIFD();
+            if (resetByteOrder != null)
+                reader.setMotorolaByteOrder(resetByteOrder);
         }
     }
@@ -350,5 +370,5 @@
                 break;
             default:
-                handler.error(String.format("Unknown format code %d for tag %d", formatCode, tagId));
+                handler.error(String.format("Invalid TIFF tag format code %d for tag 0x%04X", formatCode, tagId));
         }
     }
