pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jahew...@apache.org
Subject svn commit: r1570317 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java
Date Thu, 20 Feb 2014 19:20:26 GMT
Author: jahewson
Date: Thu Feb 20 19:20:26 2014
New Revision: 1570317

URL: http://svn.apache.org/r1570317
Log:
Fixed issue with pink images in PDFBOX-1893

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java?rev=1570317&r1=1570316&r2=1570317&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java Thu Feb 20 19:20:26
2014
@@ -16,6 +16,7 @@
  */
 package org.apache.pdfbox.filter;
 
+import java.awt.image.BufferedImage;
 import java.awt.image.DataBufferByte;
 import java.awt.image.Raster;
 import java.awt.image.WritableRaster;
@@ -66,9 +67,9 @@ public final class DCTFilter implements 
                     "a suitable JAI I/O image filter is not installed");
         }
 
-        ImageInputStream iis = ImageIO.createImageInputStream(input);
-        reader.setInput(iis);
-        Raster raster = reader.readRaster(0, null);
+        // I'd planned to use ImageReader#readRaster but it is buggy
+        BufferedImage hack = ImageIO.read(input);
+        Raster raster = hack.getRaster();
 
         // special handling for 4-component images
         if (raster.getNumBands() == 4)
@@ -96,6 +97,11 @@ public final class DCTFilter implements 
                 case 2: raster = fromYCCKtoCMYK(raster); break;
             }
         }
+        else if (raster.getNumBands() == 3)
+        {
+            // BGR to RGB
+            raster = fromBGRtoRGB(raster);
+        }
 
         DataBufferByte dataBuffer = (DataBufferByte)raster.getDataBuffer();
         output.write(dataBuffer.getData());
@@ -127,7 +133,6 @@ public final class DCTFilter implements 
     private WritableRaster fromYCCKtoCMYK(Raster raster) throws IOException
     {
         WritableRaster writableRaster = raster.createCompatibleWritableRaster();
-        writableRaster.setRect(raster);
 
         int[] value = new int[4];
         for (int y = 0, height = raster.getHeight(); y < height; y++)
@@ -152,7 +157,7 @@ public final class DCTFilter implements 
                 int magenta = 255 - g;
                 int yellow = 255 - b;
 
-                // update raster in-place
+                // update new raster
                 value[0] = cyan;
                 value[1] = magenta;
                 value[2] = yellow;
@@ -163,6 +168,28 @@ public final class DCTFilter implements 
         return writableRaster;
     }
 
+    // converts from BGR to RGB
+    private WritableRaster fromBGRtoRGB(Raster raster) throws IOException
+    {
+        WritableRaster writableRaster = raster.createCompatibleWritableRaster();
+
+        int[] bgr = new int[3];
+        int[] rgb = new int[3];
+        for (int y = 0, height = raster.getHeight(); y < height; y++)
+        {
+            for (int x = 0, width = raster.getWidth(); x < width; x++)
+            {
+                raster.getPixel(x, y, bgr);
+                rgb[0] = bgr[2];
+                rgb[1] = bgr[1];
+                rgb[2] = bgr[0];
+                writableRaster.setPixel(x, y, rgb);
+            }
+        }
+
+        return writableRaster;
+    }
+
     // clamps value to 0-255 range
     private int clamp(float value)
     {



Mime
View raw message