0

So reading PNG file specification indicates that before compression, the image is filtered. Currently only filtering method 0 is defined (2nd last byte in IHDR chunk). Method 0 has 5 sets of filtering functions:

0 = None
1 = Sub
2 = Up
3 = Average
4 = Paeth

When decoding such data, would only interlaced PNG images have filter types other than 0? Here's a same data after decompression:

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
-----------------------------------------------
00 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f
7f 7f 7f 7f 7f 7f 10 0f 22 22 7f 7f 7f 7f 7f 7f
7f 00 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f
7f 7f 13 43 07 11 38 2e 2d 33 25 29 09 62 1b 7f
7f 7f ...

Note the 00 every 32 bytes that indicates a "None" filtering scheme. My question is, would this only appear for non-interlaced images? Does this mean interlaced images would have filtering functions 1, 2, 3, or 4? If so, during my decoding, I'd have to read in 33 bytes and take the first one and tests if it's 0x01, 0x02, 0x03, or 0x04 to see which filter function to use?

Jongware
  • 22,200
  • 8
  • 54
  • 100
ChaoSXDemon
  • 860
  • 10
  • 29

2 Answers2

4

The filtering type can be any of the five types for each line. In the case of interlaced images, each "subimage" (up to 7 passes) is treated as an independent image, and each of its lines can have a different filter.

http://www.libpng.org/pub/png/spec/1.2/PNG-Filters.html

When the image is interlaced, each pass of the interlace pattern is treated as an independent image for filtering purposes. The filters work on the byte sequences formed by the pixels actually transmitted during a pass, and the "previous scanline" is the one previously transmitted in the same pass, not the one adjacent in the complete image. Note that the subimage transmitted in any one pass is always rectangular, but is of smaller width and/or height than the complete image. Filtering is not applied when this subimage is empty.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • Anyone know how to determine the width of a scan-line? – ChaoSXDemon Jun 17 '14 at 03:13
  • NVM, I was confused about the words "scanline" vs "width" which are the same. The number of bytes in a scanline = width and the number of scanlines = height. Width and height are defined in the IHDR chunk. – ChaoSXDemon Jun 17 '14 at 03:45
  • 2
    Not quite. Width and height as defined in the IHDR chunk are for the full image; but in the interlaced case, the image is encoded as a sequence of (up to) seven subimages of different "resolutions" (with different "width and height"). Furthermore width=number of pixels in the image line, not number of bytes. – leonbloy Jun 17 '14 at 10:11
2

All scanlines in any image whether they are interlaced or not. Will have the filter type prefix.

BevynQ
  • 8,089
  • 4
  • 25
  • 37