]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
bugzilla #40676: patch 29132 with changes, support for sRGB and iCCP chunks
authorLuis Bernardo <lbernardo@apache.org>
Sat, 20 Oct 2012 23:50:15 +0000 (23:50 +0000)
committerLuis Bernardo <lbernardo@apache.org>
Sat, 20 Oct 2012 23:50:15 +0000 (23:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1400536 13f79535-47bb-0310-9956-ffa450edef68

lib/xmlgraphics-commons-1.5svn.jar
src/java/org/apache/fop/render/pdf/AbstractImageAdapter.java
src/java/org/apache/fop/render/pdf/ImageRawPNGAdapter.java
test/java/org/apache/fop/render/pdf/ImageRawPNGAdapterTestCase.java

index e0cf7b10c2d9663549b999a0dcb07cbaa4b40d71..317ab33309fc42c501acda78e26003b7a3213a5b 100644 (file)
Binary files a/lib/xmlgraphics-commons-1.5svn.jar and b/lib/xmlgraphics-commons-1.5svn.jar differ
index 46e8ebe9554db77fd085d56c52026742aa7d2f30..2840ea1e36137e9945592dbe310cc5dea6eca6b0 100644 (file)
@@ -88,11 +88,12 @@ public abstract class AbstractImageAdapter implements PDFImage {
 
     /** {@inheritDoc} */
     public void setup(PDFDocument doc) {
-
         ICC_Profile prof = getEffectiveICCProfile();
         PDFDeviceColorSpace pdfCS = toPDFColorSpace(getImageColorSpace());
         if (prof != null) {
             pdfICCStream = setupColorProfile(doc, prof, pdfCS);
+        } else if (issRGB()) {
+          pdfICCStream = setupsRGBColorProfile(doc);
         }
         if (doc.getProfile().getPDFAMode().isPDFA1LevelB()) {
             if (pdfCS != null
@@ -116,6 +117,34 @@ public abstract class AbstractImageAdapter implements PDFImage {
         return image.getICCProfile();
     }
 
+    protected boolean issRGB() {
+        return false;
+    }
+
+    private static PDFICCStream getDefaultsRGBICCStream(PDFICCBasedColorSpace cs, PDFDocument doc, 
+            String profileDesc) {
+        if (cs == null) {
+            if (profileDesc == null || !profileDesc.startsWith("sRGB")) {
+                log.warn("The default sRGB profile was indicated,"
+                    + " but the profile description does not match what was expected: "
+                    + profileDesc);
+            }
+            //It's the default sRGB profile which we mapped to DefaultRGB in PDFRenderer
+            cs = (PDFICCBasedColorSpace)doc.getResources().getColorSpace(new PDFName("DefaultRGB"));
+        }
+        if (cs == null) {
+            // sRGB hasn't been set up for the PDF document
+            // so install but don't set to DefaultRGB
+            cs = PDFICCBasedColorSpace.setupsRGBColorSpace(doc);
+        }
+        return cs.getICCStream();
+    }
+
+    private static PDFICCStream setupsRGBColorProfile(PDFDocument doc) {
+        PDFICCBasedColorSpace cs = doc.getResources().getICCColorSpaceByProfileName("sRGB");
+        return getDefaultsRGBICCStream(cs, doc, "sRGB");
+    }
+
     private static PDFICCStream setupColorProfile(PDFDocument doc,
                 ICC_Profile prof, PDFDeviceColorSpace pdfCS) {
         boolean defaultsRGB = ColorProfileUtil.isDefaultsRGB(prof);
@@ -134,22 +163,7 @@ public abstract class AbstractImageAdapter implements PDFImage {
                 pdfICCStream = cs.getICCStream();
             }
         } else {
-            if (cs == null) {
-                if (desc == null || !desc.startsWith("sRGB")) {
-                    log.warn("The default sRGB profile was indicated,"
-                            + " but the profile description does not match what was expected: "
-                            + desc);
-                }
-                //It's the default sRGB profile which we mapped to DefaultRGB in PDFRenderer
-                cs = (PDFICCBasedColorSpace)doc.getResources().getColorSpace(
-                        new PDFName("DefaultRGB"));
-            }
-            if (cs == null) {
-                // sRGB hasn't been set up for the PDF document
-                // so install but don't set to DefaultRGB
-                cs = PDFICCBasedColorSpace.setupsRGBColorSpace(doc);
-            }
-            pdfICCStream = cs.getICCStream();
+            pdfICCStream = getDefaultsRGBICCStream(cs, doc, desc);
         }
         return pdfICCStream;
     }
index b9f5e1d28d6c5a1a8e6d90b20107da61ac3e132a..9742036b79b9161d27b729a6e794a89af08ddccb 100644 (file)
@@ -49,7 +49,7 @@ import org.apache.fop.pdf.PDFDocument;
 import org.apache.fop.pdf.PDFFilter;\r
 import org.apache.fop.pdf.PDFFilterException;\r
 import org.apache.fop.pdf.PDFFilterList;\r
-import org.apache.fop.pdf.PDFICCStream;\r
+import org.apache.fop.pdf.PDFName;\r
 import org.apache.fop.pdf.PDFReference;\r
 \r
 public class ImageRawPNGAdapter extends AbstractImageAdapter {\r
@@ -57,7 +57,11 @@ public class ImageRawPNGAdapter extends AbstractImageAdapter {
     /** logging instance */\r
     private static Log log = LogFactory.getLog(ImageRawPNGAdapter.class);\r
 \r
-    private PDFICCStream pdfICCStream;\r
+    private static final PDFName RI_PERCEPTUAL = new PDFName("Perceptual");\r
+    private static final PDFName RI_RELATIVE_COLORIMETRIC = new PDFName("RelativeColorimetric");\r
+    private static final PDFName RI_SATURATION = new PDFName("Saturation");\r
+    private static final PDFName RI_ABSOLUTE_COLORIMETRIC = new PDFName("AbsoluteColorimetric");\r
+\r
     private PDFFilter pdfFilter;\r
     private String maskRef;\r
     private PDFReference softMask;\r
@@ -240,21 +244,42 @@ public class ImageRawPNGAdapter extends AbstractImageAdapter {
         }\r
     }\r
 \r
-    /** {@inheritDoc} */\r
-    public PDFICCStream getICCStream() {\r
-        return pdfICCStream;\r
-    }\r
-\r
     /** {@inheritDoc} */\r
     public String getFilterHint() {\r
         return PDFFilterList.PRECOMPRESSED_FILTER;\r
     }\r
 \r
     public void populateXObjectDictionary(PDFDictionary dict) {\r
+        int renderingIntent = ((ImageRawPNG) image).getRenderingIntent();\r
+        if (renderingIntent != -1) {\r
+            switch (renderingIntent) {\r
+            case 0:\r
+                dict.put("Intent", RI_PERCEPTUAL);\r
+                break;\r
+            case 1:\r
+                dict.put("Intent", RI_RELATIVE_COLORIMETRIC);\r
+                break;\r
+            case 2:\r
+                dict.put("Intent", RI_SATURATION);\r
+                break;\r
+            case 3:\r
+                dict.put("Intent", RI_ABSOLUTE_COLORIMETRIC);\r
+                break;\r
+            default:\r
+                // ignore\r
+            }\r
+        }\r
         ColorModel cm = ((ImageRawPNG) image).getColorModel();\r
         if (cm instanceof IndexColorModel) {\r
             IndexColorModel icm = (IndexColorModel) cm;\r
             super.populateXObjectDictionaryForIndexColorModel(dict, icm);\r
         }\r
     }\r
+\r
+    protected boolean issRGB() {\r
+        if (((ImageRawPNG) image).getRenderingIntent() != -1) {\r
+            return true;\r
+        }\r
+        return false;\r
+    }\r
 }\r
index 885821f6613fac4297c21991c3f52d34ea1538c2..9577e2e018d95dca77a34e57afb0e5afb3949051 100644 (file)
 
 package org.apache.fop.render.pdf;
 
+import java.awt.color.ColorSpace;
+import java.awt.color.ICC_Profile;
 import java.awt.image.ComponentColorModel;
 import java.awt.image.IndexColorModel;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.util.zip.Deflater;
-import java.util.zip.DeflaterOutputStream;
 
 import org.junit.Test;
 
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
 import org.apache.xmlgraphics.image.loader.ImageSize;
 import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG;
+import org.apache.xmlgraphics.java2d.color.profile.ColorProfileUtil;
 
 import org.apache.fop.pdf.FlateFilter;
 import org.apache.fop.pdf.PDFAMode;
+import org.apache.fop.pdf.PDFDictionary;
 import org.apache.fop.pdf.PDFDocument;
+import org.apache.fop.pdf.PDFICCBasedColorSpace;
+import org.apache.fop.pdf.PDFICCStream;
+import org.apache.fop.pdf.PDFName;
 import org.apache.fop.pdf.PDFProfile;
+import org.apache.fop.pdf.PDFResources;
 import org.apache.fop.render.RawPNGTestUtil;
 
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
 public class ImageRawPNGAdapterTestCase {
 
     @Test
@@ -56,6 +63,7 @@ public class ImageRawPNGAdapterTestCase {
         ImageSize is = RawPNGTestUtil.getImageSize();
 
         when(irpng.getColorModel()).thenReturn(cm);
+        when(irpng.getRenderingIntent()).thenReturn(-1);
         // when(cm.hasAlpha()).thenReturn(false);
         when(doc.getProfile()).thenReturn(profile);
         when(profile.getPDFAMode()).thenReturn(PDFAMode.PDFA_1A);
@@ -75,6 +83,7 @@ public class ImageRawPNGAdapterTestCase {
         ImageSize is = RawPNGTestUtil.getImageSize();
 
         when(irpng.getColorModel()).thenReturn(cm);
+        when(irpng.getRenderingIntent()).thenReturn(-1);
         when(cm.getNumComponents()).thenReturn(3);
         // when(cm.hasAlpha()).thenReturn(false);
         when(doc.getProfile()).thenReturn(profile);
@@ -117,6 +126,7 @@ public class ImageRawPNGAdapterTestCase {
         ImageSize is = RawPNGTestUtil.getImageSize();
 
         when(irpng.getColorModel()).thenReturn(cm);
+        when(irpng.getRenderingIntent()).thenReturn(-1);
         when(cm.getNumComponents()).thenReturn(numComponents);
         // when(cm.hasAlpha()).thenReturn(false);
         when(doc.getProfile()).thenReturn(profile);
@@ -139,4 +149,55 @@ public class ImageRawPNGAdapterTestCase {
         }
     }
 
+    @Test
+    public void testPopulateXObjectDictionaryWithComponentColorModelAndsRGB() {
+        ComponentColorModel cm = mock(ComponentColorModel.class);
+        ImageRawPNG irpng = mock(ImageRawPNG.class);
+        PDFDictionary pdfDic = mock(PDFDictionary.class);
+        ImageRawPNGAdapter irpnga = new ImageRawPNGAdapter(irpng, "mock");
+
+        when(irpng.getColorModel()).thenReturn(cm);
+        when(irpng.getRenderingIntent()).thenReturn(0);
+        irpnga.populateXObjectDictionary(pdfDic);
+        verify(pdfDic).put("Intent", new PDFName("Perceptual"));
+        when(irpng.getRenderingIntent()).thenReturn(1);
+        irpnga.populateXObjectDictionary(pdfDic);
+        verify(pdfDic).put("Intent", new PDFName("RelativeColorimetric"));
+        when(irpng.getRenderingIntent()).thenReturn(2);
+        irpnga.populateXObjectDictionary(pdfDic);
+        verify(pdfDic).put("Intent", new PDFName("Saturation"));
+        when(irpng.getRenderingIntent()).thenReturn(3);
+        irpnga.populateXObjectDictionary(pdfDic);
+        verify(pdfDic).put("Intent", new PDFName("AbsoluteColorimetric"));
+    }
+
+    @Test
+    public void testRenderingIntentImpliessRGBColorProfile() {
+        ComponentColorModel cm = mock(ComponentColorModel.class);
+        ImageRawPNG irpng = mock(ImageRawPNG.class);
+        PDFDocument doc = mock(PDFDocument.class);
+        PDFProfile profile = mock(PDFProfile.class);
+        PDFResources resources = mock(PDFResources.class);
+        PDFICCBasedColorSpace cs = mock(PDFICCBasedColorSpace.class);
+        PDFICCStream stream = mock(PDFICCStream.class);
+        ICC_Profile iccprof = ICC_Profile.getInstance(ColorSpace.CS_sRGB);
+        ImageRawPNGAdapter irpnga = new ImageRawPNGAdapter(irpng, "mock");
+        ImageSize is = RawPNGTestUtil.getImageSize();
+
+        when(irpng.getColorModel()).thenReturn(cm);
+        when(irpng.getRenderingIntent()).thenReturn(0);
+        when(cm.getNumComponents()).thenReturn(3);
+        // when(cm.hasAlpha()).thenReturn(false);
+        when(doc.getProfile()).thenReturn(profile);
+        when(doc.getResources()).thenReturn(resources);
+        when(resources.getICCColorSpaceByProfileName("sRGB")).thenReturn(cs);
+        when(profile.getPDFAMode()).thenReturn(PDFAMode.PDFA_1A);
+        when(irpng.getSize()).thenReturn(is);
+        when(cs.getICCStream()).thenReturn(stream);
+        when(stream.getICCProfile()).thenReturn(iccprof);
+
+        irpnga.setup(doc);
+        PDFICCStream iccStream = irpnga.getICCStream();
+        assertTrue(ColorProfileUtil.isDefaultsRGB(iccStream.getICCProfile()));
+    }
 }