为什么我的未压缩PNG像素数据不准确?

时间:2015-07-17 04:28:58

标签: java png zlib

我正在尝试使用jzlib来扩充PNG文件的IDAT数据,并以十六进制打印出R,G,B和A值。我有一个有效的程序(没有错误),但打印出的值看起来不正确。

我的图像是一个2x2完全白色的PNG文件,没有压缩(来自Photoshop)。

这是我的代码:

import com.jcraft.jzlib.GZIPException;
import com.jcraft.jzlib.Inflater;
import com.jcraft.jzlib.JZlib;

import java.io.*;

public class main {

    private static final int imageWidth = 2;
    private static final int imageHeight = 2;

    public static void main(String[] args) {
        String pngPath = args[0];
        File pngFile = new File(pngPath);

        try {
            DataInputStream stream = new DataInputStream(new FileInputStream(pngFile));
            byte[] data = new byte[8];
            stream.readFully(data); //Read PNG Header

            while (true) {
                data = new byte[4];
                stream.readFully(data); //Read Length
                int length = ((data[0] & 0xFF) << 24)
                        | ((data[1] & 0xFF) << 16)
                        | ((data[2] & 0xFF) << 8)
                        | (data[3] & 0xFF); //Byte array to int
                stream.readFully(data); //Read Name
                String name = new String(data); //Byte array to String
                if (name.equals("IDAT")) {
                    data = new byte[length];
                    stream.readFully(data); //Read Data

                    int maxInflateBuffer = 4 * (imageWidth + 1) * imageHeight;
                    byte[] outputBuffer = new byte[maxInflateBuffer];

                    long inflatedSize = inflate(data, outputBuffer, maxInflateBuffer);

                    int index = 0;
                    byte r, g, b, a;

                    for (int y = 0; y < imageHeight; y++) {
                        index++;
                        for (int x = 0; x < imageWidth; x++) {
                            r = outputBuffer[index];
                            g = outputBuffer[index + 1];
                            b = outputBuffer[index + 2];
                            a = outputBuffer[index + 3];

                            System.out.println(byteToHex(r) + " : " + byteToHex(g) + " : " + byteToHex(b) + " : " + byteToHex(a));
                            System.out.println("------------------------");

                            index += 4;
                        }
                    }
                    return;
                } else { //Don't care about other chunks
                    data = new byte[length + 4]; //Data length + 4 byte CRC
                    stream.readFully(data); //Skip Data and CRC.
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static long inflate(byte[] data, byte[] outputBuffer, int maxInflateBuffer) throws GZIPException {
        Inflater inflater = new Inflater(15);

        inflater.setInput(data, true);

        inflater.setOutput(outputBuffer);

        try {
            inflater.inflate(JZlib.Z_NO_COMPRESSION);
        } finally {
            inflater.inflateEnd();
        }

        return inflater.getTotalOut();
    }

    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String byteToHex(byte b) {
        char[] hexChars = new char[2];
        int v = b & 0xFF;
        hexChars[0] = hexArray[v >>> 4];
        hexChars[1] = hexArray[v & 0x0F];
        return new String(hexChars);
    }
}

这是输出,依次为R:G:B:A ......

FF : FF : FF : 00
------------------------
00 : 00 : 02 : 00
------------------------
00 : 00 : 00 : 00
------------------------
00 : 00 : 00 : 00
------------------------

我的假设是它看起来更像是:

FF : FF : FF : FF
------------------------
FF : FF : FF : FF
------------------------
FF : FF : FF : FF
------------------------
FF : FF : FF : FF
------------------------

为什么不呢?怎么了?我已经花了一些时间来解决这个问题。

以下是图片的链接: http://i.imgur.com/57lhTXP.png(右键点击 - &gt;另存为)

修改

我尝试使用内置的Java Inflater库,如CoderNeji在评论中所建议的那样:

private static long inflate(byte[] data, byte[] outputBuffer, int maxInflateBuffer) {
    Inflater inflater = new Inflater();

    inflater.setInput(data, 0, maxInflateBuffer);

    try {
        inflater.inflate(outputBuffer);
    } catch (DataFormatException e) {
        e.printStackTrace();
    }

    return inflater.getTotalOut();
}

得到了相同的结果。此外,使用任一方法,2x2黑色图像提供此输出:

00 : 00 : 00 : 00
------------------------
00 : 00 : 00 : 00
------------------------
00 : 00 : 00 : 00
------------------------
00 : 00 : 00 : 00
------------------------

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

你忘记了row filters。你跳过了打印的第一个字节,所以你没有看到第一个字节(以及随后的所有其他&#39;第一个字节&#39;每行,这不是一件坏事 - 除非你太快完成了)。 /> 此外,您正在打印Alpha通道输出,而图像没有。与您所说的相反,图像不是RGBA,而是RGB,没有alpha。这条线

index += 4;

应该是

index += 3;

您可能已经通过比较打印输出大小与inflatedSize得到的内容来注意到这些错误。实际上,inflatedSize应为14,并且您打印了16个字节的数据(并且还跳过了2个字节)。

实际解压缩数据,一次一行,

01 : FF FF FF : 00 00 00
02 : 00 00 00 : 00 00 00

每行的第一个数字是行过滤器,后跟2个(宽度)* 3个(通道)像素数据条目。

这两行过滤器是Sub和Up,从左到右,从上到下应用它们,你得到

FF FF FF : FF FF FF
FF FF FF : FF FF FF

转换为&#34; 2x2全白像素&#34;,正如人们所期望的那样。