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