Trying to understand zlib/deflate in PNG files

前端 未结 2 1542
误落风尘
误落风尘 2020-12-16 16:58

I\'m currently in the middle of writing a small PNG image I/O library myself for learning purposes. My problem is the following:

I created a small PNG only 2 by 2 pi

相关标签:
2条回答
  • 2020-12-16 17:43

    I think you are missing how bits are packed inside bytes (see eg section 3.1.1 of RFC)

     Data elements are packed into bytes in order of
     increasing bit number within the byte, i.e., starting
     with the least-significant bit of the byte.
    

    Hence, if the first byte is 05 = 0000 0101 the first bit is 1.

    (BTW, it's surely quite instructive to look things at so much detail, but I wonder if you are not going a little too far if your intent is to understand PNG. )

    Further, when you come to the point were you find the uncompressed IDAT stream, bear in mind that the pixels are coded with one of five filters per row, and that there is an extra byte at the start of each row that signals the filter type. So, you won't really find the raw 12 bytes 00 00 00 FF 00 00 00 00 FF 00 FF 00, but 12+2=14 bytes instead.

    0 讨论(0)
  • 2020-12-16 17:55

    In case this helps, here is a disassembly of the IDAT chunk contents:

    ! infgen 2.2 output
    !
    zlib
    !
    last
    dynamic
    count 257 2 18
    code 1 1
    code 2 2
    code 18 2
    lens 1
    zeros 138
    zeros 116
    lens 2 2 1 1
    ! litlen 0 1
    ! litlen 255 2
    ! litlen 256 2
    ! dist 0 1
    ! dist 1 1
    literal 0 0 0 0 255 0 0 0 0 0 255 0 255 0
    end
    !
    adler
    

    You can get the infgen source code here.

    0 讨论(0)
提交回复
热议问题