src/png.bend checks
raw source on the hub · import emerging-ezimg@1.5.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 Run source · line 26 · raw
Data
where the IDAT chunks are (PNG section 5.6: they are consecutive): none yet, a run still open, or a run a later chunk has closed
RNoneRun
ROpenRun
RShutRun
type Pg source · line 35 · raw
Data
image state while chunks are read: before IHDR, or after it with the IHDR fields, the IDAT data so far (newest first), the PLTE and tRNS data once each has come, and where the IDAT run is. IEND is no state: the walk stops there (NEnd)
PgStartPg
Pg@+w:U32 -> @+h:U32 -> @+depth:U32 -> @+colour:U32 -> @+comp:U32 -> @+filt:U32 -> @+inter:U32 -> @idat:List<&2, U32> -> @plte:Maybe<&2, List<&2, U32>> -> @trns:Maybe<&2, List<&2, U32>> -> @run:Run -> Pg
type Nx source · line 43 · 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 49 · raw
Data
scanline phase
UFiltUf
UBodyUf
UBadUf
type Key source · line 55 · 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 63 · 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 Hd source · line 72 · raw
Data
what the header and the PLTE and tRNS chunks say of the samples: the picture's width and height, bit depth, colour type, palette and transparency
Hd@+w:U32 -> @+h:U32 -> @+depth:U32 -> @+colour:U32 -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Hd
type Pass source · line 77 · 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
type Ur source · line 359 · raw
Data
where unfilter stands in a row: the row above from the current byte on (prest), the bytes of it already passed (phist), the row so far, reversed (cur), the row's bytes after the current one (left) and its filter type (ft)
Ur@+prest:List<&2, U32> -> @+phist:List<&2, U32> -> @+cur:List<&2, U32> -> @+left:U32 -> @+ft:U32 -> Ur
type Tags source · line 805 · raw
Data
what a chunk after IHDR is: IHDR again, IDAT, IEND, PLTE, tRNS, and whether its type has the ancillary bit
Tags@+ihdr:Bool -> @+idat:Bool -> @+iend:Bool -> @+plte:Bool -> @+trns:Bool -> @+anc:Bool -> Tags
type Seen source · line 810 · raw
Data
what the state has seen when a chunk after IHDR comes: where the IDAT run is, whether tRNS and PLTE have come, and the colour type
Seen@run:Run -> @+saw_trns:Bool -> @+saw_plte:Bool -> @+colour:U32 -> Seen
type Pb source · line 1826 · raw
Data
where enc.pour stands in its stored blocks: room is the data bytes left
in the open block, including this one; left counts raw bytes still to
come, including this one; len and bf are the length and BFINAL of the
block a new header opens
Pb@+room:U32 -> @+left:U32 -> @+len:U32 -> @+bf:U32 -> Pb
type Bk source · line 1927 · raw
Data
where enc.rgb.go's stored blocks stand: zblock opens a block and zdone
says no raw bytes are left for one; zspan says a block is open and zneed
that it is full. need counts the bytes still open in the block, left
the raw bytes from the open block's start on, and len the open block's
length
Bk@+zblock:Bool -> @+zdone:Bool -> @+zspan:Bool -> @+zneed:Bool -> @+need:U32 -> @+left:U32 -> @+len:U32 -> Bk
type Cr source · line 1933 · raw
Data
enc.rgb.go's cursor over the samples: a filter byte, red, green, or the last sample of the row is next. px holds the samples after col, the sample being written, and row counts the row's samples from col on, of ww
Cr@+filt:Bool -> @+zred:Bool -> @+zgrn:Bool -> @+zlast:Bool -> @+px:List<&2, U32> -> @+col:U32 -> @+row:U32 -> @+ww:U32 -> Cr
type Out source · line 1938 · raw
Data
what enc.rgb.go has written: the running CRC, the newest-first bytes and the two Adler-32 sums
Out@+cc:U32 -> @acc:List<&2, U32> -> @+aa:U32 -> @+bb:U32 -> Out
Definitions
def be.u32 source · line 82 · raw
@+aa:U32 -> @+bb:U32 -> @+cc:U32 -> @+dd:U32 -> U32
big-endian unsigned 32
def tag.ihdr source · line 86 · raw
U32
IHDR
def tag.idat source · line 90 · raw
U32
IDAT
def tag.iend source · line 94 · raw
U32
IEND
def tag.plte source · line 98 · raw
U32
PLTE
def tag.trns source · line 102 · raw
U32
tRNS
def tag.anc source · line 106 · raw
@+typ:U32 -> Bool
the ancillary bit of a chunk type (bit 5 of the first byte)
def png.d.grey source · line 110 · raw
@+dd:U32 -> Bool
the bit depths of gray: 1, 2, 4, 8 and 16
def png.d.wide source · line 115 · raw
@+dd:U32 -> Bool
the bit depths of RGB, gray and alpha, and RGBA: 8 and 16
def png.d.index source · line 119 · raw
@+dd:U32 -> Bool
the bit depths of indexed colour: 1, 2, 4 and 8
def png.depth.ok source · line 124 · 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 130 · raw
@+ii:U32 -> Bool
interlace method 0 (none) or 1 (Adam7)
def png.short source · line 135 · 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 139 · raw
@+nn:U32 -> Bool
a width or height IHDR may give: from 1 to 2^31 - 1
def png.good source · line 144 · 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 154 · raw
Pg
the state before IHDR
def pg.ihdr source · line 158 · raw
@+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+comp:U32 -> @+ff:U32 -> @+ii:U32 -> Pg
state after a valid IHDR
def run.seal source · line 162 · raw
@run:Run -> Run
an open IDAT run closes; none, or a closed one, stays as it is
def png.seal source · line 172 · raw
@pg:Pg -> Pg
close the IDAT sequence once a later chunk appears
def png.bpp.ix source · line 179 · raw
@z3:Bool -> U32
def png.bpp.one source · line 186 · raw
@z0:Bool -> @z3:Bool -> U32
def png.bpp6 source · line 193 · raw
@z6:Bool -> @+cc:U32 -> U32
def png.bpp4 source · line 200 · raw
@z4:Bool -> @z6:Bool -> @+cc:U32 -> U32
def png.bpp0 source · line 207 · raw
@z2:Bool -> @z4:Bool -> @z6:Bool -> @+cc:U32 -> U32
def png.bpp source · line 215 · raw
@+cc:U32 -> U32
bytes per pixel for an 8-bit colour type
def parse_ihdr source · line 220 · 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 229 · raw
@xs:List<&2, U32> -> @zz:Bool -> @+ii:U32 -> U32
def uf.back source · line 240 · 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 243 · raw
@prest:List<&2, U32> -> U32
def uf.drop source · line 250 · raw
@prest:List<&2, U32> -> List<&2, U32>
def uf.push source · line 257 · raw
@prest:List<&2, U32> -> @phist:List<&2, U32> -> List<&2, U32>
def uf.abs.at source · line 260 · raw
@+hi:U32 -> @+lo:U32 -> @zz:Bool -> U32
def uf.abs source · line 267 · raw
@+xx:U32 -> @+yy:U32 -> U32
def uf.paeth.b source · line 270 · raw
@zb:Bool -> @+bb:U32 -> @+cc:U32 -> U32
def uf.paeth.at source · line 277 · raw
@za:Bool -> @zb:Bool -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.paeth source · line 285 · raw
@+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
PaethPredictor from the PNG specification
def uf.avg source · line 291 · raw
@+aa:U32 -> @+bb:U32 -> U32
def uf.add source · line 295 · raw
@+ff:U32 -> @+pp:U32 -> U32
a reconstructed byte: filtered byte plus predictor, mod 256 (mask first)
def uf.p4 source · line 298 · raw
@z4:Bool -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.p3 source · line 305 · raw
@z3:Bool -> @z4:Bool -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.p2 source · line 312 · raw
@z2:Bool -> @z3:Bool -> @z4:Bool -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.p1 source · line 319 · raw
@z1:Bool -> @z2:Bool -> @z3:Bool -> @z4:Bool -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.pred source · line 326 · raw
@+ft:U32 -> @+aa:U32 -> @+bb:U32 -> @+cc:U32 -> U32
def uf.recon source · line 329 · 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 339 · raw
@zz:Bool -> Uf
def uf.ok source · line 346 · raw
@+ww:U32 -> @+hh:U32 -> @+bpp:U32 -> Bool
def uf.done source · line 349 · raw
@rows:Nat -> @all:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def uf.go source · line 365 · raw
@xs:List<&2, U32> -> @ph:Uf -> @zlast:Bool -> @+bpp:U32 -> @+stride:U32 -> @rows:Nat -> @ur:Ur -> @all:List<&2, U32> -> Maybe<&2, List<&2, U32>>
walk the filtered stream: a filter byte opens each row (UFilt), then its
bytes (UBody). zlast, whether the row's left is zero, marks the row's
last byte.
def uf.start source · line 398 · raw
@zz:Bool -> @raw:List<&2, U32> -> @+ww:U32 -> @+hh:U32 -> @+bpp:U32 -> Maybe<&2, List<&2, U32>>
def unfilter source · line 406 · 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 411 · raw
@+aa:U32 -> @+rr:U32 -> @+gg:U32 -> @+bb:U32 -> U32
def px.hit.at source · line 414 · raw
@zz:Bool -> U32
def px.hit source · line 421 · raw
@+ss:U32 -> @+kk:U32 -> U32
def px.rgb.hit source · line 424 · raw
@+rr:U32 -> @+gg:U32 -> @+bb:U32 -> @+kr:U32 -> @+kg:U32 -> @+kb:U32 -> U32
def px.grey source · line 427 · raw
@xs:List<&2, U32> -> @key:Key -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.rgb source · line 438 · raw
@xs:List<&2, U32> -> @key:Key -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.ga source · line 449 · raw
@xs:List<&2, U32> -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.rgba source · line 458 · raw
@xs:List<&2, U32> -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def pt.alt source · line 470 · 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 480 · 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 493 · 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 507 · 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 512 · 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 516 · raw
@zz:Bool -> @+vv:U32 -> Maybe<&2, U32>
the entry when the index left is zero
def pt.even source · line 524 · raw
@+ix:U32 -> Bool
whether an index is even: its low bit is clear
def pt.at source · line 529 · 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 542 · raw
@mm:Maybe<&2, U32> -> @kk:(@_:U32 -> Maybe<&2, List<&2, U32>>) -> Maybe<&2, List<&2, U32>>
def px.idx source · line 549 · raw
@xs:List<&2, U32> -> @+pal:Pt -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def pal.make source · line 558 · 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 571 · 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 579 · raw
@+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Pt
the palette's lookup tree, with alphas from tRNS
def px.indexed source · line 583 · raw
@samples:List<&2, U32> -> @+plte:List<&2, U32> -> @+trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def key.u16 source · line 587 · 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 596 · 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 604 · raw
@xs:List<&2, U32> -> Key
an RGB key: tRNS of exactly six bytes; any other length is ignored
def px.k4 source · line 611 · 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 619 · 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 632 · 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 646 · 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 662 · 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 667 · raw
@xs:List<&2, U32> -> Bool
def png.ihdr.ok source · line 671 · raw
@zz:Bool -> @+ww:U32 -> @+hh:U32 -> @+dd:U32 -> @+cc:U32 -> @+comp:U32 -> @+ff:U32 -> @+ii:U32 -> Nx
def png.ihdr.put source · line 678 · raw
@mm:Maybe<&2, Ihdr> -> Nx
def png.allow source · line 685 · raw
@zz:Bool -> @pg:Pg -> Nx
def pg.idat source · line 692 · raw
@pg:Pg -> @extra:List<&2, U32> -> Pg
def pg.plte source · line 699 · raw
@pg:Pg -> @bytes:List<&2, U32> -> Pg
def pg.trns source · line 706 · raw
@pg:Pg -> @bytes:List<&2, U32> -> Pg
def png.idat.try source · line 713 · raw
@zz:Bool -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.on.start source · line 721 · raw
@zihdr:Bool -> @data:List<&2, U32> -> Nx
the first chunk: IHDR starts the state; any other is refused
def png.on.idat source · line 730 · raw
@run:Run -> @need:Bool -> @data:List<&2, U32> -> @pg:Pg -> Nx
an IDAT opens or extends the run, when the colour type has what it needs; one after the run has closed is refused
def png.plte.ok source · line 739 · raw
@+cc:U32 -> Bool
def png.trns.ok source · line 742 · raw
@+cc:U32 -> @saw_plte:Bool -> Bool
def png.on.plte source · line 750 · raw
@run:Run -> @saw_plte:Bool -> @+colour:U32 -> @+data:List<&2, U32> -> @pg:Pg -> Nx
a PLTE is taken before any IDAT, when it is the first, the colour type allows one and its length is a palette's
def png.trns.take source · line 763 · 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 770 · raw
@run:Run -> @saw_trns:Bool -> @+colour:U32 -> @saw_plte:Bool -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.iend.ok source · line 779 · raw
@zz:Bool -> @pg:Pg -> Nx
def png.iend.at source · line 786 · raw
@zempty:Bool -> @zpal:Bool -> @pg:Pg -> Nx
def png.on.iend source · line 793 · raw
@+colour:U32 -> @saw_plte:Bool -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.other.at source · line 796 · raw
@anc:Bool -> @pg:Pg -> Nx
def png.seen.idat source · line 813 · raw
@sn:Seen -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.seen.iend source · line 818 · raw
@sn:Seen -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.seen.plte source · line 823 · raw
@sn:Seen -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.seen.trns source · line 828 · raw
@sn:Seen -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply.tr source · line 833 · raw
@ztrns:Bool -> @anc:Bool -> @sn:Seen -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply.pl source · line 840 · raw
@zplte:Bool -> @ztrns:Bool -> @anc:Bool -> @sn:Seen -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply.ie source · line 847 · raw
@ziend:Bool -> @zplte:Bool -> @ztrns:Bool -> @anc:Bool -> @sn:Seen -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply.id source · line 854 · raw
@zidat:Bool -> @ziend:Bool -> @zplte:Bool -> @ztrns:Bool -> @anc:Bool -> @sn:Seen -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.apply.go source · line 871 · raw
@tags:Tags -> @sn:Seen -> @data:List<&2, U32> -> @pg:Pg -> Nx
a chunk after IHDR: a second IHDR is refused
def png.apply source · line 878 · raw
@+typ:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.judge.at source · line 892 · raw
@zz:Bool -> @+typ:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.judge source · line 899 · raw
@+got:U32 -> @crc:List<&2, U32> -> @+typ:U32 -> @data:List<&2, U32> -> @pg:Pg -> Nx
def png.zp source · line 902 · raw
@zz:Bool -> Pz
def png.pic source · line 909 · raw
@mm:Maybe<&2, List<&2, U32>> -> @+ww:U32 -> @+hh:U32 -> Maybe<&2, Pic>
def png.pack source · line 916 · 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 930 · 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 947 · raw
@+dd:U32 -> @+cc:U32 -> U32
the bits of one pixel: the bit depth times the channels
def png.fbpp.at source · line 950 · raw
@zz:Bool -> @+nn:U32 -> U32
def png.fbpp source · line 959 · 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 963 · 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 966 · raw
@zz:Bool -> @raw:List<&2, U32> -> @+stride:U32 -> @+hh:U32 -> @+bpp:U32 -> Maybe<&2, List<&2, U32>>
def unfilter.rows source · line 975 · 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 981 · 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 995 · 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 1003 · 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 1010 · raw
@z8:Bool -> @z16:Bool -> @+dd:U32 -> @+count:Nat -> @row:List<&2, U32> -> List<&2, U32>
def unpack.row source · line 1022 · 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 1026 · 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 1042 · raw
@+vv:U32 -> U32
a 16-bit sample reduced to 8 bits: its high byte
def px.up source · line 1047 · 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 1051 · 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 1063 · 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 1076 · raw
@xs:List<&2, U32> -> List<&2, U32>
every 16-bit sample reduced to its high byte
def px.grey.low source · line 1084 · 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 1096 · raw
@z2:Bool -> @z4:Bool -> @samples:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.wide.at source · line 1105 · raw
@zz:Bool -> @+cc:U32 -> @samples:List<&2, U32> -> @trns:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def px.wide source · line 1113 · 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 1116 · 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 1131 · 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 1140 · 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 1158 · 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 1168 · 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 1175 · raw
@mm:Maybe<&2, List<&2, U32>> -> @rest:List<&2, U32> -> @+pw:U32 -> @+ph:U32 -> Pass
def pass.px source · line 1182 · raw
@mm:Maybe<&2, List<&2, U32>> -> @rest:List<&2, U32> -> @+sn:Nat -> @+pw:U32 -> @+ph:U32 -> @+hd:Hd -> Pass
def hd.bits source · line 1191 · raw
@hd:Hd -> U32
bits per pixel of the header's bit depth and colour type
def pass.fit source · line 1196 · raw
@zz:Bool -> @+raw:List<&2, U32> -> @+sn:Nat -> @+pw:U32 -> @+ph:U32 -> @+hd:Hd -> Pass
def pass.open source · line 1205 · raw
@zz:Bool -> @+raw:List<&2, U32> -> @+pw:U32 -> @+ph:U32 -> @+hd:Hd -> Pass
def pass.read source · line 1216 · raw
@+raw:List<&2, U32> -> @+pw:U32 -> @+ph:U32 -> @+hd:Hd -> 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. hd's samples format is the pass's; its size is the whole picture's
def png.up source · line 1220 · raw
@+nn:U32 -> U32
ceil(n / 2)
def png.dn source · line 1224 · raw
@+nn:U32 -> U32
floor(n / 2)
def ilv.px source · line 1229 · 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 1241 · 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 1253 · 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 1261 · raw
@bb:List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>
the rows after the first
def ilv.cols source · line 1270 · 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 1278 · 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 1290 · 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 1301 · 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 1316 · 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 -> @+hd:Hd -> Maybe<&2, List<&2, U32>>
def a7.s5 source · line 1331 · 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 -> @+hd:Hd -> Maybe<&2, List<&2, U32>>
def a7.s4 source · line 1345 · raw
@r1:List<&2, List<&2, U32>> -> @r2:List<&2, List<&2, U32>> -> @r3:List<&2, List<&2, U32>> -> @pp:Pass -> @+hd:Hd -> Maybe<&2, List<&2, U32>>
def a7.s3 source · line 1359 · raw
@r1:List<&2, List<&2, U32>> -> @r2:List<&2, List<&2, U32>> -> @pp:Pass -> @+hd:Hd -> Maybe<&2, List<&2, U32>>
def a7.s2 source · line 1373 · raw
@r1:List<&2, List<&2, U32>> -> @pp:Pass -> @+hd:Hd -> Maybe<&2, List<&2, U32>>
def a7.s1 source · line 1386 · raw
@pp:Pass -> @+hd:Hd -> Maybe<&2, List<&2, U32>>
def a7.go source · line 1400 · raw
@+raw:List<&2, U32> -> @+hd:Hd -> 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 1409 · 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 1416 · raw
@+pw:U32 -> @+ph:U32 -> @+bits:U32 -> Nat
def png.a7.len source · line 1420 · 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 1430 · raw
@zz:Bool -> @+ww:U32 -> @+hh:U32 -> @+bits:U32 -> Nat
def png.budget source · line 1439 · 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 1443 · raw
@pp:Pass -> Maybe<&2, List<&2, U32>>
a picture without interlacing: one pass of the whole size
def png.deep.at source · line 1450 · 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 1467 · 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 1486 · 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 1492 · raw
@zenc:Bool -> @+ii:U32 -> @idat:List<&2, U32> -> @hd:Hd -> 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.or.nil source · line 1502 · raw
@mm:Maybe<&2, List<&2, U32>> -> List<&2, U32>
the data of a chunk that came, none for one that did not
def png.finish source · line 1510 · raw
@pg:Pg -> Maybe<&2, Pic>
the picture of the state IEND ended: none before IHDR
def png.pgof source · line 1523 · 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 1536 · 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 1548 · 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 1556 · 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 1591 · 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 1597 · raw
@+nn:U32 -> U32
the low byte
def enc.hi source · line 1601 · raw
@+nn:U32 -> U32
the next byte up
def enc.be source · line 1605 · raw
@+nn:U32 -> List<&2, U32>
four bytes, big-endian
def enc.a source · line 1610 · 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 1615 · 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 1619 · raw
@+cc:U32 -> U32
green of a 0xAARRGGBB sample
def enc.b source · line 1623 · raw
@+cc:U32 -> U32
blue of a 0xAARRGGBB sample
def enc.push.rgb source · line 1627 · 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 1631 · 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 1635 · raw
@op:Bool -> @+cc:U32 -> @acc:List<&2, U32> -> List<&2, U32>
the channels of one sample
def enc.pred source · line 1643 · raw
@nn:Nat -> Nat
one less, or zero
def enc.len source · line 1651 · raw
@xs:List<&2, U32> -> @+nn:U32 -> U32
how many bytes, counted in a word
def enc.cat.go source · line 1659 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> List<&2, U32>
a reversed prefix, consed onto the suffix
def enc.cat source · line 1667 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> List<&2, U32>
xs, then ys
def enc.nch source · line 1671 · raw
@op:Bool -> U32
channels written for one sample
def enc.raw source · line 1682 · 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 1700 · 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 1708 · raw
@fin:Bool -> @+data:List<&2, U32> -> List<&2, U32>
one stored DEFLATE block. data is chronological.
def enc.feed source · line 1714 · 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 1733 · 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 1741 · 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 1746 · 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 1753 · 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 1757 · raw
List<&2, U32>
the eight-byte signature
def enc.ct source · line 1761 · raw
@op:Bool -> U32
colour type 2 when opaque, otherwise 6
def enc.zlib source · line 1770 · 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 1775 · raw
@zz:Bool -> @+qq:U32 -> U32
how many stored blocks cover n raw bytes
def enc.nblk source · line 1783 · raw
@+nn:U32 -> U32
ceil(n / 65535). An exact multiple stays q.
def enc.idat.ln source · line 1787 · raw
@+nn:U32 -> U32
IDAT data length: zlib header, stored-block headers, raw bytes, Adler-32
def enc.crc.list source · line 1791 · 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 1799 · raw
@zz:Bool -> @+nn:U32 -> U32
the stored-block length: the bytes still open, at most 65535
def enc.cap source · line 1807 · raw
@+nn:U32 -> U32
min(n, 65535)
def enc.bf.at source · line 1811 · raw
@zz:Bool -> U32
BFINAL is 1 when this block holds every byte still open
def enc.bf source · line 1819 · raw
@+nn:U32 -> U32
1 when n fits in one stored block, otherwise 0
def enc.head.crc source · line 1830 · raw
@+len:U32 -> @+bf:U32 -> @+cc:U32 -> U32
a stored block's header bytes, BFINAL first, with the CRC run over them
def enc.head.acc source · line 1839 · raw
@+len:U32 -> @+bf:U32 -> @acc:List<&2, U32> -> List<&2, U32>
a stored block's header bytes, BFINAL first, consed newest-first
def enc.pour source · line 1850 · raw
@xs:List<&2, U32> -> @znew:Bool -> @zlast:Bool -> @pb:Pb -> @+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.
def enc.seal.crc source · line 1869 · 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 1876 · 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 1882 · 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 1894 · 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 1906 · raw
@zz:Bool -> @+tt:U32 -> U32
Adler-32 reduced once. The sum stays below twice the modulus.
def enc.add source · line 1914 · raw
@+ss:U32 -> @+xx:U32 -> U32
one Adler-32 add, modulo 65521
def enc.rgb.fin source · line 1919 · 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.head source · line 1942 · raw
@+left:U32 -> @+cc:U32 -> @acc:List<&2, U32> -> @+aa:U32 -> @+bb:U32 -> Out
a stored block's header for the left raw bytes still to come, its CRC run
def enc.rgb.byte source · line 1949 · raw
@cr:Cr -> U32
the raw byte the cursor is at: 0 for a filter byte (and for red past the samples), else the sample's red, green or blue
def enc.rgb.step source · line 1963 · raw
@cr:Cr -> Cr
the cursor one raw byte on
def enc.rgb.put source · line 1979 · raw
@+xx:U32 -> @+cc:U32 -> @acc:List<&2, U32> -> @+aa:U32 -> @+bb:U32 -> Out
one raw byte written: CRC, the bytes, and Adler-32
def enc.rgb.go source · line 1987 · raw
@fuel:Nat -> @bk:Bk -> @+cr:Cr -> @out:Out -> Pair(Pair(U32, List<&2, U32>), U32)
stored blocks for colour type 2, one decreasing step at a time: a block's header, a raw byte the cursor gives, or the step that closes a full block. Block headers match enc.pour. Adler-32 covers the raw bytes only.
def enc.wide.rgb.out source · line 2006 · raw
@pp:Pair(Pair(U32, List<&2, U32>), U32) -> @ie:List<&2, U32> -> List<&2, U32>
def enc.wide.rgb.z source · line 2012 · 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 2027 · 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 2042 · 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 2052 · 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 2066 · 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 2072 · 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 2087 · 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 2093 · 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 2105 · 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 2126 · raw
@+nn:U32 -> Bool
fewer than 2^28 samples: the count's top four bits are clear
def enc.good source · line 2140 · 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 2147 · 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 2152 · 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 2165 · raw
@zz:Bool -> @+ww:U32 -> @+hh:U32 -> @+op:Bool -> @px:List<&2, U32> -> @+nn:U32 -> List<&2, U32>
def enc.paint source · line 2180 · 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 2185 · 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 2194 · 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.