📄 pngencoder.java~91~
字号:
/* Decompiled by Mocha from PngEncoder.class */
/* Originally compiled from PngEncoder.java */
package dyimg;
import java.io.*;
import java.util.zip.*;
import java.awt.Image;
import java.awt.image.PixelGrabber;
public class PngEncoder
{
public static final boolean ENCODE_ALPHA = true;
public static final boolean NO_ALPHA = false;
public static final int FILTER_NONE = 0;
public static final int FILTER_SUB = 1;
public static final int FILTER_UP = 2;
public static final int FILTER_LAST = 2;
protected byte pngBytes[];
protected byte priorRow[];
protected byte leftBytes[];
protected Image image;
protected int width;
protected int height;
protected int bytePos;
protected int maxPos;
protected int hdrPos;
protected int dataPos;
protected int endPos;
protected CRC32 crc;
protected long crcValue;
protected boolean encodeAlpha;
protected int filter;
protected int bytesPerPixel;
protected int compressionLevel;
public PngEncoder()
{
this(null, false, 0, 0);
}
public PngEncoder(Image image)
{
this(image, false, 0, 0);
}
public PngEncoder(Image image, boolean flag)
{
this(image, flag, 0, 0);
}
public PngEncoder(Image image, boolean flag, int i)
{
this(image, flag, i, 0);
}
public PngEncoder(Image image, boolean flag, int i, int j)
{
crc = new CRC32();
this.image = image;
encodeAlpha = flag;
setFilter(i);
if (j >= 0 && j <= 9)
compressionLevel = j;
}
public void setImage(Image image)
{
this.image = image;
pngBytes = null;
}
public byte[] pngEncode(boolean flag)
{
byte ab[] = { -119, 80, 78, 71, 13, 10, 26, 10 };
if (image == null)
return null;
width = image.getWidth(null);
height = image.getHeight(null);
image = image;
pngBytes = new byte[(width + 1) * height * 3 + 200];
maxPos = 0;
bytePos = writeBytes(ab, 0);
hdrPos = bytePos;
writeHeader();
dataPos = bytePos;
if (writeImageData())
{
writeEnd();
pngBytes = resizeByteArray(pngBytes, maxPos);
}
else
pngBytes = null;
return pngBytes;
}
public byte[] pngEncode()
{
return pngEncode(encodeAlpha);
}
public void setEncodeAlpha(boolean flag)
{
encodeAlpha = flag;
}
public boolean getEncodeAlpha()
{
return encodeAlpha;
}
public void setFilter(int i)
{
filter = 0;
if (i <= 2)
filter = i;
}
public int getFilter()
{
return filter;
}
public void setCompressionLevel(int i)
{
if (i >= 0 && i <= 9)
compressionLevel = i;
}
public int getCompressionLevel()
{
return compressionLevel;
}
protected byte[] resizeByteArray(byte ab1[], int i)
{
byte ab2[] = new byte[i];
int j = ab1.length;
System.arraycopy(ab1, 0, ab2, 0, Math.min(j, i));
return ab2;
}
protected int writeBytes(byte ab[], int i)
{
maxPos = Math.max(maxPos, i + ab.length);
if (ab.length + i > pngBytes.length)
pngBytes = resizeByteArray(pngBytes, pngBytes.length + Math.max(1000, ab.length));
System.arraycopy(ab, 0, pngBytes, i, ab.length);
return i + ab.length;
}
protected int writeBytes(byte ab[], int i, int j)
{
maxPos = Math.max(maxPos, j + i);
if (i + j > pngBytes.length)
pngBytes = resizeByteArray(pngBytes, pngBytes.length + Math.max(1000, i));
System.arraycopy(ab, 0, pngBytes, j, i);
return j + i;
}
protected int writeInt2(int i, int j)
{
byte ab[] = { (byte)(i >> 8 & 255), (byte)(i & 255) };
return writeBytes(ab, j);
}
protected int writeInt4(int i, int j)
{
byte ab[] = { (byte)(i >> 24 & 255), (byte)(i >> 16 & 255), (byte)(i >> 8 & 255), (byte)(i & 255) };
return writeBytes(ab, j);
}
protected int writeByte(int i, int j)
{
byte ab[] = { (byte)i };
return writeBytes(ab, j);
}
protected int writeString(String string, int i)
{
return writeBytes(string.getBytes(), i);
}
protected void writeHeader()
{
int i = bytePos = writeInt4(13, bytePos);
bytePos = writeString("IHDR", bytePos);
width = image.getWidth(null);
height = image.getHeight(null);
bytePos = writeInt4(width, bytePos);
bytePos = writeInt4(height, bytePos);
bytePos = writeByte(8, bytePos);
bytePos = writeByte(encodeAlpha ? 6 : 2, bytePos);
bytePos = writeByte(0, bytePos);
bytePos = writeByte(0, bytePos);
bytePos = writeByte(0, bytePos);
crc.reset();
crc.update(pngBytes, i, bytePos - i);
crcValue = crc.getValue();
bytePos = writeInt4((int)crcValue, bytePos);
}
protected void filterSub(byte ab[], int i1, int j1)
{
int i2 = bytesPerPixel;
int j2 = i1 + i2;
int k2 = j1 * bytesPerPixel;
int i3 = i2;
int j3 = 0;
int k1 = j2;
while (k1<ab.length)
{
if (k1 >= i1 + k2)
leftBytes[i3] = ab[k1];
ab[k1] = (byte)((ab[k1] - leftBytes[j3]) % 256);
i3 = (i3 + 1) % 15;
j3 = (j3 + 1) % 15;
k1++;
}
}
protected void filterUp(byte ab[], int i1, int j)
{
/* int i2 = j * bytesPerPixel;
int k = 0;
byte b=0;
while (true)
{
if (k >= i2)
b = ab[i1 + k];
ab[i1 + k] = (byte)((ab[i1 + k] - priorRow[k]) % 256);
priorRow[k] = b;
k++;
}*/
}
protected boolean writeImageData()
{
setFilter(0);
int i1 = height;
int j1 = 0;
bytesPerPixel = encodeAlpha ? 4 : 3;
Deflater deflater = new Deflater(compressionLevel);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
try
{
int k1;
byte ab1[];
int i2;
int j2;
PixelGrabber pixelGrabber;
int i3;
boolean flag2;
for (; i1 > 0; i1 -= k1)
{
k1 = Math.min(32767 / (width * (bytesPerPixel + 1)), i1);
int[] local = new int[width * k1];
pixelGrabber = new PixelGrabber(image, 0, j1, width, k1, local, 0, width);
try
{
pixelGrabber.grabPixels();
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}
if ((pixelGrabber.getStatus() & 128) != 0)
{
System.err.println("image fetch aborted or errored");
return false;
}
ab1 = new byte[width * k1 * bytesPerPixel + k1];
if (filter == 1)
leftBytes = new byte[16];
if (filter == 2)
priorRow = new byte[width * bytesPerPixel];
i2 = 0;
j2 = 1;
for (i3 = 0; i3 < width * k1; i3++)
{
if (i3 % width == 0)
{
ab1[i2++] = (byte)filter;
j2 = i2;
}
ab1[i2++] = (byte)(local[i3] >> 16 & 255);
ab1[i2++] = (byte)(local[i3] >> 8 & 255);
ab1[i2++] = (byte)(local[i3] & 255);
if (encodeAlpha)
ab1[i2++] = (byte)(local[i3] >> 24 & 255);
if (i3 % width == width - 1 && filter != 0)
{
if (filter == 1)
{
filterSub(ab1, j2, width);
System.out.println("filtersub");
}
if (filter == 2)
{
filterUp(ab1, j2, width);
System.out.println("filtersub");
}
}
}
deflaterOutputStream.write(ab1, 0, i2);
j1 += k1;
}
deflaterOutputStream.close();
byte ab2[] = byteArrayOutputStream.toByteArray();
int k2 = ab2.length;
crc.reset();
bytePos = writeInt4(k2, bytePos);
bytePos = writeString("IDAT", bytePos);
crc.update("IDAT".getBytes());
bytePos = writeBytes(ab2, k2, bytePos);
crc.update(ab2, 0, k2);
crcValue = crc.getValue();
bytePos = writeInt4((int)crcValue, bytePos);
deflater.finish();
return true;
}
catch (IOException e)
{
System.err.println(e.toString());
}
boolean flag1 = false;
return flag1;
}
protected void writeEnd()
{
bytePos = writeInt4(0, bytePos);
bytePos = writeString("IEND", bytePos);
crc.reset();
crc.update("IEND".getBytes());
crcValue = crc.getValue();
bytePos = writeInt4((int)crcValue, bytePos);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -