KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > image > XmlGraphicsCommonsImage


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: XmlGraphicsCommonsImage.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.image;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Transparency JavaDoc;
24 import java.awt.image.ColorModel JavaDoc;
25 import java.awt.image.IndexColorModel JavaDoc;
26 import java.awt.image.RenderedImage JavaDoc;
27 import java.awt.image.WritableRaster JavaDoc;
28 import java.awt.image.BufferedImage JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import org.apache.xmlgraphics.image.GraphicsUtil;
32 import org.apache.xmlgraphics.image.codec.util.SeekableStream;
33 import org.apache.xmlgraphics.image.codec.util.MemoryCacheSeekableStream;
34 import org.apache.xmlgraphics.image.codec.util.FileCacheSeekableStream;
35 import org.apache.xmlgraphics.image.rendered.CachableRed;
36
37 import org.apache.commons.io.IOUtils;
38
39 /**
40  * Abstract FopImage implementation which uses the internal codecs from XML Graphics Commons.
41  * @see AbstractFopImage
42  * @see FopImage
43  */

44 public abstract class XmlGraphicsCommonsImage extends AbstractFopImage {
45
46     private byte[] softMask = null;
47
48     /**
49      * The InputStream wrapped into a SeekableStream for decoding.
50      */

51     protected SeekableStream seekableInput = null;
52
53     /**
54      * The Batik representation of the image
55      */

56     protected CachableRed cr = null;
57     
58     /**
59      * Constructs a new BatikImage instance.
60      * @param imgReader basic metadata for the image
61      */

62     public XmlGraphicsCommonsImage(FopImage.ImageInfo imgReader) {
63         super(imgReader);
64     }
65
66     /**
67      * @see org.apache.fop.image.AbstractFopImage#loadDimensions()
68      */

69     protected boolean loadDimensions() {
70         if (seekableInput == null && inputStream != null) {
71             try {
72                 seekableInput = new FileCacheSeekableStream(inputStream);
73             } catch (IOException JavaDoc ioe) {
74                 seekableInput = new MemoryCacheSeekableStream(inputStream);
75             }
76             try {
77                 this.bitsPerPixel = 8;
78                 cr = decodeImage(seekableInput);
79                 this.height = cr.getHeight();
80                 this.width = cr.getWidth();
81                 this.isTransparent = false;
82                 this.softMask = null;
83                 ColorModel JavaDoc cm = cr.getColorModel();
84
85                 this.height = cr.getHeight();
86                 this.width = cr.getWidth();
87                 this.isTransparent = false;
88                 this.softMask = null;
89
90                 int transparencyType = cm.getTransparency();
91                 if (cm instanceof IndexColorModel JavaDoc) {
92                     if (transparencyType == Transparency.BITMASK) {
93                         // Use 'transparent color'.
94
IndexColorModel JavaDoc icm = (IndexColorModel JavaDoc)cm;
95                         int numColor = icm.getMapSize();
96                         byte [] alpha = new byte[numColor];
97                         icm.getAlphas(alpha);
98                         for (int i = 0; i < numColor; i++) {
99                             if ((alpha[i] & 0xFF) == 0) {
100                                 this.isTransparent = true;
101                                 int red = (icm.getRed (i)) & 0xFF;
102                                 int grn = (icm.getGreen(i)) & 0xFF;
103                                 int blu = (icm.getBlue (i)) & 0xFF;
104                                 this.transparentColor = new Color JavaDoc(red, grn, blu);
105                                 break;
106                             }
107                         }
108                     }
109                 } else {
110                     cr = GraphicsUtil.convertTosRGB(cr);
111                 }
112
113                 // Get our current ColorModel
114
cm = cr.getColorModel();
115                 if (this.colorSpace == null) {
116                     this.colorSpace = cm.getColorSpace();
117                 }
118             } catch (IOException JavaDoc ioe) {
119                 log.error("Error while loading image (Batik): " + ioe.getMessage(), ioe);
120                 IOUtils.closeQuietly(seekableInput);
121                 IOUtils.closeQuietly(inputStream);
122                 seekableInput = null;
123                 inputStream = null;
124                 return false;
125             }
126         }
127         return this.height != -1;
128     }
129
130     /**
131      * @see org.apache.fop.image.AbstractFopImage#loadBitmap()
132      */

133     protected boolean loadBitmap() {
134         if (this.bitmaps == null) {
135             loadImage();
136         }
137
138         return this.bitmaps != null;
139     }
140
141     /**
142      * @see org.apache.fop.image.FopImage#hasSoftMask()
143      */

144     public boolean hasSoftMask() {
145         if (this.bitmaps == null && this.raw == null) {
146             loadImage();
147         }
148
149         return (this.softMask != null);
150     }
151
152     /**
153      * @see org.apache.fop.image.FopImage#getSoftMask()
154      */

155     public byte[] getSoftMask() {
156         if (this.bitmaps == null) {
157             loadImage();
158         }
159
160         return this.softMask;
161     }
162
163     /**
164      * Decodes the image from the stream.
165      * @param stream the stream to read the image from
166      * @return the decoded image
167      * @throws IOException in case an I/O problem occurs
168      */

169     protected abstract CachableRed decodeImage(SeekableStream stream) throws IOException JavaDoc;
170     
171     /**
172      * Loads the image from the InputStream.
173      */

174     protected void loadImage() {
175         if (loadDimensions()) {
176             try {
177                 if (cr == null) {
178                     throw new IllegalStateException JavaDoc(
179                             "Can't load the bitmaps data without the CachableRed instance");
180                 }
181                 
182                 // Get our current ColorModel
183
ColorModel JavaDoc cm = cr.getColorModel();
184
185                 // It has an alpha channel so generate a soft mask.
186
if (!this.isTransparent && cm.hasAlpha()) {
187                     this.softMask = new byte[this.width * this.height];
188                 }
189
190                 this.bitmaps = new byte[this.width * this.height * 3];
191
192                 constructBitmaps(cr, this.bitmaps, this.softMask);
193             } catch (Exception JavaDoc ex) {
194                 log.error("Error while loading image (Batik): " + ex.getMessage(), ex);
195             } finally {
196                 // Make sure we clean up
197
IOUtils.closeQuietly(seekableInput);
198                 IOUtils.closeQuietly(inputStream);
199                 seekableInput = null;
200                 inputStream = null;
201                 cr = null;
202             }
203         }
204     }
205
206     private static void constructBitmaps(RenderedImage JavaDoc red, byte[] bitmaps, byte[] softMask) {
207         WritableRaster JavaDoc wr = (WritableRaster JavaDoc)red.getData();
208         ColorModel JavaDoc cm = red.getColorModel();
209         BufferedImage JavaDoc bi = new BufferedImage JavaDoc
210             (cm, wr.createWritableTranslatedChild(0, 0),
211              cm.isAlphaPremultiplied(), null);
212         int width = red.getWidth();
213         int height = red.getHeight();
214         int [] tmpMap = new int[width];
215         int idx = 0;
216         int sfIdx = 0;
217         for (int y = 0; y < height; y++) {
218             tmpMap = bi.getRGB(0, y, width, 1, tmpMap, 0, width);
219             if (softMask != null) {
220                 for (int x = 0; x < width; x++) {
221                     int pix = tmpMap[x];
222                     softMask[sfIdx++] = (byte)(pix >>> 24);
223                     bitmaps[idx++] = (byte)((pix >>> 16) & 0xFF);
224                     bitmaps[idx++] = (byte)((pix >>> 8) & 0xFF);
225                     bitmaps[idx++] = (byte)((pix) & 0xFF);
226                 }
227             } else {
228                 for (int x = 0; x < width; x++) {
229                     int pix = tmpMap[x];
230                     bitmaps[idx++] = (byte)((pix >> 16) & 0xFF);
231                     bitmaps[idx++] = (byte)((pix >> 8) & 0xFF);
232                     bitmaps[idx++] = (byte)((pix) & 0xFF);
233                 }
234             }
235         }
236     }
237     
238 }
Popular Tags