package bmpviewer.bmp; import java.io.*; /** * BMP image file reader *

* @author Hiroyuki Murata * @since JDK1.1 */ public class BmpReader extends BmpHeader { File file; FileInputStream fis; private int[] rgbquad,pix; private int wid,hei,widthbytes; public BmpReader(File file) { this.file = file; } public void read() throws IOException { fis = new FileInputStream(file); readFileHeader(); readInfoHeader(); readRGBQuad(); readBitmap(); fis.close(); } public int[] getPix() { return pix; } public int getWidth() { return wid; } public int getHeight() { return hei; } //------------------------------------------------------ void readFileHeader() throws IOException { fileheader = new ByteArrayManipulator(bf_size,false); if (!readFully(fis, fileheader.getBytes())) throw new IOException("insufficient BITMAPFILEHEADER"); bfType = fileheader.getU2(pbfType); bfSize = fileheader.getS4(pbfSize); bfOffBits = fileheader.getS4(pbfOffBits); if (bfType != 0x4d42) throw new IOException("not bmp file"); } void readInfoHeader() throws IOException { infoheader = new ByteArrayManipulator(bi_size,false); if (!readFully(fis, infoheader.getBytes())) throw new IOException("insufficient BITMAPINFOHEADER"); biSize = infoheader.getS4(pbiSize); biWidth = infoheader.getS4(pbiWidth); biHeight = infoheader.getS4(pbiHeight); biBitCount = infoheader.getU2(pbiBitCount); biCompression = infoheader.getS4(pbiCompression); if (biSize != bi_size) throw new IOException("OS2 bmp?"); wid = (int)biWidth; hei = (int)biHeight; widthbytes = ((wid * biBitCount + 31) & ~31) >>> 3; if ( (biBitCount != 1) && (biBitCount != 4) && (biBitCount != 8) && (biBitCount != 24) ) throw new IOException("invalid biBitCount:" + biBitCount); if (biCompression != 0) throw new IOException("compression not supported"); } void readRGBQuad() throws IOException { int rgbquadbytes = (int)bfOffBits - bf_size - bi_size; if (rgbquadbytes > 0) { byte[] bytesRgbQuad = new byte[rgbquadbytes]; if (!readFully(fis, bytesRgbQuad)) throw new IOException("insufficient color table"); int rgbquadlen = rgbquadbytes >> 2; rgbquad = new int[rgbquadlen]; for (int i=0; i 0) { xy -= wid; if (!readFully(fis, bline)) throw new IOException("insufficient bitmap"); switch (biBitCount) { case 1: // 2 colors for (int x=0; x>>3]; for (int i=0; i<8; i++) { iline[x++] = rgbquad[(by >>> 7) & 1]; by <<= 1; } } break; case 4: // 16 colors for (int x=0; x>>1]; iline[x++] = rgbquad[(by >>> 4) & 0x0f]; iline[x++] = rgbquad[ by & 0x0f]; } break; case 8: // 256 colors for (int x=0; x 0) { int rd = is.read(bts,off,len); if (rd < 0) return false; len -= rd; off += rd; } return true; } }