我的PNG IDAT块有什么问题?

时间:2018-08-07 06:04:35

标签: image png zlib

我正在尝试手动构建一个简单的4x1,未压缩 PNG。

到目前为止,我有:

89504E47  // PNG Header
0D0A1A0A

0000000D  // byte length of IHDR chunk contents, 4 bytes, value 13
49484452  // IHDR start - 4 bytes
00000004  // Width                        4 bytes }
00000001  // Height                       4 bytes }
08        // bit depth 8 = 24/32 bit      1 byte  }
06        // color type, 6 - RGBa         1 byte  }
00        // compression, 0 = Deflate     1 byte  }
00        // filter, 0 = no filter        1 byte  }
00        // interlace, 0 = no interlace  1 byte  } Total, 13 Bytes
F93C0FCD  // CRC of IHDR chunk, 4 bytes         

00000013  // byte length of IDAT chunk contents, 4 bytes, value 19
49444154  // IDAT start - 4 bytes
0000      // ZLib 0 compression,          2 bytes }
00        // Filter = 0,                  1 bytes }
CC0000FF  // Pixel 1, Red-ish,            4 bytes }
00CC00FF  // Pixel 2, Green-ish,          4 bytes }
0000CCFF  // Pixel 3, Blue-ish,           4 bytes }
CCCCCCCC  // Pixel 4, transclucent grey,  4 bytes } Total, 19 Bytes
6464C2B0  // CRC of IHDR chunk, 4 bytes

00000000  // byte length of IEND chunk, 4 bytes (value: 0)
49454E44  // IEND start - 4 bytes
AE426082  // CRC of IEND chunk, 4 bytes

更新

我认为我遇到的问题取决于ZLib / Deflate的订购。

我认为我必须包括RFC 1951, sec. 3.2.4中的“非压缩块格式”详细信息,但是我不确定交互作用。我能找到的唯一示例是压缩块(可以理解!)

所以我现在尝试:

49444154  // IDAT start - 4 bytes
01        // BFINAL = 1, BTYPE = 00       1 byte  }
11EE      // LEN & NLEN of data           2 bytes }
00        // Filter = 0,                  1 byte  }
CC0000FF  // Pixel 1, Red-ish,            4 bytes }
00CC00FF  // Pixel 2, Green-ish,          4 bytes }
0000CCFF  // Pixel 3, Blue-ish,           4 bytes }
CCCCCCCC  // Pixel 4, transclucent grey,  4 bytes } Total, 19 Bytes
6464C2B0  // CRC of IHDR chunk, 4 bytes

整个PNG文件为:

89504E47    // PNG Block
0d0a1a0A

0000000D    // IHDR Block
49484452
00000004
00000001
08060000
00
F93C0FCD

00000014    // IDAT Block
49444154
0111EE00
CC0000FF
00CC00FF
0000CCFF
CCCCCCCC
6464C2B0

00000000    // IEND Block
49454E44
AE426082

我真的很感谢能指出问题所在的指针……甚至是工作文件的PNG数据,以便我可以对其进行反向工程?

更新2

感谢Mark Adler,我已更正了新手错误,现在有了功能代码,可以再现下面他的答案所示的结果,即4x1像素图像。由此,我现在可以愉快地生成100x1的图像!

但是,作为最后一步,我希望通过调整IHDR中的高度字段并添加其他非终端IDAT,将其扩展为4 x 2图像。不幸的是,这似乎不符合我的预期。

我现在有类似...

89504E47 // PNG Header
0D0A1A0A

0000000D // re calc'ed IHDR with 2 rows
49484452
00000004
00000002 // changed - now 2 rows
08
06
00
00
00
7FA87D63 // CRC of IHDR updated

0000001C // row 1 IDAT, non-terminal
49444154
7801
00       // BFINAL = 0, BTYPE = 00 
1100EEFF
00
CC0000FF
00CC00FF
0000CCFF
CCCCCCCC
3D3A0892
5D19A623


0000001C // row 2, terminal IDAT, as Mark Adler's answer
49444154
7801
01       // BFINAL = 1, BTYPE = 00 
1100EEFF
00
CC0000FF
00CC00FF
0000CCFF
CCCCCCCC
3D3A0892
BA0400B4

00000000
49454E44
AE426082

1 个答案:

答案 0 :(得分:3)

此:

11EE      // LEN & NLEN of data           2 bytes }

是错误的。 LENNLEN都是16位,而不是8位。因此需要:

1100EEFF  // LEN & NLEN of data           4 bytes }

您还需要围绕放气数据的zlib包装器。参见RFC 1950。

最后,您将需要更新块的CRC。 (顺便说一句,它的评论有误-应该说IDAT块的CRC。)

已修复:

89504E47  // PNG Header
0D0A1A0A

0000000D  // byte length of IHDR chunk contents, 4 bytes, value 13
49484452  // IHDR start - 4 bytes
00000004  // Width                        4 bytes }
00000001  // Height                       4 bytes }
08        // bit depth 8 = 24/32 bit      1 byte  }
06        // color type, 6 - RGBa         1 byte  }
00        // compression, 0 = Deflate     1 byte  }
00        // filter, 0 = no filter        1 byte  }
00        // interlace, 0 = no interlace  1 byte  } Total, 13 Bytes
F93C0FCD  // CRC of IHDR chunk, 4 bytes         

0000001C  // byte length of IDAT chunk contents, 4 bytes, value 28
49444154  // IDAT start - 4 bytes
7801      // zlib Header                  2 bytes }
01        // BFINAL = 1, BTYPE = 00       1 byte  }
1100EEFF  // LEN & NLEN of data           4 bytes }
00        // Filter = 0,                  1 byte  }
CC0000FF  // Pixel 1, Red-ish,            4 bytes }
00CC00FF  // Pixel 2, Green-ish,          4 bytes }
0000CCFF  // Pixel 3, Blue-ish,           4 bytes }
CCCCCCCC  // Pixel 4, transclucent grey,  4 bytes }
3d3a0892  // Adler-32 check               4 bytes }
ba0400b4  // CRC of IDAT chunk, 4 bytes

00000000  // byte length of IEND chunk, 4 bytes (value: 0)
49454E44  // IEND start - 4 bytes
AE426082  // CRC of IEND chunk, 4 bytes
相关问题