我正在从头开始制作一个简单的png图像。我有扫描线数据。现在我想把它变成zlib流而不被压缩。我怎样才能做到这一点?我已阅读" ZLIB压缩数据格式规范版本3.3" at" https://www.ietf.org/rfc/rfc1950.txt"但仍然没有理解。有人可以给我一个关于在zlib流中设置字节的提示吗?
提前致谢!
答案 0 :(得分:1)
如RFC1950中所述,压缩算法的细节在另一个 castle RFC中描述:DEFLATE Compressed Data Format Specification version 1.3 (RFC1951)。
我们找到了
3.2.3. Details of block format
Each block of compressed data begins with 3 header bits
containing the following data:
first bit BFINAL
next 2 bits BTYPE
Note that the header bits do not necessarily begin on a byte
boundary, since a block does not necessarily occupy an integral
number of bytes.
BFINAL is set if and only if this is the last block of the data
set.
BTYPE specifies how the data are compressed, as follows:
00 - no compression
[... a few other types]
这是你想要的那个。这2位BTYPE
与最后一个块标记BFINAL
结合使用,就是您需要编写的#34;未压缩的" zlib兼容数据:
3.2.4. Non-compressed blocks (BTYPE=00)
Any bits of input up to the next byte boundary are ignored.
The rest of the block consists of the following information:
0 1 2 3 4...
+---+---+---+---+================================+
| LEN | NLEN |... LEN bytes of literal data...|
+---+---+---+---+================================+
LEN is the number of data bytes in the block. NLEN is the
one's complement of LEN.
因此,伪算法是:
78 9c
("默认压缩")。01
,否则写00
[block length]
[COMP(block length)]
ᵇ请勿忘记在压缩数据的末尾添加Adler-32校验和,按照大端顺序,在压缩'之后。就是这样。 Adler-32校验和用于验证未压缩的原始数据。在PNG图像的情况下,该数据已经由其PNG过滤器处理并且附加了行过滤器字节 - 并且 是""通过此FLATE兼容算法压缩的数据。
ᵃ这是一个当时对我来说很方便的价值;写入大小为65535字节的块应该是安全的(不要试图交叉该行)。
ᵇ两个字首先是低字节,然后是高字节。在引言中简要提到了它。