KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: BmpImage.java,v 1.3.2.1 2003/02/25 13:38:22 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.image;
52
53 // Java
54
import java.net.URL JavaDoc;
55 import java.io.InputStream JavaDoc;
56 import java.io.IOException JavaDoc;
57
58 // FOP
59
import org.apache.fop.datatypes.ColorSpace;
60 import org.apache.fop.image.analyser.ImageReader;
61
62 /**
63  * FopImage object for BMP images.
64  * @author Art WELCH
65  * @see AbstractFopImage
66  * @see FopImage
67  */

68 public class BmpImage extends AbstractFopImage {
69     public BmpImage(URL JavaDoc href) throws FopImageException {
70         super(href);
71     }
72
73     public BmpImage(URL JavaDoc href,
74                     ImageReader imgReader) throws FopImageException {
75         super(href, imgReader);
76     }
77
78     protected void loadImage() throws FopImageException {
79         int wpos = 18;
80         int hpos = 22; // offset positioning for w and height in bmp files
81
int[] headermap = new int[54];
82         int filepos = 0;
83         InputStream JavaDoc file = null;
84         byte palette[] = null;
85         try {
86             file = this.m_href.openStream();
87             boolean eof = false;
88             while ((!eof) && (filepos < 54)) {
89                 int input = file.read();
90                 if (input == -1)
91                     eof = true;
92                 else
93                     headermap[filepos++] = input;
94             }
95
96             if (headermap[28] == 4 || headermap[28] == 8) {
97                 int palettesize = 1 << headermap[28];
98                 palette = new byte[palettesize * 3];
99                 int countr = 0;
100                 while (!eof && countr < palettesize) {
101                     int count2 = 2;
102                     while (!eof && count2 >= -1) {
103                         int input = file.read();
104                         if (input == -1)
105                             eof = true;
106                         else if (count2 >= 0) {
107                             palette[countr * 3 + count2] = (byte)(input
108                                                                   & 0xFF);
109                         }
110                         count2--;
111                         filepos++;
112                     }
113                     countr++;
114                 }
115             }
116         } catch (IOException JavaDoc e) {
117             throw new FopImageException("Error while loading image "
118                                         + this.m_href.toString() + " : "
119                                         + e.getClass() + " - "
120                                         + e.getMessage());
121         }
122         // gets h & w from headermap
123
this.m_width = headermap[wpos] + headermap[wpos + 1] * 256
124                        + headermap[wpos + 2] * 256 * 256
125                        + headermap[wpos + 3] * 256 * 256 * 256;
126         this.m_height = headermap[hpos] + headermap[hpos + 1] * 256
127                         + headermap[hpos + 2] * 256 * 256
128                         + headermap[hpos + 3] * 256 * 256 * 256;
129
130         int imagestart = headermap[10] + headermap[11] * 256
131                          + headermap[12] * 256 * 256
132                          + headermap[13] * 256 * 256 * 256;
133         this.m_bitsPerPixel = headermap[28];
134         this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB);
135         int bytes;
136         if (this.m_bitsPerPixel == 1)
137             bytes = (this.m_width + 7) / 8;
138         else if (this.m_bitsPerPixel == 24)
139             bytes = this.m_width * 3;
140         else if (this.m_bitsPerPixel == 4 || this.m_bitsPerPixel == 8)
141             bytes = this.m_width / (8 / this.m_bitsPerPixel);
142         else
143             throw new FopImageException("Image (" + this.m_href.toString()
144                                         + ") has " + this.m_bitsPerPixel
145                                         + " which is not a supported BMP format.");
146         if ((bytes & 0x03) != 0) {
147             bytes |= 0x03;
148             bytes++;
149         }
150
151         // Should take care of the ColorSpace and bitsPerPixel
152
this.m_bitmapsSize = this.m_width * this.m_height * 3;
153         this.m_bitmaps = new byte[this.m_bitmapsSize];
154
155         int[] temp = new int[bytes * this.m_height];
156         try {
157             int input;
158             int count = 0;
159             file.skip((long)(imagestart - filepos));
160             while ((input = file.read()) != -1)
161                 temp[count++] = input;
162             file.close();
163         } catch (IOException JavaDoc e) {
164             throw new FopImageException("Error while loading image "
165                                         + this.m_href.toString() + " : "
166                                         + e.getClass() + " - "
167                                         + e.getMessage());
168         }
169
170         for (int i = 0; i < this.m_height; i++) {
171             int x = 0;
172             int j = 0;
173             while (j < bytes) {
174                 int p = temp[(this.m_height - i - 1) * bytes + j];
175
176                 if (this.m_bitsPerPixel == 24 && x < this.m_width) {
177                     int countr = 2;
178                     do {
179                         this.m_bitmaps[3 * (i * this.m_width + x) + countr] =
180                             (byte)(temp[(this.m_height - i - 1) * bytes + j]
181                                    & 0xFF);
182                         j++;
183                     } while (--countr >= 0);
184                     x++;
185                 } else if (this.m_bitsPerPixel == 1) {
186                     for (int countr = 0; countr < 8 && x < this.m_width;
187                             countr++) {
188                         if ((p & 0x80) != 0) {
189                             this.m_bitmaps[3 * (i * this.m_width + x)] =
190                                 (byte)0xFF;
191                             this.m_bitmaps[3 * (i * this.m_width + x) + 1] =
192                                 (byte)0xFF;
193                             this.m_bitmaps[3 * (i * this.m_width + x) + 2] =
194                                 (byte)0xFF;
195                         } else {
196                             this.m_bitmaps[3 * (i * this.m_width + x)] =
197                                 (byte)0;
198                             this.m_bitmaps[3 * (i * this.m_width + x) + 1] =
199                                 (byte)0;
200                             this.m_bitmaps[3 * (i * this.m_width + x) + 2] =
201                                 (byte)0;
202                         }
203                         p <<= 1;
204                         x++;
205                     }
206                     j++;
207                 } else if (this.m_bitsPerPixel == 4) {
208                     for (int countr = 0; countr < 2 && x < this.m_width;
209                             countr++) {
210                         int pal = ((p & 0xF0) >> 4) * 3;
211                         this.m_bitmaps[3 * (i * this.m_width + x)] =
212                             palette[pal];
213                         this.m_bitmaps[3 * (i * this.m_width + x) + 1] =
214                             palette[pal + 1];
215                         this.m_bitmaps[3 * (i * this.m_width + x) + 2] =
216                             palette[pal + 2];
217                         p <<= 4;
218                         x++;
219                     }
220                     j++;
221                 } else if (this.m_bitsPerPixel == 8) {
222                     if (x < this.m_width) {
223                         p *= 3;
224                         this.m_bitmaps[3 * (i * this.m_width + x)] =
225                             palette[p];
226                         this.m_bitmaps[3 * (i * this.m_width + x) + 1] =
227                             palette[p + 1];
228                         this.m_bitmaps[3 * (i * this.m_width + x) + 2] =
229                             palette[p + 2];
230                         j++;
231                         x++;
232                     } else
233                         j = bytes;
234                 } else
235                     j++;
236             }
237         }
238
239         // This seems really strange to me, but I noticed that JimiImage hardcodes
240
// m_bitsPerPixel to 8. If I do not do this Acrobat is unable to read the resultant PDF,
241
// so we will hardcode this...
242
this.m_bitsPerPixel = 8;
243     }
244
245 }
246
Popular Tags