📄 pngread.java
字号:
package poppngread;
import java.io.FileInputStream;
import java.io.*;
//import javax.microedition.lcdui.Image;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class PngRead {
static byte[] imgdest = null;
static int b_width = 0;
static int b_height = 0;
static int b_colordepth = 0;
static int b_palettelen = 0;
static int b_istrans = 0;
static int datalen = 0;
static int translen = 0;
static int total_length = 0;
static int crc_offset = 0;
static int data_index = 0;
public static void main(String[] args) {
PngRead pngread = new PngRead();
try{
InputStream is = new FileInputStream("res/a");
for(int i=0;i<5;i++){
FileOutputStream fout = new FileOutputStream("res/car/"+i+"22.png");
// fout.write(data,head,end);
fout.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
/* try {
InputStream is = new FileInputStream("res/a");
int head = 0,end = 0;
int offset = 0;
byte[] data = new byte[10000000];
is.read(data);
for(int i = 0;i<data.length;i ++){
offset = end;
while (!(data[offset + 0] == -119 && data[offset + 1] == 80 &&
data[offset + 2] == 78 &&
data[offset + 3] == 71 && data[offset + 4] == 13 &&
data[offset + 5] == 10 &&
data[offset + 6] == 26 && data[offset + 7] == 10))
offset++;
head = offset;
end = offset;
while (!(data[offset + 0] == 0 && data[offset + 1] == 0 &&
data[offset + 2] == 0 &&
data[offset + 3] == 0 && data[offset + 4] == 73 &&
data[offset + 5] == 69 &&
data[offset + 6] == 78 && data[offset + 7] == 68 &&
data[offset + 8] == -82 && data[offset + 9] == 66 &&
data[offset + 10] == 96 && data[offset + 11] == -126))
offset++;
end=offset+12;
if(end -12== head) {is.close();return;}
FileOutputStream fout = new FileOutputStream("res/car/"+i+"22.png");
fout.write(data,head,end);
fout.close();
}
is.close();
} catch (Exception ex) {
}*/
}
public static byte[] CreateXFlipImage(String file , int palIndex) throws Exception {
byte[] temp=null;
InputStream is0 = file.getClass().getResourceAsStream(file);
DataInputStream is = new DataInputStream(is0);
int count = is.readInt();
if (count <= 0)return null;
int size = is.readInt();
byte[] imgdata = new byte[size];
is.read(imgdata);
is.close();
is = null;
is0 = null;
// Image img;
if( palIndex <= 0 ){
temp=imgdata;
//img = CreateXFlipImage(imgdata, 0);
}else{
System.arraycopy(temp,0,imgdata,0,palIndex);
// img = CreateXFlipImage(imgdata, 0, palIndex);
}
imgdata = null;
return temp;
}
private static void BIN_Xflippart(byte[] imgsrc, int off, int f_height) {
int dataoff = off; //Read4PNGStream(imgsrc, imgnum * 4);
BIN_Read_PNGinf(imgsrc, dataoff);
int new_width = b_width;
if (f_height < 0) {
f_height = b_height;
}
int new_height = f_height;
int new_byte_width = Compute_Streamlength(new_width, new_height,
b_colordepth, b_palettelen,
b_istrans);
dataoff += 4;
PNG_Buildheader(imgsrc, new_width, new_height, b_colordepth,
b_palettelen, dataoff, b_istrans, datalen);
dataoff += b_palettelen;
int src_rowbytes = (new_width * b_colordepth + 7) / 8;
int depthshift = 0;
while ( (0x01 << depthshift) < b_colordepth) depthshift++;
byte t0 = (byte) (8 - b_colordepth);
byte t1 = (byte) (0xFF << (t0));
byte t2 = (byte) (~ (0xFF << b_colordepth));
int offset = 0;
for (int r = 0; r < new_height; r++) {
imgdest[data_index + offset] = imgsrc[dataoff + offset];
offset++;
for (int c = 0; c < new_width; c++) {
int t3 = c << depthshift;
int index = offset + (t3 >> 3);
byte src = imgsrc[dataoff + index];
int m = t3 & 0x07;
//src = GETBITS(src, m, b_colordepth);
src = (byte) ( (src & (t1 >> m)) >> (t0 - m));
t3 = (new_width - c - 1) << depthshift;
index = offset + (t3 >> 3);
byte tmp = imgdest[data_index + index];
m = t3 & 0x07;
//tmp = SETBITS(tmp, src, m, b_colordepth);
tmp = (byte) ( (tmp &
( ( (0xFF << (8 - m)) | (0xFF >> (m + b_colordepth))))) |
( (src & t2) << (t0 - m)));
imgdest[data_index + index] = tmp;
}
offset += src_rowbytes;
} //*/
PNG_Buildtail(crc_offset, datalen, imgsrc,
dataoff + datalen + PNG_CHUNK_PLTE_CHECKSUM_LENGTH);
}
private static void PNG_Buildtail(int offset, int length, byte[] imgSrc,
int adleroffset) {
int adler_checksum = readNumber(imgSrc, adleroffset, 4, true);
data_index += length;
Write2PNGStream(adler_checksum, 4);
int crc_checksum = readNumber(imgSrc, adleroffset + 4, 4, true);
Write2PNGStream(crc_checksum, 4);
//*/
// IDAT chunk end
Write2PNGStream(PNG_CHUNK_IEND_LENGTH, 4);
Write2PNGStream(PNG_CHUNK_IEND_NAME, 4);
Write2PNGStream(PNG_CHUNK_IEND_CRC, 4);
// IEND chunk end
}
private static void BIN_Read_PNGinf(byte[] imgsrc, int dataoff) {
int key = readNumber(imgsrc, dataoff, 4, true); //readInt(imgsrc, dataoff);//Read4PNGStream(imgsrc, dataoff);
b_istrans = (key >> 30) & 0x01;
b_colordepth = (key >> 26) & 0x0F;
b_palettelen = ( (key & 0xff) + 1) * 3;
b_height = (key >> 8) & 0x01ff;
b_width = (key >> 17) & 0x01ff;
}
private static void Write2PNGStream(int Value, int Width) {
for (; Width > 0; Width--) {
imgdest[data_index++] = (byte) (Value >> (Width - 1) * 8);
}
}
private static void PNG_Buildheader(byte[] imgsrc, int w, int h, int d,int pl, int po, int t, int dl) {
int crc_checksum;
imgdest = new byte[total_length];
data_index = 0;
Write2PNGStream(PNG_HEADER_SIGNATURE_1, 4);
Write2PNGStream(PNG_HEADER_SIGNATURE_2, 4);
Write2PNGStream(PNG_CHUNK_IHDR_LENGTH, 4);
Write2PNGStream(PNG_CHUNK_IHDR_NAME, 4);
Write2PNGStream(w, 4);
Write2PNGStream(h, 4);
Write2PNGStream(d, 1);
Write2PNGStream(PNG_CHUNK_IHDR_COLORINDEX, 4);
crc_checksum = Compute_CRC(0xffffffff, imgdest,
PNG_CHUNK_IHDR_CRC_OFFSET,
PNG_CHUNK_IHDR_CRC_LENGTH);
Write2PNGStream(crc_checksum, 4);
// IHDR chunk end
Write2PNGStream(pl, 4);
Write2PNGStream(PNG_CHUNK_PLTE_NAME, 4);
System.arraycopy(imgsrc, po, imgdest, data_index, pl);
data_index += pl;
crc_checksum = Compute_CRC(0xffffffff, imgdest,
PNG_CHUNK_PLTE_CRC_OFFSET, pl + 4);
Write2PNGStream(crc_checksum, 4);
// PLTE chunk end
if (t != 0) {
Insert_TRNS_Chunk(PNG_TRANSPARENT_COLOR);
}
// TRNS chunk end
Write2PNGStream( (PNG_CHUNK_IDAT_LEN_APPEND + dl), 4);
Write2PNGStream(PNG_CHUNK_IDAT_NAME, 4);
Write2PNGStream(PNG_CHUNK_IDAT_HEADER, 3);
int invert_len = ( (dl << 8) | ( (dl >> 8) & 0xff)) & 0xffff;
Write2PNGStream(invert_len, 2);
Write2PNGStream(~invert_len, 2); // data header end
}
private static void Insert_TRNS_Chunk(int color_transparency) {
int i;
int off;
int crc;
Write2PNGStream(color_transparency, 4);
off = data_index;
Write2PNGStream(PNG_CHUNK_TRNS_NAME, 4);
for (i = 0; i < (color_transparency - 1); i++) {
Write2PNGStream(0xff, 1);
}
Write2PNGStream(0x00, 1);
crc = Compute_CRC(0xffffffff, imgdest, off, color_transparency + 4);
Write2PNGStream(crc, 4);
// tRNS chunk end
}
static int[] CRC_Table;
private static int Compute_CRC(int crc, byte[] data, int off, int len) {
int c = crc;
int n;
int t1;
int t2;
Compute_CRC_Table();
for (n = 0; n < len; n++) {
if (c >= 0) {
t1 = c >> 8;
}
else {
t1 = ( (c ^ 0x80000000) >> 8) | 0x00800000;
}
t2 = (c ^ data[off + n]) & 0xff;
c = CRC_Table[t2] ^ t1;
}
return c ^ 0xffffffff;
}
private static void Compute_CRC_Table() {
if (CRC_Table != null)
return;
int c;
int n;
int k;
CRC_Table = new int[256];
for (n = 0; n < 256; n++) {
c = n;
for (k = 0; k < 8; k++) {
if ( (c & 1) != 0) {
c = 0xedb88320 ^
( ( (c >> 1) >= 0) ? (c >> 1) : ( (c >> 1) ^ 0x80000000));
}
else {
c = ( (c >> 1) >= 0) ? (c >> 1) : ( (c >> 1) ^ 0x80000000);
}
}
CRC_Table[n] = c;
}
}
private static int Compute_Streamlength(int width, int height, int depth,
int palettelen, int istrans) {
int byte_width = (width * depth + 7) / 8;
translen = (istrans == 0) ? 0
: (PNG_CHUNK_LENGTH_APPEND +
PNG_TRANSPARENT_COLOR);
datalen = (byte_width + 1) * height;
total_length = ( (PNG_SIGNATURE_LENGTH + PNG_CHUNK_IHDR_LENGTH +
PNG_CHUNK_LENGTH_APPEND) +
(palettelen + PNG_CHUNK_LENGTH_APPEND) +
translen +
(datalen + PNG_CHUNK_IDAT_LEN_APPEND +
PNG_CHUNK_LENGTH_APPEND) +
PNG_CHUNK_IEND_TOTALLEN);
crc_offset = PNG_CHUNK_PLTE_LENGTH + palettelen +
PNG_CHUNK_LENGTH_APPEND + translen + PNG_CHUNK_IDAT_CRC_OFFSET;
return byte_width;
}
static final int PNG_HEADER_SIGNATURE_1 = 0x89504e47;
static final int PNG_HEADER_SIGNATURE_2 = 0x0d0a1a0a;
static final int PNG_CHUNK_IHDR_LENGTH = 0x0000000d;
static final int PNG_CHUNK_IHDR_NAME = 0x49484452;
static final int PNG_CHUNK_IDHR_COLOR_256 = 0x08;
static final int PNG_CHUNK_IDHR_COLOR_16 = 0x04;
static final int PNG_CHUNK_IHDR_COLORINDEX = 0x03000000;
static final int PNG_CHUNK_PLTE_NAME = 0x504c5445;
static final int PNG_CHUNK_IDAT_NAME = 0x49444154;
static final int PNG_CHUNK_IDAT_HEADER = 0x789c01;
static final int PNG_CHUNK_IEND_LENGTH = 0x00000000;
static final int PNG_CHUNK_IEND_NAME = 0x49454e44;
static final int PNG_CHUNK_IEND_CRC = 0xae426082;
static final int PNG_CHUNK_TRNS_NAME = 0x74524e53;
static final int PNG_SIGNATURE_LENGTH = 8;
static final int PNG_CHUNK_IHDR_WIDTH = 16;
static final int PNG_CHUNK_IHDR_HEIGHT = 20;
static final int PNG_COLOR_DEPTH = 24;
static final int PNG_CHUNK_IHDR_CRC_OFFSET = 12;
static final int PNG_CHUNK_IHDR_CRC_LENGTH = 17;
static final int PNG_CHUNK_PLTE_LENGTH = 33;
static final int PNG_CHUNK_PLTE_CRC_OFFSET = 37;
static final int PNG_CHUNK_PLTE_DATA = 41;
static final int PNG_CHUNK_LENGTH_APPEND = 12;
static final int PNG_CHUNK_IEND_TOTALLEN = 12;
static final int PNG_CHUNK_IDAT_LEN_APPEND = 11;
static final int PNG_CHUNK_IDAT_CRC_OFFSET = 4;
static final int PNG_TRANSPARENT_COLOR = 1;
static final int ADLER_BASE = 65521;
static final int PNG_CHUNK_PLTE_CHECKSUM_LENGTH = 8;
static int readNumber(byte[] b, int off, int num, boolean invert) {
int result = 0;
for (int i = 0; i < num; i++) {
result <<= 8;
result |= b[off + i] & 0xFF;
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -