src/jpeg.bend checks
raw source on the hub · import emerging-ezimg@1.4.0.0/src/jpeg.bend as Jpeg
src/jpeg: sequential 8-bit Huffman JPEG (SOF0 baseline, SOF1 extended) in one scan or several (T.81 E.2): grayscale, YCbCr, or RGB as coded when an Adobe APP14 or the component ids say so (libjpeg's rule).
Trusted, not proved: decode_jpeg reads the baseline files other encoders (libjpeg, Pillow) write, to within the IDCT accuracy T.83 asks of a decoder: its IDCT is libjpeg's accurate integer one (jidctint.c, decode.idct1), step for step, so on files libjpeg-turbo writes the gray samples are libjpeg-turbo's own and colour differs only by the YCbCr conversion's rounding (measured with Pillow, a test, not a proof). The laws place each decoded sample (laws/JpegPlacesComponentSamples.bend) but do not prove its value; that needs the other encoder. The Pillow check in bench/ exercises it.
1 import
import Base
Types
type Pic source · line 18 · raw
Data
a decoded picture: width, height, and row-major samples, each packed 0xAARRGGBB with alpha 255
Pic@w:U32 -> @h:U32 -> @pixels:List<&2, U32> -> Pic
type Huff source · line 23 · raw
Data
one Huffman table as its code tree (T.81 C): a code's bits, the first highest, lead from the root to the leaf holding its symbol, a 0 bit to a node's zero side and a 1 bit to its one side; a gap is where no code leads
HGapHuff
HLeaf@sym:U32 -> Huff
HNode@zero:Huff -> @one:Huff -> Huff
type Frame source · line 29 · raw
Data
SOF0 / SOF1 frame geometry. Sampling factors are 1 to 4.
Frame@w:U32 -> @h:U32 -> @nf:U32 -> @ids:List<&2, U32> -> @hs:List<&2, U32> -> @vs:List<&2, U32> -> @tq:List<&2, U32> -> @hmax:U32 -> @vmax:U32 -> Frame
type Scan source · line 34 · raw
Data
one SOS header
Scan@ns:U32 -> @sids:List<&2, U32> -> @td:List<&2, U32> -> @ta:List<&2, U32> -> @ss:U32 -> @se:U32 -> @ah:U32 -> Scan
type Tabs source · line 38 · raw
Data
four quant tables and four DC / AC Huffman tables
Tabs@q0:List<&2, U32> -> @q1:List<&2, U32> -> @q2:List<&2, U32> -> @q3:List<&2, U32> -> @dc0:Huff -> @dc1:Huff -> @dc2:Huff -> @dc3:Huff -> @ac0:Huff -> @ac1:Huff -> @ac2:Huff -> @ac3:Huff -> Tabs
type Phase source · line 45 · raw
Data
marker walk: seeking, a marker byte, a length, a payload, or the entropy scan. Rest: a scan ended at marker mark, not RSTn or EOI, after a frame header; acc holds the bytes after it, last first, for the next walk (decode.chain), since tables and further scans may follow a scan (T.81 B.2.1, E.2)
SeekPhase
MarkPhase
LenHi@mark:U32 -> Phase
LenLo@mark:U32 -> @hi:U32 -> Phase
Pay@mark:U32 -> @left:Nat -> @acc:List<&2, U32> -> Phase
EntPhase
EntFFPhase
StopPhase
Rest@mark:U32 -> @acc:List<&2, U32> -> Phase
type St source · line 57 · raw
Data
parser state. kind 1 is a baseline frame. bad 1 rejects the file.
St@phase:Phase -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @ri:U32 -> @kind:U32 -> @bad:U32 -> St
type Bits source · line 61 · raw
Data
bit reader. n bits remain in buf. ok is 0 after a truncated or marked stream.
Bits@n:U32 -> @ok:U32 -> @buf:U32 -> @xs:List<&2, U32> -> Bits
type Hit source · line 65 · raw
Data
one decoded Huffman symbol
Hit@sym:U32 -> @bits:Bits -> @ok:U32 -> Hit
type R8 source · line 69 · raw
Data
eight values: one row of a block
R8@v0:U32 -> @v1:U32 -> @v2:U32 -> @v3:U32 -> @v4:U32 -> @v5:U32 -> @v6:U32 -> @v7:U32 -> R8
type B8 source · line 73 · raw
Data
an 8 by 8 block of values, row by row
B8@r0:R8 -> @r1:R8 -> @r2:R8 -> @r3:R8 -> @r4:R8 -> @r5:R8 -> @r6:R8 -> @r7:R8 -> B8
type Ac source · line 78 · raw
Data
AC run state inside one block: the next coefficient's zigzag index k, the dequantised coefficients so far in natural order, and the quantisation table's entries from index k on
Ac@k:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> @done:U32 -> @ok:U32 -> Ac
type Blk source · line 82 · raw
Data
one decoded 8x8 block, level-shifted samples, and the DC predictor
Blk@samples:List<&2, U32> -> @bits:Bits -> @pred:U32 -> @ok:U32 -> Blk
type Preds source · line 86 · raw
Data
DC predictors, one per scan component
Preds@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> Preds
type Ctrl source · line 90 · raw
Data
where the current block sits in the frame
Ctrl@comp:U32 -> @bi:U32 -> @mx:U32 -> @my:U32 -> @mcu:U32 -> @rst:U32 -> Ctrl
type Adv source · line 94 · raw
Data
the block that follows, and whether a restart marker comes first
Adv@ctrl:Ctrl -> @due:U32 -> @expect:U32 -> Adv
type Geom source · line 98 · raw
Data
pixel rectangle one block sample covers
Geom@ox:U32 -> @oy:U32 -> @pw:U32 -> @ph:U32 -> @w:U32 -> @h:U32 -> Geom
type Comps source · line 103 · raw
Data
SOF component lists while they are being read, and whether every component so far was read whole with sampling factors the decoder places
Comps@ids:List<&2, U32> -> @hs:List<&2, U32> -> @vs:List<&2, U32> -> @tq:List<&2, U32> -> @ok:Bool -> Comps
type Walk source · line 441 · raw
Data
a Huffman lookup in progress: the subtree of the code tree the bits read so far lead to, and the reader after them
Walk@tab:Huff -> @bits:Bits -> Walk
type Ups source · line 1481 · raw
Data
the three components' upsampling
Ups@m0:U32 -> @m1:U32 -> @m2:U32 -> Ups
type Pass source · line 2087 · raw
Data
a scan read: its header, the tables and restart interval it was read with, and its entropy-coded bytes in file order
Pass@scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @ri:U32 -> Pass
type Plan source · line 2195 · raw
Data
how a scan is decoded over a frame: the frame position of each of its components, those components as a mask (0 when the scan is not one decode.passes.ok takes), the frame it is decoded over, and its blocks
Plan@ix:List<&2, U32> -> @mask:U32 -> @frame:Frame -> @nb:U32 -> Plan
type Chain source · line 3389 · raw
Data
the scans of a file: whether every walk read them cleanly, and the passes, last first
Chain@ok:Bool -> @passes:List<&2, Pass> -> Chain
type Fs source · line 3544 · raw
Data
the facts pass between two bytes: ph 0 between segments, 1 after an FF, 2 and 3 a length field (its high byte in hi), 4 inside a body of left more bytes (its length in hi, its first 16 bytes kept in got, newest first, for APP0 and APP14), 5 stopped at the first scan's SOS or at EOI; acc the facts so far
Fs@ph:U32 -> @mark:U32 -> @hi:U32 -> @left:Nat -> @got:List<&2, U32> -> @acc:U32 -> Fs
Definitions
def decode.sign source · line 106 · raw
@+aa:U32 -> U32
def decode.neg32 source · line 109 · raw
@+aa:U32 -> U32
def decode.abs.s source · line 112 · raw
@ss:U32 -> @+aa:U32 -> U32
def decode.abs source · line 119 · raw
@+aa:U32 -> U32
def decode.carry.b source · line 122 · raw
@cc:Bool -> U32
def decode.carry source · line 129 · raw
@+aa:U32 -> @+sum:U32 -> U32
def decode.neg64.c source · line 132 · raw
@zz:Bool -> @+nh:U32 -> @+nl:U32 -> Pair(U32, U32)
def decode.neg64 source · line 139 · raw
@+hh:U32 -> @+ll:U32 -> Pair(U32, U32)
def decode.split source · line 144 · raw
@+aa:U32 -> Pair(U32, U32)
def decode.widemul.pack source · line 147 · raw
@+neg:U32 -> @+hi:U32 -> @+lo:U32 -> Pair(U32, U32)
def decode.widemul.bb source · line 154 · raw
@+a0:U32 -> @+a1:U32 -> @bb:Pair(U32, U32) -> @+neg:U32 -> Pair(U32, U32)
def decode.widemul.aa source · line 165 · raw
@aa:Pair(U32, U32) -> @+bb:U32 -> @+neg:U32 -> Pair(U32, U32)
def decode.widemul.go source · line 169 · raw
@+aa:U32 -> @+bb:U32 -> @+neg:U32 -> Pair(U32, U32)
def decode.widemul source · line 172 · raw
@+aa:U32 -> @+bb:U32 -> Pair(U32, U32)
def decode.qs.res source · line 179 · raw
@fine:Bool -> @+hh:U32 -> @+ll:U32 -> U32
the encoder's forward DCT (src/jpeg_enc.bend, encode.q17) rounds with this: a 64-bit dot product (hh, ll), in units of 2^-16, as the pass's result. The row pass (fine) halves it and keeps four fraction bits (a shift by 13), so the column pass rounds once, at the end, from what the row pass computed, taking those four bits off with its half (a shift by 21). Rounding to the nearest, half away from 0.
def decode.qs.half source · line 187 · raw
@fine:Bool -> U32
half the pass's last unit, added before the shift so the shift rounds
def decode.qs.sign2 source · line 194 · raw
@neg:Bool -> @+res:U32 -> U32
def decode.qs.div source · line 201 · raw
@+hh:U32 -> @+ll:U32 -> @neg:Bool -> @+fine:Bool -> U32
def decode.qs.neg source · line 206 · raw
@pp:Pair(U32, U32) -> @+fine:Bool -> U32
def decode.qs.pos source · line 210 · raw
@pos:Bool -> @+hh:U32 -> @+ll:U32 -> @+fine:Bool -> U32
def decode.qs source · line 217 · raw
@+hh:U32 -> @+ll:U32 -> @+fine:Bool -> U32
def decode.at.m source · line 220 · raw
@mm:Maybe<&2, U32> -> U32
def decode.at source · line 227 · raw
@xs:List<&2, U32> -> @+ii:U32 -> U32
def decode.cos source · line 230 · raw
List<&2, U32>
def decode.clamp.hi source · line 238 · raw
@hi:Bool -> @+vv:U32 -> U32
def decode.clamp8.s source · line 245 · raw
@ss:U32 -> @+vv:U32 -> U32
def decode.clamp8 source · line 252 · raw
@+vv:U32 -> U32
def decode.huff0 source · line 255 · raw
Huff
def decode.frame0 source · line 258 · raw
Frame
def decode.scan0 source · line 261 · raw
Scan
def decode.tabs0 source · line 264 · raw
Tabs
def decode.st0 source · line 268 · raw
St
def decode.nlist source · line 271 · raw
@xs:List<&2, U32> -> @+nn:U32 -> U32
def decode.drop source · line 278 · raw
@xs:List<&2, U32> -> U32
def decode.ht.pair source · line 287 · raw
@ns:List<&2, Huff> -> List<&2, Huff>
the nodes at a depth of the code tree from the nodes one level down, left to right, two to a node; a last one alone has a gap on its one side
def decode.ht.leaves source · line 299 · raw
@ys:List<&2, U32> -> @ns:List<&2, Huff> -> List<&2, Huff>
a leaf for each symbol, in order, before the nodes ns
def decode.ht.groups source · line 307 · raw
@cs:List<&2, U32> -> @+ys:List<&2, U32> -> @acc:List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>
the symbols of each code length, the counts' first length first, as a list onto acc, the last length first
def decode.ht.up source · line 317 · raw
@gs:List<&2, List<&2, U32>> -> @ns:List<&2, Huff> -> List<&2, Huff>
the nodes at each depth from the deepest up: at a depth, the leaves of the codes of that length, left to right in symbol order, then the nodes the depth below pairs up. Canonical codes (T.81 C) of one length are consecutive and come before every longer code's prefix, so this is the tree of the canonical codes
def decode.ht.root source · line 325 · raw
@ns:List<&2, Huff> -> Huff
the root of the tree: the one node over depth 1, a gap for no codes
def decode.canon source · line 335 · raw
@+counts:List<&2, U32> -> @+symbols:List<&2, U32> -> Huff
the code tree of a DHT table's 16 counts (codes of length 1 to 16) and its symbols in order (T.81 C, Figure C.1 to C.3), built level by level from the counts, with no code arithmetic
def decode.index.add source · line 338 · raw
@eq:Bool -> @+ii:U32 -> U32
def decode.index source · line 347 · raw
@xs:List<&2, U32> -> @eq:Bool -> @+want:U32 -> @+ii:U32 -> U32
the position of want in xs, the first if it is there more than once, 0 when it is absent. eq says the element before xs was want, and ii counts the elements before it. Matches with U32.is_eq, so a law reaches it.
def decode.head.is source · line 365 · raw
@xs:List<&2, U32> -> @+want:U32 -> Bool
whether a list of bytes opens with want. U32.is_eq, not a literal pattern, so a law reaches a symbolic byte.
def decode.nbits source · line 377 · raw
@left:Nat -> @eq:Bool -> @xs:List<&2, U32> -> @ff:Bool -> @h255:Bool -> @h0:Bool -> @+nn:U32 -> @+buf:U32 -> @+ok:U32 -> @+acc:U32 -> Pair(Bits, U32)
left more bits onto acc, most significant first. nn bits remain in buf, top bit first at bit 7; eq says buf is empty. ff says the byte before xs was a 255, and h255 and h0 whether xs opens with 255 or 0, each asked with U32.is_eq so a law reaches a symbolic byte. After a 255, 0 is a stuffed 255 (eight one bits) and 255 a fill byte; any other byte is a marker, and it or the end of the bytes reads zero bits and sets ok to 0.
def decode.nbits.of source · line 426 · raw
@left:Nat -> @+xs:List<&2, U32> -> @+nn:U32 -> @+buf:U32 -> @+ok:U32 -> Pair(Bits, U32)
nn more bits from a reader, the first of them at the next byte when its buffer is empty
def decode.one source · line 429 · raw
@bits:Bits -> Pair(Bits, U32)
def decode.huff.hit source · line 435 · raw
@+sym:U32 -> @bits:Bits -> Hit
a symbol found, the reader after its code
def decode.huff.next source · line 445 · raw
@got:Pair(Bits, U32) -> @zero:Huff -> @one:Huff -> Walk
the walk after one more bit: the zero side of a node for a 0 bit, else its one side
def decode.huff.walk source · line 458 · raw
@left:Nat -> @ww:Walk -> Hit
the symbol a walk ends at: a leaf is its symbol, the reader left after its code; a node reads one more bit and goes on from the side it leads to, at most left more bits; a gap, or a code longer than that, is no symbol, ok 0
def decode.huff source · line 482 · raw
@left:Nat -> @tab:Huff -> @bits:Bits -> Hit
the symbol whose code the reader holds next, from a table's code tree (T.81 C), codes of at most left bits (T.81 F.2.2.3 decodes codes of at most 16 bits)
def decode.bits.of source · line 485 · raw
@bits:Bits -> Pair(Nat, Pair(U32, Pair(List<&2, U32>, Pair(U32, U32))))
def decode.read.n source · line 490 · raw
@+cat:U32 -> @bits:Bits -> Pair(Bits, U32)
def decode.extend.s source · line 495 · raw
@small:Bool -> @+mag:U32 -> @+cat:U32 -> U32
def decode.extend source · line 502 · raw
@+cat:U32 -> @+mag:U32 -> U32
def decode.zig.at source · line 510 · raw
@+kk:U32 -> U32
the natural-order position (row * 8 + column) of zigzag index k (T.81 Figure A.6)
def decode.r8.set source · line 642 · raw
@rr:R8 -> @+cc:U32 -> @+vv:U32 -> R8
a row with column cc set to vv
def decode.b8.set source · line 672 · raw
@bb:B8 -> @+rw:U32 -> @+cc:U32 -> @+vv:U32 -> B8
a block with row rw, column cc set to vv
def decode.cf.put source · line 694 · raw
@zz:B8 -> @+kk:U32 -> @+vv:U32 -> B8
a block with the coefficient of zigzag index k set to vv
def decode.r8.zero source · line 699 · raw
R8
eight zeros
def decode.b8.zero source · line 703 · raw
B8
a block of zeros
def decode.nth source · line 708 · raw
@left:Nat -> @qs:List<&2, U32> -> U32
the entry of a list after left more (a block's samples, a quantisation table's entries), 0 past its end
def decode.skip source · line 724 · raw
@left:Nat -> @qs:List<&2, U32> -> List<&2, U32>
the entries of a list after left more
def decode.ac.stored source · line 735 · raw
@full:Bool -> @+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> @+ok:U32 -> Ac
def decode.ac.store source · line 743 · raw
@got:Pair(Bits, U32) -> @+sz:U32 -> @+nk:U32 -> @+run:U32 -> @zz:B8 -> @+qs:List<&2, U32> -> @+ok:U32 -> Ac
coefficient nk, run places after the coefficient before it, dequantised by its entry; the entries after it go on
def decode.ac.bad source · line 749 · raw
@zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> Ac
def decode.ac.run.b source · line 752 · raw
@zzz:Bool -> @over:Bool -> @+sz:U32 -> @+nk:U32 -> @+run:U32 -> @bits:Bits -> @zz:B8 -> @qs:List<&2, U32> -> @+ok:U32 -> Ac
def decode.ac.run source · line 773 · raw
@+sym:U32 -> @bits:Bits -> @+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @+ok:U32 -> Ac
def decode.ac.zrl.eq source · line 779 · raw
@eq:Bool -> @zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> @+ok:U32 -> Ac
def decode.ac.zrl.k source · line 786 · raw
@more:Bool -> @+nk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> @+ok:U32 -> Ac
def decode.ac.zrl source · line 793 · raw
@+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> @+ok:U32 -> Ac
def decode.ac.sym.b source · line 798 · raw
@eob:Bool -> @zrl:Bool -> @+sym:U32 -> @bits:Bits -> @+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @+ok:U32 -> Ac
an AC symbol: EOB (0), ZRL (240), or a run and size. U32.is_eq, not literal patterns, so a law reaches a symbolic symbol.
def decode.ac.sym source · line 821 · raw
@+sym:U32 -> @bits:Bits -> @+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @+ok:U32 -> Ac
def decode.ac.step source · line 824 · raw
@hit:Hit -> @+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @+ok:U32 -> Ac
def decode.ac.full source · line 829 · raw
@done:Bool -> @short:Bool -> @ac:Ac -> Ac
def decode.ac source · line 842 · raw
@left:Nat -> @ac:Ac -> @+tab:Huff -> Ac
def decode.zig source · line 858 · raw
List<&2, U32>
def decode.asr.s source · line 864 · raw
@ss:U32 -> @+xx:U32 -> @nn:Nat -> U32
xx shifted right nn bits, the sign bit copied in (an arithmetic shift)
def decode.asr source · line 871 · raw
@+xx:U32 -> @nn:Nat -> U32
def decode.idct.out source · line 876 · raw
@fine:Bool -> @+xx:U32 -> U32
one output of a 1-D pass: the row pass (fine) keeps PASS1_BITS = 2 fraction bits, a shift by CONST_BITS - PASS1_BITS = 11; the column pass shifts by CONST_BITS + PASS1_BITS + 3 = 18, levels by 128 and clamps
def decode.idct1 source · line 885 · raw
@rr:R8 -> @+fine:Bool -> R8
one 1-D pass of libjpeg's accurate integer IDCT (jidctint.c, jpeg_idct_islow: the Loeffler, Ligtenberg and Moschytz factorisation, 12 multiplies, CONST_BITS = 13), over eight values
def decode.b8.t source · line 912 · raw
@bb:B8 -> B8
a block's rows as its columns
def decode.b8.pass source · line 922 · raw
@bb:B8 -> @+fine:Bool -> B8
one 1-D pass over each row
def decode.r8.list source · line 929 · raw
@rr:R8 -> @acc:List<&2, U32> -> List<&2, U32>
a row's values onto acc
def decode.b8.list source · line 935 · raw
@bb:B8 -> List<&2, U32>
a block's values, row by row
def decode.idct source · line 943 · raw
@zz:B8 -> List<&2, U32>
the 2-D IDCT of dequantised coefficients in natural order: the column pass (libjpeg's pass 1, on the columns) then the row pass, to level-shifted samples clamped to 0 to 255, row by row
def decode.block.ac source · line 946 · raw
@ac:Ac -> @+pred:U32 -> @+qok:U32 -> Blk
def decode.block.diff source · line 951 · raw
@got:Pair(Bits, U32) -> @+cat:U32 -> @+pred:U32 -> @+ok:U32 -> @ac:Huff -> @+quant:List<&2, U32> -> @+qok:U32 -> Blk
def decode.block.cat source · line 965 · raw
@zz:Bool -> @+sym:U32 -> @bits:Bits -> @+pred:U32 -> @+ok:U32 -> @ac:Huff -> @quant:List<&2, U32> -> @+qok:U32 -> Blk
def decode.block.dc source · line 981 · raw
@hit:Hit -> @+pred:U32 -> @ac:Huff -> @quant:List<&2, U32> -> @+qok:U32 -> Blk
def decode.qok.b source · line 986 · raw
@ok:Bool -> U32
def decode.qok source · line 993 · raw
@quant:List<&2, U32> -> U32
def decode.block.go source · line 996 · raw
@bits:Bits -> @+pred:U32 -> @dc:Huff -> @ac:Huff -> @+quant:List<&2, U32> -> Blk
def decode.dc.get source · line 999 · raw
@tabs:Tabs -> @+id:U32 -> Huff
def decode.ac.get source · line 1012 · raw
@tabs:Tabs -> @+id:U32 -> Huff
def decode.q.get source · line 1027 · raw
@tabs:Tabs -> @+id:U32 -> List<&2, U32>
table Tq of a frame component: no table for Tq above 3 (T.81 B.2.2), so the component's blocks are refused as for a table never defined
def decode.pred.get source · line 1043 · raw
@preds:Preds -> @+ii:U32 -> U32
def decode.pred.put source · line 1056 · raw
@preds:Preds -> @+ii:U32 -> @+vv:U32 -> Preds
def decode.pred.zero source · line 1069 · raw
Preds
def decode.preds.next source · line 1072 · raw
@due:U32 -> @preds:Preds -> @+comp:U32 -> @+pred:U32 -> Preds
def decode.block.of source · line 1081 · raw
@bits:Bits -> @+comp:U32 -> @preds:Preds -> @frame:Frame -> @scan:Scan -> @+tabs:Tabs -> Blk
def decode.rst.ok source · line 1096 · raw
@eq:Bool -> @rest:List<&2, U32> -> Bits
def decode.rst.xs source · line 1103 · raw
@xs:List<&2, U32> -> @+want:U32 -> Bits
def decode.rst source · line 1110 · raw
@bits:Bits -> @+want:U32 -> Bits
def decode.restart source · line 1115 · raw
@bits:Bits -> @+due:U32 -> @+expect:U32 -> Bits
def decode.ceil source · line 1122 · raw
@+nn:U32 -> @+dd:U32 -> U32
def decode.ri.nz source · line 1125 · raw
@+ri:U32 -> U32
def decode.adv.due.b source · line 1132 · raw
@zero:Bool -> @hit:Bool -> @+mcu:U32 -> @+mx:U32 -> @+my:U32 -> @+rst:U32 -> Adv
def decode.adv.due source · line 1143 · raw
@+mcu:U32 -> @+mx:U32 -> @+my:U32 -> @+rst:U32 -> @+ri:U32 -> Adv
def decode.adv.mx source · line 1146 · raw
@inb:Bool -> @+mcu:U32 -> @+mx:U32 -> @+my:U32 -> @+rst:U32 -> @+ri:U32 -> Adv
def decode.adv.mcu source · line 1153 · raw
@+mcu:U32 -> @+mx:U32 -> @+my:U32 -> @+rst:U32 -> @+ww:U32 -> @+hmax:U32 -> @+ri:U32 -> Adv
def decode.adv.comp source · line 1156 · raw
@more:Bool -> @+comp:U32 -> @+mx:U32 -> @+my:U32 -> @+mcu:U32 -> @+rst:U32 -> @+ww:U32 -> @+hmax:U32 -> @+ri:U32 -> Adv
def decode.adv.bi source · line 1173 · raw
@more:Bool -> @+comp:U32 -> @+bi:U32 -> @+mx:U32 -> @+my:U32 -> @+mcu:U32 -> @+rst:U32 -> @+ww:U32 -> @+hmax:U32 -> @+ns:U32 -> @+ri:U32 -> Adv
def decode.adv.go source · line 1192 · raw
@+comp:U32 -> @+bi:U32 -> @+mx:U32 -> @+my:U32 -> @+mcu:U32 -> @+rst:U32 -> @+ww:U32 -> @+hmax:U32 -> @+ns:U32 -> @sids:List<&2, U32> -> @ids:List<&2, U32> -> @hs:List<&2, U32> -> @vs:List<&2, U32> -> @+ri:U32 -> Adv
def decode.adv source · line 1213 · raw
@ctrl:Ctrl -> @frame:Frame -> @scan:Scan -> @+ri:U32 -> Adv
def decode.block.next.go source · line 1222 · raw
@adv:Adv -> @bits:Bits -> @preds:Preds -> @+pred:U32 -> @+comp:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> Blk
def decode.block.next source · line 1239 · raw
@bits:Bits -> @preds:Preds -> @+pred:U32 -> @ctrl:Ctrl -> @+frame:Frame -> @+scan:Scan -> @tabs:Tabs -> @+ri:U32 -> Blk
def decode.geom.nz source · line 1253 · raw
@+nn:U32 -> U32
def decode.geom.go source · line 1263 · raw
@+comp:U32 -> @+bi:U32 -> @+mx:U32 -> @+my:U32 -> @+ww:U32 -> @+hh:U32 -> @+hmax:U32 -> @+vmax:U32 -> @sids:List<&2, U32> -> @ids:List<&2, U32> -> @hs:List<&2, U32> -> @vs:List<&2, U32> -> Geom
where block bi of a component sits in its MCU: a component's hi * vi blocks run left to right, hi to a row, then top to bottom (T.81 A.2.3), so its column is bi mod hi and its row bi div hi
def decode.geom source · line 1286 · raw
@ctrl:Ctrl -> @frame:Frame -> @scan:Scan -> Geom
def decode.splat.in source · line 1295 · raw
@xin:Bool -> @yin:Bool -> @plane:Array<U32> -> @+xx:U32 -> @+yy:U32 -> @+ww:U32 -> @+sample:U32 -> Array<U32>
def decode.splat.px source · line 1308 · raw
@left:Nat -> @plane:Array<U32> -> @+x0:U32 -> @+yy:U32 -> @+px:U32 -> @+ww:U32 -> @+hh:U32 -> @+vv:U32 -> Array<U32>
one pixel row of the pixels a sample covers: columns x0 + px onward, left pixels left, each written only when it lies inside the frame
def decode.splat.py source · line 1326 · raw
@left:Nat -> @plane:Array<U32> -> @+x0:U32 -> @+y0:U32 -> @+py:U32 -> @+pw:U32 -> @+ww:U32 -> @+hh:U32 -> @+vv:U32 -> Array<U32>
the pw by ph pixels one sample covers, from (x0, y0 + py), row by row
def decode.splat.col source · line 1346 · raw
@left:Nat -> @plane:Array<U32> -> @+xs:List<&2, U32> -> @+row:U32 -> @+col:U32 -> @+ox:U32 -> @+oy:U32 -> @+pw:U32 -> @+ph:U32 -> @+ww:U32 -> @+hh:U32 -> Array<U32>
the samples xs of one block row from column col on, each over the pw by ph pixels at (ox + col * pw, oy + row * ph): xs's first sample at column col, the next at col + 1, 0 for a sample past the end of xs
def decode.splat source · line 1368 · raw
@left:Nat -> @plane:Array<U32> -> @+xs:List<&2, U32> -> @+row:U32 -> @+ox:U32 -> @+oy:U32 -> @+pw:U32 -> @+ph:U32 -> @+ww:U32 -> @+hh:U32 -> Array<U32>
the rows of an 8 by 8 block from row down, xs its samples from that row's first, each sample replicated over pw by ph pixels; a block's samples are read once, in order
def decode.paint.which source · line 1387 · raw
@which:Bool -> @samples:List<&2, U32> -> @plane:Array<U32> -> @+ox:U32 -> @+oy:U32 -> @+pw:U32 -> @+ph:U32 -> @+ww:U32 -> @+hh:U32 -> Array<U32>
def decode.paint.use source · line 1405 · raw
@gg:Geom -> @which:Bool -> @samples:List<&2, U32> -> @plane:Array<U32> -> Array<U32>
def decode.ctrl.comp source · line 1410 · raw
@ctrl:Ctrl -> U32
def decode.sink.leaf source · line 1415 · raw
@aa:Array<U32> -> U32
def decode.sink.p source · line 1422 · raw
@pp:Pair(Array<U32>, U32) -> U32
def decode.sink source · line 1426 · raw
@aa:Array<U32> -> U32
def decode.depth.of source · line 1431 · raw
@zero:Bool -> @+nn:U32 -> Nat
the depth of a plane of nn points: 0 for none, else log2(2 * nn - 1) in U32, so 2^depth >= nn while 2 * nn does not wrap
def decode.depth source · line 1439 · raw
@+nn:U32 -> Nat
U32.is_eq, not a literal pattern, so a law reaches every nn
def decode.plane source · line 1442 · raw
@+nn:U32 -> Array<U32>
def decode.emit source · line 1447 · raw
@left:Nat -> @got:Pair(Array<U32>, U32) -> @+ii:U32 -> List<&2, U32>
the plane's points in order: got is the plane and the value of point ii - 1, just read, then the left - 1 points from ii on. One read past the last point is made and dropped.
def decode.points source · line 1460 · raw
@+nn:Nat -> @plane:Array<U32> -> List<&2, U32>
the first nn points of a plane, in order
def decode.up.mode source · line 1471 · raw
@+hc:U32 -> @+vc:U32 -> @+hmax:U32 -> @+vmax:U32 -> @+ww:U32 -> U32
the upsampling each component gets, as libjpeg-turbo's jinit_upsampler picks it: 1 the triangle filter across (h2v1_fancy_upsample) for a component of half the frame's horizontal factor and its vertical one, 2 across and down (h2v2_fancy_upsample) for half of both, 3 down (h1v2_fancy_upsample) for the horizontal factor and half the vertical one, and 0, each sample repeated over the pixels it covers as decode.paint paints it, for any other factors (int_upsample) and for a component of half the horizontal factor at most 2 samples wide, a frame at most 4 pixels wide (h2v1_upsample, h2v2_upsample)
def decode.ups source · line 1484 · raw
@+hs:List<&2, U32> -> @+vs:List<&2, U32> -> @+hmax:U32 -> @+vmax:U32 -> @+ww:U32 -> Ups
def decode.up.far source · line 1491 · raw
@+xx:U32 -> @+nn:U32 -> U32
the pixel, along one axis of nn pixels, that holds the farther sample the filter mixes into pixel xx of a plane painted two pixels to a sample: the pixel before an even one, the pixel after an odd one, and xx itself past an edge, where libjpeg repeats the edge sample
def decode.up.mix source · line 1499 · raw
@+mode:U32 -> @+xx:U32 -> @+yy:U32 -> @+v0:U32 -> @+vh:U32 -> @+vv:U32 -> @+vd:U32 -> U32
the triangle filter's value at pixel (xx, yy) from the nearer sample v0, the farther one across vh, the farther one down vv and the one across and down vd: 3/4 nearer and 1/4 farther along each axis it filters, rounded as jdsample.c rounds (a bias of 1 then 2 for the two pixels of a sample across, 1 then 2 down, 8 then 7 across after filtering down)
def decode.up.cell.d source · line 1507 · raw
@got:Pair(Array<U32>, U32) -> @+mode:U32 -> @+xx:U32 -> @+yy:U32 -> @+v0:U32 -> @+vh:U32 -> @+vv:U32 -> Pair(Array<U32>, U32)
pixel (xx, yy) of a plane upsampled by a filter mode, from the plane as painted: four reads, the plane handed on
def decode.up.cell.c source · line 1519 · raw
@got:Pair(Array<U32>, U32) -> @+mode:U32 -> @+xx:U32 -> @+yy:U32 -> @+xf:U32 -> @+yf:U32 -> @+ww:U32 -> @+v0:U32 -> @+vh:U32 -> Pair(Array<U32>, U32)
def decode.up.cell.b source · line 1533 · raw
@got:Pair(Array<U32>, U32) -> @+mode:U32 -> @+xx:U32 -> @+yy:U32 -> @+xf:U32 -> @+yf:U32 -> @+ww:U32 -> @+v0:U32 -> Pair(Array<U32>, U32)
def decode.up.cell.a source · line 1546 · raw
@got:Pair(Array<U32>, U32) -> @+mode:U32 -> @+xx:U32 -> @+yy:U32 -> @+xf:U32 -> @+yf:U32 -> @+ww:U32 -> Pair(Array<U32>, U32)
def decode.up.cell source · line 1558 · raw
@+mode:U32 -> @+xx:U32 -> @+yy:U32 -> @+ww:U32 -> @+hh:U32 -> @plane:Array<U32> -> Pair(Array<U32>, U32)
def decode.up.walk source · line 1564 · raw
@ly:Nat -> @lx:Nat -> @got:Pair(Array<U32>, U32) -> @+xx:U32 -> @+yy:U32 -> @+mode:U32 -> @+ww:U32 -> @+hh:U32 -> @+wn:Nat -> List<&2, U32>
the pixels from (xx, yy) on: lx more of row yy after it, then ly more rows of 1 + wn pixels. got holds the plane and pixel (xx, yy), just read.
def decode.up.rows source · line 1598 · raw
@hn:Nat -> @wn:Nat -> @+mode:U32 -> @+ww:U32 -> @+hh:U32 -> @plane:Array<U32> -> List<&2, U32>
a plane's rows of ww pixels, hn of them, filtered
def decode.up.out.z source · line 1613 · raw
@zero:Bool -> @+mode:U32 -> @+nn:Nat -> @+ww:U32 -> @+hh:U32 -> @plane:Array<U32> -> List<&2, U32>
a plane's nn = ww * hh points in order, upsampled by mode: as painted for mode 0, else filtered
def decode.up.out source · line 1620 · raw
@+mode:U32 -> @+nn:Nat -> @+ww:U32 -> @+hh:U32 -> @plane:Array<U32> -> List<&2, U32>
def opaque source · line 1626 · raw
@+xx:U32 -> U32
a sample's colour bits made opaque: alpha 255 over the low 24 bits. The constant is the first operand, so a law over any sample sees its alpha without a case split on the sample's bits.
def rgb.ch.hi source · line 1630 · raw
@+qq:U32 -> U32
a channel clamped from above: 255 for a quotient past it
def rgb.ch.lt source · line 1635 · raw
@lt:Bool -> @+pos:U32 -> @+neg:U32 -> U32
a channel whose value, plus one half, is below zero is 0; otherwise the whole millions in pos - neg
def rgb.ch source · line 1647 · raw
@+pos:U32 -> @+neg:U32 -> U32
one channel of T.871 rounded to nearest and clamped to 0..255, from its value in millionths split into what is added (pos, with the half for rounding already in) and what is subtracted (neg). For inputs 0..255 both stay below 2^32, so the arithmetic is exact: no fixed-point constant and one rounding per channel.
def rgb.bits source · line 1655 · raw
@+yy:U32 -> @+cb:U32 -> @+cr:U32 -> U32
YCbCr to colour bits, (R << 16) | (G << 8) | B, per JFIF T.871 exactly: R = Y + 1.402 (Cr - 128), G = Y - 0.344136 (Cb - 128) - 0.714136 (Cr - 128), B = Y + 1.772 (Cb - 128), each rounded to nearest and clamped. The 128 offsets are the constants 179456000 = 128 * 1402000, 135458816 = 128 * (344136 + 714136) and 226816000 = 128 * 1772000.
def rgb source · line 1670 · raw
@+yy:U32 -> @+cb:U32 -> @+cr:U32 -> U32
YCbCr to a packed opaque sample, 0xFFRRGGBB
Trusted, not proved: rgb is the T.871 YCbCr to RGB conversion, rounded to nearest and clamped to 0 to 255, for every y, cb and cr from 0 to 255. Checked outside the gate: a Python copy of rgb.bits's U32 arithmetic, compared with T.871 computed in exact rationals over all 2^24 inputs, differs on none. A proof in the gate needs U32 division and products near 10^9, which the Nat lemmas reach only through unary numerals the checker cannot hold in memory.
def gray.bits source · line 1674 · raw
@+yy:U32 -> U32
a gray level's low byte in R, G and B
def gray source · line 1679 · raw
@+yy:U32 -> U32
a gray level as a packed opaque sample, 0xFFvvvvvv
def decode.grays source · line 1683 · raw
@ys:List<&2, U32> -> List<&2, U32>
every gray level of a picture as a packed opaque sample
def direct source · line 1691 · raw
@+rr:U32 -> @+gg:U32 -> @+bb:U32 -> U32
an R, G and B sample coded as they are (no colour transform) as a packed opaque sample, each channel's low byte
def decode.directs source · line 1695 · raw
@rs:List<&2, U32> -> @gs:List<&2, U32> -> @bs:List<&2, U32> -> List<&2, U32>
every R, G, B triple of a picture coded without a colour transform, as many as the shortest list holds
def decode.rgbs source · line 1717 · raw
@ys:List<&2, U32> -> @bs:List<&2, U32> -> @rs:List<&2, U32> -> List<&2, U32>
every Y, Cb, Cr triple of a picture as a packed opaque sample, as many as the shortest list holds
def decode.done.drop source · line 1738 · raw
@yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>
def decode.done.gray source · line 1744 · raw
@+ww:U32 -> @+hh:U32 -> @+ups:Ups -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>
def decode.done.pack source · line 1753 · raw
@rgb:Bool -> @+nn:Nat -> @+ww:U32 -> @+hh:U32 -> @ups:Ups -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> List<&2, U32>
the samples of three components read out in order, each upsampled as libjpeg upsamples it: R, G and B as coded when rgb is set, else Y, Cb and Cr converted per T.871
def decode.done.color source · line 1776 · raw
@rgb:Bool -> @+ww:U32 -> @+hh:U32 -> @ups:Ups -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>
a colour picture: each point's three samples read out in order, then packed
def decode.done.nf3 source · line 1788 · raw
@three:Bool -> @rgb:Bool -> @+ww:U32 -> @+hh:U32 -> @ups:Ups -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>
three components are colour; any other count is none
def decode.done.nf1 source · line 1807 · raw
@one:Bool -> @+nf:U32 -> @rgb:Bool -> @+ww:U32 -> @+hh:U32 -> @ups:Ups -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>
one component is gray; the rest is decided by decode.done.nf3
def decode.done.nf source · line 1826 · raw
@+nf:U32 -> @rgb:Bool -> @+ww:U32 -> @+hh:U32 -> @ups:Ups -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>
the picture by its component count. U32.is_eq, not a literal pattern, so a law reaches every count.
def decode.done.ok source · line 1839 · raw
@bad:Bool -> @frame:Frame -> @rgb:Bool -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>
a failed scan is none; otherwise the picture by the frame's component count
def decode.done source · line 1857 · raw
@+ok:U32 -> @frame:Frame -> @rgb:Bool -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>
the planes as a picture, or none when ok is 0. rgb says three components are R, G and B as coded, not YCbCr.
def decode.adv.ctrl source · line 1860 · raw
@adv:Adv -> Ctrl
def decode.adv.duef source · line 1865 · raw
@adv:Adv -> U32
def decode.blk.live source · line 1871 · raw
@blk:Blk -> Bool
a block that decoded: its ok flag is not 0 (its reader stayed inside the data and every code was in its table)
def decode.blocks source · line 1878 · raw
@left:Nat -> @live:Bool -> @blk:Blk -> @+ctrl:Ctrl -> @+preds:Preds -> @+frame:Frame -> @+scan:Scan -> @+tabs:Tabs -> @+ri:U32 -> @+rgb:Bool -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> @+ok:U32 -> Maybe<&2, Pic>
the block run from blk, left more after it, live saying blk decoded. A block that failed stops the run, none, before another block is read; otherwise each block is painted onto its component's plane
def decode.blocks.sum source · line 1923 · raw
@hs:List<&2, U32> -> @vs:List<&2, U32> -> @+nn:U32 -> U32
def decode.start source · line 1936 · raw
@nn:Nat -> @bits:Bits -> @+frame:Frame -> @+scan:Scan -> @+tabs:Tabs -> @+ri:U32 -> @+rgb:Bool -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>
def decode.nblocks source · line 1959 · raw
@+ww:U32 -> @+hh:U32 -> @+hs:List<&2, U32> -> @+vs:List<&2, U32> -> @+hmax:U32 -> @+vmax:U32 -> U32
the blocks a scan of the frame holds: ceil(w / 8 hmax) * ceil(h / 8 vmax) MCUs, each with the frame's components' hi * vi data units
def decode.area.ok source · line 1971 · raw
@+ww:U32 -> @+hh:U32 -> Bool
a frame of at most 2^31 points, each side at most 65535 (so the U32 product is exact): a larger frame would alias in the planes, whose indexes are U32
def decode.need.bits source · line 1975 · raw
@nn:Nat -> Nat
two bits for each of nn blocks: every block takes at least two, one for its DC code and one for its AC codes
def decode.ent.bits source · line 1983 · raw
@ent:List<&2, U32> -> Nat
the bits entropy-coded bytes hold, eight to a byte
def decode.ids.same source · line 1991 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> @+ok:Bool -> Bool
the scan's component ids are the frame's, in the frame's order, ok saying the ids before matched
def decode.scan.match source · line 2012 · raw
@+ns:U32 -> @+nf:U32 -> @sids:List<&2, U32> -> @ids:List<&2, U32> -> Bool
a scan holding every frame component, in the frame's order (T.81 B.2.3), so a block's scan position is its frame component: its plane and its quantisation table
def decode.fits source · line 2018 · raw
@+ww:U32 -> @+hh:U32 -> @+nb:U32 -> @ent:List<&2, U32> -> @+nf:U32 -> @ids:List<&2, U32> -> @scan:Scan -> Bool
a scan the decoder runs: the frame's at most 2^31 points, entropy-coded bytes enough for its blocks (two bits a block, so a short file cannot ask for planes far larger than itself), and every frame component in the scan, in the frame's order
def decode.run.fit source · line 2032 · raw
@fit:Bool -> @+nb:U32 -> @+ww:U32 -> @+hh:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+rgb:Bool -> Maybe<&2, Pic>
def decode.run.n source · line 2051 · raw
@empty:Bool -> @+ww:U32 -> @+hh:U32 -> @frame:Frame -> @+scan:Scan -> @tabs:Tabs -> @+ent:List<&2, U32> -> @+ri:U32 -> @+rgb:Bool -> Maybe<&2, Pic>
def decode.run source · line 2074 · raw
@frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+rgb:Bool -> Maybe<&2, Pic>
the scan decode of a frame. rgb says three components are R, G and B as coded, not YCbCr.
def decode.bit source · line 2091 · raw
@+ii:U32 -> U32
the bit of frame component ii in a mask of components (a frame has at most 3)
def decode.pos source · line 2106 · raw
@xs:List<&2, U32> -> @eq:Bool -> @+want:U32 -> @+ii:U32 -> U32
the position of want in xs, 255 when it is absent. eq says the element before xs was want, and ii counts the elements before it (as decode.index, which gives 0 for an absent want)
def decode.ix source · line 2123 · raw
@sids:List<&2, U32> -> @+ids:List<&2, U32> -> List<&2, U32>
the frame position of each scan component
def decode.ix.mask source · line 2132 · raw
@ix:List<&2, U32> -> @ok:Bool -> @+lo:U32 -> @+nf:U32 -> @+acc:U32 -> U32
the components of a scan as a mask over the frame's, 0 when one is not in the frame or they are not in the frame's order, each after the one before (B.2.3); lo is the least position the next may have
def decode.units source · line 2145 · raw
@ix:List<&2, U32> -> @+hs:List<&2, U32> -> @+vs:List<&2, U32> -> @+acc:U32 -> U32
the data units in one MCU of an interleaved scan: hi * vi for each of its components (A.2.3)
def decode.pass.frame source · line 2155 · raw
@one:Bool -> @+ww:U32 -> @+hh:U32 -> @+nf:U32 -> @+ids:List<&2, U32> -> @hs:List<&2, U32> -> @vs:List<&2, U32> -> @tq:List<&2, U32> -> @+hmax:U32 -> @+vmax:U32 -> @+sid:U32 -> @+fi:U32 -> Frame
the frame a scan is decoded over. An interleaved scan keeps the frame: its MCUs hold each of its components' hi * vi blocks (A.2.3). A scan of one component has one block to an MCU, in raster order over that component's blocks (A.2.2): the frame of that component alone, sampled 1 by 1, with hmax / hi by vmax / vi pixels to a sample.
def decode.pass.units source · line 2180 · raw
@one:Bool -> @+ix:List<&2, U32> -> @+hs:List<&2, U32> -> @+vs:List<&2, U32> -> U32
the units in an MCU of a scan: one for a scan of one component, else its components' hi * vi
def decode.pass.nb source · line 2188 · raw
@frame:Frame -> @+units:U32 -> U32
the blocks of a scan: ceil(w / 8 hmax) * ceil(h / 8 vmax) MCUs over the frame it is decoded over, units to an MCU
def decode.frame.ids source · line 2198 · raw
@frame:Frame -> List<&2, U32>
def decode.plan.go source · line 2203 · raw
@+ns:U32 -> @sids:List<&2, U32> -> @+ix:List<&2, U32> -> @frame:Frame -> Plan
def decode.plan source · line 2210 · raw
@scan:Scan -> @+frame:Frame -> Plan
def decode.plan.mask source · line 2215 · raw
@pl:Plan -> U32
def decode.plan.nb source · line 2220 · raw
@pl:Plan -> U32
def decode.plan.ix source · line 2225 · raw
@pl:Plan -> List<&2, U32>
def decode.plan.frame source · line 2230 · raw
@pl:Plan -> Frame
def decode.frame.nf source · line 2235 · raw
@frame:Frame -> U32
def decode.passes.ok source · line 2243 · raw
@passes:List<&2, Pass> -> @+frame:Frame -> @+mask:U32 -> @ok:Bool -> Bool
whether every pass has its components in the frame, in the frame's order, none of them in an earlier pass, and entropy-coded bytes enough for its blocks (two bits a block, as decode.fits asks of a single scan); and, at the end, every frame component in some pass. mask holds the components of the passes before.
def decode.pass.blocks source · line 2257 · raw
@left:Nat -> @live:Bool -> @blk:Blk -> @+ctrl:Ctrl -> @+preds:Preds -> @+frame:Frame -> @+scan:Scan -> @+tabs:Tabs -> @+ri:U32 -> @+ix:List<&2, U32> -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> @+ok:U32 -> Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>)))
the block run of a pass, as decode.blocks runs a single scan, painting each block onto the plane of its frame component (ix gives the frame position of each scan component). Gives 0 or 1 for whether every block decoded, and the planes.
def decode.pass.start source · line 2303 · raw
@nn:Nat -> @bits:Bits -> @+frame:Frame -> @+scan:Scan -> @+tabs:Tabs -> @+ri:U32 -> @+ix:List<&2, U32> -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>)))
the nn blocks of a pass from its first, predictors and restart count starting afresh (E.2.4, F.2.1.3.1)
def decode.pass.then source · line 2324 · raw
@+ok:U32 -> @got:Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>))) -> Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>)))
the planes after one more pass: ok is 0 once a pass has failed, and a failed pass stops the passes after it
def decode.pass.run source · line 2331 · raw
@live:Bool -> @pass:Pass -> @frame:Frame -> @+ok:U32 -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>)))
def decode.pass.ok.at source · line 2351 · raw
@+ok:U32 -> @pass:Pass -> @+frame:Frame -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>)))
def decode.pass.at source · line 2362 · raw
@pass:Pass -> @+frame:Frame -> @got:Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>))) -> Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>)))
a pass painted onto the planes got holds, with the ok flag before it
def decode.passes.go source · line 2371 · raw
@passes:List<&2, Pass> -> @+frame:Frame -> @got:Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>))) -> Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>)))
the passes painted in file order onto the planes, got holding the ok flag and the planes so far
def decode.passes.done source · line 2382 · raw
@got:Pair(U32, Pair(Array<U32>, Pair(Array<U32>, Array<U32>))) -> @frame:Frame -> @rgb:Bool -> Maybe<&2, Pic>
def decode.passes.fit source · line 2386 · raw
@fit:Bool -> @+frame:Frame -> @passes:List<&2, Pass> -> @+ww:U32 -> @+hh:U32 -> @rgb:Bool -> Maybe<&2, Pic>
def decode.passes source · line 2403 · raw
@frame:Frame -> @+passes:List<&2, Pass> -> @rgb:Bool -> Maybe<&2, Pic>
the picture of a frame coded over the scans passes: none for an empty frame (DNL is not read), one of more than 2^31 points, or scans decode.passes.ok refuses; else each pass painted in turn. rgb as for decode.run.
def decode.facts.soi source · line 2411 · raw
@+facts:U32 -> Bool
the file opened with SOI (bit 0 of what decode.facts reads)
def decode.rgb.ids source · line 2415 · raw
@ids:List<&2, U32> -> Bool
component ids 'R', 'G', 'B'
def decode.rgb.pick source · line 2424 · raw
@jfif:Bool -> @adobe:Bool -> @t0:Bool -> @ids:List<&2, U32> -> Bool
YCbCr unless a JFIF APP0 is absent and an Adobe APP14's transform flag is 0, or neither marker is there and the component ids are 'R', 'G' and 'B'
def decode.rgb.of source · line 2443 · raw
@+facts:U32 -> @frame:Frame -> Bool
three components coded as R, G and B, no colour transform, chosen as libjpeg-turbo chooses (jdapimin.c, default_decompress_parms): a JFIF APP0 means YCbCr; else an Adobe APP14 means RGB when its transform flag is 0; else component ids 'R', 'G' and 'B' mean RGB; anything else is YCbCr
def decode.finish.ok source · line 2449 · raw
@ok1:Bool -> @ok2:Bool -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+rgb:Bool -> Maybe<&2, Pic>
def decode.finish.ph source · line 2469 · raw
@done:Bool -> @+frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> @+facts:U32 -> Maybe<&2, Pic>
def decode.max.b source · line 2487 · raw
@gt:Bool -> @+aa:U32 -> @+bb:U32 -> U32
def decode.max source · line 2494 · raw
@+aa:U32 -> @+bb:U32 -> U32
def decode.vmax source · line 2497 · raw
@vs:List<&2, U32> -> @+mm:U32 -> U32
def decode.fac.ok source · line 2507 · raw
@+ff:U32 -> Bool
a sampling factor the decoder places: 1, 2 or 4 (laws/JpegPlacesComponentSamples.bend). A Bool of U32.is_eq, not a match on literals, so a law can case-split on it.
def decode.fac.pair source · line 2512 · raw
@+hv:U32 -> Bool
both sampling factors of one SOF component byte, H in the high nibble and V in the low nibble, are ones the decoder places
def decode.nf.ok source · line 2516 · raw
@+nf:U32 -> Bool
a component count the decoder reads: 1 (gray) or 3 (YCbCr)
def decode.u16 source · line 2519 · raw
@+hi:U32 -> @+lo:U32 -> U32
def decode.comps.rev source · line 2522 · raw
@cc:Comps -> Comps
def decode.read.cs source · line 2528 · raw
@left:Nat -> @xs:List<&2, U32> -> @ids:List<&2, U32> -> @hs:List<&2, U32> -> @vs:List<&2, U32> -> @tqs:List<&2, U32> -> @ok:Bool -> Comps
def decode.read.sof.ok source · line 2551 · raw
@good:Bool -> @zmax:Bool -> @+ww:U32 -> @+hh:U32 -> @+nf:U32 -> @ids:List<&2, U32> -> @+hs:List<&2, U32> -> @+vs:List<&2, U32> -> @tq:List<&2, U32> -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> St
def decode.read.sof.c source · line 2584 · raw
@cok:Bool -> @good:Bool -> @zmax:Bool -> @+ww:U32 -> @+hh:U32 -> @+nf:U32 -> @ids:List<&2, U32> -> @+hs:List<&2, U32> -> @+vs:List<&2, U32> -> @tq:List<&2, U32> -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> St
the components first: a factor outside 1, 2 and 4, or a short component list, refuses the file before the count and the refusals so far are asked
def decode.fac.any source · line 2608 · raw
@+ff:U32 -> Bool
a sampling factor T.81 B.2.2 allows: 1 to 4
def decode.fac.one source · line 2612 · raw
@hs:List<&2, U32> -> @vs:List<&2, U32> -> Bool
the one component of a frame read whole, its factors each 1 to 4
def decode.read.sof.n1 source · line 2633 · raw
@one:Bool -> @cok:Bool -> @+ww:U32 -> @+hh:U32 -> @+nf:U32 -> @+ids:List<&2, U32> -> @+hs:List<&2, U32> -> @+vs:List<&2, U32> -> @+tq:List<&2, U32> -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
a frame of one component has one scan of one component, which T.81 A.2.2 codes non-interleaved: one data unit to an MCU, in raster order over the component's own ceil(ceil(X * H / Hmax) / 8) blocks to a row. With one component H is Hmax, so that is the grid of a component sampled 1 by 1, which is how such a frame is kept, whatever factors 1 to 4 its SOF names. A frame of three components is read as its SOF says, each factor 1, 2 or 4 (laws/JpegPlacesComponentSamples.bend).
def decode.read.sof.f source · line 2659 · raw
@cc:Comps -> @+ww:U32 -> @+hh:U32 -> @+nf:U32 -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.read.sof.p source · line 2676 · raw
@p8:Bool -> @xs:List<&2, U32> -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
the SOF0 body after its precision byte: precision 8 goes on to the size and the components, any other stops
def decode.read.sof source · line 2698 · raw
@xs:List<&2, U32> -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
an SOF0 body. The precision is compared with U32.is_eq, not a literal pattern, so a law reaches it.
def decode.take source · line 2713 · raw
@left:Nat -> @xs:List<&2, U32> -> @acc:List<&2, U32> -> Pair(List<&2, U32>, List<&2, U32>)
def decode.q.put source · line 2724 · raw
@tabs:Tabs -> @+id:U32 -> @vals:List<&2, U32> -> Tabs
def decode.q.wide source · line 2738 · raw
@xs:List<&2, U32> -> List<&2, U32>
16-bit quantisation entries, two bytes each, high byte first
def decode.q.vals source · line 2751 · raw
@wide:Bool -> @xs:List<&2, U32> -> List<&2, U32>
a table's entries as read: one byte each for Pq = 0, two for Pq = 1 (T.81 B.2.4.1)
def decode.read.dqt source · line 2759 · raw
@xs:List<&2, U32> -> @ok:Bool -> @phase:Nat -> @+id:U32 -> @+wide:Bool -> @acc:List<&2, U32> -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
a DQT body: each table's Pq and Tq byte (Pq 0 or 1, Tq at most 3), then its 64 entries, of 8 or 16 bits
def decode.sum.counts source · line 2811 · raw
@xs:List<&2, U32> -> @+nn:U32 -> U32
def decode.huff.fits source · line 2820 · raw
@cs:List<&2, U32> -> @+code:U32 -> @+lim:U32 -> @+ok:Bool -> Bool
the canonical codes of a table's 16 counts each fit their length without the all-ones code (T.81 C; libjpeg's jpeg_make_d_derived_tbl): code is the next code of length si, lim 2^si
def decode.huff.counts.ok source · line 2829 · raw
@+cs:List<&2, U32> -> Bool
a table's counts: at most 256 symbols, and codes that fit
def decode.huff.sym.ok source · line 2834 · raw
@+cls:U32 -> @+sym:U32 -> Bool
a symbol a table may hold: a DC table's (class 0) are difference categories, 0 to 11 for 8-bit samples (T.81 F.1.2.1); an AC table's are any byte
def decode.huff.put.dc source · line 2837 · raw
@+id:U32 -> @hh:Huff -> @q0:List<&2, U32> -> @q1:List<&2, U32> -> @q2:List<&2, U32> -> @q3:List<&2, U32> -> @dc0:Huff -> @dc1:Huff -> @dc2:Huff -> @dc3:Huff -> @ac0:Huff -> @ac1:Huff -> @ac2:Huff -> @ac3:Huff -> Tabs
def decode.huff.put.ac source · line 2863 · raw
@+id:U32 -> @hh:Huff -> @q0:List<&2, U32> -> @q1:List<&2, U32> -> @q2:List<&2, U32> -> @q3:List<&2, U32> -> @dc0:Huff -> @dc1:Huff -> @dc2:Huff -> @dc3:Huff -> @ac0:Huff -> @ac1:Huff -> @ac2:Huff -> @ac3:Huff -> Tabs
def decode.huff.put source · line 2889 · raw
@tabs:Tabs -> @+cls:U32 -> @+id:U32 -> @hh:Huff -> Tabs
def decode.read.dht source · line 2898 · raw
@xs:List<&2, U32> -> @ok:Bool -> @sym:Bool -> @phase:Nat -> @+cls:U32 -> @+id:U32 -> @+counts:List<&2, U32> -> @+syms:List<&2, U32> -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.scan.rev source · line 2987 · raw
@sids:List<&2, U32> -> @td:List<&2, U32> -> @ta:List<&2, U32> -> @+ns:U32 -> Scan
def decode.read.sos.ok source · line 2990 · raw
@ss0:Bool -> @se63:Bool -> @ah0:Bool -> @sids:List<&2, U32> -> @td:List<&2, U32> -> @ta:List<&2, U32> -> @+ns:U32 -> @frame:Frame -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.read.sos.end source · line 3019 · raw
@xs:List<&2, U32> -> @+tok:Bool -> @sids:List<&2, U32> -> @td:List<&2, U32> -> @ta:List<&2, U32> -> @+ns:U32 -> @frame:Frame -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.read.sos.cs source · line 3040 · raw
@left:Nat -> @xs:List<&2, U32> -> @+tok:Bool -> @sids:List<&2, U32> -> @td:List<&2, U32> -> @ta:List<&2, U32> -> @+ns:U32 -> @frame:Frame -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.read.sos source · line 3067 · raw
@xs:List<&2, U32> -> @frame:Frame -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.read.dri source · line 3082 · raw
@xs:List<&2, U32> -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+kind:U32 -> @+bad:U32 -> St
def decode.dispatch source · line 3097 · raw
@+mark:U32 -> @xs:List<&2, U32> -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.between source · line 3124 · raw
@+bb:U32 -> @+lo:U32 -> @+hi:U32 -> Bool
def decode.mark.range.b source · line 3127 · raw
@sof:Bool -> @app:Bool -> @rst:Bool -> @+bb:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.mark.range source · line 3154 · raw
@+bb:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.mark source · line 3167 · raw
@+bb:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.step.pay0 source · line 3203 · raw
@left:Nat -> @+mark:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.step.len.b source · line 3220 · raw
@small:Bool -> @+mark:U32 -> @+len:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.step.last source · line 3238 · raw
@left:Nat -> @+bb:U32 -> @+mark:U32 -> @acc:List<&2, U32> -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.step.pay source · line 3257 · raw
@left:Nat -> @+bb:U32 -> @+mark:U32 -> @acc:List<&2, U32> -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.ent.next source · line 3278 · raw
@framed:Bool -> @+bb:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
a marker other than RSTn and EOI inside a scan ends the scan: after a frame header the walk keeps the bytes after it (Rest); before one the file is refused
def decode.ent.rst.b source · line 3295 · raw
@rst:Bool -> @+bb:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.step.ent source · line 3314 · raw
@ff:Bool -> @+bb:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
a byte of the entropy-coded data: 255 may open a stuffed pair or a marker, any other byte is data. Compares with U32.is_eq, not a literal pattern, so a law reaches a symbolic byte.
def decode.step.ph source · line 3332 · raw
@phase:Phase -> @+bb:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> St
def decode.step source · line 3376 · raw
@+bb:U32 -> @st:St -> St
def decode.walk source · line 3381 · raw
@xs:List<&2, U32> -> @st:St -> St
def decode.st.good source · line 3393 · raw
@st:St -> Bool
a walk that read one frame header and no error
def decode.scan.ns source · line 3398 · raw
@scan:Scan -> U32
def decode.chain.last source · line 3405 · raw
@none:Bool -> @scan:Scan -> @tabs:Tabs -> @ent:List<&2, U32> -> @+ri:U32 -> @passes:List<&2, Pass> -> List<&2, Pass>
the passes with the last walk's scan, unless that walk read no SOS (only other segments, then EOI): none says its component count is 0, asked with U32.is_eq so a law reaches a symbolic count
def decode.chain source · line 3428 · raw
@left:Nat -> @good:Bool -> @st:St -> @passes:List<&2, Pass> -> Chain
the scans of the walks from st on, good saying st read cleanly: a walk that ended at a marker after its scan (Rest) hands on a pass of its scan, with the tables and restart interval it read it with, and the bytes after the marker are walked on from that marker with the same frame, tables and restart interval (T.81 B.2.4: tables may be redefined between scans). A walk ending where a scan may end is the last; one ending anywhere else, or with an error or a second frame header, refuses the file.
def decode.multi.go source · line 3453 · raw
@ok:Bool -> @passes:List<&2, Pass> -> @frame:Frame -> @rgb:Bool -> Maybe<&2, Pic>
def decode.multi source · line 3462 · raw
@chain:Chain -> @+frame:Frame -> @+facts:U32 -> Maybe<&2, Pic>
the picture of a file of several scans: its chain of walks read cleanly from SOI, then its passes
def decode.finish.at source · line 3471 · raw
@phase:Phase -> @+frame:Frame -> @+scan:Scan -> @+tabs:Tabs -> @+ent:List<&2, U32> -> @+ri:U32 -> @+kind:U32 -> @+bad:U32 -> @+facts:U32 -> Maybe<&2, Pic>
the picture of a finished walk, facts being what decode.facts read of the file's markers. A walk that ended at a marker after its scan reads on (decode.chain). One that read EOI (Stop), or whose bytes ended inside the entropy-coded data, which libjpeg also takes as the end of the image (with a warning), hands its scan on; any other has no picture.
def decode.finish source · line 3496 · raw
@st:St -> @+facts:U32 -> Maybe<&2, Pic>
the picture of a finished walk, facts being what decode.facts read of the file's markers
def soi source · line 3502 · raw
List<&2, U32>
the JPEG start-of-image marker, FF D8
def eoi source · line 3506 · raw
List<&2, U32>
the JPEG end-of-image marker, FF D9
def decode.facts.id source · line 3510 · raw
@want:List<&2, U32> -> @body:List<&2, U32> -> @+ok:Bool -> Bool
a segment body opens with the identifier want, ok saying the bytes before matched
def decode.facts.at source · line 3525 · raw
@body:List<&2, U32> -> @kk:Nat -> U32
byte kk of a segment body, 0 past its end
def decode.facts.app source · line 3530 · raw
@+mark:U32 -> @+nn:U32 -> @+body:List<&2, U32> -> @+acc:U32 -> U32
what an APPn segment's body adds: an APP0 opening with JFIF and its null, at least 14 bytes, sets bit 1; an APP14 opening with Adobe, at least 12 bytes, sets bit 2 and puts its transform flag (byte 11) in bits 8 to 15
def decode.facts.keep source · line 3548 · raw
@+mark:U32 -> @+bb:U32 -> @+got:List<&2, U32> -> List<&2, U32>
a body byte kept for the facts: only APP0 and APP14 are read, and only their first 16 bytes
def decode.facts.end source · line 3553 · raw
@+mark:U32 -> @+nn:U32 -> @got:List<&2, U32> -> @+acc:U32 -> Fs
a segment's end: its body read for facts, and the pass back between segments
def decode.facts.len source · line 3557 · raw
@left:Nat -> @+mark:U32 -> @+nn:U32 -> @+acc:U32 -> Fs
a length field read: an empty body ends the segment at once
def decode.facts.mark source · line 3566 · raw
@+bb:U32 -> @+acc:U32 -> Fs
after an FF: a fill FF, a marker that stands alone (TEM, RST0 to RST7, SOI), the first scan's SOS or EOI, which stop the pass, or a marker that opens a segment
def decode.facts.step source · line 3582 · raw
@+bb:U32 -> @fs:Fs -> Fs
one byte of the facts pass. A byte between segments that is not an FF is skipped, as the walk skips it.
def decode.facts.acc source · line 3610 · raw
@fs:Fs -> U32
the facts so far
def decode.facts.walk source · line 3617 · raw
@xs:List<&2, U32> -> @fs:Fs -> U32
the facts pass over bytes, stopping at the first scan
def decode.facts.soi.at source · line 3632 · raw
@soi:Bool -> @rest:List<&2, U32> -> U32
a file that opens with SOI: bit 0, then the colour facts of its markers
def decode.facts source · line 3642 · raw
@bytes:List<&2, U32> -> U32
what libjpeg reads from a file's markers before its first scan: bit 0 the file opens with SOI (T.81 B.2.1; a file that does not is refused), and the colour facts decode.facts.app collects
def decode source · line 3650 · raw
@+bytes:List<&2, U32> -> Maybe<&2, Pic>
baseline or extended sequential 8-bit Huffman JPEG bytes, or none when the file is not one this decoder reads