View | Details | Raw Unified | Return to bug 40676
Collapse All | Expand All

(-)src/java/org/apache/fop/render/pdf/AbstractImageAdapter.java (-17 / +31 lines)
Lines 88-98 Link Here
88
88
89
    /** {@inheritDoc} */
89
    /** {@inheritDoc} */
90
    public void setup(PDFDocument doc) {
90
    public void setup(PDFDocument doc) {
91
92
        ICC_Profile prof = getEffectiveICCProfile();
91
        ICC_Profile prof = getEffectiveICCProfile();
93
        PDFDeviceColorSpace pdfCS = toPDFColorSpace(getImageColorSpace());
92
        PDFDeviceColorSpace pdfCS = toPDFColorSpace(getImageColorSpace());
94
        if (prof != null) {
93
        if (prof != null) {
95
            pdfICCStream = setupColorProfile(doc, prof, pdfCS);
94
            pdfICCStream = setupColorProfile(doc, prof, pdfCS);
95
        } else if (issRGB()) {
96
          pdfICCStream = setupsRGBColorProfile(doc);
96
        }
97
        }
97
        if (doc.getProfile().getPDFAMode().isPDFA1LevelB()) {
98
        if (doc.getProfile().getPDFAMode().isPDFA1LevelB()) {
98
            if (pdfCS != null
99
            if (pdfCS != null
Lines 116-121 Link Here
116
        return image.getICCProfile();
117
        return image.getICCProfile();
117
    }
118
    }
118
119
120
    protected boolean issRGB() {
121
        return false;
122
    }
123
124
    private static PDFICCStream getDefaultsRGBICCStream(PDFICCBasedColorSpace cs, PDFDocument doc, 
125
            String profileDesc) {
126
        if (cs == null) {
127
            if (profileDesc == null || !profileDesc.startsWith("sRGB")) {
128
                log.warn("The default sRGB profile was indicated,"
129
                    + " but the profile description does not match what was expected: "
130
                    + profileDesc);
131
            }
132
            //It's the default sRGB profile which we mapped to DefaultRGB in PDFRenderer
133
            cs = (PDFICCBasedColorSpace)doc.getResources().getColorSpace(new PDFName("DefaultRGB"));
134
        }
135
        if (cs == null) {
136
            // sRGB hasn't been set up for the PDF document
137
            // so install but don't set to DefaultRGB
138
            cs = PDFICCBasedColorSpace.setupsRGBColorSpace(doc);
139
        }
140
        return cs.getICCStream();
141
    }
142
143
    private static PDFICCStream setupsRGBColorProfile(PDFDocument doc) {
144
        PDFICCBasedColorSpace cs = doc.getResources().getICCColorSpaceByProfileName("sRGB");
145
        return getDefaultsRGBICCStream(cs, doc, "sRGB");
146
    }
147
119
    private static PDFICCStream setupColorProfile(PDFDocument doc,
148
    private static PDFICCStream setupColorProfile(PDFDocument doc,
120
                ICC_Profile prof, PDFDeviceColorSpace pdfCS) {
149
                ICC_Profile prof, PDFDeviceColorSpace pdfCS) {
121
        boolean defaultsRGB = ColorProfileUtil.isDefaultsRGB(prof);
150
        boolean defaultsRGB = ColorProfileUtil.isDefaultsRGB(prof);
Lines 134-155 Link Here
134
                pdfICCStream = cs.getICCStream();
163
                pdfICCStream = cs.getICCStream();
135
            }
164
            }
136
        } else {
165
        } else {
137
            if (cs == null) {
166
            pdfICCStream = getDefaultsRGBICCStream(cs, doc, desc);
138
                if (desc == null || !desc.startsWith("sRGB")) {
139
                    log.warn("The default sRGB profile was indicated,"
140
                            + " but the profile description does not match what was expected: "
141
                            + desc);
142
                }
143
                //It's the default sRGB profile which we mapped to DefaultRGB in PDFRenderer
144
                cs = (PDFICCBasedColorSpace)doc.getResources().getColorSpace(
145
                        new PDFName("DefaultRGB"));
146
            }
147
            if (cs == null) {
148
                // sRGB hasn't been set up for the PDF document
149
                // so install but don't set to DefaultRGB
150
                cs = PDFICCBasedColorSpace.setupsRGBColorSpace(doc);
151
            }
152
            pdfICCStream = cs.getICCStream();
153
        }
167
        }
154
        return pdfICCStream;
168
        return pdfICCStream;
155
    }
169
    }
(-)src/java/org/apache/fop/render/pdf/ImageRawPNGAdapter.java (-7 / +20 lines)
Lines 49-55 Link Here
49
import org.apache.fop.pdf.PDFFilter;
49
import org.apache.fop.pdf.PDFFilter;
50
import org.apache.fop.pdf.PDFFilterException;
50
import org.apache.fop.pdf.PDFFilterException;
51
import org.apache.fop.pdf.PDFFilterList;
51
import org.apache.fop.pdf.PDFFilterList;
52
import org.apache.fop.pdf.PDFICCStream;
52
import org.apache.fop.pdf.PDFName;
53
import org.apache.fop.pdf.PDFReference;
53
import org.apache.fop.pdf.PDFReference;
54
54
55
public class ImageRawPNGAdapter extends AbstractImageAdapter {
55
public class ImageRawPNGAdapter extends AbstractImageAdapter {
Lines 57-63 Link Here
57
    /** logging instance */
57
    /** logging instance */
58
    private static Log log = LogFactory.getLog(ImageRawPNGAdapter.class);
58
    private static Log log = LogFactory.getLog(ImageRawPNGAdapter.class);
59
59
60
    private PDFICCStream pdfICCStream;
61
    private PDFFilter pdfFilter;
60
    private PDFFilter pdfFilter;
62
    private String maskRef;
61
    private String maskRef;
63
    private PDFReference softMask;
62
    private PDFReference softMask;
Lines 241-260 Link Here
241
    }
240
    }
242
241
243
    /** {@inheritDoc} */
242
    /** {@inheritDoc} */
244
    public PDFICCStream getICCStream() {
245
        return pdfICCStream;
246
    }
247
248
    /** {@inheritDoc} */
249
    public String getFilterHint() {
243
    public String getFilterHint() {
250
        return PDFFilterList.PRECOMPRESSED_FILTER;
244
        return PDFFilterList.PRECOMPRESSED_FILTER;
251
    }
245
    }
252
246
253
    public void populateXObjectDictionary(PDFDictionary dict) {
247
    public void populateXObjectDictionary(PDFDictionary dict) {
248
        int renderingIntent = ((ImageRawPNG) image).getRenderingIntent();
249
        if (renderingIntent != -1) {
250
            if (renderingIntent == 0) {
251
                dict.put("Intent", new PDFName("Perceptual"));
252
            } else if (renderingIntent == 1) {
253
                dict.put("Intent", new PDFName("RelativeColorimetric"));
254
            } else if (renderingIntent == 2) {
255
                dict.put("Intent", new PDFName("Saturation"));
256
            } else if (renderingIntent == 3) {
257
                dict.put("Intent", new PDFName("AbsoluteColorimetric"));
258
            }
259
        }
254
        ColorModel cm = ((ImageRawPNG) image).getColorModel();
260
        ColorModel cm = ((ImageRawPNG) image).getColorModel();
255
        if (cm instanceof IndexColorModel) {
261
        if (cm instanceof IndexColorModel) {
256
            IndexColorModel icm = (IndexColorModel) cm;
262
            IndexColorModel icm = (IndexColorModel) cm;
257
            super.populateXObjectDictionaryForIndexColorModel(dict, icm);
263
            super.populateXObjectDictionaryForIndexColorModel(dict, icm);
258
        }
264
        }
259
    }
265
    }
266
267
    protected boolean issRGB() {
268
        if (((ImageRawPNG) image).getRenderingIntent() != -1) {
269
            return true;
270
        }
271
        return false;
272
    }
260
}
273
}
(-)test/java/org/apache/fop/render/pdf/ImageRawPNGAdapterTestCase.java (-2 / +64 lines)
Lines 19-47 Link Here
19
19
20
package org.apache.fop.render.pdf;
20
package org.apache.fop.render.pdf;
21
21
22
import java.awt.color.ColorSpace;
23
import java.awt.color.ICC_Profile;
22
import java.awt.image.ComponentColorModel;
24
import java.awt.image.ComponentColorModel;
23
import java.awt.image.IndexColorModel;
25
import java.awt.image.IndexColorModel;
24
import java.io.ByteArrayInputStream;
26
import java.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
27
import java.io.ByteArrayOutputStream;
26
import java.io.IOException;
28
import java.io.IOException;
27
import java.util.zip.Deflater;
28
import java.util.zip.DeflaterOutputStream;
29
29
30
import org.junit.Test;
30
import org.junit.Test;
31
31
32
import org.apache.xmlgraphics.image.loader.ImageSize;
32
import org.apache.xmlgraphics.image.loader.ImageSize;
33
import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG;
33
import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG;
34
import org.apache.xmlgraphics.java2d.color.profile.ColorProfileUtil;
34
35
35
import org.apache.fop.pdf.FlateFilter;
36
import org.apache.fop.pdf.FlateFilter;
36
import org.apache.fop.pdf.PDFAMode;
37
import org.apache.fop.pdf.PDFAMode;
38
import org.apache.fop.pdf.PDFDictionary;
37
import org.apache.fop.pdf.PDFDocument;
39
import org.apache.fop.pdf.PDFDocument;
40
import org.apache.fop.pdf.PDFICCBasedColorSpace;
41
import org.apache.fop.pdf.PDFICCStream;
42
import org.apache.fop.pdf.PDFName;
38
import org.apache.fop.pdf.PDFProfile;
43
import org.apache.fop.pdf.PDFProfile;
44
import org.apache.fop.pdf.PDFResources;
39
import org.apache.fop.render.RawPNGTestUtil;
45
import org.apache.fop.render.RawPNGTestUtil;
40
46
41
import static org.junit.Assert.assertArrayEquals;
47
import static org.junit.Assert.assertArrayEquals;
42
import static org.junit.Assert.assertEquals;
48
import static org.junit.Assert.assertEquals;
49
import static org.junit.Assert.assertTrue;
43
50
44
import static org.mockito.Mockito.mock;
51
import static org.mockito.Mockito.mock;
52
import static org.mockito.Mockito.verify;
45
import static org.mockito.Mockito.when;
53
import static org.mockito.Mockito.when;
46
54
47
public class ImageRawPNGAdapterTestCase {
55
public class ImageRawPNGAdapterTestCase {
Lines 56-61 Link Here
56
        ImageSize is = RawPNGTestUtil.getImageSize();
64
        ImageSize is = RawPNGTestUtil.getImageSize();
57
65
58
        when(irpng.getColorModel()).thenReturn(cm);
66
        when(irpng.getColorModel()).thenReturn(cm);
67
        when(irpng.getRenderingIntent()).thenReturn(-1);
59
        // when(cm.hasAlpha()).thenReturn(false);
68
        // when(cm.hasAlpha()).thenReturn(false);
60
        when(doc.getProfile()).thenReturn(profile);
69
        when(doc.getProfile()).thenReturn(profile);
61
        when(profile.getPDFAMode()).thenReturn(PDFAMode.PDFA_1A);
70
        when(profile.getPDFAMode()).thenReturn(PDFAMode.PDFA_1A);
Lines 75-80 Link Here
75
        ImageSize is = RawPNGTestUtil.getImageSize();
84
        ImageSize is = RawPNGTestUtil.getImageSize();
76
85
77
        when(irpng.getColorModel()).thenReturn(cm);
86
        when(irpng.getColorModel()).thenReturn(cm);
87
        when(irpng.getRenderingIntent()).thenReturn(-1);
78
        when(cm.getNumComponents()).thenReturn(3);
88
        when(cm.getNumComponents()).thenReturn(3);
79
        // when(cm.hasAlpha()).thenReturn(false);
89
        // when(cm.hasAlpha()).thenReturn(false);
80
        when(doc.getProfile()).thenReturn(profile);
90
        when(doc.getProfile()).thenReturn(profile);
Lines 117-122 Link Here
117
        ImageSize is = RawPNGTestUtil.getImageSize();
127
        ImageSize is = RawPNGTestUtil.getImageSize();
118
128
119
        when(irpng.getColorModel()).thenReturn(cm);
129
        when(irpng.getColorModel()).thenReturn(cm);
130
        when(irpng.getRenderingIntent()).thenReturn(-1);
120
        when(cm.getNumComponents()).thenReturn(numComponents);
131
        when(cm.getNumComponents()).thenReturn(numComponents);
121
        // when(cm.hasAlpha()).thenReturn(false);
132
        // when(cm.hasAlpha()).thenReturn(false);
122
        when(doc.getProfile()).thenReturn(profile);
133
        when(doc.getProfile()).thenReturn(profile);
Lines 139-142 Link Here
139
        }
150
        }
140
    }
151
    }
141
152
153
    @Test
154
    public void testPopulateXObjectDictionaryWithComponentColorModelAndsRGB() {
155
        ComponentColorModel cm = mock(ComponentColorModel.class);
156
        ImageRawPNG irpng = mock(ImageRawPNG.class);
157
        PDFDictionary pdfDic = mock(PDFDictionary.class);
158
        ImageRawPNGAdapter irpnga = new ImageRawPNGAdapter(irpng, "mock");
159
160
        when(irpng.getColorModel()).thenReturn(cm);
161
        when(irpng.getRenderingIntent()).thenReturn(0);
162
        irpnga.populateXObjectDictionary(pdfDic);
163
        verify(pdfDic).put("Intent", new PDFName("Perceptual"));
164
        when(irpng.getRenderingIntent()).thenReturn(1);
165
        irpnga.populateXObjectDictionary(pdfDic);
166
        verify(pdfDic).put("Intent", new PDFName("RelativeColorimetric"));
167
        when(irpng.getRenderingIntent()).thenReturn(2);
168
        irpnga.populateXObjectDictionary(pdfDic);
169
        verify(pdfDic).put("Intent", new PDFName("Saturation"));
170
        when(irpng.getRenderingIntent()).thenReturn(3);
171
        irpnga.populateXObjectDictionary(pdfDic);
172
        verify(pdfDic).put("Intent", new PDFName("AbsoluteColorimetric"));
173
    }
174
175
    @Test
176
    public void testRenderingIntentImpliessRGBColorProfile() {
177
        ComponentColorModel cm = mock(ComponentColorModel.class);
178
        ImageRawPNG irpng = mock(ImageRawPNG.class);
179
        PDFDocument doc = mock(PDFDocument.class);
180
        PDFProfile profile = mock(PDFProfile.class);
181
        PDFResources resources = mock(PDFResources.class);
182
        PDFICCBasedColorSpace cs = mock(PDFICCBasedColorSpace.class);
183
        PDFICCStream stream = mock(PDFICCStream.class);
184
        ICC_Profile iccprof = ICC_Profile.getInstance(ColorSpace.CS_sRGB);
185
        ImageRawPNGAdapter irpnga = new ImageRawPNGAdapter(irpng, "mock");
186
        ImageSize is = RawPNGTestUtil.getImageSize();
187
188
        when(irpng.getColorModel()).thenReturn(cm);
189
        when(irpng.getRenderingIntent()).thenReturn(0);
190
        when(cm.getNumComponents()).thenReturn(3);
191
        // when(cm.hasAlpha()).thenReturn(false);
192
        when(doc.getProfile()).thenReturn(profile);
193
        when(doc.getResources()).thenReturn(resources);
194
        when(resources.getICCColorSpaceByProfileName("sRGB")).thenReturn(cs);
195
        when(profile.getPDFAMode()).thenReturn(PDFAMode.PDFA_1A);
196
        when(irpng.getSize()).thenReturn(is);
197
        when(cs.getICCStream()).thenReturn(stream);
198
        when(stream.getICCProfile()).thenReturn(iccprof);
199
200
        irpnga.setup(doc);
201
        PDFICCStream iccStream = irpnga.getICCStream();
202
        assertTrue(ColorProfileUtil.isDefaultsRGB(iccStream.getICCProfile()));
203
    }
142
}
204
}

Return to bug 40676