Source for gnu.javax.imageio.bmp.BMPInfoHeader

   1: /* BMPInfoHeader.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.io.OutputStream;
  43: import java.nio.ByteBuffer;
  44: import java.nio.ByteOrder;
  45: import java.awt.Dimension;
  46: 
  47: public class BMPInfoHeader {
  48:     /** Size of the bitmap info header*/
  49:     private int biSize; 
  50: 
  51:     /** Pixel width of the bitmap */
  52:     private int biWidth; 
  53: 
  54:     /** Pixel height of the bitmap */
  55:     private int biHeight;
  56: 
  57:     /** Number of bitplanes = 1 */
  58:     private short biPlanes;
  59: 
  60:     /** Number of bpp = 1,4,8,24 */
  61:     private short biBitCount;
  62: 
  63:     /** Compression type, RGB8, RLE8, RLE4, BITFIELDS */
  64:     private int biCompression;
  65: 
  66:     /** Byte size of the uncompressed bitmap, can be 0. */
  67:     private int biSizeImage;
  68: 
  69:     /** X resolution, dots per meter */
  70:     private int biXPelsPerMeter;
  71: 
  72:     /** Y resolution, dots per meter */
  73:     private int biYPelsPerMeter;
  74: 
  75:     /** Number of colors used (palette only, can be 0 for all) */
  76:     private int biClrUsed;
  77: 
  78:     /** Number of 'important' colors, 0 for all */
  79:     private int biClrImportant;
  80: 
  81:     /** BITMAPINFOHEADER is 40 bytes */
  82:     public static final int SIZE = 40;
  83: 
  84:     /**
  85:      * Compression types
  86:      */
  87:     public static final int BI_RGB = 0;
  88:     public static final int BI_RLE8 = 1;
  89:     public static final int BI_RLE4 = 2;
  90:     public static final int BI_BITFIELDS = 3;
  91: 
  92:     /**
  93:      * Creates the header from an input stream, which is not closed.
  94:      * @throws IOException if an I/O error occured.
  95:      * @throws BMPException if the header was invalid
  96:      */
  97:     public BMPInfoHeader(ImageInputStream in) throws IOException, BMPException {
  98:         byte[] data = new byte[SIZE];
  99:         
 100:     if (in.read(data) != SIZE)
 101:         throw new IOException("Couldn't read header.");
 102:     ByteBuffer buf = ByteBuffer.wrap(data);
 103:     buf.order(ByteOrder.LITTLE_ENDIAN);
 104: 
 105:     int n;
 106:     if((n=buf.getInt()) != SIZE)
 107:         throw new BMPException("Invalid BITMAPINFOHEADER size: "+n);
 108: 
 109:     biWidth = buf.getInt();
 110:     biHeight = buf.getInt();
 111:     biPlanes = buf.getShort();
 112:     setBitCount(buf.getShort());
 113:     setCompression(buf.getInt());
 114:     biSizeImage = buf.getInt();
 115:     biXPelsPerMeter = buf.getInt();
 116:     biYPelsPerMeter = buf.getInt();
 117:     biClrUsed = buf.getInt();
 118:     biClrImportant = buf.getInt();
 119:     }
 120: 
 121:     public void setBitCount(short bitcount) throws BMPException {
 122:     switch(bitcount){
 123:     case 1:
 124:     case 4:
 125:     case 8:
 126:     case 16:
 127:     case 24:
 128:     case 32:
 129:         biBitCount = bitcount;
 130:         break;
 131: 
 132:     default:
 133:         throw new BMPException("Invalid number of bits per pixel: "+
 134:                    bitcount);
 135:     }
 136:     }
 137: 
 138:     public short getBitCount() { return biBitCount; }
 139: 
 140:     public void setCompression(int compression) throws BMPException {
 141:     switch(compression){
 142:     case BI_RLE8:
 143:         if(getBitCount() != 8)
 144:         throw new BMPException("Invalid number of bits per pixel.");
 145:         biCompression = compression;
 146:         break;
 147:     case BI_RLE4:
 148:         if(getBitCount() != 4)
 149:         throw new BMPException("Invalid number of bits per pixel.");
 150:         biCompression = compression;
 151:         break;
 152: 
 153:     case BI_RGB:
 154:     case BI_BITFIELDS:
 155:         biCompression = compression;
 156:         break;
 157: 
 158:     default:
 159:         throw new BMPException("Unknown bitmap compression type.");
 160:     }
 161:     }
 162: 
 163:     public int getNumberOfPaletteEntries(){
 164:     if(biClrUsed == 0)
 165:         switch(biBitCount){
 166:         case 1:
 167:         return 2;
 168:         case 4:
 169:         return 16;
 170:         case 8:
 171:         return 256;
 172: 
 173:         default: // should not happen
 174:         return 0;
 175:         }
 176: 
 177:     return biClrUsed;
 178:     }
 179: 
 180:     public int getCompression(){
 181:     return biCompression;
 182:     }
 183: 
 184:     public Dimension getSize(){
 185:     return new Dimension(biWidth, biHeight);
 186:     }
 187: 
 188:     public int getWidth(){
 189:     return biWidth;
 190:     }
 191: 
 192:     public int getHeight(){
 193:     return biHeight;
 194:     }
 195: 
 196:     public void setSize(Dimension d){
 197:     biWidth = (int)d.getWidth();
 198:     biHeight = (int)d.getHeight();
 199:     }
 200: 
 201: }