1: 
  37: 
  38: package ;
  39: 
  40: import ;
  41: import ;
  42: import ;
  43: import ;
  44: import ;
  45: import ;
  46: import ;
  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:     
 122:     public int getNumImages(boolean allowSearch){
 123:         return 1;
 124:     }
 125: 
 126: 
 127:     
 128:     public IIOMetadata getImageMetadata(int imageIndex){
 129:         validateIndex(imageIndex);
 130:         return null;
 131:     }
 132: 
 133:     
 134:     public IIOMetadata getStreamMetadata(){
 135:         return null;
 136:     }
 137: 
 138:     
 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: }