Source for gnu.javax.imageio.bmp.BMPImageReader

   1: /* BMPImageReader.java --
   2:    Copyright (C)  2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.javax.imageio.bmp;
  39: 
  40: import java.io.IOException;
  41: import javax.imageio.*;
  42: import javax.imageio.spi.*;
  43: import javax.imageio.metadata.*;
  44: import javax.imageio.stream.ImageInputStream;
  45: import java.util.Iterator;
  46: import java.awt.image.BufferedImage;
  47: 
  48: public class BMPImageReader extends ImageReader {
  49:     private BMPInfoHeader infoHeader;
  50:     private BMPFileHeader fileHeader;
  51:     private BMPDecoder decoder;
  52: 
  53:     protected BMPImageReader(ImageReaderSpi originatingProvider){
  54:         super(originatingProvider);
  55:         infoHeader = null;
  56:         fileHeader = null;
  57:         decoder = null;
  58:     }
  59: 
  60:     private void validateIndex(int imageIndex)
  61:         throws IndexOutOfBoundsException {
  62:         if (imageIndex != 0)
  63:             throw new IndexOutOfBoundsException("Invalid image index.");
  64:     }
  65: 
  66:     public void setInput(Object input) {
  67:         super.setInput(input);
  68:     }
  69: 
  70:     public void setInput(Object input,
  71:                          boolean seekForwardOnly,
  72:                          boolean ignoreMetadata) {
  73:         super.setInput(input, seekForwardOnly, ignoreMetadata);
  74:     }
  75: 
  76:     public void setInput(Object input, boolean isStreamable) {
  77:         super.setInput(input, isStreamable);
  78: 
  79:         if (!(input instanceof ImageInputStream))
  80:             throw new IllegalArgumentException("Input not an ImageInputStream.");
  81:     }
  82: 
  83:     private void checkStream() throws IOException {
  84:         if (!(input instanceof ImageInputStream))
  85:             throw new IllegalStateException("Input not an ImageInputStream.");
  86:         if(input == null)
  87:             throw new IllegalStateException("No input stream.");
  88: 
  89:     }
  90: 
  91:     private void readHeaders() throws IOException, IIOException {
  92:         if(fileHeader != null)
  93:             return;
  94: 
  95:         checkStream();
  96: 
  97:         fileHeader = new BMPFileHeader((ImageInputStream)input);
  98:         infoHeader = new BMPInfoHeader((ImageInputStream)input);
  99:         decoder = BMPDecoder.getDecoder(fileHeader, infoHeader);
 100:     }
 101: 
 102:     public int getWidth(int imageIndex) throws IOException {
 103:         validateIndex(imageIndex);
 104:         readHeaders();
 105:         return infoHeader.getWidth();
 106:     }
 107: 
 108:     public int getHeight(int imageIndex) throws IOException {
 109:         validateIndex(imageIndex);
 110:         readHeaders();
 111:         return infoHeader.getHeight();
 112:     }
 113: 
 114:     public Iterator getImageTypes(int imageIndex){
 115:         validateIndex(imageIndex);
 116:         return null;
 117:     }
 118: 
 119:     /**
 120:      * Returns the number of images. BMP files can only contain a single one.
 121:      */
 122:     public int getNumImages(boolean allowSearch){
 123:         return 1;
 124:     }
 125: 
 126: 
 127:     // FIXME: Support metadata
 128:     public IIOMetadata getImageMetadata(int imageIndex){
 129:         validateIndex(imageIndex);
 130:         return null;
 131:     }
 132: 
 133:     // FIXME: Support metadata
 134:     public IIOMetadata getStreamMetadata(){
 135:         return null;
 136:     }
 137: 
 138:     /**
 139:      * Reads the image indexed by imageIndex and returns it as
 140:      * a complete BufferedImage, using a supplied ImageReadParam.
 141:     */
 142:     public BufferedImage read(int imageIndex, ImageReadParam param)
 143:         throws IOException, IIOException {
 144:         validateIndex(imageIndex);
 145:         readHeaders();
 146:         return decoder.decode((ImageInputStream)input);
 147:     }
 148: }