📄 bitmap.java
字号:
isvalid = true;
break;
}
if( !isvalid ) return false;
if( this.dataSize < 0 ) return false;
if( this.hResolution < 0 ) return false;
if( this.vResolution < 0 ) return false;
if( this.colors < 0 ) return false;
if( this.importantColors < 0 ) return false;
return true;
}
public boolean read(byte[] buffer, int index) {
if( buffer.length < index + 0x0036 ) return false;
this.flag = getString(buffer, 0x0000, 2);
this.fileSize = getint(buffer, 0x0002);
this.reserved = getint(buffer, 0x0006);
this.offset = getint(buffer, 0x000A);
this.headerSize = getint(buffer, 0x000E);
this.width = getint(buffer, 0x0012);
this.height = getint(buffer, 0x0016);
this.planes = getshort(buffer, 0x001A);
this.bitsPerPixel = getshort(buffer, 0x001C);
this.compression = getint(buffer, 0x001E);
this.dataSize = getint(buffer, 0x0022);
this.hResolution = getint(buffer, 0x0026);
this.vResolution = getint(buffer, 0x002A);
this.colors = getint(buffer, 0x002E);
this.importantColors= getint(buffer, 0x0032);
int paletteSize = this.offset - 0x0036;
if( paletteSize < 0 || (paletteSize % Palette.SIZE) != 0 ) return false;
if( buffer.length < index + 0x0036 + paletteSize ) return false;
int paletteCount = paletteSize / Palette.SIZE;
this.palette = new Palette[paletteCount];
for(int i=0; i<paletteCount; i++) {
this.palette[i].read(buffer, 0x0036+i*Palette.SIZE);
}
return this.valid();
}
public byte[] getByte() {
byte[] buffer = new byte[100];
fill(buffer, 0, this.flag.getBytes(), 0, 2);
fill(buffer, 0x0002, this.fileSize);
fill(buffer, 0x0006, this.reserved);
fill(buffer, 0x000A, this.offset);
fill(buffer, 0x000E, this.headerSize);
fill(buffer, 0x0012, this.width);
fill(buffer, 0x0016, this.height);
fill(buffer, 0x001A, this.planes);
fill(buffer, 0x001C, this.bitsPerPixel);
fill(buffer, 0x001E, this.compression);
fill(buffer, 0x0022, this.dataSize);
fill(buffer, 0x0026, this.hResolution);
fill(buffer, 0x002A, this.vResolution);
fill(buffer, 0x002E, this.colors);
fill(buffer, 0x0032, this.importantColors);
if( this.palette != null ) {
for(int i=0; i<this.palette.length; i++)
fill(buffer, 0x0036+i*Palette.SIZE, this.palette[i].getByte());
}
return buffer;
}
public void setFlag(String flag) {
this.flag = flag;
}
public void setFileSize(int fileSize) {
this.fileSize = fileSize;
}
public void setOffset(int offset) {
this.offset = offset;
}
public void setHeaderSize(int headerSize) {
this.headerSize = headerSize;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public void setBitsPerPixel(short bitsPerPixel) {
this.bitsPerPixel = bitsPerPixel;
}
public void setCompression(int compression) {
this.compression = compression;
}
public void setDataSize(int dataSize) {
this.dataSize = dataSize;
}
public void setHResolution(int resolution) {
hResolution = resolution;
}
public void setVResolution(int resolution) {
vResolution = resolution;
}
public void setColors(int colors) {
this.colors = colors;
}
public void setImportantColors(int importantColors) {
this.importantColors = importantColors;
}
public void setPalette(Palette[] palette) {
this.palette = palette;
}
public String getFlag() {
return flag;
}
public int getFileSize() {
return fileSize;
}
public int getOffset() {
return offset;
}
public int getHeaderSize() {
return headerSize;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public short getPlanes() {
return planes;
}
public short getBitsPerPixel() {
return bitsPerPixel;
}
public int getCompression() {
return compression;
}
public int getDataSize() {
return dataSize;
}
public int getHResolution() {
return hResolution;
}
public int getVResolution() {
return vResolution;
}
public int getColors() {
return colors;
}
public int getImportantColors() {
return importantColors;
}
public Palette[] getPalette() {
return palette;
}
}
/**
* BITMAP调色板表项
* 用4个字节来描述RGB的值:
* 1字节用于蓝色分量
* 1字节用于绿色分量
* 1字节用于红色分量
* 1字节用于填充符(设置为0)
*
* @author Katherine
*
*/
private class Palette {
private final static int SIZE = 4;
private byte blue;
private byte green;
private byte red;
private byte flag = 0;
public boolean valid() {
if( flag != 0 ) return false;
return true;
}
public void read(byte[] buffer, int index) {
this.blue = buffer[index + index];
this.green = buffer[index + index + 1];
this.red = buffer[index + index + 2];
this.flag = buffer[index + index + 3];
}
public byte[] getByte() {
byte[] buffer = new byte[4];
buffer[0] = blue;
buffer[1] = green;
buffer[2] = red;
buffer[3] = flag;
return buffer;
}
public int getColor() {
return this.red * 0x10000 + this.green * 0x100 + this.blue;
}
public byte getBlue() {
return blue;
}
public void setBlue(byte blue) {
this.blue = blue;
}
public byte getGreen() {
return green;
}
public void setGreen(byte green) {
this.green = green;
}
public byte getRed() {
return red;
}
public void setRed(byte red) {
this.red = red;
}
}
private void fill(byte[] buffer, int offset, byte[] value) {
for(int i=0; i<value.length; i++)
buffer[offset+i] = value[i];
}
private void fill(byte[] buffer, int offset, short value) {
int temp = value;
byte[] b = new byte[2];
for (int i= b.length-1; i>-1; i--){
b[i] = new Integer(temp&0xff).byteValue(); //将最高位保存在最低位
temp = temp >> 8; //向右移8位
}
this.fill(buffer, offset, b);
}
private String getString(byte[] buffer, int index, int length) {
return new String(this.getbytes(buffer, index, length));
}
private byte[] getbytes(byte[] buffer, int index, int length) {
byte[] result = new byte[length];
for(int i=0; i<length; i++)
result[i] = buffer[index+i];
return result;
}
private int getint(byte[] buffer, int index) {
return this.getLong(buffer, index, 4).intValue();
}
private short getshort(byte[] buffer, int index) {
return this.getLong(buffer, index, 2).shortValue();
}
private void fill(byte[] buffer, int offset, long value) {
int temp = new Long(value).intValue();
byte[] b = new byte[4];
for (int i=b.length-1; i>-1; i--){
b[i] = new Integer(temp&0xff).byteValue(); //将最高位保存在最低位
temp = temp >> 8; //向右移8位
}
this.fill(buffer, offset, b);
}
private void fill(byte[] toBuffer, int toIndex, byte[] fromBuffer, int fromIndex, int length) {
for(int i=0; i<length; i++)
toBuffer[i+toIndex] = fromBuffer[i+fromIndex];
}
private Long getLong(byte[] buffer, int index, int length) {
if( length > 8 ) return new Long(0);
if( buffer == null || buffer.length < index+length ) return new Long(0);
String s = "";
for(int i=0; i<length; i++) {
String ss = "00" + Integer.toHexString(buffer[index+i]);
s = ss.substring(ss.length()-2) + s;
}
return Long.valueOf(s, 16);
}
private boolean isequal(byte[] source, byte[] dest) {
if( source == null && dest == null ) return true;
if( source == null || dest == null ) return false;
if( source.length != dest.length ) return false;
for(int i=0; i<source.length; i++)
if( source[i] != dest[i] ) return false;
return true;
}
public void setHeader(Header header) {
this.header = header;
}
public void setData(byte[] data) {
this.data = data;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -