pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1576821 - /pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java
Date Wed, 12 Mar 2014 17:05:29 GMT
Author: tilman
Date: Wed Mar 12 17:05:28 2014
New Revision: 1576821

URL: http://svn.apache.org/r1576821
Log:
PDFBOX-1977: Check whether chunk needs adjusting before writing EOD

Modified:
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java?rev=1576821&r1=1576820&r2=1576821&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java Wed Mar
12 17:05:28 2014
@@ -105,22 +105,8 @@ public class LZWFilter implements Filter
                         result.write(newData);
                         codeTable.add(newData);
                     }
-                    if (codeTable.size() >= 2047)
-                    {
-                        chunk = 12;
-                    }
-                    else if (codeTable.size() >= 1023)
-                    {
-                        chunk = 11;
-                    }
-                    else if (codeTable.size() >= 511)
-                    {
-                        chunk = 10;
-                    }
-                    else
-                    {
-                        chunk = 9;
-                    }
+                    
+                    chunk = calculateChunk(codeTable.size());
                     prevCommand = nextCommand;
                 }
             }
@@ -168,6 +154,7 @@ public class LZWFilter implements Filter
                 if (newFoundCode == -1)
                 {
                     // use previous
+                    chunk = calculateChunk(codeTable.size() - 1);
                     out.writeBits(foundCode, chunk);
                     // create new table entry
                     codeTable.add(inputPattern);
@@ -191,29 +178,22 @@ public class LZWFilter implements Filter
                     foundCode = newFoundCode;
                 }
             }
-            if (codeTable.size() - 1 >= 2047)
-            {
-                chunk = 12;
-            }
-            else if (codeTable.size() - 1 >= 1023)
-            {
-                chunk = 11;
-            }
-            else if (codeTable.size() - 1 >= 511)
-            {
-                chunk = 10;
-            }
-            else
-            {
-                chunk = 9;
-            }
         }
         if (foundCode != -1)
         {
+            chunk = calculateChunk(codeTable.size() - 1);
             out.writeBits(foundCode, chunk);
         }
+
+        // PPDFBOX-1977: the decoder wouldn't know that the encoder would output 
+        // an EOD as code, so he would have increased his own code table and 
+        // possibly adjusted the chunk. Therefore, the encoder must behave as 
+        // if the code table had just grown and thus it must be checked it is
+        // needed to adjust the chunk, based on an increased table size parameter
+        chunk = calculateChunk(codeTable.size());
+
         out.writeBits(EOD, chunk);
-        out.writeBits(0, 7);
+        out.writeBits(0, 7); // pad with 0
         out.flush(); // must do or file will be empty :-(
         codeTable.clear();
     }
@@ -275,4 +255,27 @@ public class LZWFilter implements Filter
         codeTable.add(null); // 257 CLEAR_TABLE
     }
 
+    /**
+     * Calculate the appropriate chunk size
+     *
+     * @param tabSize the size of the code table
+     *
+     * @return a value between 9 and 12
+     */
+    private int calculateChunk(int tabSize)
+    {
+        if (tabSize >= 2047)
+        {
+            return 12;
+        }
+        if (tabSize >= 1023)
+        {
+            return 11;
+        }
+        if (tabSize >= 511)
+        {
+            return 10;
+        }
+        return 9;
+    }
 }



Mime
View raw message