Undersanding IDAT chunk翻转图像

时间:2017-06-04 22:23:48

标签: png default.png

首先介绍一下。我需要翻转PNG图像。我得到每个字节的信息(按顺序),我必须将其作为流翻转。我设法分割和解析块。但是当我有 IDAT块数据时,我不知道如何处理它。

来自IHDR的 INFO:位深度= 08(每个样本8位),颜色类型= 06。 图像为800 x 600 = 480000

IDAT大小为179502,因此是像素大小总数的0.374(奇怪)。

如果我翻转像素(所有IDAT数据都是这样),我会得到一个图像,当我打开它时,它只显示一个透明图像。我也尝试使用4个字节= 1个像素,因为我有RGB + alpha但仍然没有好结果。 (总是一个透明的图像......也很奇怪......我希望随机图像,而不是透明图像)。

我已经读过信息是压缩的,所以我的计划是解压缩它,翻转RGB数组然后再压缩,但是...我怎么能再解压缩并压缩它?我无法找到信息/算法来做到这一点。

我也发现0x78十六进制几乎总是在IDAT部分之后,所以我推断出压缩像素数组不会在IDAT之后直接启动但是之后的一些字节...如果是这样...... 它是怎么回事组织的?它只是像素阵列直接或在IDAT块数据之前和/或之后是否有一些信息?

1 个答案:

答案 0 :(得分:1)

The IDAT chunk(s) include the pixel data of the image, prefiltered and compressed. To get the RGB you'd need to:

  • concatenate all the IDAT chunks
  • uncompress the stream (ZLIB)
  • unfilter the filter applied to each row
  • according to the pixel format of the image you might have the RGB components in each byte, or perhaps in words, or perhaps in some bits (and perhaps they are not RGB components, but indexes into a Palette, or grayscale, or with Alpha...)

In short, you need to decode a PNG image, which is not trivial. There are many PNG decoders out there, so I don't know why you'd want to reivent the wheel. If you really want to do that yourself, then you need to read the details of the PNG standard.