Source for gnu.javax.imageio.bmp.BMPDecoder

   1: /* BMPDecoder.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.stream.ImageInputStream;
  42: import java.nio.ByteBuffer;
  43: import java.nio.ByteOrder;
  44: import java.awt.image.ColorModel;
  45: import java.awt.image.IndexColorModel;
  46: import java.awt.image.BufferedImage;
  47: 
  48: public abstract class BMPDecoder {
  49: 
  50:     protected BMPInfoHeader infoHeader;
  51:     protected BMPFileHeader fileHeader;
  52:     protected long offset;
  53:     
  54:     public BMPDecoder(BMPFileHeader fh, BMPInfoHeader ih){
  55:     fileHeader = fh;
  56:     infoHeader = ih;
  57:     offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE;
  58:     }
  59: 
  60:     /**
  61:      * Determines the coding type of the bitmap and returns the corresponding
  62:      * decoder.
  63:      */
  64:     public static BMPDecoder getDecoder(BMPFileHeader fh, BMPInfoHeader ih){
  65:     switch(ih.getCompression()){
  66:     case BMPInfoHeader.BI_RGB: // uncompressed RGB
  67:         switch(ih.getBitCount()){
  68:         case 32:
  69:         return new DecodeBF32(fh, ih, true);
  70: 
  71:         case 24:
  72:         return new DecodeRGB24(fh, ih);
  73: 
  74:         case 16:
  75:         return new DecodeBF16(fh, ih, true);
  76: 
  77:         case 8:
  78:         return new DecodeRGB8(fh, ih);
  79: 
  80:         case 4:
  81:         return new DecodeRGB4(fh, ih);
  82: 
  83:         case 1:
  84:         return new DecodeRGB1(fh, ih);
  85: 
  86:         default:
  87:         return null;
  88:         }
  89: 
  90:     case BMPInfoHeader.BI_RLE8:
  91:         return new DecodeRLE8(fh, ih);
  92: 
  93:     case BMPInfoHeader.BI_RLE4:
  94:         return new DecodeRLE4(fh, ih);
  95: 
  96:     case BMPInfoHeader.BI_BITFIELDS:
  97:         switch(ih.getBitCount()){
  98:         case 16:
  99:         return new DecodeBF16(fh, ih, false);
 100: 
 101:         case 32:
 102:         return new DecodeBF32(fh, ih, false);
 103: 
 104:         default:
 105:         return null;
 106:         }
 107:         
 108:     default:
 109:         return null;
 110:     }
 111:     }
 112: 
 113:     /**
 114:      * The image decoder.
 115:      */
 116:     public abstract BufferedImage decode(ImageInputStream in) 
 117:     throws IOException, BMPException;
 118: 
 119:     /**
 120:      * Reads r,g,b bit masks from an inputstream
 121:      */
 122:     protected int[] readBitMasks(ImageInputStream in) throws IOException {
 123:     int[] bitmasks = new int[3];
 124:     byte[] temp = new byte[12];
 125:     if(in.read(temp) != 12)
 126:         throw new IOException("Couldn't read bit masks.");
 127:     offset += 12;
 128: 
 129:     ByteBuffer buf = ByteBuffer.wrap(temp);
 130:     buf.order(ByteOrder.LITTLE_ENDIAN);
 131:     bitmasks[0] = buf.getInt();
 132:     bitmasks[1] = buf.getInt();
 133:     bitmasks[2] = buf.getInt();
 134:     return bitmasks;
 135:     }
 136: 
 137:     /**
 138:      * Reads an N-color palette from an inputstream in RGBQUAD format and 
 139:      * returns an equivalent ColorModel object
 140:      */
 141:     protected IndexColorModel readPalette(ImageInputStream in) throws IOException {
 142:     int N = infoHeader.getNumberOfPaletteEntries();
 143:     byte[] r = new byte[N];
 144:     byte[] g = new byte[N];
 145:     byte[] b = new byte[N];
 146:     for(int i=0;i<N;i++){
 147:         byte[] RGBquad = new byte[4];
 148:         if(in.read(RGBquad) != 4)
 149:         throw new IOException("Error reading palette information.");
 150:         // RGBQUAD structure is b,g,r,0
 151:         r[i] = RGBquad[2];
 152:         g[i] = RGBquad[1];
 153:         b[i] = RGBquad[0];
 154:     }
 155:     
 156:     offset += 4*N;
 157:     return new IndexColorModel(8, N, r, g, b);
 158:     }
 159: 
 160:     /**
 161:      * Read bytes to the start of the image data
 162:      */
 163:     protected void skipToImage(ImageInputStream in) throws IOException {
 164:     byte[] d = new byte[1];
 165:     long n = fileHeader.getOffset() - offset;
 166:     for(int i=0;i<n;i++)
 167:         in.read(d);
 168:     }
 169: }