1 7 8 package com.sun.imageio.plugins.wbmp; 9 10 import java.awt.Rectangle ; 11 import java.awt.image.BufferedImage ; 12 import java.awt.image.DataBufferByte ; 13 import java.awt.image.MultiPixelPackedSampleModel ; 14 import java.awt.image.Raster ; 15 import java.awt.image.WritableRaster ; 16 17 import javax.imageio.IIOException ; 18 import javax.imageio.ImageReader ; 19 import javax.imageio.ImageReadParam ; 20 import javax.imageio.ImageTypeSpecifier ; 21 import javax.imageio.metadata.IIOMetadata ; 22 import javax.imageio.spi.ImageReaderSpi ; 23 import javax.imageio.stream.ImageInputStream ; 24 25 import java.io.*; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 29 import com.sun.imageio.plugins.common.I18N; 30 31 36 public class WBMPImageReader extends ImageReader { 37 38 private ImageInputStream iis = null; 39 40 41 private boolean gotHeader = false; 42 43 44 private int width; 45 46 47 private int height; 48 49 private int wbmpType; 50 51 private WBMPMetadata metadata; 52 53 56 public WBMPImageReader(ImageReaderSpi originator) { 57 super(originator); 58 } 59 60 61 public void setInput(Object input, 62 boolean seekForwardOnly, 63 boolean ignoreMetadata) { 64 super.setInput(input, seekForwardOnly, ignoreMetadata); 65 iis = (ImageInputStream ) input; gotHeader = false; 67 } 68 69 70 public int getNumImages(boolean allowSearch) throws IOException { 71 if (iis == null) { 72 throw new IllegalStateException (I18N.getString("GetNumImages0")); 73 } 74 if (seekForwardOnly && allowSearch) { 75 throw new IllegalStateException (I18N.getString("GetNumImages1")); 76 } 77 return 1; 78 } 79 80 public int getWidth(int imageIndex) throws IOException { 81 checkIndex(imageIndex); 82 readHeader(); 83 return width; 84 } 85 86 public int getHeight(int imageIndex) throws IOException { 87 checkIndex(imageIndex); 88 readHeader(); 89 return height; 90 } 91 92 public boolean isRandomAccessEasy(int imageIndex) throws IOException { 93 checkIndex(imageIndex); 94 return true; 95 } 96 97 private void checkIndex(int imageIndex) { 98 if (imageIndex != 0) { 99 throw new IndexOutOfBoundsException (I18N.getString("WBMPImageReader0")); 100 } 101 } 102 103 public void readHeader() throws IOException { 104 if (gotHeader) 105 return; 106 107 if (iis == null) { 108 throw new IllegalStateException ("Input source not set!"); 109 } 110 111 metadata = new WBMPMetadata(); 112 113 wbmpType = iis.readByte(); byte fixHeaderField = iis.readByte(); 115 116 if (fixHeaderField != 0 118 || !isValidWbmpType(wbmpType)) 119 { 120 throw new IIOException (I18N.getString("WBMPImageReader2")); 121 } 122 123 metadata.wbmpType = wbmpType; 124 125 width = readMultiByteInteger(); 127 metadata.width = width; 128 129 height = readMultiByteInteger(); 131 metadata.height = height; 132 133 gotHeader = true; 134 } 135 136 public Iterator getImageTypes(int imageIndex) 137 throws IOException { 138 checkIndex(imageIndex); 139 readHeader(); 140 141 BufferedImage bi = 142 new BufferedImage (1, 1, BufferedImage.TYPE_BYTE_BINARY); 143 ArrayList list = new ArrayList (1); 144 list.add(new ImageTypeSpecifier (bi)); 145 return list.iterator(); 146 } 147 148 public ImageReadParam getDefaultReadParam() { 149 return new ImageReadParam (); 150 } 151 152 public IIOMetadata getImageMetadata(int imageIndex) 153 throws IOException { 154 checkIndex(imageIndex); 155 if (metadata == null) { 156 readHeader(); 157 } 158 return metadata; 159 } 160 161 public IIOMetadata getStreamMetadata() throws IOException { 162 return null; 163 } 164 165 public BufferedImage read(int imageIndex, ImageReadParam param) 166 throws IOException { 167 168 if (iis == null) { 169 throw new IllegalStateException (I18N.getString("WBMPImageReader1")); 170 } 171 172 checkIndex(imageIndex); 173 clearAbortRequest(); 174 processImageStarted(imageIndex); 175 if (param == null) 176 param = getDefaultReadParam(); 177 178 readHeader(); 180 181 Rectangle sourceRegion = new Rectangle (0, 0, 0, 0); 182 Rectangle destinationRegion = new Rectangle (0, 0, 0, 0); 183 184 computeRegions(param, this.width, this.height, 185 param.getDestination(), 186 sourceRegion, 187 destinationRegion); 188 189 int scaleX = param.getSourceXSubsampling(); 190 int scaleY = param.getSourceYSubsampling(); 191 int xOffset = param.getSubsamplingXOffset(); 192 int yOffset = param.getSubsamplingYOffset(); 193 194 BufferedImage bi = param.getDestination(); 196 197 if (bi == null) 198 bi = new BufferedImage (destinationRegion.x + destinationRegion.width, 199 destinationRegion.y + destinationRegion.height, 200 BufferedImage.TYPE_BYTE_BINARY); 201 202 boolean noTransform = 203 destinationRegion.equals(new Rectangle (0, 0, width, height)) && 204 destinationRegion.equals(new Rectangle (0, 0, bi.getWidth(), bi.getHeight())); 205 206 WritableRaster tile = bi.getWritableTile(0, 0); 208 209 MultiPixelPackedSampleModel sm = 211 (MultiPixelPackedSampleModel )bi.getSampleModel(); 212 213 if (noTransform) { 214 if (abortRequested()) { 215 processReadAborted(); 216 return bi; 217 } 218 219 iis.read(((DataBufferByte )tile.getDataBuffer()).getData(), 221 0, height*sm.getScanlineStride()); 222 processImageUpdate(bi, 223 0, 0, 224 width, height, 1, 1, 225 new int[]{0}); 226 processImageProgress(100.0F); 227 } else { 228 int len = (this.width + 7) / 8; 229 byte[] buf = new byte[len]; 230 byte[] data = ((DataBufferByte )tile.getDataBuffer()).getData(); 231 int lineStride = sm.getScanlineStride(); 232 iis.skipBytes(len * sourceRegion.y); 233 int skipLength = len * (scaleY - 1); 234 235 int[] srcOff = new int[destinationRegion.width]; 237 int[] destOff = new int[destinationRegion.width]; 238 int[] srcPos = new int[destinationRegion.width]; 239 int[] destPos = new int[destinationRegion.width]; 240 241 for (int i = destinationRegion.x, x = sourceRegion.x, j = 0; 242 i < destinationRegion.x + destinationRegion.width; 243 i++, j++, x += scaleX) { 244 srcPos[j] = x >> 3; 245 srcOff[j] = 7 - (x & 7); 246 destPos[j] = i >> 3; 247 destOff[j] = 7 - (i & 7); 248 } 249 250 for (int j = 0, y = sourceRegion.y, 251 k = destinationRegion.y * lineStride; 252 j < destinationRegion.height; j++, y+=scaleY) { 253 254 if (abortRequested()) 255 break; 256 iis.read(buf, 0, len); 257 for (int i = 0; i < destinationRegion.width; i++) { 258 int v = (buf[srcPos[i]] >> srcOff[i]) & 1; 260 data[k + destPos[i]] |= v << destOff[i]; 261 } 262 263 k += lineStride; 264 iis.skipBytes(skipLength); 265 processImageUpdate(bi, 266 0, j, 267 destinationRegion.width, 1, 1, 1, 268 new int[]{0}); 269 processImageProgress(100.0F*j/destinationRegion.height); 270 } 271 } 272 273 if (abortRequested()) 274 processReadAborted(); 275 else 276 processImageComplete(); 277 return bi; 278 } 279 280 public boolean canReadRaster() { 281 return true; 282 } 283 284 public Raster readRaster(int imageIndex, 285 ImageReadParam param) throws IOException { 286 BufferedImage bi = read(imageIndex, param); 287 return bi.getData(); 288 } 289 290 public void reset() { 291 super.reset(); 292 iis = null; 293 gotHeader = false; 294 } 295 296 private int readMultiByteInteger() throws IOException { 297 int value = iis.readByte(); 298 int result = value & 0x7f; 299 while((value & 0x80) == 0x80) { 300 result <<= 7; 301 value = iis.readByte(); 302 result |= (value & 0x7f); 303 } 304 return result; 305 } 306 307 311 boolean isValidWbmpType(int type) { 312 return type == 0; 313 } 314 } 315 | Popular Tags |