pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1842325 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/JPXFilter.java
Date Sat, 29 Sep 2018 11:50:28 GMT
Author: tilman
Date: Sat Sep 29 11:50:28 2018
New Revision: 1842325

URL: http://svn.apache.org/viewvc?rev=1842325&view=rev
Log:
PDFBOX-4326: override colorspace to avoid wrong number of 3 colors; provide passing integer
RGB raster for possible future use

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/JPXFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/JPXFilter.java?rev=1842325&r1=1842324&r2=1842325&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/JPXFilter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/JPXFilter.java Sat Sep 29 11:50:28
2018
@@ -15,11 +15,13 @@
  * limitations under the License.
  */
 package org.apache.pdfbox.filter;
-
+import java.awt.color.ColorSpace;
 import java.awt.image.BufferedImage;
 import java.awt.image.DataBuffer;
 import java.awt.image.DataBufferByte;
 import java.awt.image.DataBufferUShort;
+import java.awt.image.IndexColorModel;
+import java.awt.image.MultiPixelPackedSampleModel;
 import java.awt.image.WritableRaster;
 import java.io.IOException;
 import java.io.InputStream;
@@ -49,6 +51,9 @@ import org.apache.pdfbox.pdmodel.graphic
  */
 public final class JPXFilter extends Filter
 {
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public DecodeResult decode(InputStream encoded, OutputStream decoded, COSDictionary
             parameters, int index, DecodeOptions options) throws IOException
@@ -74,6 +79,23 @@ public final class JPXFilter extends Fil
                 }
                 return result;
 
+            case DataBuffer.TYPE_INT:
+                // not yet used (as of October 2018) but works as fallback
+                // if we decide to convert to BufferedImage.TYPE_INT_RGB
+                int[] ar = new int[raster.getNumBands()];
+                for (int y = 0; y < image.getHeight(); ++y)
+                {
+                    for (int x = 0; x < image.getWidth(); ++x)
+                    {
+                        raster.getPixel(x, y, ar);
+                        for (int i = 0; i < ar.length; ++i)
+                        {
+                            decoded.write(ar[i]);
+                        }
+                    }
+                }
+                return result;
+
             default:
                 throw new IOException("Data type " + raster.getDataBuffer().getDataType()
+ " not implemented");
         }
@@ -133,7 +155,21 @@ public final class JPXFilter extends Fil
             // extract embedded color space
             if (!parameters.containsKey(COSName.COLORSPACE))
             {
-                result.setColorSpace(new PDJPXColorSpace(image.getColorModel().getColorSpace()));
+                if (image.getSampleModel() instanceof MultiPixelPackedSampleModel &&
+                    image.getColorModel().getPixelSize() == 1 &&
+                    image.getRaster().getNumBands() == 1 && 
+                    image.getColorModel() instanceof IndexColorModel)
+                {
+                    // PDFBOX-4326:
+                    // force CS_GRAY colorspace because colorspace in IndexColorModel
+                    // has 3 colors despite that there is only 1 color per pixel
+                    // in raster
+                    result.setColorSpace(new PDJPXColorSpace(ColorSpace.getInstance(ColorSpace.CS_GRAY)));
+                }
+                else
+                {
+                    result.setColorSpace(new PDJPXColorSpace(image.getColorModel().getColorSpace()));
+                }
             }
 
             return image;
@@ -144,6 +180,9 @@ public final class JPXFilter extends Fil
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     protected void encode(InputStream input, OutputStream encoded, COSDictionary parameters)
             throws IOException



Mime
View raw message