aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/image/AbstractFopImage.java
blob: 97f1767126b256a83ce8ab112454f08489991e12 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * 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$ */

package org.apache.fop.image;

// Java
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.io.InputStream;
import java.awt.Color;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.datatypes.Length;

/**
 * Base class to implement the FopImage interface.
 * @author Eric SCHAEFFER
 * @author Modified by Eric Dalquist - 9/14/2001 - ebdalqui@mtu.edu
 * @see FopImage
 */
public abstract class AbstractFopImage implements FopImage {

    /**
     * logging instance
     */
    protected static Log log = LogFactory.getLog(AbstractFopImage.class);

    /**
     * Keeps track of what has been loaded.
     */
    protected int loaded = 0;

    /**
     * Image width (in pixel).
     */
    protected int width = 0;

    /**
     * Image height (in pixel).
     */
    protected int height = 0;
    
    /** Horizontal bitmap resolution (in dpi) */
    protected double dpiHorizontal = 72.0f;

    /** Vertical bitmap resolution (in dpi) */
    protected double dpiVertical = 72.0f;

    /**
     * Image input stream.
     */
    protected InputStream inputStream = null;

    /**
     * ImageReader object (to obtain image header informations).
     */
    protected FopImage.ImageInfo imageInfo = null;

    /**
     * Image color space (java.awt.color.ColorSpace).
     */
    protected ColorSpace colorSpace = null;

    /**
     * Bits per pixel.
     */
    protected int bitsPerPixel = 0;

    /**
     * Image data (pixels, uncompressed).
     */
    protected byte[] bitmaps = null;

    /**
     * Image data (undecoded, compressed, for image formats that can be embedded without decoding.
     */
    protected byte[] raw = null;

    /**
     * Image transparency.
     */
    protected boolean isTransparent = false;

    /**
     * Transparent color (java.awt.Color).
     */
    protected Color transparentColor = null;

    /**
     * Photoshop generated CMYK JPEGs are inverted.
     */
     protected boolean invertImage = false;
    
    /**
     * Constructor.
     * Construct a new FopImage object and initialize its default properties:
     * <UL>
     * <LI>image width
     * <LI>image height
     * </UL>
     * The image data isn't kept in memory.
     * @param info image information
     */
    public AbstractFopImage(FopImage.ImageInfo info) {
        this.inputStream = info.inputStream;
        this.imageInfo = info;
        if (this.imageInfo.width != -1) {
            width = imageInfo.width;
            height = imageInfo.height;
            dpiHorizontal = imageInfo.dpiHorizontal;
            dpiVertical = imageInfo.dpiVertical;
            loaded = loaded | DIMENSIONS;
        }
    }

    /**
     * Get the mime type for this image.
     *
     * @return the mime type for the image
     */
    public String getMimeType() {
        return imageInfo.mimeType;
    }

    /** @see org.apache.fop.image.FopImage#getOriginalURI() */
    public String getOriginalURI() {
        return this.imageInfo.originalURI;
    }
    
    /**
     * Load image data and initialize its properties.
     *
     * @param type the type of loading to do
     * @return true if the loading was successful
     */
    public synchronized boolean load(int type) {
        if ((loaded & type) != 0) {
            return true;
        }
        boolean success = true;
        if (((type & DIMENSIONS) != 0) && ((loaded & DIMENSIONS) == 0)) {
            success = success && loadDimensions();

            if (!success) {
                return false;
            }
            loaded = loaded | DIMENSIONS;
        }
        if (((type & BITMAP) != 0) && ((loaded & BITMAP) == 0)) {
            success = success && loadBitmap();
            if (success) {
                loaded = loaded | BITMAP;
            }
        }
        if (((type & ORIGINAL_DATA) != 0) && ((loaded & ORIGINAL_DATA) == 0)) {
            success = success && loadOriginalData();
            if (success) {
                loaded = loaded | ORIGINAL_DATA;
            }
        }
        return success;
    }

    /**
     * Load the dimensions of the image.
     * All implementations should override this to get and
     * return the dimensions.
     *
     * @return true if the loading was successful
     */
    protected boolean loadDimensions() {
        return false;
    }

    /**
     * Load a bitmap array of the image.
     * If the renderer requires a bitmap image then the
     * implementations should override this to load the bitmap.
     *
     * @return true if the loading was successful
     */
    protected boolean loadBitmap() {
        return false;
    }

    /**
     * Load the original image data.
     * In some cases the original data can be used by the renderer.
     * This should load the data and any other associated information.
     *
     * @return true if the loading was successful
     */
    protected boolean loadOriginalData() {
        return false;
    }

    /**
     * Load the original image data. This is generic code for use by any
     * subclass that wants to use this from a loadOriginalData() implementation.
     *
     * @return true if the loading was successful
     */
    protected boolean loadDefaultOriginalData() {
        if (inputStream == null) {
            throw new IllegalStateException("inputStream is already null or was never set");
        }
        try {
            this.raw = IOUtils.toByteArray(inputStream);
        } catch (java.io.IOException ex) {
            log.error("Error while reading raw image: " + ex.getMessage(), ex);
            return false;
        } finally {
            IOUtils.closeQuietly(inputStream);
            inputStream = null;
        }
        return true;
    }
    
    /**
     * @return the image width (in pixels)
     */
    public int getWidth() {
        return this.width;
    }
    
    /**
     * @return the image height (in pixels)
     */
    public int getHeight() {
        return this.height;
    }

    /** @see org.apache.fop.image.FopImage#getIntrinsicWidth() */
    public int getIntrinsicWidth() {
        return (int)(getWidth() * 72000 / getHorizontalResolution());
    }

    /** @see org.apache.fop.image.FopImage#getIntrinsicHeight() */
    public int getIntrinsicHeight() {
        return (int)(getHeight() * 72000 / getVerticalResolution());
    }
    
    /** @see org.apache.fop.image.FopImage#getIntrinsicAlignmentAdjust() */
    public Length getIntrinsicAlignmentAdjust() {
        return this.imageInfo.alignmentAdjust;
    }

    /** @see org.apache.fop.image.FopImage#getHorizontalResolution() */
    public double getHorizontalResolution() {
        return this.dpiHorizontal;
    }
    
    /** @see org.apache.fop.image.FopImage#getVerticalResolution() */
    public double getVerticalResolution() {
        return this.dpiVertical;
    }
    
    /**
     * Return the image color space.
     * @return the image color space (java.awt.color.ColorSpace)
     */
    public ColorSpace getColorSpace() {
        return this.colorSpace;
    }

    /**
     * Get ICC profile for this image.
     * @return the icc profile or null if not applicable
     */
    public ICC_Profile getICCProfile() {
        if (this.colorSpace != null && this.colorSpace instanceof ICC_ColorSpace) {
            return ((ICC_ColorSpace)this.colorSpace).getProfile();
        }
        return null;
    }

    /**
     * Return the number of bits per pixel.
     * @return number of bits per pixel
     */
    public int getBitsPerPixel() {
        return this.bitsPerPixel;
    }

    /**
     * Return the image transparency.
     * @return true if the image is transparent
     */
    public boolean isTransparent() {
        return this.isTransparent;
    }

    /**
     * Check if this image has a soft mask.
     *
     * @return true if the image also has a soft transparency mask
     */
    public boolean hasSoftMask() {
        return false;
    }

    /**
     * Get the soft mask.
     * The soft mask should have the same bitdepth as the image data.
     *
     * @return the data array of soft mask values
     */
    public byte[] getSoftMask() {
        return null;
    }

    /**
     * Return the transparent color.
     * @return the transparent color (java.awt.Color)
     */
    public Color getTransparentColor() {
        return this.transparentColor;
    }

    /** @return true for CMYK images generated by Adobe Photoshop */
    public boolean isInverted() {
        return this.invertImage;
    }
    
    /**
     * Return the image data (pixels, uncompressed).
     * @return the image data
     */
    public byte[] getBitmaps() {
        return this.bitmaps;
    }

    /**
     * Return the image data size (number of bytes taken up by the uncompressed pixels).
     * @return the image data size
     */
    public int getBitmapsSize() {
        return (bitmaps != null ? bitmaps.length : 0);
    }

    /**
     * Return the original image data (compressed).
     * @return the original image data
     */
    public byte[] getRessourceBytes() {
        return raw;
    }

    /**
     * Return the original image data size (compressed).
     * @return the original image data size
     */
    public int getRessourceBytesSize() {
        return (raw != null ? raw.length : 0);
    }

}