aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/pdf/ImageRawPNGAdapter.java
blob: f40ca6d943b868522e368dca1a4fda997453abd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

// Original author: Matthias Reichenbacher

package org.apache.fop.render.pdf;

import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG;
import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;

import org.apache.fop.pdf.BitmapImage;
import org.apache.fop.pdf.FlateFilter;
import org.apache.fop.pdf.PDFColor;
import org.apache.fop.pdf.PDFDeviceColorSpace;
import org.apache.fop.pdf.PDFDictionary;
import org.apache.fop.pdf.PDFDocument;
import org.apache.fop.pdf.PDFFilter;
import org.apache.fop.pdf.PDFFilterException;
import org.apache.fop.pdf.PDFFilterList;
import org.apache.fop.pdf.PDFName;
import org.apache.fop.pdf.PDFReference;

public class ImageRawPNGAdapter extends AbstractImageAdapter {

    /** logging instance */
    private static Log log = LogFactory.getLog(ImageRawPNGAdapter.class);

    private static final PDFName RI_PERCEPTUAL = new PDFName("Perceptual");
    private static final PDFName RI_RELATIVE_COLORIMETRIC = new PDFName("RelativeColorimetric");
    private static final PDFName RI_SATURATION = new PDFName("Saturation");
    private static final PDFName RI_ABSOLUTE_COLORIMETRIC = new PDFName("AbsoluteColorimetric");

    private PDFFilter pdfFilter;
    private String maskRef;
    private PDFReference softMask;
    private int numberOfInterleavedComponents;

    /**
     * Creates a new PDFImage from an Image instance.
     * @param image the image
     * @param key XObject key
     */
    public ImageRawPNGAdapter(ImageRawPNG image, String key) {
        super(image, key);
    }

    /** {@inheritDoc} */
    public void setup(PDFDocument doc) {
        super.setup(doc);
        ColorModel cm = ((ImageRawPNG) this.image).getColorModel();
        if (cm instanceof IndexColorModel) {
            numberOfInterleavedComponents = 1;
        } else {
            // this can be 1 (gray), 2 (gray + alpha), 3 (rgb) or 4 (rgb + alpha)
            // numberOfInterleavedComponents = (cm.hasAlpha() ? 1 : 0) + cm.getNumColorComponents();
            numberOfInterleavedComponents = cm.getNumComponents();
        }

        // set up image compression for non-alpha channel
        FlateFilter flate;
        try {
            flate = new FlateFilter();
            flate.setApplied(true);
            flate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);
            if (numberOfInterleavedComponents < 3) {
                // means palette (1) or gray (1) or gray + alpha (2)
                flate.setColors(1);
            } else {
                // means rgb (3) or rgb + alpha (4)
                flate.setColors(3);
            }
            flate.setColumns(image.getSize().getWidthPx());
            flate.setBitsPerComponent(this.getBitsPerComponent());
        } catch (PDFFilterException e) {
            throw new RuntimeException("FlateFilter configuration error", e);
        }
        this.pdfFilter = flate;
        this.disallowMultipleFilters();

        // Handle transparency channel if applicable; note that for palette images the transparency is
        // not TRANSLUCENT
        if (cm.hasAlpha() && cm.getTransparency() == ColorModel.TRANSLUCENT) {
            doc.getProfile().verifyTransparencyAllowed(image.getInfo().getOriginalURI());
            // TODO: Implement code to combine image with background color if transparency is not allowed
            // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha channel
            // and then deflate it back again
            ByteArrayOutputStream baos = null;
            DeflaterOutputStream dos = null;
            InputStream in = null;
            InflaterInputStream infStream = null;
            DataInputStream dataStream = null;
            try {
                baos = new ByteArrayOutputStream();
                dos = new DeflaterOutputStream(baos, new Deflater());
                in = ((ImageRawStream) image).createInputStream();
                try {
                    infStream = new InflaterInputStream(in, new Inflater());
                    dataStream = new DataInputStream(infStream);
                    // offset is the byte offset of the alpha component
                    int offset = numberOfInterleavedComponents - 1; // 1 for GA, 3 for RGBA
                    int numColumns = image.getSize().getWidthPx();
                    int bytesPerRow = numberOfInterleavedComponents * numColumns;
                    int filter;
                    // read line by line; the first byte holds the filter
                    while ((filter = dataStream.read()) != -1) {
                        byte[] bytes = new byte[bytesPerRow];
                        dataStream.readFully(bytes, 0, bytesPerRow);
                        dos.write((byte) filter);
                        for (int j = 0; j < numColumns; j++) {
                            dos.write(bytes, offset, 1);
                            offset += numberOfInterleavedComponents;
                        }
                        offset = numberOfInterleavedComponents - 1;
                    }
                } catch (IOException e) {
                    throw new RuntimeException("Error processing transparency channel:", e);
                }
                // set up alpha channel compression
                FlateFilter transFlate;
                try {
                    transFlate = new FlateFilter();
                    transFlate.setApplied(true);
                    transFlate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);
                    transFlate.setColors(1);
                    transFlate.setColumns(image.getSize().getWidthPx());
                    transFlate.setBitsPerComponent(this.getBitsPerComponent());
                } catch (PDFFilterException e) {
                    throw new RuntimeException("FlateFilter configuration error", e);
                }
                BitmapImage alphaMask = new BitmapImage("Mask:" + this.getKey(), image.getSize().getWidthPx(),
                        image.getSize().getHeightPx(), baos.toByteArray(), null);
                alphaMask.setPDFFilter(transFlate);
                alphaMask.disallowMultipleFilters();
                alphaMask.setColorSpace(new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_GRAY));
                softMask = doc.addImage(null, alphaMask).makeReference();
            } finally {
                IOUtils.closeQuietly(infStream);
                IOUtils.closeQuietly(dataStream);
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(dos);
                IOUtils.closeQuietly(baos);
            }
        }
    }

    /** {@inheritDoc} */
    public PDFDeviceColorSpace getColorSpace() {
        // DeviceGray, DeviceRGB, or DeviceCMYK
        return toPDFColorSpace(image.getColorSpace());
    }

    /** {@inheritDoc} */
    public int getBitsPerComponent() {
        return ((ImageRawPNG) this.image).getBitDepth();
    }

    /** {@inheritDoc} */
    public boolean isTransparent() {
        return ((ImageRawPNG) this.image).isTransparent();
    }

    /** {@inheritDoc} */
    public PDFColor getTransparentColor() {
        return new PDFColor(((ImageRawPNG) this.image).getTransparentColor());
    }

    /** {@inheritDoc} */
    public String getMask() {
        return maskRef;
    }

    /** {@inheritDoc} */
    public String getSoftMask() {
        return softMask.toString();
    }

    /** {@inheritDoc} */
    public PDFReference getSoftMaskReference() {
        return softMask;
    }

    /** {@inheritDoc} */
    public PDFFilter getPDFFilter() {
        return pdfFilter;
    }

    /** {@inheritDoc} */
    public void outputContents(OutputStream out) throws IOException {
        InputStream in = ((ImageRawStream) image).createInputStream();
        InflaterInputStream infStream = null;
        DataInputStream dataStream = null;
        DeflaterOutputStream dos = null;
        try {
            if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {
                // means we have Gray, RGB, or Palette
                IOUtils.copy(in, out);
            } else {
                // means we have Gray + alpha or RGB + alpha
                // TODO: since we have alpha here do this when the alpha channel is extracted
                int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB
                int numColumns = image.getSize().getWidthPx();
                infStream = new InflaterInputStream(in, new Inflater());
                dataStream = new DataInputStream(infStream);
                int offset = 0;
                int bytesPerRow = numberOfInterleavedComponents * numColumns;
                int filter;
                // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha
                // channel and then deflate the RGB channels back again
                dos = new DeflaterOutputStream(out, new Deflater());
                while ((filter = dataStream.read()) != -1) {
                    byte[] bytes = new byte[bytesPerRow];
                    dataStream.readFully(bytes, 0, bytesPerRow);
                    dos.write((byte) filter);
                    for (int j = 0; j < numColumns; j++) {
                        dos.write(bytes, offset, numBytes);
                        offset += numberOfInterleavedComponents;
                    }
                    offset = 0;
                }
            }
        } finally {
            IOUtils.closeQuietly(dos);
            IOUtils.closeQuietly(dataStream);
            IOUtils.closeQuietly(infStream);
            IOUtils.closeQuietly(in);
        }
    }

    /** {@inheritDoc} */
    public String getFilterHint() {
        return PDFFilterList.PRECOMPRESSED_FILTER;
    }

    public void populateXObjectDictionary(PDFDictionary dict) {
        int renderingIntent = ((ImageRawPNG) image).getRenderingIntent();
        if (renderingIntent != -1) {
            switch (renderingIntent) {
            case 0:
                dict.put("Intent", RI_PERCEPTUAL);
                break;
            case 1:
                dict.put("Intent", RI_RELATIVE_COLORIMETRIC);
                break;
            case 2:
                dict.put("Intent", RI_SATURATION);
                break;
            case 3:
                dict.put("Intent", RI_ABSOLUTE_COLORIMETRIC);
                break;
            default:
                // ignore
            }
        }
        ColorModel cm = ((ImageRawPNG) image).getColorModel();
        if (cm instanceof IndexColorModel) {
            IndexColorModel icm = (IndexColorModel) cm;
            super.populateXObjectDictionaryForIndexColorModel(dict, icm);
        }
    }

    protected boolean issRGB() {
        if (((ImageRawPNG) image).getRenderingIntent() != -1) {
            return true;
        }
        return false;
    }
}