You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2015/09/07 18:52:30 UTC

svn commit: r1701652 - /pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/Predictor.java

Author: tilman
Date: Mon Sep  7 16:52:29 2015
New Revision: 1701652

URL: http://svn.apache.org/r1701652
Log:
PDFBOX-2962: handle TIFF predictor for bpc 2 and 4 / optimize existing predictor code

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

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/Predictor.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/Predictor.java?rev=1701652&r1=1701651&r2=1701652&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/Predictor.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/Predictor.java Mon Sep  7 16:52:29 2015
@@ -51,15 +51,14 @@ public class Predictor
                 if (predictor >= 10)
                 {
                     // PNG predictor; each row starts with predictor type (0, 1, 2, 3, 4)
-                    linepredictor = in.read();// read per line predictor
+                    // read per line predictor
+                    linepredictor = in.read();
                     if (linepredictor == -1)
                     {
                         return;
                     }
-                    else
-                    {
-                        linepredictor += 10; // add 10 to tread value 0 as 10, 1 as 11, ...
-                    }
+                    // add 10 to tread value 0 as 10, 1 as 11, ...
+                    linepredictor += 10;
                 }
 
                 // read line
@@ -72,31 +71,33 @@ public class Predictor
                 // do prediction as specified in PNG-Specification 1.2
                 switch (linepredictor)
                 {
-                    case 2:// PRED TIFF SUB
-                        // TODO decode tiff with bitsPerComponent < 8;
-                        // e.g. for 4 bpc each nibble must be subtracted separately
+                    case 2:
+                        // PRED TIFF SUB
+                        if (bitsPerComponent == 8)
+                        {
+                            // for 8 bits per component it is the same algorithm as PRED SUB of PNG format
+                            for (int p = bytesPerPixel; p < rowlength; p++)
+                            {
+                                int sub = actline[p] & 0xff;
+                                int left = actline[p - bytesPerPixel] & 0xff;
+                                actline[p] = (byte) (sub + left);
+                            }
+                            break;
+                        }
                         if (bitsPerComponent == 16)
                         {
-                            for (int p = 0; p < rowlength; p += 2)
+                            for (int p = bytesPerPixel; p < rowlength; p += 2)
                             {
                                 int sub = ((actline[p] & 0xff) << 8) + (actline[p + 1] & 0xff);
-                                int left = p - bytesPerPixel >= 0
-                                        ? (((actline[p - bytesPerPixel] & 0xff) << 8)
-                                        + (actline[p - bytesPerPixel + 1] & 0xff))
-                                        : 0;
+                                int left = (((actline[p - bytesPerPixel] & 0xff) << 8)
+                                        + (actline[p - bytesPerPixel + 1] & 0xff));
                                 actline[p] = (byte) (((sub + left) >> 8) & 0xff);
                                 actline[p + 1] = (byte) ((sub + left) & 0xff);
                             }
                             break;
                         }
-                        if (bitsPerComponent == 1)
+                        if (bitsPerComponent == 1 && colors == 1)
                         {
-                            if (colors != 1)
-                            {
-                                // possible, but unlikely 
-                                throw new IOException("TIFF-Predictor with 1 bit per component and " + 
-                                        colors + " is not supported; please open JIRA issue with sample PDF");
-                            }
                             // bytesPerPixel cannot be used:
                             // "A row shall occupy a whole number of bytes, rounded up if necessary.
                             // Samples and their components shall be packed into bytes 
@@ -106,23 +107,20 @@ public class Predictor
                                 for (int bit = 7; bit >= 0; --bit)
                                 {
                                     int sub = (actline[p] >> bit) & 1;
-                                    int left;
                                     if (p == 0 && bit == 7)
                                     {
-                                        left = 0;
+                                        continue;
+                                    }
+                                    int left;
+                                    if (bit == 7)
+                                    {
+                                        // use bit #0 from previous byte
+                                        left = actline[p - 1] & 1;
                                     }
                                     else
                                     {
-                                        if (bit == 7)
-                                        {
-                                            // use bit #0 from previous byte
-                                            left = actline[p - 1] & 1;
-                                        }
-                                        else
-                                        {
-                                            // use "previous" bit
-                                            left = (actline[p] >> (bit + 1)) & 1;
-                                        }
+                                        // use "previous" bit
+                                        left = (actline[p] >> (bit + 1)) & 1;
                                     }
                                     if (((sub + left) & 1) == 0)
                                     {
@@ -138,31 +136,35 @@ public class Predictor
                             }
                             break;
                         }
-                        if (bitsPerComponent != 8)
-                        {
-                            throw new IOException("TIFF-Predictor with " + bitsPerComponent
-                                    + " bits per component not supported");
-                        }
-                        // for 8 bits per component it is the same algorithm as PRED SUB of PNG format
-                        for (int p = 0; p < rowlength; p++)
-                        {
-                            int sub = actline[p] & 0xff;
-                            int left = p - bytesPerPixel >= 0 ? actline[p - bytesPerPixel] & 0xff : 0;
-                            actline[p] = (byte) (sub + left);
+                        // everything else, i.e. bpc 2 and 4, but has been tested for bpc 1 and 8 too
+                        int elements = columns * colors;
+                        for (int p = colors; p < elements; ++p)
+                        {
+                            int bytePosSub = p * bitsPerComponent / 8;
+                            int bitPosSub = 8 - p * bitsPerComponent % 8 - bitsPerComponent;
+                            int bytePosLeft = (p - colors) * bitsPerComponent / 8;
+                            int bitPosLeft = 8 - (p - colors) * bitsPerComponent % 8 - bitsPerComponent;
+
+                            int sub = getBitSeq(actline[bytePosSub], bitPosSub, bitsPerComponent);
+                            int left = getBitSeq(actline[bytePosLeft], bitPosLeft, bitsPerComponent);
+                            actline[bytePosSub] = (byte) calcSetBitSeq(actline[bytePosSub], bitPosSub, bitsPerComponent, sub + left);
                         }
                         break;
-                    case 10:// PRED NONE
+                    case 10:
+                        // PRED NONE
                         // do nothing
                         break;
-                    case 11:// PRED SUB
-                        for (int p = 0; p < rowlength; p++)
+                    case 11:
+                        // PRED SUB
+                        for (int p = bytesPerPixel; p < rowlength; p++)
                         {
                             int sub = actline[p];
-                            int left = p - bytesPerPixel >= 0 ? actline[p - bytesPerPixel] : 0;
+                            int left = actline[p - bytesPerPixel];
                             actline[p] = (byte) (sub + left);
                         }
                         break;
-                    case 12:// PRED UP
+                    case 12:
+                        // PRED UP
                         for (int p = 0; p < rowlength; p++)
                         {
                             int up = actline[p] & 0xff;
@@ -170,16 +172,18 @@ public class Predictor
                             actline[p] = (byte) ((up + prior) & 0xff);
                         }
                         break;
-                    case 13:// PRED AVG
+                    case 13:
+                        // PRED AVG
                         for (int p = 0; p < rowlength; p++)
                         {
                             int avg = actline[p] & 0xff;
                             int left = p - bytesPerPixel >= 0 ? actline[p - bytesPerPixel] & 0xff : 0;
                             int up = lastline[p] & 0xff;
-                            actline[p] = (byte) ((avg + (int) Math.floor((left + up) / 2)) & 0xff);
+                            actline[p] = (byte) ((avg + (left + up) / 2) & 0xff);
                         }
                         break;
-                    case 14:// PRED PAETH
+                    case 14:
+                        // PRED PAETH
                         for (int p = 0; p < rowlength; p++)
                         {
                             int paeth = actline[p] & 0xff;
@@ -213,5 +217,21 @@ public class Predictor
             }
         }
     }
+    
+    // get value from bit interval from a byte
+    static int getBitSeq(int by, int startBit, int bitSize)
+    {
+        int mask = ((1 << bitSize) - 1);
+        return (by >>> startBit) & mask;
+    }
+
+    // set value in a bit interval and return that value
+    static int calcSetBitSeq(int by, int startBit, int bitSize, int val)
+    {
+        int mask = ((1 << bitSize) - 1);
+        int truncatedVal = val & mask;
+        mask = ~(mask << startBit);
+        return (by & mask) | (truncatedVal << startBit);
+    }
 
 }