class ImageCanvas
extends Canvas {
byte[] b;
int imagelength;
Image i;
ImageCanvas() {
imageRGBConvertInit();
try {
b = readImageToBytesEasy("/1.png", is);//Zane 40
//Zane b = readImageToBytes("/1.png", is);//Zane 60
//Zane b = readImageToBytes("/1.png", is,2355);//Zane 通用的
}
catch (IOException ioe) {}
b = imageRGBConvert(b, 0, 0, 0, 0, 255, 0);
if(b==null)
System.out.println("Image file do not contain this COLOR!");
try {
i = Image.createImage(b, 0, b.length);
}
catch (Exception e) {
System.out.println(e + "CREATEIMAGE");
}
}
public void paint(Graphics g) {
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(i, getWidth() / 2, getHeight() / 2, g.VCENTER | g.HCENTER);
}
public byte[] imageRGBConvert(byte[] image, int R, int G, int B, int cR,
int cG, int cB) {
int i;
int crc;
int length = 0;
int temp;
String[] sIDAT = {
"49", "44", "41", "54"};
String[] sPLTE = {
"50", "4c", "54", "45"};
int cnt = 0;
if (image[1] != 0x50 ||
image[2] != 0x4e ||
image[3] != 0x47 ||
image[4] != 0x0d ||
image[5] != 0x0a ||
image[6] != 0x1a ||
image[7] != 0x0a) {
System.out.println("INCORRECT");
return null;
}
while (true) {
for (i = 0; i < image.length; i++) {
if (Integer.toHexString(image[i]).equals(sPLTE[cnt])) {
cnt++;
if (cnt >= 4) {
for (int j = 4; j > 0; j--) {
if (image[i - j - 3] != 0x00) {
switch (j) {
case 4:
length = image[i - j - 3] * 0xffffff;
break;
case 3:
length = image[i - j - 3] * 0xffff;
break;
case 2:
length = image[i - j - 3] * 0xff;
break;
case 1:
length = image[i - j - 3] * 1;
break;
default:
length = -1;
System.out.println("length ERROR!");
break;
}
}
}
if(!changeColor(image, length, i + 1, R, G, B, cR, cG, cB))
return null;//Zane 图片里没有这个颜色。
cnt = 0;
break;
}
}
else {
if (Integer.toHexString(image[i]).equals(sIDAT[cnt])) {
cnt++;
if (cnt >= 4) {
System.out.println(
"This file do NOT contain PLTE!!!You should recreate your PNG file.");
break;
}
}
else {
if (cnt != 0) {
cnt = 0;
}
continue;
}
}
}
break;
}
crc = updateCRC(image, i - 3, length + 4);
image[i + length + 4] = (byte) (crc >>> 0);
image[i + length + 3] = (byte) (crc >>> 8);
image[i + length + 2] = (byte) (crc >>> 16);
image[i + length + 1] = (byte) (crc >>> 24);
System.out.println("CRC==" + Integer.toHexString(crc));
return image;
}
public byte[] readImageToBytes(String url, InputStream is, int length) throws
IOException { //Zane 通用的,效率高
System.out.println("readImageToBytes LENGTH");
byte[] ref;
is = getClass().getResourceAsStream(url);
//System.out.println(is.available());
ref = new byte[length];
is.read(ref);
is.close();
System.gc();
return ref;
}
public byte[] readImageToBytesEasy(String url, InputStream is) throws
IOException { //Zane for 40 效率低
byte[] ref;
is = getClass().getResourceAsStream(url);
int length = 0;
while (is.read() != -1) {
length++;
}
System.out.println("length==" + length);
is = getClass().getResourceAsStream(url);
ref = new byte[length];
is.read(ref);
is.close();
System.gc();
return ref;
}
public byte[] readImageToBytes(String url, InputStream is) throws
IOException { //Zane for 60 , 在60上支持is.available()
byte[] ref;
is = getClass().getResourceAsStream(url);
ref = new byte[is.available()];
is.read(ref);
is.close();
System.gc();
return ref;
}
public boolean changeColor(byte[] image, int length, int i, int r, int g,
int b, int r1,
int g1, int b1) {
int temp = i;
while (true) {
if (i - temp > length) {
return false;
}
if (image[i] == getValue(r) && image[i + 1] == getValue(g) &&
image[i + 2] == getValue(b)) {
image[i] = getValue(r1);
image[i + 1] = getValue(g1);
image[i + 2] = getValue(b1);
return true;
}
else {
i += 3;
}
}
}
public byte getValue(int i) {
return i <= -1 ? (byte) (i + 256) : (byte) (i);
}
}
//Zane 重新生成CRC校验
static int[] crcTable = new int[256];
public void imageRGBConvertInit() {
// Initialize CRC table
for (int n = 0; n < 256; n++) {
int c = n;
for (int k = 0; k < 8; k++) {
if ( (c & 1) == 1) {
c = 0xedb88320 ^ (c >>> 1);
}
else {
c >>>= 1;
}
crcTable[n] = c;
}
}
}
public int updateCRC(byte[] data, int off, int len) {
int c = 0xffffffff;
for (int n = 0; n < len; n++) {
c = crcTable[ (c ^ data[off + n]) & 0xff] ^ (c >>> 8);
}
return c ^ 0xffffffff;
}