< prev index next >

src/java.desktop/share/classes/com/sun/imageio/plugins/wbmp/WBMPImageReader.java

Print this page




  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 }


  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     @Override
  81     public void setInput(Object input,
  82                          boolean seekForwardOnly,
  83                          boolean ignoreMetadata) {
  84         super.setInput(input, seekForwardOnly, ignoreMetadata);
  85         iis = (ImageInputStream) input; // Always works
  86         gotHeader = false;
  87     }
  88 
  89     /** Overrides the method defined in the superclass. */
  90     @Override
  91     public int getNumImages(boolean allowSearch) throws IOException {
  92         if (iis == null) {
  93             throw new IllegalStateException(I18N.getString("GetNumImages0"));
  94         }
  95         if (seekForwardOnly && allowSearch) {
  96             throw new IllegalStateException(I18N.getString("GetNumImages1"));
  97         }
  98         return 1;
  99     }
 100 
 101     @Override
 102     public int getWidth(int imageIndex) throws IOException {
 103         checkIndex(imageIndex);
 104         readHeader();
 105         return width;
 106     }
 107 
 108     @Override
 109     public int getHeight(int imageIndex) throws IOException {
 110         checkIndex(imageIndex);
 111         readHeader();
 112         return height;
 113     }
 114 
 115     @Override
 116     public boolean isRandomAccessEasy(int imageIndex) throws IOException {
 117         checkIndex(imageIndex);
 118         return true;
 119     }
 120 
 121     private void checkIndex(int imageIndex) {
 122         if (imageIndex != 0) {
 123             throw new IndexOutOfBoundsException(I18N.getString("WBMPImageReader0"));
 124         }
 125     }
 126 
 127     public void readHeader() throws IOException {
 128         if (gotHeader)
 129             return;
 130 
 131         if (iis == null) {
 132             throw new IllegalStateException("Input source not set!");
 133         }
 134 
 135         metadata = new WBMPMetadata();


 140         // check for valid wbmp image
 141         if (fixHeaderField != 0
 142             || !isValidWbmpType(wbmpType))
 143         {
 144             throw new IIOException(I18N.getString("WBMPImageReader2"));
 145         }
 146 
 147         metadata.wbmpType = wbmpType;
 148 
 149         // Read image width
 150         width = ReaderUtil.readMultiByteInteger(iis);
 151         metadata.width = width;
 152 
 153         // Read image height
 154         height = ReaderUtil.readMultiByteInteger(iis);
 155         metadata.height = height;
 156 
 157         gotHeader = true;
 158     }
 159 
 160     @Override
 161     public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex)
 162         throws IOException {
 163         checkIndex(imageIndex);
 164         readHeader();
 165 
 166         BufferedImage bi =
 167             new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY);
 168         ArrayList<ImageTypeSpecifier> list = new ArrayList<>(1);
 169         list.add(new ImageTypeSpecifier(bi));
 170         return list.iterator();
 171     }
 172 
 173     @Override
 174     public ImageReadParam getDefaultReadParam() {
 175         return new ImageReadParam();
 176     }
 177 
 178     @Override
 179     public IIOMetadata getImageMetadata(int imageIndex)
 180         throws IOException {
 181         checkIndex(imageIndex);
 182         if (metadata == null) {
 183             readHeader();
 184         }
 185         return metadata;
 186     }
 187 
 188     @Override
 189     public IIOMetadata getStreamMetadata() throws IOException {
 190         return null;
 191     }
 192 
 193     @Override
 194     public BufferedImage read(int imageIndex, ImageReadParam param)
 195         throws IOException {
 196 
 197         if (iis == null) {
 198             throw new IllegalStateException(I18N.getString("WBMPImageReader1"));
 199         }
 200 
 201         checkIndex(imageIndex);
 202         clearAbortRequest();
 203         processImageStarted(imageIndex);
 204         if (param == null)
 205             param = getDefaultReadParam();
 206 
 207         //read header
 208         readHeader();
 209 
 210         Rectangle sourceRegion = new Rectangle(0, 0, 0, 0);
 211         Rectangle destinationRegion = new Rectangle(0, 0, 0, 0);
 212 
 213         computeRegions(param, this.width, this.height,


 289                     data[k + destPos[i]] |= v << destOff[i];
 290                 }
 291 
 292                 k += lineStride;
 293                 iis.skipBytes(skipLength);
 294                         processImageUpdate(bi,
 295                                            0, j,
 296                                            destinationRegion.width, 1, 1, 1,
 297                                            new int[]{0});
 298                         processImageProgress(100.0F*j/destinationRegion.height);
 299             }
 300         }
 301 
 302         if (abortRequested())
 303             processReadAborted();
 304         else
 305             processImageComplete();
 306         return bi;
 307     }
 308 
 309     @Override
 310     public boolean canReadRaster() {
 311         return true;
 312     }
 313 
 314     @Override
 315     public Raster readRaster(int imageIndex,
 316                              ImageReadParam param) throws IOException {
 317         BufferedImage bi = read(imageIndex, param);
 318         return bi.getData();
 319     }
 320 
 321     @Override
 322     public void reset() {
 323         super.reset();
 324         iis = null;
 325         gotHeader = false;
 326     }
 327 
 328     /*
 329      * This method verifies that given byte is valid wbmp type marker.
 330      * At the moment only 0x0 marker is described by wbmp spec.
 331      */
 332     boolean isValidWbmpType(int type) {
 333         return type == 0;
 334     }
 335 }
< prev index next >