?? bmp.java
字號(hào):
package hideInfo;
import java.awt.Image;
import java.awt.image.*;
import java.awt.Toolkit;
import java.io.*;
import java.net.URL;
public class BMP {
private java.awt.Image image;
private BmpFileheader bmp_fileheader = new BmpFileheader();
private BmpInfoHeader bmp_infoheader = new BmpInfoHeader();
private BmpPalette bmp_palette;
private int width;
private int height;
public BMP(File f) {
try {
PCBinaryInputStream file = new PCBinaryInputStream(f);
read(file);
} catch (IOException e) {
System.err.println(e);
}
}
public BMP(URL url) {
try {
PCBinaryInputStream file = new PCBinaryInputStream(url);
read(file);
} catch (IOException e) {
System.err.println(e);
}
}
/**
* Creates an BMP from the given Image
*/
public BMP(Image image) {
this.image=image;
width=image.getWidth(null);
height=image.getHeight(null);
bmp_infoheader.biWidth = (short)width;
bmp_infoheader.biHeight = (short)height;
bmp_infoheader.biBitCount = 24;
bmp_infoheader.biClrUsed = 0;
}
public Image getImage() {
return image;
}
public void write(File f) {
try {
PCBinaryOutputStream file = new PCBinaryOutputStream(f);
write(file);
} catch (IOException e) {
System.err.println(e);
}
}
/**
* Saves the BMP in a given file.
*/
public void write(URL url) {
try {
PCBinaryOutputStream file = new PCBinaryOutputStream(url);
write(file);
} catch (IOException e) {
System.err.println(e);
}
}
/**
* Saves the BMP in a given file.
*/
void write(PCBinaryOutputStream file) {
try {
int rawData[] = new int[width*height];
PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, rawData, 0, width);
bmp_palette = new BmpPalette(grabber.getColorModel());
try {
grabber.grabPixels();
} catch (InterruptedException e) { System.err.println(e); }
ColorModel model = grabber.getColorModel();
bmp_fileheader.bfOffBits = bmp_fileheader.getSize() +
bmp_infoheader.getSize() +
bmp_palette.getSize();
int x=0,y=0,i=0,j=0,k=0;
byte bitmap[];
int w=width*3;
int h=height-1;
// dword align width
if (w%4 != 0)
w += (4-(w%4));
if (model instanceof IndexColorModel) {
// not tested - (can't get IndexColorModel from PixelGrabber??!)
bitmap = new byte[width*bmp_infoheader.biHeight];
for (y=h;y>=0;y--) {
i=(h-y)*width;
j=y*width;
for (x=0;x<width;x++) {
bitmap[j++]=(byte)(rawData[i+x]);
}
}
bmp_fileheader.bfSize = bmp_fileheader.bfOffBits +
width*bmp_infoheader.biHeight;
}
else {
bitmap = new byte[w*bmp_infoheader.biHeight];
for (y=0;y<height;y++) {
i=(h-y)*width;
j=y*w;
for (x=0;x<width;x++) {
k=i+x;
bitmap[j++]=(byte)((model.getBlue(rawData[k]))&0xFF);
bitmap[j++]=(byte)((model.getGreen(rawData[k]))&0xFF);
bitmap[j++]=(byte)((model.getRed(rawData[k]))&0xFF);
}
}
}
rawData = null;
// bmp_infoheader.biWidth=w;
bmp_fileheader.bfSize = bmp_fileheader.bfOffBits + w*bmp_infoheader.biHeight;
bmp_fileheader.write(file);
bmp_infoheader.write(file);
bmp_palette.write(file);
file.writeByteArray(bitmap);
bitmap = null;
file.close();
} catch (IOException e) { System.err.println(e); }
}
/**
* Read a BMP from a given file.
*/
void read(PCBinaryInputStream file) {
int coloursUsed = 0;
int scanlineSize = 0;
int bitplaneSize = 0;
byte [] rawData = null;
try {
bmp_fileheader.read(file);
bmp_infoheader.read(file);
if (bmp_infoheader.biClrUsed != 0)
coloursUsed = bmp_infoheader.biClrUsed;
else if (bmp_infoheader.biBitCount < 16)
coloursUsed = 1 << bmp_infoheader.biBitCount;
bmp_palette = new BmpPalette(coloursUsed);
bmp_palette.read(file);
long skip = bmp_fileheader.bfOffBits -
(bmp_fileheader.getSize()+
bmp_infoheader.getSize()+
bmp_palette.getSize());
if (skip > 0) {
file.skip(skip);
}
scanlineSize = ((bmp_infoheader.biWidth*bmp_infoheader.biBitCount+31)/32)*4;
if (bmp_infoheader.biSizeImage != 0)
bitplaneSize = bmp_infoheader.biSizeImage;
else
bitplaneSize = scanlineSize * bmp_infoheader.biHeight;
rawData = new byte[bitplaneSize];
file.readByteArray(rawData);
file.close();
} catch (IOException e) {
System.err.println(e);
}
if (rawData != null) {
if (bmp_infoheader.biBitCount > 8) {
image = unpack24(rawData, scanlineSize);
}
else {
image = unpack08(rawData, scanlineSize);
}
}
rawData = null;
}
Image unpack24(byte [] rawData, int scanlineSize) {
int b=0, k=0, x=0, y=0;
int [] data = new int[bmp_infoheader.biWidth * bmp_infoheader.biHeight];
try {
for (y=0; y < bmp_infoheader.biHeight; y++) {
b=(bmp_infoheader.biHeight-1-y)*bmp_infoheader.biWidth;
k=y*scanlineSize;
for (x=0; x < bmp_infoheader.biWidth; x++) {
data[x+b] = 0xFF000000 |
(((int)(rawData[k++])) & 0xFF) |
(((int)(rawData[k++])) & 0xFF) << 8 |
(((int)(rawData[k++])) & 0xFF) << 16;
}
}
}catch (Exception e) {};
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(bmp_infoheader.biWidth, bmp_infoheader.biHeight, ColorModel.getRGBdefault(), data, 0, bmp_infoheader.biWidth));
}
Image unpack08(byte [] rawData, int scanlineSize) {
int b=0, k=0, i=0, x=0, y=0;
byte [] data = new byte[bmp_infoheader.biWidth * bmp_infoheader.biHeight];
try {
if (bmp_infoheader.biBitCount == 1) {
for (y=0; y < bmp_infoheader.biHeight; y++) {
b=(bmp_infoheader.biHeight-1-y)*bmp_infoheader.biWidth;
k=y*scanlineSize;
for (x=0; x < (bmp_infoheader.biWidth-8); x+=8) {
data[b+x+7] = (byte)((rawData[k]) & 1);
data[b+x+6] = (byte)((rawData[k] >>> 1) & 1);
data[b+x+5] = (byte)((rawData[k] >>> 2) & 1);
data[b+x+4] = (byte)((rawData[k] >>> 3) & 1);
data[b+x+3] = (byte)((rawData[k] >>> 4) & 1);
data[b+x+2] = (byte)((rawData[k] >>> 5) & 1);
data[b+x+1] = (byte)((rawData[k] >>> 6) & 1);
data[b+x] = (byte)((rawData[k] >>> 7) & 1);
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -