src/png.bend checks
raw source on the hub · import emerging-ezimg@1.4.0.0/src/png.bend as Png
src/png: PNG decode and encode (ISO/IEC 15948 / W3C PNG). Chunks, IHDR, zlib method 0, and filter method 0. Decode reads every colour type and bit depth of PNG table 11.1, interlace methods 0 and Adam7. Encode writes colour type 2 or 6, filter None, and a stored IDAT.
3 imports
import Base import ./crc.bend as Crc import ./inflate.bend as Inf
Types
type Pic source · line 10 · raw
Data
a decoded picture: width, height, and 0xAARRGGBB samples
Pic@+w:U32 -> @+h:U32 -> @px:List<&2, U32> -> Pic
type Ihdr source · line 14 · raw
Data
IHDR fields, in specification order
Ihdr@+w:U32 -> @+h:U32 -> @+depth:U32 -> @+colour:U32 -> @+comp:U32 -> @+filter:U32 -> @+inter:U32 -> Ihdr
type Pz source · line 18 · raw
Data
chunk reader phase
ZLenPz
ZTypPz
ZDatPz
ZCrcPz
type Pg source · line 25 · raw
Data
image state while chunks are read
Pg@+w:U32 -> @+h:U32 -> @+depth:U32 -> @+colour:U32 -> @+comp:U32 -> @+filt:U32 -> @+inter:U32 -> @idat:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> @ihdr:Bool -> @saw_plte:Bool -> @saw_idat:Bool -> @idat_done:Bool -> @saw_trns:Bool -> @iend:Bool -> Pg
type Nx source · line 33 · raw
Data
a chunk was rejected, ended the image, or left more chunks
NBadNx
NEnd@pg:Pg -> Nx
NMore@pg:Pg -> Nx
type Uf source · line 39 · raw
Data
scanline phase
UFiltUf
UBodyUf
UBadUf
type Key source · line 45 · raw
Data
transparency key carried by tRNS, at the samples' full width
KNoneKey
KGrey@+g:U32 -> Key
KRgb@+r:U32 -> @+g:U32 -> @+b:U32 -> Key
type Pt source · line 53 · raw
Data
a palette for lookup by index: none, one entry, the entries at even and at odd positions (an index's low bit picks one, the rest of it the entry there), or, past the fuel, the list itself
PNonePt
POne@+v:U32 -> Pt
PTwo@ev:Pt -> @od:Pt -> Pt
PList@xs:List<&2, U32> -> Pt
type Pass source · line 61 · raw
Data
a pass read from the front of the inflated data: its pixel rows and the bytes after it, or refused
PBadPass
PGot@rows:List<&2, List<&2, U32>> -> @rest:List<&2, U32> -> Pass
Definitions
def be.u32 source · line 66 · raw
@+aa:U32 -> @+bb:U32 -> @+cc:U32 -> @+dd:U32 -> U32
big-endian unsigned 32
def tag.ihdr source · line 70 · raw
U32
IHDR
def tag.idat source · line 74 · raw
U32
IDAT
def tag.iend source · line 78 · raw
U32
IEND
def tag.plte source · line 82 · raw
U32
PLTE
def tag.trns source · line 86 · raw
U32
tRNS
def tag.anc source · line 90 · raw
@+typ:U32 -> Bool
the ancillary bit of a chunk type (bit 5 of the first byte)
def png.d.grey source · line 94 · raw
@+dd:U32 -> Bool
the bit depths of gray: 1, 2, 4, 8 and 16
def png.d.wide source · line 99 · raw
@+dd:U32 -> Bool
the bit depths of RGB, gray and alpha, and RGBA: 8 and 16
def png.d.index source · line 103 · raw
@+dd:U32 -> Bool
the bit depths of indexed colour: 1, 2, 4 and 8
def png.depth.ok source · line 108 · raw
@+cc:U32 -> @+dd:U32 -> Bool
a colour type and bit depth PNG table 11.1 allows. The depth is asked first in each arm, so at a known depth the test is one about the colour
def png.inter.ok source · line 114 · raw
@+ii:U32 -> Bool
interlace method 0 (none) or 1 (Adam7)
def png.short source · line 119 · raw
@+nn:U32 -> Bool
below 2^31: the bound PNG section 7.1 puts on a chunk's length and section 11.2.1 on the width and height (each at most 2^31 - 1)
def png.side source · line 123 · raw
@+nn:U32 -> Bool
a width or height IHDR may give: from 1 to 2^31 - 1
def png.good source · line 128 · raw
@+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+comp:U32 -> @+ff:U32 -> @+ii:U32 -> Bool
IHDR constraints: sides from 1 to 2^31 - 1, a colour type and bit depth of table 11.1, compression and filter method 0, and interlace method 0 or 1
def pg.empty source · line 138 · raw
Pg
an empty image state
def pg.ihdr source · line 142 · raw
@+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+comp:U32 -> @+ff:U32 -> @+ii:U32 -> Pg
state after a valid IHDR
def png.seal source · line 146 · raw
@pg:Pg -> Pg
close the IDAT sequence once a later chunk appears
def png.bpp.ix source · line 152 · raw
@z3:Bool -> U32
def png.bpp.one source · line 159 · raw
@z0:Bool -> @z3:Bool -> U32
def png.bpp6 source · line 166 · raw
@z6:Bool -> @+cc:U32 -> U32
def png.bpp4 source · line 173 · raw
@z4:Bool -> @z6:Bool -> @+cc:U32 -> U32
def png.bpp0 source · line 180 · raw
@z2:Bool -> @z4:Bool -> @z6:Bool -> @+cc:U32 -> U32
def png.bpp source · line 188 · raw
@+cc:U32 -> U32
bytes per pixel for an 8-bit colour type
def parse_ihdr source · line 193 · raw
@xs:List<&2, U32> -> Maybe<&2, Ihdr>
ISO/IEC 15948 / W3C PNG — IHDR is width, height, bit depth, colour type, compression method, filter method, and interlace method, each big-endian.
def uf.nth source · line 202 · raw
@xs:List<&2, U32> -> @zz:Bool -> @+ii:U32 -> U32
def uf.back source · line 213 · raw
@xs:List<&2, U32> -> @+bpp:U32 -> U32
the byte bpp back, or zero before the first pixel: xs holds the bytes
before this one in the row, newest first, so it is too short there
def uf.bb source · line 216 · raw
@prest:List<&2, U32> -> U32
def uf.drop source · line 223 · raw
@prest:List<&2, U32> -> List<&2, U32>
def uf.push source · line 230 · raw
@prest:List<&2, U32> -> @phist:List<&2, U32> -> List<&2, U32>
def uf.abs.at source · line 233 · raw
@+hi:U32 -> @+lo:U32 -> @zz:Bool -> U32
def uf.abs source · line 240 · raw
@+xx:U32 -> @+yy:U32 -> U32
def uf.paeth.b source · line 243 · raw
@zb:Bool -> @+bb:U32 -> @+cc:U32 -> U32
def uf.paeth.at source · line 250 · raw
@za:Bool -> @zb:Bool -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.paeth source · line 258 · raw
@+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
PaethPredictor from the PNG specification
def uf.avg source · line 264 · raw
@+aa:U32 -> @+bb:U32 -> U32
def uf.add source · line 268 · raw
@+ff:U32 -> @+pp:U32 -> U32
a reconstructed byte: filtered byte plus predictor, mod 256 (mask first)
def uf.p4 source · line 271 · raw
@z4:Bool -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.p3 source · line 278 · raw
@z3:Bool -> @z4:Bool -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.p2 source · line 285 · raw
@z2:Bool -> @z3:Bool -> @z4:Bool -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.p1 source · line 292 · raw
@z1:Bool -> @z2:Bool -> @z3:Bool -> @z4:Bool -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.pred source · line 299 · raw
@+ft:U32 -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.recon source · line 302 · raw
@+ff:U32 -> @+ft:U32 -> @+bpp:U32 -> @prest:List<&2, U32> -> @phist:List<&2, U32> -> @cur:List<&2, U32> -> U32
def uf.phase source · line 312 · raw
@zz:Bool -> Uf
def uf.ok source · line 319 · raw
@+ww:U32 -> @+hh:U32 -> @+bpp:U32 -> Bool
def uf.done source · line 322 · raw
@rows:Nat -> @all:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def uf.go source · line 332 · raw
@xs:List<&2, U32> -> @ph:Uf -> @zlast:Bool -> @+bpp:U32 -> @+stride:U32 -> @rows:Nat -> @+prest:List<&2, U32> -> @+phist:List<&2, U32> -> @+cur:List<&2, U32> -> @all:List<&2, U32> -> @+left:U32 -> @+ft:U32 -> Maybe<&2, List<&2, U32>>
walk the filtered stream: a filter byte opens each row (UFilt), then its
bytes (UBody). left counts the row's bytes after the current one, and
zlast, whether it is zero, marks the row's last byte.
def uf.start source · line 378 · raw
@zz:Bool -> @raw:List<&2, U32> -> @+ww:U32 -> @+hh:U32 -> @+bpp:U32 -> Maybe<&2, List<&2, U32>>
def unfilter source · line 386 · raw
@raw:List<&2, U32> -> @+ww:U32 -> @+hh:U32 -> @+bpp:U32 -> Maybe<&2, List<&2, U32>>
ISO/IEC 15948 / W3C PNG — undo filter types None, Sub, Up, Average, and Paeth.
def px.pack source · line 391 · raw
@+aa:U32 -> @+rr:U32 -> @+gg:U32 -> @+bb:U32 -> U32
def px.hit.at source · line 394 · raw
@zz:Bool -> U32
def px.hit source · line 401 · raw
@+ss:U32 -> @+kk:U32 -> U32
def px.rgb.hit source · line 404 · raw
@+rr:U32 -> @+gg:U32 -> @+bb:U32 -> @+kr:U32 -> @+kg:U32 -> @+kb:U32 -> U32
def px.grey source · line 407 · raw
@xs:List<&2, U32> -> @key:Key -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.rgb source · line 418 · raw
@xs:List<&2, U32> -> @key:Key -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.ga source · line 429 · raw
@xs:List<&2, U32> -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.rgba source · line 438 · raw
@xs:List<&2, U32> -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def pt.alt source · line 450 · raw
@xs:List<&2, U32> -> @keep:Bool -> List<&2, U32>
every other entry, from the first when keep and from the second otherwise
def pt.leaf source · line 460 · raw
@xs:List<&2, U32> -> Pt
the tree once the fuel is spent: no entry, one, or the list itself
def pt.build source · line 473 · raw
@fuel:Nat -> @xs:List<&2, U32> -> Pt
a lookup tree of the entries: an index's low bit picks the half its entry is in, so a palette of 256 entries is found in eight steps, not 256
def pt.tree source · line 491 · raw
@+xs:List<&2, U32> -> Pt
the lookup tree of a list: as many halvings as there are entries is more fuel than the halving needs, so the tree never falls back on the list
def pt.list source · line 496 · raw
@xs:List<&2, U32> -> @+ix:U32 -> Maybe<&2, U32>
the entry at an index of a list the fuel ran out on: never reached from pt.tree, there for a tree built with too little fuel
def pt.hit source · line 500 · raw
@zz:Bool -> @+vv:U32 -> Maybe<&2, U32>
the entry when the index left is zero
def pt.even source · line 508 · raw
@+ix:U32 -> Bool
whether an index is even: its low bit is clear
def pt.at source · line 513 · raw
@tr:Pt -> @even:Bool -> @+ix:U32 -> Maybe<&2, U32>
the entry at an index, none past the last; even is pt.even(ix), which
picks the half of a split the entry is in
def px.use source · line 526 · raw
@mm:Maybe<&2, U32> -> @kk:(@_:U32 -> Maybe<&2, List<&2, U32>>) -> Maybe<&2, List<&2, U32>>
def px.idx source · line 533 · raw
@xs:List<&2, U32> -> @+pal:Pt -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def pal.make source · line 542 · raw
@xs:List<&2, U32> -> @als:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>
the palette's entries, 0xAARRGGBB, each alpha from tRNS while it lasts and 255 after; a last entry cut short is no entry
def pal.alphas source · line 555 · raw
@zz:Bool -> @als:List<&2, U32> -> List<&2, U32>
tRNS's alphas when it has no more entries than PLTE; none otherwise, as libpng ignores a palette tRNS longer than the palette
def pal.tree source · line 563 · raw
@+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Pt
the palette's lookup tree, with alphas from tRNS
def px.indexed source · line 567 · raw
@samples:List<&2, U32> -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def key.u16 source · line 571 · raw
@zz:Bool -> @+hi:U32 -> @+lo:U32 -> U32
a 16-bit tRNS value from its two bytes, most significant first
def key.grey source · line 580 · raw
@xs:List<&2, U32> -> Key
a gray key: tRNS of exactly two bytes. Any other length is a faulty tRNS, ignored (PNG section 13.1)
def key.rgb source · line 588 · raw
@xs:List<&2, U32> -> Key
an RGB key: tRNS of exactly six bytes; any other length is ignored
def px.k4 source · line 595 · raw
@z6:Bool -> @samples:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.k3 source · line 603 · raw
@z4:Bool -> @z6:Bool -> @samples:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
gray and alpha, and RGBA, carry their own alpha: a tRNS there is ignored
def px.k2 source · line 616 · raw
@z2:Bool -> @z4:Bool -> @z6:Bool -> @samples:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.k0 source · line 630 · raw
@z0:Bool -> @z2:Bool -> @z4:Bool -> @z6:Bool -> @samples:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.of source · line 646 · raw
@+colour:U32 -> @samples:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
8-bit samples to 0xAARRGGBB for a colour type
def png.mod3 source · line 651 · raw
@xs:List<&2, U32> -> Bool
def png.ihdr.ok source · line 655 · raw
@zz:Bool -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+comp:U32 -> @+ff:U32 -> @+ii:U32 -> Nx
def png.ihdr.put source · line 662 · raw
@mm:Maybe<&2, Ihdr> -> Nx
def png.allow source · line 669 · raw
@zz:Bool -> @pg:Pg -> Nx
def pg.idat source · line 676 · raw
@pg:Pg -> @extra:List<&2, U32> -> Pg
def pg.plte source · line 682 · raw
@pg:Pg -> @bytes:List<&2, U32> -> Pg
def pg.trns source · line 687 · raw
@pg:Pg -> @bytes:List<&2, U32> -> Pg
def pg.iend source · line 692 · raw
@pg:Pg -> Pg
def png.idat.try source · line 697 · raw
@zz:Bool -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.on.ihdr source · line 704 · raw
@ihdr:Bool -> @data:List<&2, U32> -> Nx
def png.on.idat source · line 711 · raw
@ihdr:Bool -> @idat_done:Bool -> @need:Bool -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.plte.ok source · line 718 · raw
@+cc:U32 -> Bool
def png.trns.ok source · line 721 · raw
@+cc:U32 -> @saw_plte:Bool -> Bool
def png.on.plte source · line 727 · raw
@ihdr:Bool -> @saw_plte:Bool -> @saw_idat:Bool -> @+colour:U32 -> @+data:List<&2, U32> -> @pg:Pg -> Nx
def png.trns.take source · line 740 · raw
@zz:Bool -> @data:List<&2, U32> -> @pg:Pg -> Nx
a tRNS the decoder takes: the first, before IDAT, for a colour type that allows one. Any other is ignored as a faulty ancillary chunk (PNG section 13.1; libpng's benign error), the way png.other.at passes an unknown one
def png.on.trns source · line 747 · raw
@ihdr:Bool -> @saw_trns:Bool -> @saw_idat:Bool -> @+colour:U32 -> @saw_plte:Bool -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.iend.ok source · line 762 · raw
@zz:Bool -> @pg:Pg -> Nx
def png.iend.at source · line 769 · raw
@zempty:Bool -> @zpal:Bool -> @pg:Pg -> Nx
def png.on.iend source · line 776 · raw
@ihdr:Bool -> @+colour:U32 -> @saw_plte:Bool -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.other.at source · line 783 · raw
@anc:Bool -> @pg:Pg -> Nx
def png.on.other source · line 790 · raw
@ihdr:Bool -> @anc:Bool -> @pg:Pg -> Nx
def png.apply.tr source · line 797 · raw
@ztrns:Bool -> @anc:Bool -> @ihdr:Bool -> @saw_plte:Bool -> @saw_idat:Bool -> @saw_trns:Bool -> @+colour:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply.pl source · line 814 · raw
@zplte:Bool -> @ztrns:Bool -> @anc:Bool -> @ihdr:Bool -> @saw_plte:Bool -> @saw_idat:Bool -> @saw_trns:Bool -> @+colour:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply.ie source · line 832 · raw
@ziend:Bool -> @zplte:Bool -> @ztrns:Bool -> @anc:Bool -> @ihdr:Bool -> @saw_plte:Bool -> @saw_idat:Bool -> @saw_trns:Bool -> @+colour:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply.id source · line 851 · raw
@zidat:Bool -> @ziend:Bool -> @zplte:Bool -> @ztrns:Bool -> @anc:Bool -> @ihdr:Bool -> @saw_plte:Bool -> @saw_idat:Bool -> @idat_done:Bool -> @saw_trns:Bool -> @+colour:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply.go source · line 872 · raw
@zihdr:Bool -> @zidat:Bool -> @ziend:Bool -> @zplte:Bool -> @ztrns:Bool -> @anc:Bool -> @ihdr:Bool -> @saw_plte:Bool -> @saw_idat:Bool -> @idat_done:Bool -> @saw_trns:Bool -> @+colour:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply.at source · line 894 · raw
@iend:Bool -> @zihdr:Bool -> @zidat:Bool -> @ziend:Bool -> @zplte:Bool -> @ztrns:Bool -> @anc:Bool -> @ihdr:Bool -> @saw_plte:Bool -> @saw_idat:Bool -> @idat_done:Bool -> @saw_trns:Bool -> @+colour:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply source · line 918 · raw
@+typ:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.judge.at source · line 929 · raw
@zz:Bool -> @+typ:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.judge source · line 936 · raw
@+got:U32 -> @crc:List<&2, U32> -> @+typ:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.zp source · line 939 · raw
@zz:Bool -> Pz
def png.pic source · line 946 · raw
@mm:Maybe<&2, List<&2, U32>> -> @+ww:U32 -> @+hh:U32 -> Maybe<&2, Pic>
def png.pack source · line 953 · raw
@mm:Maybe<&2, List<&2, U32>> -> @+ww:U32 -> @+hh:U32 -> @+colour:U32 -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, Pic>
def png.samples source · line 967 · raw
@mm:Maybe<&2, List<&2, U32>> -> @+ww:U32 -> @+hh:U32 -> @+colour:U32 -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, Pic>
def png.bits source · line 984 · raw
@+dd:U32 -> @+cc:U32 -> U32
the bits of one pixel: the bit depth times the channels
def png.fbpp.at source · line 987 · raw
@zz:Bool -> @+nn:U32 -> U32
def png.fbpp source · line 996 · raw
@+bits:U32 -> U32
the filters' bytes per pixel (PNG section 9.2): a pixel's whole bytes, at least one
def png.stride source · line 1000 · raw
@pw:Nat -> @+bits:U32 -> Nat
a scanline's bytes after its filter byte, for pw pixels: ceil(pw * bits / 8)
def uf.rows.at source · line 1003 · raw
@zz:Bool -> @raw:List<&2, U32> -> @+stride:U32 -> @+hh:U32 -> @+bpp:U32 -> Maybe<&2, List<&2, U32>>
def unfilter.rows source · line 1012 · raw
@raw:List<&2, U32> -> @+stride:U32 -> @+hh:U32 -> @+bpp:U32 -> Maybe<&2, List<&2, U32>>
undo the filters of hh rows of stride bytes each, bpp bytes per pixel
back (1 to 8): the scanlines of any bit depth, or of one Adam7 pass
def unpack.byte source · line 1018 · raw
@z1:Bool -> @z2:Bool -> @+bb:U32 -> @acc:List<&2, U32> -> List<&2, U32>
the samples of one byte at bit depth 1, 2 or 4, most significant first, pushed onto acc newest first
def unpack.low source · line 1032 · raw
@+dd:U32 -> @row:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>
every sample of a row's bytes at bit depth 1, 2 or 4, newest first
def unpack.wide source · line 1040 · raw
@row:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>
the 16-bit samples of a row, two bytes each, most significant first; newest first
def unpack.row.at source · line 1047 · raw
@z8:Bool -> @z16:Bool -> @+dd:U32 -> @+count:Nat -> @row:List<&2, U32> -> List<&2, U32>
def unpack.row source · line 1059 · raw
@+dd:U32 -> @+count:Nat -> @row:List<&2, U32> -> List<&2, U32>
the samples of one scanline, in order: count samples of dd bits (PNG section 7.2: packed most significant bit first, 16-bit samples most significant byte first), the unused bits at the end of a row dropped
def pass.samples source · line 1063 · raw
@rows:Nat -> @+dd:U32 -> @+bytes:List<&2, U32> -> @+stride:Nat -> @+count:Nat -> @acc:List<&2, U32> -> List<&2, U32>
the samples of a pass's unfiltered rows, stride bytes each, in order
def px.hi source · line 1079 · raw
@+vv:U32 -> U32
a 16-bit sample reduced to 8 bits: its high byte
def px.up source · line 1084 · raw
@+mx:U32 -> @+vv:U32 -> U32
a sample of bit depth 1, 2 or 4 scaled to 8 bits as libpng and Pillow do, v * 255 / (2^d - 1): bit replication (PNG section 13.12)
def px.grey16 source · line 1088 · raw
@xs:List<&2, U32> -> @key:Key -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
16-bit gray, the key compared at 16 bits
def px.rgb16 source · line 1100 · raw
@xs:List<&2, U32> -> @key:Key -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
16-bit RGB, the key compared at 16 bits
def px.his source · line 1113 · raw
@xs:List<&2, U32> -> List<&2, U32>
every 16-bit sample reduced to its high byte
def px.grey.low source · line 1121 · raw
@+mx:U32 -> @xs:List<&2, U32> -> @key:Key -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
gray of bit depth 1, 2 or 4 (mx = 2^d - 1), the key compared unscaled
def px.w2 source · line 1133 · raw
@z2:Bool -> @z4:Bool -> @samples:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.wide.at source · line 1142 · raw
@zz:Bool -> @+cc:U32 -> @samples:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.wide source · line 1150 · raw
@+cc:U32 -> @samples:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
16-bit samples to 0xAARRGGBB for a colour type (0, 2, 4 or 6)
def px.low.at source · line 1153 · raw
@zz:Bool -> @+dd:U32 -> @samples:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.low source · line 1168 · raw
@+cc:U32 -> @+dd:U32 -> @samples:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
samples of bit depth 1, 2 or 4 to 0xAARRGGBB: gray scaled up, or palette indices
def px.deep.at source · line 1177 · raw
@z8:Bool -> @z16:Bool -> @+cc:U32 -> @+dd:U32 -> @samples:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.deep source · line 1195 · raw
@+cc:U32 -> @+dd:U32 -> @samples:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
samples of any bit depth to 0xAARRGGBB for a colour type
def rows.split source · line 1205 · raw
@rows:Nat -> @+xs:List<&2, U32> -> @+nn:Nat -> List<&2, List<&2, U32>>
rows of nn samples each, rows of them
def pass.rows source · line 1212 · raw
@mm:Maybe<&2, List<&2, U32>> -> @rest:List<&2, U32> -> @+pw:U32 -> @+ph:U32 -> Pass
def pass.px source · line 1219 · raw
@mm:Maybe<&2, List<&2, U32>> -> @rest:List<&2, U32> -> @+sn:Nat -> @+pw:U32 -> @+ph:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Pass
def pass.fit source · line 1237 · raw
@zz:Bool -> @+raw:List<&2, U32> -> @+sn:Nat -> @+pw:U32 -> @+ph:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Pass
def pass.open source · line 1257 · raw
@zz:Bool -> @+raw:List<&2, U32> -> @+pw:U32 -> @+ph:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Pass
def pass.read source · line 1276 · raw
@+raw:List<&2, U32> -> @+pw:U32 -> @+ph:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Pass
a pass of pw by ph pixels, its scanlines filtered on their own (PNG section 8.2); a pass with a side 0 is empty and has no bytes
def png.up source · line 1288 · raw
@+nn:U32 -> U32
ceil(n / 2)
def png.dn source · line 1292 · raw
@+nn:U32 -> U32
floor(n / 2)
def ilv.px source · line 1297 · raw
@aa:List<&2, U32> -> @bb:List<&2, U32> -> List<&2, U32>
a's samples at even places and b's at odd: a0 b0 a1 b1 and so on, to the end of a
def ilv.rows source · line 1309 · raw
@aa:List<&2, List<&2, U32>> -> @bb:List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>
a's rows at even places and b's at odd
def rows.head source · line 1321 · raw
@bb:List<&2, List<&2, U32>> -> List<&2, U32>
the first row, or an empty one when there is none
def rows.tail source · line 1329 · raw
@bb:List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>
the rows after the first
def ilv.cols source · line 1338 · raw
@aa:List<&2, List<&2, U32>> -> @+bb:List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>
each row of a with b's row of the same place, a's samples at even columns and b's at odd; a row b lacks is empty
def a7.end source · line 1346 · raw
@zz:Bool -> @rows:List<&2, List<&2, U32>> -> Maybe<&2, List<&2, U32>>
the picture's samples, when the data held exactly the passes
def a7.merge source · line 1358 · raw
@r1:List<&2, List<&2, U32>> -> @+r2:List<&2, List<&2, U32>> -> @r3:List<&2, List<&2, U32>> -> @+r4:List<&2, List<&2, U32>> -> @r5:List<&2, List<&2, U32>> -> @+r6:List<&2, List<&2, U32>> -> @r7:List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>
the seven passes put back together (PNG section 8.2), by halves: pass 7 holds the odd rows and passes 1 to 6 the even ones; of those, pass 6 holds the odd columns and passes 1 to 5 the even ones; and so on down to pass 1, every eighth sample of every eighth row. Each step interleaves rows (ilv.rows) or columns (ilv.cols)
def a7.s7 source · line 1369 · raw
@r1:List<&2, List<&2, U32>> -> @r2:List<&2, List<&2, U32>> -> @r3:List<&2, List<&2, U32>> -> @r4:List<&2, List<&2, U32>> -> @r5:List<&2, List<&2, U32>> -> @r6:List<&2, List<&2, U32>> -> @pp:Pass -> Maybe<&2, List<&2, U32>>
def a7.s6 source · line 1384 · raw
@r1:List<&2, List<&2, U32>> -> @r2:List<&2, List<&2, U32>> -> @r3:List<&2, List<&2, U32>> -> @r4:List<&2, List<&2, U32>> -> @r5:List<&2, List<&2, U32>> -> @pp:Pass -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def a7.s5 source · line 1404 · raw
@r1:List<&2, List<&2, U32>> -> @r2:List<&2, List<&2, U32>> -> @r3:List<&2, List<&2, U32>> -> @r4:List<&2, List<&2, U32>> -> @pp:Pass -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def a7.s4 source · line 1423 · raw
@r1:List<&2, List<&2, U32>> -> @r2:List<&2, List<&2, U32>> -> @r3:List<&2, List<&2, U32>> -> @pp:Pass -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def a7.s3 source · line 1442 · raw
@r1:List<&2, List<&2, U32>> -> @r2:List<&2, List<&2, U32>> -> @pp:Pass -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def a7.s2 source · line 1461 · raw
@r1:List<&2, List<&2, U32>> -> @pp:Pass -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def a7.s1 source · line 1479 · raw
@pp:Pass -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def a7.go source · line 1498 · raw
@+raw:List<&2, U32> -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
Adam7 (PNG section 8.2): seven passes in order, each a picture of its own, read at the sizes png.a7.len counts and put back together by a7.merge
def png.pass.len.at source · line 1513 · raw
@zz:Bool -> @+pw:U32 -> @+ph:U32 -> @+bits:U32 -> Nat
the filtered bytes of a pass of pw by ph pixels: a filter byte and a scanline for each row, none for an empty pass
def png.pass.len source · line 1520 · raw
@+pw:U32 -> @+ph:U32 -> @+bits:U32 -> Nat
def png.a7.len source · line 1524 · raw
@+ww:U32 -> @+hh:U32 -> @+bits:U32 -> Nat
the filtered bytes of the seven Adam7 passes, as a7.go reads them
def png.budget.at source · line 1534 · raw
@zz:Bool -> @+ww:U32 -> @+hh:U32 -> @+bits:U32 -> Nat
def png.budget source · line 1543 · raw
@+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+ii:U32 -> Nat
the exact length the inflated IDAT data must have for the header: what unfilter reads, so the inflater can stop as soon as a stream passes it
def png.flat source · line 1547 · raw
@pp:Pass -> Maybe<&2, List<&2, U32>>
a picture without interlacing: one pass of the whole size
def png.deep.at source · line 1554 · raw
@zz:Bool -> @+raw:List<&2, U32> -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def png.deep source · line 1571 · raw
@mm:Maybe<&2, List<&2, U32>> -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+ii:U32 -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, Pic>
the picture of any bit depth and interlace method from the inflated data
def png.budget8 source · line 1590 · raw
@+ww:U32 -> @+hh:U32 -> @+cc:U32 -> Nat
the filtered bytes of 8-bit samples without interlacing: h rows, each a filter byte and w pixels of bpp bytes. png.budget at bit depth 8 and interlace method 0, in the form the round trip reads
def png.finish.go source · line 1596 · raw
@zenc:Bool -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+colour:U32 -> @+ii:U32 -> @idat:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, Pic>
8-bit samples without interlacing take png.samples; any other bit depth, or Adam7, png.deep. Either way the inflater stops once the data passes the length the header implies
def png.finish.at source · line 1615 · raw
@zz:Bool -> @zenc:Bool -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+colour:U32 -> @+ii:U32 -> @idat:List<&2, U32> -> @plte:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, Pic>
def png.finish source · line 1633 · raw
@pg:Pg -> Maybe<&2, Pic>
def png.pgof source · line 1644 · raw
@nx:Nx -> Pg
after a judged chunk: none when it was refused, the picture when it was IEND (whatever bytes follow it, as libpng and other decoders stop reading there), and the walk over the rest otherwise. The walk is started only in that last case: a refusal or IEND reads nothing more the state a judged chunk leaves; any state for one refused
def png.crc source · line 1657 · raw
@nx:Nx -> @more:(@_:Unit -> Maybe<&2, Pic>) -> Maybe<&2, Pic>
after a judged chunk: none when it was refused, the picture when it was IEND (whatever bytes follow it, as libpng and other decoders stop reading there), and the walk over the rest otherwise. The walk is a thunk, forced only in that last case: after a refusal or IEND nothing more is read
def png.len.ok source · line 1669 · raw
@zz:Bool -> @more:(@_:Unit -> Maybe<&2, Pic>) -> Maybe<&2, Pic>
after a chunk's length field: the walk over the rest when the length is below 2^31 (PNG section 7.1), none otherwise. The walk is a thunk, forced only when the length is taken
def png.go source · line 1677 · raw
@xs:List<&2, U32> -> @pz:Pz -> @zlast:Bool -> @+left:U32 -> @+typ:U32 -> @crc:List<&2, U32> -> @data:List<&2, U32> -> @pg:Pg -> Maybe<&2, Pic>
one byte of a PNG chunk stream. zlast is the last data byte of the chunk.
def decode source · line 1712 · raw
@xs:List<&2, U32> -> Maybe<&2, Pic>
ISO/IEC 15948 / W3C PNG — chunks after the signature, CRC-32 over type and data, then IDAT inflated as zlib DEFLATE (compression method 0).
def enc.byte source · line 1718 · raw
@+nn:U32 -> U32
the low byte
def enc.hi source · line 1722 · raw
@+nn:U32 -> U32
the next byte up
def enc.be source · line 1726 · raw
@+nn:U32 -> List<&2, U32>
four bytes, big-endian
def enc.a source · line 1731 · raw
@+cc:U32 -> U32
alpha of a 0xAARRGGBB sample. The mask is the first operand, so once a law takes the sample's bits apart, the masked byte reduces to them.
def enc.r source · line 1736 · raw
@+cc:U32 -> U32
red of a 0xAARRGGBB sample. The mask goes first here too, so a sample's bits, once taken apart, reduce to the byte.
def enc.g source · line 1740 · raw
@+cc:U32 -> U32
green of a 0xAARRGGBB sample
def enc.b source · line 1744 · raw
@+cc:U32 -> U32
blue of a 0xAARRGGBB sample
def enc.push.rgb source · line 1748 · raw
@+cc:U32 -> @acc:List<&2, U32> -> List<&2, U32>
one opaque pixel, newest-first, so a later reverse is R, G, B
def enc.push.rgba source · line 1752 · raw
@+cc:U32 -> @acc:List<&2, U32> -> List<&2, U32>
one pixel, newest-first, so a later reverse is R, G, B, A
def enc.push source · line 1756 · raw
@op:Bool -> @+cc:U32 -> @acc:List<&2, U32> -> List<&2, U32>
the channels of one sample
def enc.pred source · line 1764 · raw
@nn:Nat -> Nat
one less, or zero
def enc.len source · line 1772 · raw
@xs:List<&2, U32> -> @+nn:U32 -> U32
how many bytes, counted in a word
def enc.cat.go source · line 1780 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> List<&2, U32>
a reversed prefix, consed onto the suffix
def enc.cat source · line 1788 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> List<&2, U32>
xs, then ys
def enc.nch source · line 1792 · raw
@op:Bool -> U32
channels written for one sample
def enc.raw source · line 1803 · raw
@px:List<&2, U32> -> @left:Nat -> @+ww:Nat -> @+op:Bool -> @acc:List<&2, U32> -> @+nn:U32 -> Pair(List<&2, U32>, U32)
scanlines, filter None, newest-first. left is 0 at the first sample of a
row, where the filter byte is written; otherwise it is the samples still
to come after this one. n is how many bytes are in acc. One reverse at
the end puts the bytes in order.
def enc.head source · line 1821 · raw
@fin:Bool -> @+len:U32 -> @+nlen:U32 -> List<&2, U32>
the five-byte stored-block header: BFINAL, then LEN and NLEN, little-endian
def enc.stored source · line 1829 · raw
@fin:Bool -> @+data:List<&2, U32> -> List<&2, U32>
one stored DEFLATE block. data is chronological.
def enc.feed source · line 1835 · raw
@xs:List<&2, U32> -> @zz:Bool -> @+room:U32 -> @+max:U32 -> @acc:List<&2, U32> -> List<&2, U32>
stored blocks of at most max bytes. z means this byte opens a block.
acc is the open block, newest-first.
def enc.blocks.fit source · line 1854 · raw
@zz:Bool -> @+max:U32 -> @+nn:U32 -> @+xs:List<&2, U32> -> List<&2, U32>
one final stored block when the payload already fits
def enc.blocks source · line 1862 · raw
@+max:U32 -> @+xs:List<&2, U32> -> List<&2, U32>
stored DEFLATE blocks, each at most max bytes, the last one final
def enc.chunk source · line 1867 · raw
@+tag:U32 -> @+data:List<&2, U32> -> List<&2, U32>
a chunk: length, type, data, and CRC-32 over the type and the data
def enc.ihdr source · line 1874 · raw
@+ww:U32 -> @+hh:U32 -> @+ct:U32 -> List<&2, U32>
IHDR: width, height, depth 8, colour type, method 0, filter 0, interlace 0
def enc.sig source · line 1878 · raw
List<&2, U32>
the eight-byte signature
def enc.ct source · line 1882 · raw
@op:Bool -> U32
colour type 2 when opaque, otherwise 6
def enc.zlib source · line 1891 · raw
@+raw:List<&2, U32> -> @+nn:U32 -> List<&2, U32>
zlib wrapper, CMF/FLG 120 1, around stored DEFLATE blocks and an Adler-32.
n is the length of raw, already counted while the scanlines were built.
def enc.nblk.at source · line 1896 · raw
@zz:Bool -> @+qq:U32 -> U32
how many stored blocks cover n raw bytes
def enc.nblk source · line 1904 · raw
@+nn:U32 -> U32
ceil(n / 65535). An exact multiple stays q.
def enc.idat.ln source · line 1908 · raw
@+nn:U32 -> U32
IDAT data length: zlib header, stored-block headers, raw bytes, Adler-32
def enc.crc.list source · line 1912 · raw
@xs:List<&2, U32> -> @+cc:U32 -> @acc:List<&2, U32> -> Pair(U32, List<&2, U32>)
CRC-32 and cons chronological bytes onto a newest-first accumulator
def enc.cap.at source · line 1920 · raw
@zz:Bool -> @+nn:U32 -> U32
the stored-block length: the bytes still open, at most 65535
def enc.cap source · line 1928 · raw
@+nn:U32 -> U32
min(n, 65535)
def enc.bf.at source · line 1932 · raw
@zz:Bool -> U32
BFINAL is 1 when this block holds every byte still open
def enc.bf source · line 1940 · raw
@+nn:U32 -> U32
1 when n fits in one stored block, otherwise 0
def enc.pour source · line 1947 · raw
@xs:List<&2, U32> -> @znew:Bool -> @zlast:Bool -> @+room:U32 -> @+left:U32 -> @+len:U32 -> @+bf:U32 -> @+cc:U32 -> @acc:List<&2, U32> -> Pair(U32, List<&2, U32>)
stored blocks, newest-first, CRC running over headers and data. znew means
this byte opens a block of length len with BFINAL bf. zlast means this
data byte closes the block. room is the data bytes left in the open block,
including this one. left counts raw bytes still to come, including h.
def enc.seal.crc source · line 1996 · raw
@pp:Pair(U32, List<&2, U32>) -> @ie:List<&2, U32> -> List<&2, U32>
Adler-32, then the CRC field, then IEND, then one reverse of the whole file
def enc.seal.adler source · line 2003 · raw
@pp:Pair(U32, List<&2, U32>) -> @+sum:U32 -> @ie:List<&2, U32> -> List<&2, U32>
the four Adler bytes belong to the IDAT data and to its CRC
def enc.seal.run source · line 2009 · raw
@pp:Pair(U32, List<&2, U32>) -> @raw:List<&2, U32> -> @+nn:U32 -> @+sum:U32 -> @ie:List<&2, U32> -> List<&2, U32>
zlib header, then the stored blocks
def enc.seal.body source · line 2021 · raw
@pp:Pair(U32, List<&2, U32>) -> @raw:List<&2, U32> -> @+nn:U32 -> @+sum:U32 -> @ie:List<&2, U32> -> List<&2, U32>
IDAT type, then the zlib stream
def enc.add.at source · line 2033 · raw
@zz:Bool -> @+tt:U32 -> U32
Adler-32 reduced once. The sum stays below twice the modulus.
def enc.add source · line 2041 · raw
@+ss:U32 -> @+xx:U32 -> U32
one Adler-32 add, modulo 65521
def enc.rgb.fin source · line 2046 · raw
@+cc:U32 -> @acc:List<&2, U32> -> @+aa:U32 -> @+bb:U32 -> Pair(Pair(U32, List<&2, U32>), U32)
the running CRC, the newest-first bytes, and the packed Adler-32
def enc.rgb.go source · line 2053 · raw
@fuel:Nat -> @zblock:Bool -> @zdone:Bool -> @zspan:Bool -> @zneed:Bool -> @filt:Bool -> @zred:Bool -> @zgrn:Bool -> @zlast:Bool -> @px:List<&2, U32> -> @+col:U32 -> @+row:U32 -> @+ww:U32 -> @+need:U32 -> @+cc:U32 -> @acc:List<&2, U32> -> @+aa:U32 -> @+bb:U32 -> @+left:U32 -> @+len:U32 -> Pair(Pair(U32, List<&2, U32>), U32)
stored blocks for colour type 2, one decreasing step at a time. zblock opens a block and zspan counts its raw bytes. The other flags are the cursor: a filter byte, red, green, or the last sample of the row. Block headers match enc.pour. Adler-32 covers the raw bytes only.
def enc.wide.rgb.out source · line 2192 · raw
@pp:Pair(Pair(U32, List<&2, U32>), U32) -> @ie:List<&2, U32> -> List<&2, U32>
def enc.wide.rgb.z source · line 2198 · raw
@pp:Pair(U32, List<&2, U32>) -> @px:List<&2, U32> -> @+ww:U32 -> @+nn:U32 -> @ie:List<&2, U32> -> List<&2, U32>
zlib header, then the stored blocks written from the samples
def enc.wide.rgb.go source · line 2215 · raw
@pp:Pair(U32, List<&2, U32>) -> @px:List<&2, U32> -> @+ww:U32 -> @+nn:U32 -> @ie:List<&2, U32> -> List<&2, U32>
IDAT type, then the zlib stream
def enc.wide.rgb source · line 2230 · raw
@+ww:U32 -> @+hh:U32 -> @px:List<&2, U32> -> @+nn:U32 -> List<&2, U32>
a colour-type-2 picture whose filtered bytes do not fit in one stored block. Each filter and channel byte is consed into its stored block, with Adler-32 and the IDAT CRC, so the raw scanline list is not built. The signature and IHDR go in front once the rest is reversed.
def enc.seal.wide source · line 2240 · raw
@+ww:U32 -> @+hh:U32 -> @op:Bool -> @+raw:List<&2, U32> -> @+nn:U32 -> List<&2, U32>
one picture whose filtered bytes do not fit in a single stored block. The IDAT length and the payload are consed newest-first and reversed once, then the signature and IHDR go in front. Adler-32 stays a separate pass over the raw bytes.
def enc.seal.one source · line 2254 · raw
@+ww:U32 -> @+hh:U32 -> @op:Bool -> @+raw:List<&2, U32> -> @+nn:U32 -> List<&2, U32>
signature, IHDR, IDAT, IEND when the raw bytes fit in one stored block
def enc.seal.at source · line 2260 · raw
@zz:Bool -> @+ww:U32 -> @+hh:U32 -> @op:Bool -> @raw:List<&2, U32> -> @+nn:U32 -> List<&2, U32>
one stored block when the payload fits; several blocks written once otherwise
def enc.seal source · line 2275 · raw
@+ww:U32 -> @+hh:U32 -> @op:Bool -> @got:Pair(List<&2, U32>, U32) -> List<&2, U32>
signature, IHDR, IDAT, IEND
def enc.opaque source · line 2281 · raw
@px:List<&2, U32> -> @zz:Bool -> Bool
every sample so far is opaque, and z carries that fact forward
def enc.rows source · line 2293 · raw
@fuel:List<&2, U32> -> @zh:Bool -> @zlt:Bool -> @+ww:U32 -> @+hh:U32 -> @+cc:U32 -> Bool
c less w, h times over: for w above 0, true exactly when c is w * h. zh
means no rows are left, zlt that c is below w. The samples are fuel: c is
at most their count, so the walk stops before they run out.
def enc.small source · line 2314 · raw
@+nn:U32 -> Bool
fewer than 2^28 samples: the count's top four bits are clear
def enc.good source · line 2328 · raw
@+ww:U32 -> @+hh:U32 -> @+px:List<&2, U32> -> Bool
a positive area whose sample list covers it, below 2^28 samples: both sides above 0, the sample count, taken in a word, is w * h, and that count is below 2^28.
The bound keeps every count the encoder takes in a word exact and the one IDAT chunk inside PNG's chunk-length limit (ISO/IEC 15948 5.3, 2^31 - 1 bytes): below 2^28 samples the filtered bytes h (k w + 1), k at most 4, are at most 5 * 2^28 - 5, and with a 5-byte header per 65535-byte stored block and 6 bytes of zlib framing the IDAT data stays below 1.35e9 bytes. That margin is computed here, not proved; the round trip (laws/PngRoundTrips.bend) is proved for every raster this accepts.
def enc.nbytes source · line 2335 · raw
@op:Bool -> @+ww:U32 -> @+hh:U32 -> U32
filtered bytes: one per row, plus the channels of every sample. enc.good's bound keeps the product below 2^31, so the word does not wrap.
def enc.paint.wide source · line 2340 · raw
@op:Bool -> @+ww:U32 -> @+hh:U32 -> @px:List<&2, U32> -> @+nn:U32 -> List<&2, U32>
colour type 6, and a colour type 2 that fits in one block, still build the raw list. A larger colour type 2 writes its stored blocks from the samples.
def enc.paint.at source · line 2353 · raw
@zz:Bool -> @+ww:U32 -> @+hh:U32 -> @+op:Bool -> @px:List<&2, U32> -> @+nn:U32 -> List<&2, U32>
def enc.paint source · line 2368 · raw
@+ww:U32 -> @+hh:U32 -> @+op:Bool -> @px:List<&2, U32> -> List<&2, U32>
the PNG bytes of a picture already known to be in range
def enc.open source · line 2373 · raw
@zz:Bool -> @+ww:U32 -> @+hh:U32 -> @+px:List<&2, U32> -> Maybe<&2, List<&2, U32>>
the bytes, or none when the picture is not encodable
def enc.file source · line 2382 · raw
@+ww:U32 -> @+hh:U32 -> @+px:List<&2, U32> -> Maybe<&2, List<&2, U32>>
ISO/IEC 15948 / W3C PNG — an 8-bit encoding, interlace 0, filter None. Colour type 2 when every sample is opaque, otherwise colour type 6.