Image 什么';我的PNG IDAT块有什么问题?

Image 什么';我的PNG IDAT块有什么问题?,image,png,zlib,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

我试图手动构建一个简单的、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顺序

我认为我必须包括来自的“非压缩块格式”细节,但我对交互有点不确定。我能找到的唯一例子是压缩块(可以理解!)

所以我现在试着:

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 多亏了马克·阿德勒,我纠正了我的新手错误,现在有了功能代码,可以重现下面他的答案中显示的结果,即4x1像素图像。从这个我现在可以愉快地产生一个100x1图像

然而,作为最后一步,我希望通过调整IHDR中的高度字段并添加额外的非终端IDAT,将其扩展到4x2图像。不幸的是,这似乎不像我预期的那样有效

我现在有点像

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
这:

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

1100EEFF  // LEN & NLEN of data           4 bytes }
在deflate数据周围还需要一个zlib包装器。见RFC 1950

最后,您需要更新区块的CRC。(顺便说一句,它的注释是错误的——应该是IDAT chunk的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

恩狄安呢?想法不错-但即使是错误的方法,我也应该得到一些输出;-)不知道?不知道,只是问问。。。顺便说一句,你期望什么样的输出,来自什么样的软件?4x1像素PNG红色、绿色、蓝色、透明。马克-真的很荣幸让你回答这个问题!谢谢我已经编写了我的代码(见上文),现在它正好生成您上面提供的代码。太棒了!然而,我打算使用额外的非终端IDAT向图像中添加多行。不幸的是,这失败了。IDAT块的序列必须包含一个zlib流。不幸的是,你不能仅仅在ZLIB流的中间插入数据。至少在不更新zlib流的开始和结束的情况下是这样。啊哈!所以我需要把整个图像放在一个块中?或者。。。你的意思是我可以把整个图像的LEN和NLEN放在第一个数据块中,然后在块之间划分行。。。我要试一试。感谢againNo,您需要将整个图像放到一个zlib流中。您可以将zlib流分解为多个块,或者将其全部放在单个块中。
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