pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1817064 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java
Date Mon, 04 Dec 2017 07:05:09 GMT
Author: tilman
Date: Mon Dec  4 07:05:09 2017
New Revision: 1817064

URL: http://svn.apache.org/viewvc?rev=1817064&view=rev
Log:
PDFBOX-4024: support YCbCr JPEGs, as suggested by Savan Patel

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=1817064&r1=1817063&r2=1817064&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 Mon Dec  4 07:05:09
2017
@@ -120,8 +120,7 @@ final class DCTFilter extends Filter
                         // already CMYK
                         break;
                     case 1:
-                        // TODO YCbCr
-                        LOG.warn("YCbCr JPEGs not implemented");
+                        raster = fromYCbCrtoCMYK(raster);
                         break;
                     case 2:
                         raster = fromYCCKtoCMYK(raster);
@@ -231,6 +230,44 @@ final class DCTFilter extends Filter
 
                 // naive RGB to CMYK
                 int cyan = 255 - r;
+                int magenta = 255 - g;
+                int yellow = 255 - b;
+
+                // update new raster
+                value[0] = cyan;
+                value[1] = magenta;
+                value[2] = yellow;
+                value[3] = (int)K;
+                writableRaster.setPixel(x, y, value);
+            }
+        }
+        return writableRaster;
+    }
+
+    private WritableRaster fromYCbCrtoCMYK(Raster raster)
+    {
+        WritableRaster writableRaster = raster.createCompatibleWritableRaster();
+
+        int[] value = new int[4];
+        for (int y = 0, height = raster.getHeight(); y < height; y++)
+        {
+            for (int x = 0, width = raster.getWidth(); x < width; x++)
+            {
+                raster.getPixel(x, y, value);
+
+                // 4-channels 0..255
+                float Y = value[0];
+                float Cb = value[1];
+                float Cr = value[2];
+                float K = value[3];
+
+                // YCbCr to RGB, see http://www.equasys.de/colorconversion.html
+                int r = clamp( (1.164f * (Y-16)) + (1.596f * (Cr - 128)) );
+                int g = clamp( (1.164f * (Y-16)) + (-0.392f * (Cb-128)) + (-0.813f * (Cr-128)));
+                int b = clamp( (1.164f * (Y-16)) + (2.017f * (Cb-128)));
+
+                // naive RGB to CMYK
+                int cyan = 255 - r;
                 int magenta = 255 - g;
                 int yellow = 255 - b;
 



Mime
View raw message