【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了BMPLoader.java相关资料,希望对各位网友有所帮助!
package javazoom.Util;
/**
* This class implements a BMP Loader.
*/
import java.awt.*;
import java.awt.image.*;
import java.io.*;
/**
* A decoder for Windows bitmap (.BMP) files.
* Compression not supported.
*/
public class BMPLoader
{
private InputStream is;
private int curPos = 0;
private int bitmapOffset; // starting position of image data
private int width; // image width in pixels
private int height; // image height in pixels
private short bitsPerPixel; // 1, 4, 8, or 24 (no color map)
private int compression; // 0 (none), 1 (8-bit RLE), or 2 (4-bit RLE)
private int actualSizeOfBitmap;
private int scanLineSize;
private int actualColorsUsed;
private byte r[], g[], b[]; // color palette
private int noOfEntries;
private byte[] byteData; // Unpacked data
private int[] intData; // Unpacked data
public BMPLoader()
{
}
public Image getBMPImage(InputStream stream) throws Exception
{
read(stream);
return Toolkit.getDefaultToolkit().createImage(getImageSource());
}
private int readInt() throws IOException {
int b1 = is.read();
int b2 = is.read();
int b3 = is.read();
int b4 = is.read();
curPos += 4;
return ((b4 << 24) + (b3 << 16) + (b2 << 8) + (b1 << 0));
}
private short readShort() throws IOException {
int b1 = is.read();
int b2 = is.read();
curPos += 4;
return (short)((b2 << 8) + b1);
}
void getFileHeader() throws IOException, Exception {
// Actual contents (14 bytes):
short fileType = 0x4d42;// always "BM"
int fileSize; // size of file in bytes
short reserved1 = 0; // always 0
short reserved2 = 0; // always 0
fileType = readShort();
if (fileType != 0x4d42)
throw new Exception("Not a BMP file"); // wrong file type
fileSize = readInt();
reserved1 = readShort();
reserved2 = readShort();
bitmapOffset = readInt();
}
void getBitmapHeader() throws IOException {
// Actual contents (40 bytes):
int size; // size of this header in bytes
short planes; // no. of color planes: always 1
int sizeOfBitmap; // size of bitmap in bytes (may be 0: if so, calculate)
int horzResolution; // horizontal resolution, pixels/meter (may be 0)
int vertResolution; // vertical resolution, pixels/meter (may be 0)
int colorsUsed; // no. of colors in palette (if 0, calculate)
int colorsImportant; // no. of important colors (appear first in palette) (0 means all are important)
boolean topDown;
int noOfPixels;
size = readInt();
width = readInt();
height = readInt();
planes = readShort();
bitsPerPixel = readShort();
compression = readInt();
sizeOfBitmap = readInt();
horzResolution = readInt();
vertResolution = readInt();
colorsUsed = readInt();
colorsImportant = readInt();
topDown = (height < 0);
noOfPixels = width * height;
// Scan line is padded with zeroes to be a multiple of four bytes
scanLineSize = ((width * bitsPerPixel + 31) / 32) * 4;
if (sizeOfBitmap != 0)
actualSizeOfBitmap = sizeOfBitmap;
else
// a value of 0 doesn't mean zero - it means we have to calculate it
actualSizeOfBitmap = scanLineSize * height;
if (colorsUsed != 0)
actualColorsUsed = colorsUsed;
else
// a value of 0 means we determine this based on the bits per pixel
if (bitsPerPixel < 16)
actualColorsUsed = 1 << bitsPerPixel;
else
actualColorsUsed = 0; // no palette
}
void getPalette() throws IOException {
noOfEntries = actualColorsUsed;
//IJ.write("noOfEntries: " + noOfEntries);
if (noOfEntries>0) {
r = new byte[noOfEntries];
g = new byte[noOfEntries];
b = new byte[noOfEntries];
int reserved;
for (int i = 0; i < noOfEntries; i++) {
b[i] = (byte)is.read();
g[i] = (byte)is.read();
r[i] = (byte)is.read();
reserved = is.read();
curPos += 4;
}
}
}
void unpack(byte[] rawData, int rawOffset, int[] intData, int intOffset, int w) {
int j = intOffset;
int k = rawOffset;
int mask = 0xff;
for (int i = 0; i < w; i++) {
int b0 = (((int)(rawData[k++])) & mask);
int b1 = (((int)(rawData[k++])) & mask) << 8;
int b2 = (((int)(rawData[k++])) & mask) << 16;
intData[j] = 0xff000000 | b0 | b1 | b2;
j++;
}
}
void unpack(byte[] rawData, int rawOffset, int bpp,
byte[] byteData, int byteOffset, int w) throws Exception {
int j = byteOffset;
int k = rawOffset;
byte mask;
int pixPerByte;
switch (bpp) {
case 1: mask = (byte)0x01; pixPerByte = 8; break;
case 4: mask = (byte)0x0f; pixPerByte = 2; break;
case 8: mask = (byte)0xff; pixPerByte = 1; break;
default:
throw new Exception("Unsupported bits-per-pixel value");
}
for (int i = 0;;) {
int