KickJava   Java API By Example, From Geeks To Geeks.

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


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: JpegImageIOImage.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.image;
21
22 // AWT
23
import java.awt.Color JavaDoc;
24 import java.awt.image.ColorModel JavaDoc;
25 import java.awt.image.IndexColorModel JavaDoc;
26 import java.awt.image.BufferedImage JavaDoc;
27
28 // ImageIO
29
import javax.imageio.ImageIO JavaDoc;
30
31 import org.apache.commons.io.IOUtils;
32
33 /**
34  * FopImage object using ImageIO.
35  * Special class to allow the use of JpegImage for those
36  * renderers which can embed Jpeg directly but for renderers
37  * which require the decoded data this class delivers it.
38  * @see AbstractFopImage
39  * @see JpegImage
40  */

41 public class JpegImageIOImage extends JpegImage {
42
43     /**
44      * Creates a new JpegImageIOImage.
45      * @param info the image info from the ImageReader
46      */

47     public JpegImageIOImage(FopImage.ImageInfo info) {
48         super(info);
49     }
50
51     /**
52      * @see org.apache.fop.image.AbstractFopImage#loadDimensions()
53      */

54     protected boolean loadDimensions() {
55         if (this.bitmaps == null) {
56             return loadBitmap();
57         }
58         return true;
59     }
60     
61     /** @see org.apache.fop.image.AbstractFopImage#loadBitmap() */
62     protected boolean loadBitmap() {
63         try {
64             inputStream.reset();
65             BufferedImage JavaDoc imageData = ImageIO.read(inputStream);
66
67             this.height = imageData.getHeight();
68             this.width = imageData.getWidth();
69
70             ColorModel JavaDoc cm = imageData.getColorModel();
71             this.bitsPerPixel = cm.getComponentSize(0); //only use first, we assume all are equal
72
this.colorSpace = cm.getColorSpace();
73
74             int[] tmpMap = imageData.getRGB(0, 0, this.width,
75                                             this.height, null, 0, this.width);
76
77             if (cm.hasAlpha()) {
78                 // java.awt.Transparency. BITMASK or OPAQUE or TRANSLUCENT
79
int transparencyType = cm.getTransparency();
80                 
81                 if (transparencyType == java.awt.Transparency.OPAQUE) {
82                     this.isTransparent = false;
83                 } else if (transparencyType == java.awt.Transparency.BITMASK) {
84                     if (cm instanceof IndexColorModel JavaDoc) {
85                         this.isTransparent = false;
86                         byte[] alphas = new byte[
87                                           ((IndexColorModel JavaDoc) cm).getMapSize()];
88                         byte[] reds = new byte[
89                                         ((IndexColorModel JavaDoc) cm).getMapSize()];
90                         byte[] greens = new byte[
91                                           ((IndexColorModel JavaDoc) cm).getMapSize()];
92                         byte[] blues = new byte[
93                                          ((IndexColorModel JavaDoc) cm).getMapSize()];
94                         ((IndexColorModel JavaDoc) cm).getAlphas(alphas);
95                         ((IndexColorModel JavaDoc) cm).getReds(reds);
96                         ((IndexColorModel JavaDoc) cm).getGreens(greens);
97                         ((IndexColorModel JavaDoc) cm).getBlues(blues);
98                         for (int i = 0;
99                                 i < ((IndexColorModel JavaDoc) cm).getMapSize();
100                                 i++) {
101                             if ((alphas[i] & 0xFF) == 0) {
102                                 this.isTransparent = true;
103                                 this.transparentColor = new Color JavaDoc(
104                                                             (int)(reds[i] & 0xFF),
105                                                             (int)(greens[i] & 0xFF),
106                                                             (int)(blues[i] & 0xFF));
107                                 break;
108                             }
109                         }
110                     } else {
111                         // TRANSLUCENT
112
/*
113                          * this.isTransparent = false;
114                          * for (int i = 0; i < this.width * this.height; i++) {
115                          * if (cm.getAlpha(tmpMap[i]) == 0) {
116                          * this.isTransparent = true;
117                          * this.transparentColor = new PDFColor(cm.getRed(tmpMap[i]),
118                          * cm.getGreen(tmpMap[i]), cm.getBlue(tmpMap[i]));
119                          * break;
120                          * }
121                          * }
122                          * // or use special API...
123                          */

124                         this.isTransparent = false;
125                     }
126                 } else {
127                     this.isTransparent = false;
128                 }
129             } else {
130                 this.isTransparent = false;
131             }
132
133             // Should take care of the ColorSpace and bitsPerPixel
134
this.bitmaps = new byte[this.width * this.height * 3];
135             for (int i = 0; i < this.height; i++) {
136                 for (int j = 0; j < this.width; j++) {
137                     int p = tmpMap[i * this.width + j];
138                     int r = (p >> 16) & 0xFF;
139                     int g = (p >> 8) & 0xFF;
140                     int b = (p) & 0xFF;
141                     this.bitmaps[3 * (i * this.width + j)]
142                         = (byte)(r & 0xFF);
143                     this.bitmaps[3 * (i * this.width + j) + 1]
144                         = (byte)(g & 0xFF);
145                     this.bitmaps[3 * (i * this.width + j) + 2]
146                         = (byte)(b & 0xFF);
147                 }
148             }
149
150         } catch (Exception JavaDoc ex) {
151             log.error("Error while loading image: " + ex.getMessage(), ex);
152             return false;
153         } finally {
154             IOUtils.closeQuietly(inputStream);
155             inputStream = null;
156         }
157         return true;
158     }
159
160 }
161
162
Popular Tags