59 /** Indicates whether the header is read. */ 60 private boolean gotHeader = false; 61 62 /** The original image width. */ 63 private int width; 64 65 /** The original image height. */ 66 private int height; 67 68 private int wbmpType; 69 70 private WBMPMetadata metadata; 71 72 /** Constructs {@code WBMPImageReader} from the provided 73 * {@code ImageReaderSpi}. 74 */ 75 public WBMPImageReader(ImageReaderSpi originator) { 76 super(originator); 77 } 78 79 /** Overrides the method defined in the superclass. */ 80 public void setInput(Object input, 81 boolean seekForwardOnly, 82 boolean ignoreMetadata) { 83 super.setInput(input, seekForwardOnly, ignoreMetadata); 84 iis = (ImageInputStream) input; // Always works 85 gotHeader = false; 86 } 87 88 /** Overrides the method defined in the superclass. */ 89 public int getNumImages(boolean allowSearch) throws IOException { 90 if (iis == null) { 91 throw new IllegalStateException(I18N.getString("GetNumImages0")); 92 } 93 if (seekForwardOnly && allowSearch) { 94 throw new IllegalStateException(I18N.getString("GetNumImages1")); 95 } 96 return 1; 97 } 98 99 public int getWidth(int imageIndex) throws IOException { 100 checkIndex(imageIndex); 101 readHeader(); 102 return width; 103 } 104 105 public int getHeight(int imageIndex) throws IOException { 106 checkIndex(imageIndex); 107 readHeader(); 108 return height; 109 } 110 111 public boolean isRandomAccessEasy(int imageIndex) throws IOException { 112 checkIndex(imageIndex); 113 return true; 114 } 115 116 private void checkIndex(int imageIndex) { 117 if (imageIndex != 0) { 118 throw new IndexOutOfBoundsException(I18N.getString("WBMPImageReader0")); 119 } 120 } 121 122 public void readHeader() throws IOException { 123 if (gotHeader) 124 return; 125 126 if (iis == null) { 127 throw new IllegalStateException("Input source not set!"); 128 } 129 130 metadata = new WBMPMetadata(); 135 // check for valid wbmp image 136 if (fixHeaderField != 0 137 || !isValidWbmpType(wbmpType)) 138 { 139 throw new IIOException(I18N.getString("WBMPImageReader2")); 140 } 141 142 metadata.wbmpType = wbmpType; 143 144 // Read image width 145 width = ReaderUtil.readMultiByteInteger(iis); 146 metadata.width = width; 147 148 // Read image height 149 height = ReaderUtil.readMultiByteInteger(iis); 150 metadata.height = height; 151 152 gotHeader = true; 153 } 154 155 public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex) 156 throws IOException { 157 checkIndex(imageIndex); 158 readHeader(); 159 160 BufferedImage bi = 161 new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY); 162 ArrayList<ImageTypeSpecifier> list = new ArrayList<>(1); 163 list.add(new ImageTypeSpecifier(bi)); 164 return list.iterator(); 165 } 166 167 public ImageReadParam getDefaultReadParam() { 168 return new ImageReadParam(); 169 } 170 171 public IIOMetadata getImageMetadata(int imageIndex) 172 throws IOException { 173 checkIndex(imageIndex); 174 if (metadata == null) { 175 readHeader(); 176 } 177 return metadata; 178 } 179 180 public IIOMetadata getStreamMetadata() throws IOException { 181 return null; 182 } 183 184 public BufferedImage read(int imageIndex, ImageReadParam param) 185 throws IOException { 186 187 if (iis == null) { 188 throw new IllegalStateException(I18N.getString("WBMPImageReader1")); 189 } 190 191 checkIndex(imageIndex); 192 clearAbortRequest(); 193 processImageStarted(imageIndex); 194 if (param == null) 195 param = getDefaultReadParam(); 196 197 //read header 198 readHeader(); 199 200 Rectangle sourceRegion = new Rectangle(0, 0, 0, 0); 201 Rectangle destinationRegion = new Rectangle(0, 0, 0, 0); 202 203 computeRegions(param, this.width, this.height, 279 data[k + destPos[i]] |= v << destOff[i]; 280 } 281 282 k += lineStride; 283 iis.skipBytes(skipLength); 284 processImageUpdate(bi, 285 0, j, 286 destinationRegion.width, 1, 1, 1, 287 new int[]{0}); 288 processImageProgress(100.0F*j/destinationRegion.height); 289 } 290 } 291 292 if (abortRequested()) 293 processReadAborted(); 294 else 295 processImageComplete(); 296 return bi; 297 } 298 299 public boolean canReadRaster() { 300 return true; 301 } 302 303 public Raster readRaster(int imageIndex, 304 ImageReadParam param) throws IOException { 305 BufferedImage bi = read(imageIndex, param); 306 return bi.getData(); 307 } 308 309 public void reset() { 310 super.reset(); 311 iis = null; 312 gotHeader = false; 313 } 314 315 /* 316 * This method verifies that given byte is valid wbmp type marker. 317 * At the moment only 0x0 marker is described by wbmp spec. 318 */ 319 boolean isValidWbmpType(int type) { 320 return type == 0; 321 } 322 } | 59 /** Indicates whether the header is read. */ 60 private boolean gotHeader = false; 61 62 /** The original image width. */ 63 private int width; 64 65 /** The original image height. */ 66 private int height; 67 68 private int wbmpType; 69 70 private WBMPMetadata metadata; 71 72 /** Constructs {@code WBMPImageReader} from the provided 73 * {@code ImageReaderSpi}. 74 */ 75 public WBMPImageReader(ImageReaderSpi originator) { 76 super(originator); 77 } 78 79 @Override 80 public void setInput(Object input, 81 boolean seekForwardOnly, 82 boolean ignoreMetadata) { 83 super.setInput(input, seekForwardOnly, ignoreMetadata); 84 iis = (ImageInputStream) input; // Always works 85 gotHeader = false; 86 } 87 88 @Override 89 public int getNumImages(boolean allowSearch) throws IOException { 90 if (iis == null) { 91 throw new IllegalStateException(I18N.getString("GetNumImages0")); 92 } 93 if (seekForwardOnly && allowSearch) { 94 throw new IllegalStateException(I18N.getString("GetNumImages1")); 95 } 96 return 1; 97 } 98 99 @Override 100 public int getWidth(int imageIndex) throws IOException { 101 checkIndex(imageIndex); 102 readHeader(); 103 return width; 104 } 105 106 @Override 107 public int getHeight(int imageIndex) throws IOException { 108 checkIndex(imageIndex); 109 readHeader(); 110 return height; 111 } 112 113 @Override 114 public boolean isRandomAccessEasy(int imageIndex) throws IOException { 115 checkIndex(imageIndex); 116 return true; 117 } 118 119 private void checkIndex(int imageIndex) { 120 if (imageIndex != 0) { 121 throw new IndexOutOfBoundsException(I18N.getString("WBMPImageReader0")); 122 } 123 } 124 125 public void readHeader() throws IOException { 126 if (gotHeader) 127 return; 128 129 if (iis == null) { 130 throw new IllegalStateException("Input source not set!"); 131 } 132 133 metadata = new WBMPMetadata(); 138 // check for valid wbmp image 139 if (fixHeaderField != 0 140 || !isValidWbmpType(wbmpType)) 141 { 142 throw new IIOException(I18N.getString("WBMPImageReader2")); 143 } 144 145 metadata.wbmpType = wbmpType; 146 147 // Read image width 148 width = ReaderUtil.readMultiByteInteger(iis); 149 metadata.width = width; 150 151 // Read image height 152 height = ReaderUtil.readMultiByteInteger(iis); 153 metadata.height = height; 154 155 gotHeader = true; 156 } 157 158 @Override 159 public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex) 160 throws IOException { 161 checkIndex(imageIndex); 162 readHeader(); 163 164 BufferedImage bi = 165 new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY); 166 ArrayList<ImageTypeSpecifier> list = new ArrayList<>(1); 167 list.add(new ImageTypeSpecifier(bi)); 168 return list.iterator(); 169 } 170 171 @Override 172 public ImageReadParam getDefaultReadParam() { 173 return new ImageReadParam(); 174 } 175 176 @Override 177 public IIOMetadata getImageMetadata(int imageIndex) 178 throws IOException { 179 checkIndex(imageIndex); 180 if (metadata == null) { 181 readHeader(); 182 } 183 return metadata; 184 } 185 186 @Override 187 public IIOMetadata getStreamMetadata() throws IOException { 188 return null; 189 } 190 191 @Override 192 public BufferedImage read(int imageIndex, ImageReadParam param) 193 throws IOException { 194 195 if (iis == null) { 196 throw new IllegalStateException(I18N.getString("WBMPImageReader1")); 197 } 198 199 checkIndex(imageIndex); 200 clearAbortRequest(); 201 processImageStarted(imageIndex); 202 if (param == null) 203 param = getDefaultReadParam(); 204 205 //read header 206 readHeader(); 207 208 Rectangle sourceRegion = new Rectangle(0, 0, 0, 0); 209 Rectangle destinationRegion = new Rectangle(0, 0, 0, 0); 210 211 computeRegions(param, this.width, this.height, 287 data[k + destPos[i]] |= v << destOff[i]; 288 } 289 290 k += lineStride; 291 iis.skipBytes(skipLength); 292 processImageUpdate(bi, 293 0, j, 294 destinationRegion.width, 1, 1, 1, 295 new int[]{0}); 296 processImageProgress(100.0F*j/destinationRegion.height); 297 } 298 } 299 300 if (abortRequested()) 301 processReadAborted(); 302 else 303 processImageComplete(); 304 return bi; 305 } 306 307 @Override 308 public boolean canReadRaster() { 309 return true; 310 } 311 312 @Override 313 public Raster readRaster(int imageIndex, 314 ImageReadParam param) throws IOException { 315 BufferedImage bi = read(imageIndex, param); 316 return bi.getData(); 317 } 318 319 @Override 320 public void reset() { 321 super.reset(); 322 iis = null; 323 gotHeader = false; 324 } 325 326 /* 327 * This method verifies that given byte is valid wbmp type marker. 328 * At the moment only 0x0 marker is described by wbmp spec. 329 */ 330 boolean isValidWbmpType(int type) { 331 return type == 0; 332 } 333 } |