KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > image > OS2BMPFileFormat


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.internal.image;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.graphics.*;
16 import java.io.*;
17
18 final class OS2BMPFileFormat extends FileFormat {
19     static final int BMPFileHeaderSize = 14;
20     static final int BMPHeaderFixedSize = 12;
21     int width, height, bitCount;
22
23 boolean isFileFormat(LEDataInputStream stream) {
24     try {
25         byte[] header = new byte[18];
26         stream.read(header);
27         stream.unread(header);
28         int infoHeaderSize = (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) | ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24);
29         return header[0] == 0x42 && header[1] == 0x4D && infoHeaderSize == BMPHeaderFixedSize;
30     } catch (Exception JavaDoc e) {
31         return false;
32     }
33 }
34 byte[] loadData(byte[] infoHeader) {
35     int stride = (width * bitCount + 7) / 8;
36     stride = (stride + 3) / 4 * 4; // Round up to 4 byte multiple
37
byte[] data = loadData(infoHeader, stride);
38     flipScanLines(data, stride, height);
39     return data;
40 }
41 byte[] loadData(byte[] infoHeader, int stride) {
42     int dataSize = height * stride;
43     byte[] data = new byte[dataSize];
44     try {
45         if (inputStream.read(data) != dataSize)
46             SWT.error(SWT.ERROR_INVALID_IMAGE);
47     } catch (IOException e) {
48         SWT.error(SWT.ERROR_IO, e);
49     }
50     return data;
51 }
52 int[] loadFileHeader() {
53     int[] header = new int[5];
54     try {
55         header[0] = inputStream.readShort();
56         header[1] = inputStream.readInt();
57         header[2] = inputStream.readShort();
58         header[3] = inputStream.readShort();
59         header[4] = inputStream.readInt();
60     } catch (IOException e) {
61         SWT.error(SWT.ERROR_IO, e);
62     }
63     if (header[0] != 0x4D42)
64         SWT.error(SWT.ERROR_INVALID_IMAGE);
65     return header;
66 }
67 ImageData[] loadFromByteStream() {
68     int[] fileHeader = loadFileHeader();
69     byte[] infoHeader = new byte[BMPHeaderFixedSize];
70     try {
71         inputStream.read(infoHeader);
72     } catch (Exception JavaDoc e) {
73         SWT.error(SWT.ERROR_IO, e);
74     }
75     width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8);
76     height = (infoHeader[6] & 0xFF) | ((infoHeader[7] & 0xFF) << 8);
77     bitCount = (infoHeader[10] & 0xFF) | ((infoHeader[11] & 0xFF) << 8);
78     PaletteData palette = loadPalette(infoHeader);
79     if (inputStream.getPosition() < fileHeader[4]) {
80         // Seek to the specified offset
81
try {
82             inputStream.skip(fileHeader[4] - inputStream.getPosition());
83         } catch (IOException e) {
84             SWT.error(SWT.ERROR_IO, e);
85         }
86     }
87     byte[] data = loadData(infoHeader);
88     int type = SWT.IMAGE_OS2_BMP;
89     return new ImageData[] {
90         ImageData.internal_new(
91             width,
92             height,
93             bitCount,
94             palette,
95             4,
96             data,
97             0,
98             null,
99             null,
100             -1,
101             -1,
102             type,
103             0,
104             0,
105             0,
106             0)
107     };
108 }
109 PaletteData loadPalette(byte[] infoHeader) {
110     if (bitCount <= 8) {
111         int numColors = 1 << bitCount;
112         byte[] buf = new byte[numColors * 3];
113         try {
114             if (inputStream.read(buf) != buf.length)
115                 SWT.error(SWT.ERROR_INVALID_IMAGE);
116         } catch (IOException e) {
117             SWT.error(SWT.ERROR_IO, e);
118         }
119         return paletteFromBytes(buf, numColors);
120     }
121     if (bitCount == 16) return new PaletteData(0x7C00, 0x3E0, 0x1F);
122     if (bitCount == 24) return new PaletteData(0xFF, 0xFF00, 0xFF0000);
123     return new PaletteData(0xFF00, 0xFF0000, 0xFF000000);
124 }
125 PaletteData paletteFromBytes(byte[] bytes, int numColors) {
126     int bytesOffset = 0;
127     RGB[] colors = new RGB[numColors];
128     for (int i = 0; i < numColors; i++) {
129         colors[i] = new RGB(bytes[bytesOffset + 2] & 0xFF,
130             bytes[bytesOffset + 1] & 0xFF,
131             bytes[bytesOffset] & 0xFF);
132         bytesOffset += 3;
133     }
134     return new PaletteData(colors);
135 }
136 /**
137  * Answer a byte array containing the BMP representation of
138  * the given device independent palette.
139  */

140 static byte[] paletteToBytes(PaletteData pal) {
141     int n = pal.colors == null ? 0 : (pal.colors.length < 256 ? pal.colors.length : 256);
142     byte[] bytes = new byte[n * 3];
143     int offset = 0;
144     for (int i = 0; i < n; i++) {
145         RGB col = pal.colors[i];
146         bytes[offset] = (byte)col.blue;
147         bytes[offset + 1] = (byte)col.green;
148         bytes[offset + 2] = (byte)col.red;
149         offset += 3;
150     }
151     return bytes;
152 }
153 /**
154  * Unload the given image's data into the given byte stream.
155  * Answer the number of bytes written.
156  */

157 int unloadData(ImageData image, OutputStream out) {
158     int bmpBpl = 0;
159     try {
160         int bpl = (image.width * image.depth + 7) / 8;
161         bmpBpl = (bpl + 3) / 4 * 4; // BMP pads scanlines to multiples of 4 bytes
162
int linesPerBuf = 32678 / bmpBpl;
163         byte[] buf = new byte[linesPerBuf * bmpBpl];
164         byte[] data = image.data;
165         int imageBpl = image.bytesPerLine;
166         int dataIndex = imageBpl * (image.height - 1); // Start at last line
167
if (image.depth == 16) {
168             for (int y = 0; y < image.height; y += linesPerBuf) {
169                 int count = image.height - y;
170                 if (linesPerBuf < count) count = linesPerBuf;
171                 int bufOffset = 0;
172                 for (int i = 0; i < count; i++) {
173                     for (int wIndex = 0; wIndex < bpl; wIndex += 2) {
174                         buf[bufOffset + wIndex + 1] = data[dataIndex + wIndex + 1];
175                         buf[bufOffset + wIndex] = data[dataIndex + wIndex];
176                     }
177                     bufOffset += bmpBpl;
178                     dataIndex -= imageBpl;
179                 }
180                 out.write(buf, 0, bufOffset);
181             }
182         } else {
183             for (int y = 0; y < image.height; y += linesPerBuf) {
184                 int tmp = image.height - y;
185                 int count = tmp < linesPerBuf ? tmp : linesPerBuf;
186                 int bufOffset = 0;
187                 for (int i = 0; i < count; i++) {
188                     System.arraycopy(data, dataIndex, buf, bufOffset, bpl);
189                     bufOffset += bmpBpl;
190                     dataIndex -= imageBpl;
191                 }
192                 out.write(buf, 0, bufOffset);
193             }
194         }
195     } catch (IOException e) {
196         SWT.error(SWT.ERROR_IO, e);
197     }
198     return bmpBpl * image.height;
199 }
200 /**
201  * Unload a DeviceIndependentImage using Windows .BMP format into the given
202  * byte stream.
203  */

204 void unloadIntoByteStream(ImageLoader loader) {
205     ImageData image = loader.data[0];
206     byte[] rgbs;
207     int numCols;
208     if (!((image.depth == 1) || (image.depth == 4) || (image.depth == 8) ||
209           (image.depth == 16) || (image.depth == 24) || (image.depth == 32)))
210             SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);
211     PaletteData pal = image.palette;
212     if ((image.depth == 16) || (image.depth == 24) || (image.depth == 32)) {
213         if (!pal.isDirect)
214             SWT.error(SWT.ERROR_INVALID_IMAGE);
215         numCols = 0;
216         rgbs = null;
217     } else {
218         if (pal.isDirect)
219             SWT.error(SWT.ERROR_INVALID_IMAGE);
220         numCols = pal.colors.length;
221         rgbs = paletteToBytes(pal);
222     }
223     // Fill in file header, except for bfsize, which is done later.
224
int headersSize = BMPFileHeaderSize + BMPHeaderFixedSize;
225     int[] fileHeader = new int[5];
226     fileHeader[0] = 0x4D42; // Signature
227
fileHeader[1] = 0; // File size - filled in later
228
fileHeader[2] = 0; // Reserved 1
229
fileHeader[3] = 0; // Reserved 2
230
fileHeader[4] = headersSize; // Offset to data
231
if (rgbs != null) {
232         fileHeader[4] += rgbs.length;
233     }
234
235     // Prepare data. This is done first so we don't have to try to rewind
236
// the stream and fill in the details later.
237
ByteArrayOutputStream out = new ByteArrayOutputStream();
238     unloadData(image, out);
239     byte[] data = out.toByteArray();
240     
241     // Calculate file size
242
fileHeader[1] = fileHeader[4] + data.length;
243
244     // Write the headers
245
try {
246         outputStream.writeShort(fileHeader[0]);
247         outputStream.writeInt(fileHeader[1]);
248         outputStream.writeShort(fileHeader[2]);
249         outputStream.writeShort(fileHeader[3]);
250         outputStream.writeInt(fileHeader[4]);
251     } catch (IOException e) {
252         SWT.error(SWT.ERROR_IO, e);
253     }
254     try {
255         outputStream.writeInt(BMPHeaderFixedSize);
256         outputStream.writeShort(image.width);
257         outputStream.writeShort(image.height);
258         outputStream.writeShort(1);
259         outputStream.writeShort((short)image.depth);
260     } catch (IOException e) {
261         SWT.error(SWT.ERROR_IO, e);
262     }
263     
264     // Unload palette
265
if (numCols > 0) {
266         try {
267             outputStream.write(rgbs);
268         } catch (IOException e) {
269             SWT.error(SWT.ERROR_IO, e);
270         }
271     }
272
273     // Unload the data
274
try {
275         outputStream.write(data);
276     } catch (IOException e) {
277         SWT.error(SWT.ERROR_IO, e);
278     }
279 }
280 void flipScanLines(byte[] data, int stride, int height) {
281     int i1 = 0;
282     int i2 = (height - 1) * stride;
283     for (int i = 0; i < height / 2; i++) {
284         for (int index = 0; index < stride; index++) {
285             byte b = data[index + i1];
286             data[index + i1] = data[index + i2];
287             data[index + i2] = b;
288         }
289         i1 += stride;
290         i2 -= stride;
291     }
292 }
293
294 }
295
Popular Tags