为缺少调色板的png图片添加调色板
public class AddPLTE {
public AddPLTE (){
}
public byte[] getbyte(InputStream bm) {
try {
byte[] pixel = InputStream2Bytes2(bm);
byte[] haha = imgConvert(pixel);
return haha;
} catch (Exception e) {
return null;
}
}
public boolean noPlte(InputStream bm){
try {
boolean ispltepng = false;
boolean nopltepng = false;
byte[] content = InputStream2Bytes2(bm);
if (content[25] == 3) {
ispltepng = true;
}
int findplte = 0;
for (int idx = 0; idx < content.length; idx++) {
if (content[idx] == 80 && content[idx + 1] == 76 && content[idx + 2] == 84 && content[idx + 3] == 69) {
findplte = idx;
break;
}
}
if (findplte == 0) {
nopltepng = true;
}
if (ispltepng && nopltepng) {
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
}
}
public byte[] imgConvert(byte content[]) {
try {
int end = 0;
for (int idx = 0; idx < content.length; idx++) {
if (content[idx] == 0x49 && content[idx + 1] == 0x44 && content[idx + 2] == 0x41 && content[idx + 3] == 0x54) {
end = idx;
break;
}
}
end = end-4;
byte[] sub1 = Arrays.copyOf(content, end);
byte[] sub2 = Arrays.copyOfRange(content, end, content.length);
byte[] head = concat(sub1, plte);
byte[] tail = concat(head, sub2);
return tail;
} catch (Exception e) {
return content;
}
}
private static byte[] InputStream2Bytes2(InputStream inputStream) throws IOException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024*32];
int num = inputStream.read(buffer);
while (num != -1) {
baos.write(buffer, 0, num);
num = inputStream.read(buffer);
}
baos.flush();
return baos.toByteArray();
} finally {
}
}
private static byte[] concat(byte[] a, byte[] b) {
byte[] c= new byte[a.length+b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
private byte plte[] = {
0, 0, 1, -128,
80, 76, 84, 69,
0, 0, 0,
-1, 0, 0,
0, -1, 0,
-1, -1, 0,
0, 0, -1,
-1, 0, -1,
0, -1, -1,
-1, -1, -1,
0, 0, 0,
-81, 0, 0,
0, -81, 0,
-81, -81, 0,
0, 0, -81,
-81, 0, -81,
0, -81, -81,
-81, -81, -81,
0, 0, 85,
0, 85, 0,
0, 85, 85,
0, 85, -81,
0, 85, -1,
0, -81, 85,
0, -81, -1,
0, -1, 85,
0, -1, -81,
85, 0, 0,
85, 0, 85,
85, 0, -81,
85, 0, -1,
85, 85, 0,
85, 85, 85,
85, 85, -81,
85, 85, -1,
85, -81, 0,
85, -81, 85,
85, -81, -81,
85, -81, -1,
85, -1, 0,
85, -1, 85,
85, -1, -81,
85, -1, -1,
-81, 0, 85,
-81, 0, -1,
-81, 85, 0,
-81, 85, 85,
-81, 85, -81,
-81, 85, -1,
-81, -81, 85,
-81, -81, -1,
-81, -1, 0,
-81, -1, 85,
-81, -1, -81,
-81, -1, -1,
-1, 0, 85,
-1, 0, -81,
-1, 85, 0,
-1, 85, 85,
-1, 85, -81,
-1, 85, -1,
-1, -81, 0,
-1, -81, 85,
-1, -81, -81,
-1, -81, -1,
-1, -1, 85,
-1, -1, -81,
0, 0, 0,
-1, 0, 0,
0, -1, 0,
-1, -1, 0,
0, 0, -1,
-1, 0, -1,
0, -1, -1,
-1, -1, -1,
-81, 0, 0,
0, -81, 0,
-81, -81, 0,
0, 0, -81,
-81, 0, -81,
0, -81, -81,
-81, -81, -81,
0, 0, 85,
0, 85, 0,
0, 85, 85,
0, 85, -81,
0, 85, -1,
0, -81, 85,
0, -81, -1,
0, -1, 85,
0, -1, -81,
85, 0, 0,
85, 0, 85,
85, 0, -81,
85, 0, -1,
85, 85, 0,
85, 85, 85,
85, 85, -81,
85, 85, -1,
85, -81, 0,
85, -81, 85,
85, -81, -81,
85, -81, -1,
85, -1, 0,
85, -1, 85,
85, -1, -81,
85, -1, -1,
-81, 0, 85,
-81, 0, -1,
-81, 85, 0,
-81, 85, 85,
-81, 85, -81,
-81, 85, -1,
-81, -81, 85,
-81, -81, -1,
-81, -1, 0,
-81, -1, 85,
-81, -1, -81,
-81, -1, -1,
-1, 0, 85,
-1, 0, -81,
-1, 85, 0,
-1, 85, 85,
-1, 85, -81,
-1, 85, -1,
-1, -81, 0,
-1, -81, 85,
-1, -81, -81,
-1, -81, -1,
-1, -1, 85,
61, 25, 64, -66
};
}