2
|
1 |
/*
|
|
2 |
* Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.imageio.plugins.wbmp;
|
|
27 |
|
|
28 |
import java.awt.Rectangle;
|
|
29 |
import java.awt.image.BufferedImage;
|
|
30 |
import java.awt.image.DataBufferByte;
|
|
31 |
import java.awt.image.MultiPixelPackedSampleModel;
|
|
32 |
import java.awt.image.Raster;
|
|
33 |
import java.awt.image.WritableRaster;
|
|
34 |
|
|
35 |
import javax.imageio.IIOException;
|
|
36 |
import javax.imageio.ImageReader;
|
|
37 |
import javax.imageio.ImageReadParam;
|
|
38 |
import javax.imageio.ImageTypeSpecifier;
|
|
39 |
import javax.imageio.metadata.IIOMetadata;
|
|
40 |
import javax.imageio.spi.ImageReaderSpi;
|
|
41 |
import javax.imageio.stream.ImageInputStream;
|
|
42 |
|
|
43 |
import java.io.*;
|
|
44 |
import java.util.ArrayList;
|
|
45 |
import java.util.Iterator;
|
|
46 |
|
|
47 |
import com.sun.imageio.plugins.common.I18N;
|
|
48 |
|
|
49 |
/** This class is the Java Image IO plugin reader for WBMP images.
|
|
50 |
* It may subsample the image, clip the image,
|
|
51 |
* and shift the decoded image origin if the proper decoding parameter
|
|
52 |
* are set in the provided <code>WBMPImageReadParam</code>.
|
|
53 |
*/
|
|
54 |
public class WBMPImageReader extends ImageReader {
|
|
55 |
/** The input stream where reads from */
|
|
56 |
private ImageInputStream iis = null;
|
|
57 |
|
|
58 |
/** Indicates whether the header is read. */
|
|
59 |
private boolean gotHeader = false;
|
|
60 |
|
|
61 |
/** The original image width. */
|
|
62 |
private int width;
|
|
63 |
|
|
64 |
/** The original image height. */
|
|
65 |
private int height;
|
|
66 |
|
|
67 |
private int wbmpType;
|
|
68 |
|
|
69 |
private WBMPMetadata metadata;
|
|
70 |
|
|
71 |
/** Constructs <code>WBMPImageReader</code> from the provided
|
|
72 |
* <code>ImageReaderSpi</code>.
|
|
73 |
*/
|
|
74 |
public WBMPImageReader(ImageReaderSpi originator) {
|
|
75 |
super(originator);
|
|
76 |
}
|
|
77 |
|
|
78 |
/** Overrides the method defined in the superclass. */
|
|
79 |
public void setInput(Object input,
|
|
80 |
boolean seekForwardOnly,
|
|
81 |
boolean ignoreMetadata) {
|
|
82 |
super.setInput(input, seekForwardOnly, ignoreMetadata);
|
|
83 |
iis = (ImageInputStream) input; // Always works
|
|
84 |
gotHeader = false;
|
|
85 |
}
|
|
86 |
|
|
87 |
/** Overrides the method defined in the superclass. */
|
|
88 |
public int getNumImages(boolean allowSearch) throws IOException {
|
|
89 |
if (iis == null) {
|
|
90 |
throw new IllegalStateException(I18N.getString("GetNumImages0"));
|
|
91 |
}
|
|
92 |
if (seekForwardOnly && allowSearch) {
|
|
93 |
throw new IllegalStateException(I18N.getString("GetNumImages1"));
|
|
94 |
}
|
|
95 |
return 1;
|
|
96 |
}
|
|
97 |
|
|
98 |
public int getWidth(int imageIndex) throws IOException {
|
|
99 |
checkIndex(imageIndex);
|
|
100 |
readHeader();
|
|
101 |
return width;
|
|
102 |
}
|
|
103 |
|
|
104 |
public int getHeight(int imageIndex) throws IOException {
|
|
105 |
checkIndex(imageIndex);
|
|
106 |
readHeader();
|
|
107 |
return height;
|
|
108 |
}
|
|
109 |
|
|
110 |
public boolean isRandomAccessEasy(int imageIndex) throws IOException {
|
|
111 |
checkIndex(imageIndex);
|
|
112 |
return true;
|
|
113 |
}
|
|
114 |
|
|
115 |
private void checkIndex(int imageIndex) {
|
|
116 |
if (imageIndex != 0) {
|
|
117 |
throw new IndexOutOfBoundsException(I18N.getString("WBMPImageReader0"));
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
public void readHeader() throws IOException {
|
|
122 |
if (gotHeader)
|
|
123 |
return;
|
|
124 |
|
|
125 |
if (iis == null) {
|
|
126 |
throw new IllegalStateException("Input source not set!");
|
|
127 |
}
|
|
128 |
|
|
129 |
metadata = new WBMPMetadata();
|
|
130 |
|
|
131 |
wbmpType = iis.readByte(); // TypeField
|
|
132 |
byte fixHeaderField = iis.readByte();
|
|
133 |
|
|
134 |
// check for valid wbmp image
|
|
135 |
if (fixHeaderField != 0
|
|
136 |
|| !isValidWbmpType(wbmpType))
|
|
137 |
{
|
|
138 |
throw new IIOException(I18N.getString("WBMPImageReader2"));
|
|
139 |
}
|
|
140 |
|
|
141 |
metadata.wbmpType = wbmpType;
|
|
142 |
|
|
143 |
// Read image width
|
|
144 |
width = readMultiByteInteger();
|
|
145 |
metadata.width = width;
|
|
146 |
|
|
147 |
// Read image height
|
|
148 |
height = readMultiByteInteger();
|
|
149 |
metadata.height = height;
|
|
150 |
|
|
151 |
gotHeader = true;
|
|
152 |
}
|
|
153 |
|
|
154 |
public Iterator getImageTypes(int imageIndex)
|
|
155 |
throws IOException {
|
|
156 |
checkIndex(imageIndex);
|
|
157 |
readHeader();
|
|
158 |
|
|
159 |
BufferedImage bi =
|
|
160 |
new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY);
|
|
161 |
ArrayList list = new ArrayList(1);
|
|
162 |
list.add(new ImageTypeSpecifier(bi));
|
|
163 |
return list.iterator();
|
|
164 |
}
|
|
165 |
|
|
166 |
public ImageReadParam getDefaultReadParam() {
|
|
167 |
return new ImageReadParam();
|
|
168 |
}
|
|
169 |
|
|
170 |
public IIOMetadata getImageMetadata(int imageIndex)
|
|
171 |
throws IOException {
|
|
172 |
checkIndex(imageIndex);
|
|
173 |
if (metadata == null) {
|
|
174 |
readHeader();
|
|
175 |
}
|
|
176 |
return metadata;
|
|
177 |
}
|
|
178 |
|
|
179 |
public IIOMetadata getStreamMetadata() throws IOException {
|
|
180 |
return null;
|
|
181 |
}
|
|
182 |
|
|
183 |
public BufferedImage read(int imageIndex, ImageReadParam param)
|
|
184 |
throws IOException {
|
|
185 |
|
|
186 |
if (iis == null) {
|
|
187 |
throw new IllegalStateException(I18N.getString("WBMPImageReader1"));
|
|
188 |
}
|
|
189 |
|
|
190 |
checkIndex(imageIndex);
|
|
191 |
clearAbortRequest();
|
|
192 |
processImageStarted(imageIndex);
|
|
193 |
if (param == null)
|
|
194 |
param = getDefaultReadParam();
|
|
195 |
|
|
196 |
//read header
|
|
197 |
readHeader();
|
|
198 |
|
|
199 |
Rectangle sourceRegion = new Rectangle(0, 0, 0, 0);
|
|
200 |
Rectangle destinationRegion = new Rectangle(0, 0, 0, 0);
|
|
201 |
|
|
202 |
computeRegions(param, this.width, this.height,
|
|
203 |
param.getDestination(),
|
|
204 |
sourceRegion,
|
|
205 |
destinationRegion);
|
|
206 |
|
|
207 |
int scaleX = param.getSourceXSubsampling();
|
|
208 |
int scaleY = param.getSourceYSubsampling();
|
|
209 |
int xOffset = param.getSubsamplingXOffset();
|
|
210 |
int yOffset = param.getSubsamplingYOffset();
|
|
211 |
|
|
212 |
// If the destination is provided, then use it. Otherwise, create new one
|
|
213 |
BufferedImage bi = param.getDestination();
|
|
214 |
|
|
215 |
if (bi == null)
|
|
216 |
bi = new BufferedImage(destinationRegion.x + destinationRegion.width,
|
|
217 |
destinationRegion.y + destinationRegion.height,
|
|
218 |
BufferedImage.TYPE_BYTE_BINARY);
|
|
219 |
|
|
220 |
boolean noTransform =
|
|
221 |
destinationRegion.equals(new Rectangle(0, 0, width, height)) &&
|
|
222 |
destinationRegion.equals(new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
|
|
223 |
|
|
224 |
// Get the image data.
|
|
225 |
WritableRaster tile = bi.getWritableTile(0, 0);
|
|
226 |
|
|
227 |
// Get the SampleModel.
|
|
228 |
MultiPixelPackedSampleModel sm =
|
|
229 |
(MultiPixelPackedSampleModel)bi.getSampleModel();
|
|
230 |
|
|
231 |
if (noTransform) {
|
|
232 |
if (abortRequested()) {
|
|
233 |
processReadAborted();
|
|
234 |
return bi;
|
|
235 |
}
|
|
236 |
|
|
237 |
// If noTransform is necessary, read the data.
|
|
238 |
iis.read(((DataBufferByte)tile.getDataBuffer()).getData(),
|
|
239 |
0, height*sm.getScanlineStride());
|
|
240 |
processImageUpdate(bi,
|
|
241 |
0, 0,
|
|
242 |
width, height, 1, 1,
|
|
243 |
new int[]{0});
|
|
244 |
processImageProgress(100.0F);
|
|
245 |
} else {
|
|
246 |
int len = (this.width + 7) / 8;
|
|
247 |
byte[] buf = new byte[len];
|
|
248 |
byte[] data = ((DataBufferByte)tile.getDataBuffer()).getData();
|
|
249 |
int lineStride = sm.getScanlineStride();
|
|
250 |
iis.skipBytes(len * sourceRegion.y);
|
|
251 |
int skipLength = len * (scaleY - 1);
|
|
252 |
|
|
253 |
// cache the values to avoid duplicated computation
|
|
254 |
int[] srcOff = new int[destinationRegion.width];
|
|
255 |
int[] destOff = new int[destinationRegion.width];
|
|
256 |
int[] srcPos = new int[destinationRegion.width];
|
|
257 |
int[] destPos = new int[destinationRegion.width];
|
|
258 |
|
|
259 |
for (int i = destinationRegion.x, x = sourceRegion.x, j = 0;
|
|
260 |
i < destinationRegion.x + destinationRegion.width;
|
|
261 |
i++, j++, x += scaleX) {
|
|
262 |
srcPos[j] = x >> 3;
|
|
263 |
srcOff[j] = 7 - (x & 7);
|
|
264 |
destPos[j] = i >> 3;
|
|
265 |
destOff[j] = 7 - (i & 7);
|
|
266 |
}
|
|
267 |
|
|
268 |
for (int j = 0, y = sourceRegion.y,
|
|
269 |
k = destinationRegion.y * lineStride;
|
|
270 |
j < destinationRegion.height; j++, y+=scaleY) {
|
|
271 |
|
|
272 |
if (abortRequested())
|
|
273 |
break;
|
|
274 |
iis.read(buf, 0, len);
|
|
275 |
for (int i = 0; i < destinationRegion.width; i++) {
|
|
276 |
//get the bit and assign to the data buffer of the raster
|
|
277 |
int v = (buf[srcPos[i]] >> srcOff[i]) & 1;
|
|
278 |
data[k + destPos[i]] |= v << destOff[i];
|
|
279 |
}
|
|
280 |
|
|
281 |
k += lineStride;
|
|
282 |
iis.skipBytes(skipLength);
|
|
283 |
processImageUpdate(bi,
|
|
284 |
0, j,
|
|
285 |
destinationRegion.width, 1, 1, 1,
|
|
286 |
new int[]{0});
|
|
287 |
processImageProgress(100.0F*j/destinationRegion.height);
|
|
288 |
}
|
|
289 |
}
|
|
290 |
|
|
291 |
if (abortRequested())
|
|
292 |
processReadAborted();
|
|
293 |
else
|
|
294 |
processImageComplete();
|
|
295 |
return bi;
|
|
296 |
}
|
|
297 |
|
|
298 |
public boolean canReadRaster() {
|
|
299 |
return true;
|
|
300 |
}
|
|
301 |
|
|
302 |
public Raster readRaster(int imageIndex,
|
|
303 |
ImageReadParam param) throws IOException {
|
|
304 |
BufferedImage bi = read(imageIndex, param);
|
|
305 |
return bi.getData();
|
|
306 |
}
|
|
307 |
|
|
308 |
public void reset() {
|
|
309 |
super.reset();
|
|
310 |
iis = null;
|
|
311 |
gotHeader = false;
|
|
312 |
}
|
|
313 |
|
|
314 |
private int readMultiByteInteger() throws IOException {
|
|
315 |
int value = iis.readByte();
|
|
316 |
int result = value & 0x7f;
|
|
317 |
while((value & 0x80) == 0x80) {
|
|
318 |
result <<= 7;
|
|
319 |
value = iis.readByte();
|
|
320 |
result |= (value & 0x7f);
|
|
321 |
}
|
|
322 |
return result;
|
|
323 |
}
|
|
324 |
|
|
325 |
/*
|
|
326 |
* This method verifies that given byte is valid wbmp type marker.
|
|
327 |
* At the moment only 0x0 marker is described by wbmp spec.
|
|
328 |
*/
|
|
329 |
boolean isValidWbmpType(int type) {
|
|
330 |
return type == 0;
|
|
331 |
}
|
|
332 |
}
|