~/bend-docscommunity

src/jpeg.bend checks

raw source on the hub · import emerging-ezimg@1.5.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

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

type Frame source · line 29 · raw

Data

SOF0 / SOF1 frame geometry. Sampling factors are 1 to 4.

type Scan source · line 34 · raw

Data

one SOS header

type Tabs source · line 38 · raw

Data

four quant tables and four DC / AC Huffman tables

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)

type St source · line 57 · raw

Data

parser state. kind 1 is a baseline frame. bad 1 rejects the file.

type Bits source · line 61 · raw

Data

bit reader. n bits remain in buf. ok is 0 after a truncated or marked stream.

type Hit source · line 65 · raw

Data

one decoded Huffman symbol

type R8 source · line 69 · raw

Data

eight values: one row of a block

type B8 source · line 73 · raw

Data

an 8 by 8 block of values, row by row

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

type Blk source · line 82 · raw

Data

one decoded 8x8 block, level-shifted samples, and the DC predictor

type Preds source · line 86 · raw

Data

DC predictors, one per scan component

type Ctrl source · line 90 · raw

Data

where the current block sits in the frame

type Adv source · line 94 · raw

Data

the block that follows, and whether a restart marker comes first

type Geom source · line 98 · raw

Data

pixel rectangle one block sample covers

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

type Nb source · line 376 · raw

Data

the bit reader inside decode.nbits, apart from its bytes and whether buf is empty: ff says the byte before the bytes was a 255, and h255 and h0 whether they open with 255 or 0, each asked with U32.is_eq so a law reaches a symbolic byte. nn bits remain in buf, top bit first at bit 7; acc holds the bits read so far.

type Walk source · line 434 · 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

type Lay source · line 1121 · raw

Data

the frame's width, its largest horizontal sampling factor and the restart interval: what moving to the next MCU reads

type Dims source · line 1186 · raw

Data

the frame's size and its largest sampling factors

type Wh source · line 1239 · raw

Data

the frame's width and height, which bound the pixels a sample may write

type Ups source · line 1359 · raw

Data

the three components' upsampling

type Grid source · line 1439 · raw

Data

how a plane is upsampled: its filter mode, width and height

type Planes source · line 1605 · raw

Type

the three planes, Y (or R), Cb (or G) and Cr (or B)

type Job source · line 1609 · raw

Data

what a block run reads and does not change: the frame, the scan, the tables in force and the restart interval

type Paint source · line 1613 · raw

Type

what a block run has made so far: 1 while every block decoded (else 0) and the three planes painted

type Pass source · line 1911 · raw

Data

a scan read: its header, the tables and restart interval it was read with, and its entropy-coded bytes in file order

type Plan source · line 2006 · 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

type Rd source · line 2432 · raw

Data

what the marker walk has read besides its phase: the other fields of a St, which every segment reader takes and hands on

type Dq source · line 2500 · raw

Data

a DQT body being read: whether it is well formed so far, the bytes left of the table being read (0 between tables), that table's Tq and whether its entries are 16-bit, its entries so far, newest first, and what the walk has read

type Dh source · line 2615 · raw

Data

a DHT body being read: whether it is well formed so far, whether the symbols (not the 16 counts) are being read, the bytes left of them (0 between tables), the table's class and Th, its counts and its symbols so far, newest first, and what the walk has read

type Chain source · line 3004 · raw

Data

the scans of a file: whether every walk read them cleanly, and the passes, last first

type Fs source · line 3145 · 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

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.at source · line 380 · raw

@+rest:List<&2, U32> -> @+nn:U32 -> @+buf:U32 -> @+ok:U32 -> @+acc:U32 -> Nb

the reader at rest, the bytes after one it took, with nn bits of that byte left in buf

def decode.nbits.ff source · line 385 · raw

@h0:Bool -> @+rest:List<&2, U32> -> @+ok:U32 -> @+acc:U32 -> Nb

the byte after a 255 that is not 255 itself: 0 is a stuffed 255 (eight one bits); any other byte is a marker, which reads zero bits and sets ok to 0

def decode.nbits.hold source · line 393 · raw

@+rest:List<&2, U32> -> @+ok:U32 -> @+acc:U32 -> Nb

a 255 just taken: read with the byte after it, a fill byte after a 255 included

def decode.nbits source · line 399 · raw

@left:Nat -> @eq:Bool -> @xs:List<&2, U32> -> @nb:Nb -> Pair(Bits, U32)

left more bits onto acc from bytes xs, most significant first; eq says the buffer is empty. 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 419 · 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 422 · raw

@bits:Bits -> Pair(Bits, U32)

def decode.huff.hit source · line 428 · raw

@+sym:U32 -> @bits:Bits -> Hit

a symbol found, the reader after its code

def decode.huff.next source · line 438 · 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 451 · 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 471 · 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 474 · raw

@bits:Bits -> Pair(Nat, Pair(U32, Pair(List<&2, U32>, Pair(U32, U32))))

def decode.read.n source · line 479 · raw

@+cat:U32 -> @bits:Bits -> Pair(Bits, U32)

def decode.extend.s source · line 484 · raw

@small:Bool -> @+mag:U32 -> @+cat:U32 -> U32

def decode.extend source · line 491 · raw

@+cat:U32 -> @+mag:U32 -> U32

def decode.zig.at source · line 499 · 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 631 · raw

@rr:R8 -> @+cc:U32 -> @+val:U32 -> R8

a row with column cc set to val

def decode.b8.set source · line 661 · 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 683 · 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 688 · raw

R8

eight zeros

def decode.b8.zero source · line 692 · raw

B8

a block of zeros

def decode.nth source · line 697 · 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 713 · raw

@left:Nat -> @qs:List<&2, U32> -> List<&2, U32>

the entries of a list after left more

def decode.ac.stored source · line 724 · raw

@full:Bool -> @+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> @+ok:U32 -> Ac

def decode.ac.store source · line 732 · 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 738 · raw

@zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> Ac

def decode.ac.run.b source · line 742 · raw

@zzz:Bool -> @over:Bool -> @+sym:U32 -> @+kk:U32 -> @bits:Bits -> @zz:B8 -> @qs:List<&2, U32> -> @+ok:U32 -> Ac

a run/size symbol sym after coefficient kk: size 0 here, or a run past the block, is a bad code

def decode.ac.run source · line 754 · raw

@+sym:U32 -> @bits:Bits -> @+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @+ok:U32 -> Ac

def decode.ac.zrl.eq source · line 758 · raw

@eq:Bool -> @zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> @+ok:U32 -> Ac

def decode.ac.zrl.k source · line 765 · raw

@more:Bool -> @+nk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> @+ok:U32 -> Ac

def decode.ac.zrl source · line 772 · raw

@+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @bits:Bits -> @+ok:U32 -> Ac

def decode.ac.sym.b source · line 777 · 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 800 · raw

@+sym:U32 -> @bits:Bits -> @+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @+ok:U32 -> Ac

def decode.ac.step source · line 803 · raw

@hit:Hit -> @+kk:U32 -> @zz:B8 -> @qs:List<&2, U32> -> @+ok:U32 -> Ac

def decode.ac.full source · line 808 · raw

@done:Bool -> @short:Bool -> @ac:Ac -> Ac

def decode.ac source · line 817 · raw

@left:Nat -> @ac:Ac -> @+tab:Huff -> Ac

def decode.zig source · line 829 · raw

List<&2, U32>

def decode.asr.s source · line 835 · 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 842 · raw

@+xx:U32 -> @nn:Nat -> U32

def decode.idct.out source · line 847 · 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 856 · 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 883 · raw

@bb:B8 -> B8

a block's rows as its columns

def decode.b8.pass source · line 893 · raw

@bb:B8 -> @+fine:Bool -> B8

one 1-D pass over each row

def decode.r8.list source · line 900 · raw

@rr:R8 -> @acc:List<&2, U32> -> List<&2, U32>

a row's values onto acc

def decode.b8.list source · line 906 · raw

@bb:B8 -> List<&2, U32>

a block's values, row by row

def decode.idct source · line 914 · 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 917 · raw

@ac:Ac -> @+pred:U32 -> @+qok:U32 -> Blk

def decode.block.diff source · line 922 · 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 936 · 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 952 · raw

@hit:Hit -> @+pred:U32 -> @ac:Huff -> @quant:List<&2, U32> -> @+qok:U32 -> Blk

def decode.qok.b source · line 957 · raw

@ok:Bool -> U32

def decode.qok source · line 964 · raw

@quant:List<&2, U32> -> U32

def decode.block.go source · line 967 · raw

@bits:Bits -> @+pred:U32 -> @dc:Huff -> @ac:Huff -> @+quant:List<&2, U32> -> Blk

def decode.dc.get source · line 970 · raw

@tabs:Tabs -> @+id:U32 -> Huff

def decode.ac.get source · line 983 · raw

@tabs:Tabs -> @+id:U32 -> Huff

def decode.q.get source · line 998 · 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 1014 · raw

@preds:Preds -> @+ii:U32 -> U32

def decode.pred.put source · line 1027 · raw

@preds:Preds -> @+ii:U32 -> @+vv:U32 -> Preds

def decode.pred.zero source · line 1040 · raw

Preds

def decode.preds.next source · line 1043 · raw

@due:U32 -> @preds:Preds -> @+comp:U32 -> @+pred:U32 -> Preds

def decode.block.of source · line 1052 · raw

@bits:Bits -> @+comp:U32 -> @preds:Preds -> @frame:Frame -> @scan:Scan -> @+tabs:Tabs -> Blk

def decode.rst.ok source · line 1059 · raw

@eq:Bool -> @rest:List<&2, U32> -> Bits

def decode.rst.xs source · line 1066 · raw

@xs:List<&2, U32> -> @+want:U32 -> Bits

def decode.rst source · line 1073 · raw

@bits:Bits -> @+want:U32 -> Bits

def decode.restart source · line 1078 · raw

@bits:Bits -> @+due:U32 -> @+expect:U32 -> Bits

def decode.ceil source · line 1085 · raw

@+nn:U32 -> @+dd:U32 -> U32

def decode.ri.nz source · line 1088 · raw

@+ri:U32 -> U32

def decode.adv.due.b source · line 1095 · raw

@zero:Bool -> @hit:Bool -> @+mcu:U32 -> @+mx:U32 -> @+my:U32 -> @+rst:U32 -> Adv

def decode.adv.due source · line 1106 · raw

@+mcu:U32 -> @+mx:U32 -> @+my:U32 -> @+rst:U32 -> @+ri:U32 -> Adv

def decode.adv.mx source · line 1109 · raw

@inb:Bool -> @+mcu:U32 -> @+mx:U32 -> @+my:U32 -> @+rst:U32 -> @+ri:U32 -> Adv

def decode.adv.mcu source · line 1116 · raw

@+mcu:U32 -> @+mx:U32 -> @+my:U32 -> @+rst:U32 -> @+ww:U32 -> @+hmax:U32 -> @+ri:U32 -> Adv

def decode.adv.comp source · line 1124 · raw

@more:Bool -> @+comp:U32 -> @+mx:U32 -> @+my:U32 -> @+mcu:U32 -> @+rst:U32 -> @lay:Lay -> Adv

def decode.adv.bi source · line 1131 · raw

@more:Bool -> @ctrl:Ctrl -> @+ww:U32 -> @+hmax:U32 -> @+ns:U32 -> @+ri:U32 -> Adv

def decode.adv source · line 1138 · raw

@ctrl:Ctrl -> @frame:Frame -> @scan:Scan -> @+ri:U32 -> Adv

def decode.block.next.go source · line 1147 · raw

@adv:Adv -> @bits:Bits -> @preds:Preds -> @+pred:U32 -> @+comp:U32 -> @frame:Frame -> @scan:Scan -> @tabs:Tabs -> Blk

def decode.block.next source · line 1164 · raw

@bits:Bits -> @preds:Preds -> @+pred:U32 -> @ctrl:Ctrl -> @+frame:Frame -> @+scan:Scan -> @tabs:Tabs -> @+ri:U32 -> Blk

def decode.geom.nz source · line 1178 · raw

@+nn:U32 -> U32

def decode.geom.at source · line 1192 · raw

@+bi:U32 -> @+mx:U32 -> @+my:U32 -> @+hi:U32 -> @+vi:U32 -> @dims:Dims -> Geom

where block bi of a component with factors hi and vi sits in MCU (mx, my): 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 1201 · raw

@ctrl:Ctrl -> @frame:Frame -> @scan:Scan -> Geom

def decode.splat.in source · line 1208 · raw

@xin:Bool -> @yin:Bool -> @plane:Array<U32> -> @+xx:U32 -> @+yy:U32 -> @+ww:U32 -> @+sample:U32 -> Array<U32>

def decode.splat.px source · line 1221 · 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 1243 · raw

@left:Nat -> @plane:Array<U32> -> @+x0:U32 -> @+y0:U32 -> @+py:U32 -> @+pw:U32 -> @wh:Wh -> @+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 1262 · raw

@left:Nat -> @plane:Array<U32> -> @+xs:List<&2, U32> -> @+row:U32 -> @+col:U32 -> @gg:Geom -> 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) of gg: 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 1272 · raw

@left:Nat -> @plane:Array<U32> -> @+xs:List<&2, U32> -> @+row:U32 -> @+gg:Geom -> 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 at gg; a block's samples are read once, in order

def decode.paint.use source · line 1279 · raw

@gg:Geom -> @which:Bool -> @samples:List<&2, U32> -> @plane:Array<U32> -> Array<U32>

def decode.ctrl.comp source · line 1288 · raw

@ctrl:Ctrl -> U32

def decode.sink.leaf source · line 1293 · raw

@aa:Array<U32> -> U32

def decode.sink.p source · line 1300 · raw

@pp:Pair(Array<U32>, U32) -> U32

def decode.sink source · line 1304 · raw

@aa:Array<U32> -> U32

def decode.depth.of source · line 1309 · 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 1317 · raw

@+nn:U32 -> Nat

U32.is_eq, not a literal pattern, so a law reaches every nn

def decode.plane source · line 1320 · raw

@+nn:U32 -> Array<U32>

def decode.emit source · line 1325 · 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 1338 · raw

@+nn:Nat -> @plane:Array<U32> -> List<&2, U32>

the first nn points of a plane, in order

def decode.up.mode source · line 1349 · 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 1362 · raw

@+hs:List<&2, U32> -> @+vs:List<&2, U32> -> @+hmax:U32 -> @+vmax:U32 -> @+ww:U32 -> Ups

def decode.up.far source · line 1369 · 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 1377 · 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 1385 · 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 1397 · raw

@got:Pair(Array<U32>, U32) -> @+mode:U32 -> @+xx:U32 -> @+yy:U32 -> @+at:U32 -> @+v0:U32 -> @+vh:U32 -> Pair(Array<U32>, U32)

def decode.up.cell.b source · line 1409 · 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 1422 · 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 1434 · raw

@+mode:U32 -> @+xx:U32 -> @+yy:U32 -> @+ww:U32 -> @+hh:U32 -> @plane:Array<U32> -> Pair(Array<U32>, U32)

def decode.up.walk source · line 1444 · raw

@ly:Nat -> @lx:Nat -> @grid:Grid -> @got:Pair(Array<U32>, U32) -> @+col:U32 -> @+row:U32 -> @+wn:Nat -> List<&2, U32>

the pixels from (col, row) on: lx more of row row after it, then ly more rows of 1 + wn pixels. got holds the plane and pixel (col, row), just read.

def decode.up.rows source · line 1472 · 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 1487 · 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 1494 · raw

@+mode:U32 -> @+nn:Nat -> @+ww:U32 -> @+hh:U32 -> @plane:Array<U32> -> List<&2, U32>

def opaque source · line 1500 · 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 1504 · raw

@+qq:U32 -> U32

a channel clamped from above: 255 for a quotient past it

def rgb.ch.lt source · line 1509 · 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 1521 · 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 1529 · 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 1544 · 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 1548 · raw

@+yy:U32 -> U32

a gray level's low byte in R, G and B

def gray source · line 1553 · raw

@+yy:U32 -> U32

a gray level as a packed opaque sample, 0xFFvvvvvv

def decode.grays source · line 1557 · raw

@ys:List<&2, U32> -> List<&2, U32>

every gray level of a picture as a packed opaque sample

def direct source · line 1565 · 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 1569 · 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 1587 · 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 1616 · raw

@yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>

def decode.done.gray source · line 1622 · 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 1631 · 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 1654 · 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 1666 · 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 1685 · raw

@one:Bool -> @+nf:U32 -> @rgb:Bool -> @+ww:U32 -> @+hh:U32 -> @ups:Ups -> @planes:Planes -> Maybe<&2, Pic>

one component is gray; the rest is decided by decode.done.nf3

def decode.done.nf source · line 1694 · 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 1707 · 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 1725 · 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 1728 · raw

@adv:Adv -> Ctrl

def decode.adv.duef source · line 1733 · raw

@adv:Adv -> U32

def decode.blk.live source · line 1739 · 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 1746 · raw

@left:Nat -> @live:Bool -> @blk:Blk -> @+ctrl:Ctrl -> @+preds:Preds -> @+job:Job -> @+rgb:Bool -> @paint:Paint -> 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 1774 · raw

@hs:List<&2, U32> -> @vs:List<&2, U32> -> @+nn:U32 -> U32

def decode.start source · line 1787 · raw

@nn:Nat -> @bits:Bits -> @+job:Job -> @+rgb:Bool -> @yy:Array<U32> -> @cb:Array<U32> -> @cr:Array<U32> -> Maybe<&2, Pic>

def decode.nblocks source · line 1807 · 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 1819 · 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 1823 · 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 1831 · raw

@ent:List<&2, U32> -> Nat

the bits entropy-coded bytes hold, eight to a byte

def decode.ids.same source · line 1839 · 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 1860 · 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 1866 · 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 1880 · raw

@fit:Bool -> @+nb:U32 -> @+ww:U32 -> @+hh:U32 -> @job:Job -> @ent:List<&2, U32> -> @+rgb:Bool -> Maybe<&2, Pic>

def decode.run.n source · line 1888 · raw

@empty:Bool -> @+ww:U32 -> @+hh:U32 -> @job:Job -> @+ent:List<&2, U32> -> @+rgb:Bool -> Maybe<&2, Pic>

def decode.run source · line 1898 · 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 1915 · 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 1930 · 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 1947 · 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 1956 · 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 1969 · 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 1979 · raw

@one:Bool -> @frame:Frame -> @+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 1991 · 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 1999 · 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 2009 · raw

@frame:Frame -> List<&2, U32>

def decode.plan.go source · line 2014 · raw

@+ns:U32 -> @sids:List<&2, U32> -> @+ix:List<&2, U32> -> @frame:Frame -> Plan

def decode.plan source · line 2021 · raw

@scan:Scan -> @+frame:Frame -> Plan

def decode.plan.mask source · line 2026 · raw

@pl:Plan -> U32

def decode.plan.nb source · line 2031 · raw

@pl:Plan -> U32

def decode.plan.ix source · line 2036 · raw

@pl:Plan -> List<&2, U32>

def decode.plan.frame source · line 2041 · raw

@pl:Plan -> Frame

def decode.frame.nf source · line 2046 · raw

@frame:Frame -> U32

def decode.passes.ok source · line 2054 · 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 2068 · raw

@left:Nat -> @live:Bool -> @blk:Blk -> @+ctrl:Ctrl -> @+preds:Preds -> @+job:Job -> @+ix:List<&2, U32> -> @paint:Paint -> 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 2098 · raw

@nn:Nat -> @bits:Bits -> @+job:Job -> @+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 2116 · 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 2123 · 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 2143 · 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 2154 · 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 2163 · 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 2174 · 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 2178 · raw

@fit:Bool -> @+frame:Frame -> @passes:List<&2, Pass> -> @+ww:U32 -> @+hh:U32 -> @rgb:Bool -> Maybe<&2, Pic>

def decode.passes source · line 2195 · 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 2203 · raw

@+facts:U32 -> Bool

the file opened with SOI (bit 0 of what decode.facts reads)

def decode.rgb.ids source · line 2207 · raw

@ids:List<&2, U32> -> Bool

component ids 'R', 'G', 'B'

def decode.rgb.pick source · line 2216 · 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 2235 · 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 2241 · 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 2261 · raw

@done:Bool -> @+frame:Frame -> @pass:Pass -> @+kind:U32 -> @+bad:U32 -> @+facts:U32 -> Maybe<&2, Pic>

def decode.max.b source · line 2269 · raw

@gt:Bool -> @+aa:U32 -> @+bb:U32 -> U32

def decode.max source · line 2276 · raw

@+aa:U32 -> @+bb:U32 -> U32

def decode.vmax source · line 2279 · raw

@vs:List<&2, U32> -> @+mm:U32 -> U32

def decode.fac.ok source · line 2289 · 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 2294 · 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 2298 · raw

@+nf:U32 -> Bool

a component count the decoder reads: 1 (gray) or 3 (YCbCr)

def decode.u16 source · line 2301 · raw

@+hi:U32 -> @+lo:U32 -> U32

def decode.comps.rev source · line 2304 · raw

@cc:Comps -> Comps

def decode.read.cs source · line 2310 · 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 2333 · raw

@good:Bool -> @zmax:Bool -> @frame:Frame -> @pass:Pass -> @+kind:U32 -> St

def decode.read.sof.c source · line 2346 · raw

@cok:Bool -> @good:Bool -> @zmax:Bool -> @frame:Frame -> @pass:Pass -> @+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 2354 · raw

@+ff:U32 -> Bool

a sampling factor T.81 B.2.2 allows: 1 to 4

def decode.fac.one source · line 2358 · 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 2379 · raw

@one:Bool -> @cc:Comps -> @+ww:U32 -> @+hh:U32 -> @+nf:U32 -> @pass:Pass -> @+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 2389 · raw

@cc:Comps -> @+ww:U32 -> @+hh:U32 -> @+nf:U32 -> @pass:Pass -> @+kind:U32 -> @+bad:U32 -> St

def decode.read.sof.p source · line 2393 · 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 2415 · 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.rd.st source · line 2436 · raw

@phase:Phase -> @rd:Rd -> St

the walk at a phase, with what it has read

def decode.rd.fail source · line 2442 · raw

@rd:Rd -> St

the walk stopped on a fault, with what it has read

def decode.read.sof.of source · line 2448 · raw

@xs:List<&2, U32> -> @rd:Rd -> St

an SOF0 or SOF1 body, read with what the walk has read; its frame is replaced

def decode.take source · line 2453 · raw

@left:Nat -> @xs:List<&2, U32> -> @acc:List<&2, U32> -> Pair(List<&2, U32>, List<&2, U32>)

def decode.q.put source · line 2464 · raw

@tabs:Tabs -> @+id:U32 -> @vals:List<&2, U32> -> Tabs

def decode.q.wide source · line 2478 · raw

@xs:List<&2, U32> -> List<&2, U32>

16-bit quantisation entries, two bytes each, high byte first

def decode.q.vals source · line 2491 · 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.dqt.end source · line 2504 · raw

@ok:Bool -> @phase:Nat -> @acc:List<&2, U32> -> @rd:Rd -> St

the end of a DQT body: well formed when it ends between tables

def decode.dqt.put source · line 2519 · raw

@+id:U32 -> @vals:List<&2, U32> -> @rd:Rd -> Rd

what the walk has read, with a quantisation table put

def decode.dqt.next source · line 2525 · raw

@+byte:U32 -> @phase:Nat -> @+id:U32 -> @+wide:Bool -> @acc:List<&2, U32> -> @rd:Rd -> Dq

a DQT body after one more byte of a well-formed one: a table's Pq and Tq byte, its last entry byte, or another

def decode.read.dqt source · line 2541 · raw

@xs:List<&2, U32> -> @dq:Dq -> 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 2553 · raw

@xs:List<&2, U32> -> @+nn:U32 -> U32

def decode.huff.fits source · line 2562 · 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 2571 · 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 2576 · 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 2579 · raw

@tabs:Tabs -> @+id:U32 -> @huff:Huff -> Tabs

def decode.huff.put.ac source · line 2592 · raw

@tabs:Tabs -> @+id:U32 -> @huff:Huff -> Tabs

def decode.huff.put source · line 2605 · raw

@tabs:Tabs -> @+cls:U32 -> @+id:U32 -> @hh:Huff -> Tabs

def decode.dht.put source · line 2619 · raw

@+cls:U32 -> @+id:U32 -> @huff:Huff -> @rd:Rd -> Rd

what the walk has read, with a Huffman table put

def decode.dht.end.counts source · line 2625 · raw

@phase:Nat -> @+counts:List<&2, U32> -> @+syms:List<&2, U32> -> @rd:Rd -> St

the end of a DHT body in its counts: well formed between tables

def decode.dht.end.syms source · line 2637 · raw

@phase:Nat -> @+cls:U32 -> @+id:U32 -> @+counts:List<&2, U32> -> @+syms:List<&2, U32> -> @rd:Rd -> St

the end of a DHT body in a table's symbols: well formed for a table of no symbols, which is put

def decode.dht.end source · line 2648 · raw

@ok:Bool -> @sym:Bool -> @phase:Nat -> @+cls:U32 -> @+id:U32 -> @+counts:List<&2, U32> -> @+syms:List<&2, U32> -> @rd:Rd -> St

the end of a DHT body

def decode.dht.head source · line 2671 · raw

@+byte:U32 -> @rd:Rd -> Dh

a table's class and Th byte: class at most 1, Th at most 3

def decode.dht.count source · line 2676 · raw

@+byte:U32 -> @phase:Nat -> @+cls:U32 -> @+id:U32 -> @+counts:List<&2, U32> -> @+syms:List<&2, U32> -> @rd:Rd -> Dh

one more byte among a table's counts, or its class and Th byte

def decode.dht.sym source · line 2700 · raw

@+byte:U32 -> @phase:Nat -> @+cls:U32 -> @+id:U32 -> @+counts:List<&2, U32> -> @+syms:List<&2, U32> -> @rd:Rd -> Dh

one more byte among a table's symbols, or the next table's class and Th byte after a table of no symbols

def decode.dht.next source · line 2722 · raw

@+byte:U32 -> @sym:Bool -> @phase:Nat -> @+cls:U32 -> @+id:U32 -> @+counts:List<&2, U32> -> @+syms:List<&2, U32> -> @rd:Rd -> Dh

a DHT body after one more byte of a well-formed one

def decode.read.dht source · line 2738 · raw

@xs:List<&2, U32> -> @dh:Dh -> St

def decode.scan.rev source · line 2751 · raw

@sids:List<&2, U32> -> @td:List<&2, U32> -> @ta:List<&2, U32> -> @+ns:U32 -> Scan

def decode.sos.fail source · line 2755 · raw

@rd:Rd -> St

the walk stopped on a fault in an SOS header, which leaves no scan

def decode.sos.in source · line 2761 · raw

@+sids:List<&2, U32> -> @+td:List<&2, U32> -> @+ta:List<&2, U32> -> @+ns:U32 -> @rd:Rd -> St

the walk in the scan an SOS header read

def decode.read.sos.ok source · line 2766 · raw

@ss0:Bool -> @se63:Bool -> @ah0:Bool -> @sids:List<&2, U32> -> @td:List<&2, U32> -> @ta:List<&2, U32> -> @+ns:U32 -> @rd:Rd -> St

def decode.read.sos.end source · line 2786 · raw

@xs:List<&2, U32> -> @+tok:Bool -> @sids:List<&2, U32> -> @td:List<&2, U32> -> @ta:List<&2, U32> -> @+ns:U32 -> @rd:Rd -> St

def decode.read.sos.cs source · line 2801 · raw

@left:Nat -> @xs:List<&2, U32> -> @+tok:Bool -> @sids:List<&2, U32> -> @td:List<&2, U32> -> @ta:List<&2, U32> -> @+ns:U32 -> @rd:Rd -> St

def decode.read.sos source · line 2822 · raw

@xs:List<&2, U32> -> @rd:Rd -> St

def decode.read.dri source · line 2829 · raw

@xs:List<&2, U32> -> @rd:Rd -> St

def decode.dispatch source · line 2836 · raw

@+mark:U32 -> @xs:List<&2, U32> -> @rd:Rd -> St

def decode.between source · line 2853 · raw

@+bb:U32 -> @+lo:U32 -> @+hi:U32 -> Bool

def decode.mark.range.b source · line 2856 · raw

@sof:Bool -> @app:Bool -> @rst:Bool -> @+byte:U32 -> @rd:Rd -> St

def decode.mark.range source · line 2867 · raw

@+byte:U32 -> @rd:Rd -> St

def decode.mark source · line 2871 · raw

@+byte:U32 -> @rd:Rd -> St

def decode.step.pay0 source · line 2898 · raw

@left:Nat -> @+mark:U32 -> @rd:Rd -> St

def decode.step.len.b source · line 2905 · raw

@small:Bool -> @+mark:U32 -> @+len:U32 -> @rd:Rd -> St

def decode.step.last source · line 2912 · raw

@left:Nat -> @+bb:U32 -> @+mark:U32 -> @acc:List<&2, U32> -> @rd:Rd -> St

def decode.step.pay source · line 2919 · raw

@left:Nat -> @+bb:U32 -> @+mark:U32 -> @acc:List<&2, U32> -> @rd:Rd -> St

def decode.ent.next source · line 2928 · raw

@framed:Bool -> @+bb:U32 -> @rd:Rd -> 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 2935 · raw

@rst:Bool -> @+bb:U32 -> @rd:Rd -> St

def decode.step.ent source · line 2944 · raw

@ff:Bool -> @+bb:U32 -> @rd:Rd -> 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.ff source · line 2953 · raw

@rd:Rd -> @+byte:U32 -> St

a byte after an FF inside the entropy-coded data: a stuffed zero, a fill FF, EOI, or another marker

def decode.step.ph source · line 2966 · raw

@phase:Phase -> @+byte:U32 -> @rd:Rd -> St

def decode.step source · line 2991 · raw

@+bb:U32 -> @st:St -> St

def decode.walk source · line 2996 · raw

@xs:List<&2, U32> -> @st:St -> St

def decode.st.good source · line 3008 · raw

@st:St -> Bool

a walk that read one frame header and no error

def decode.scan.ns source · line 3013 · raw

@scan:Scan -> U32

def decode.chain.last source · line 3020 · 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 3043 · 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 3064 · raw

@ok:Bool -> @passes:List<&2, Pass> -> @frame:Frame -> @rgb:Bool -> Maybe<&2, Pic>

def decode.multi source · line 3073 · 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 3082 · raw

@phase:Phase -> @rd:Rd -> @+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 3097 · 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 3103 · raw

List<&2, U32>

the JPEG start-of-image marker, FF D8

def eoi source · line 3107 · raw

List<&2, U32>

the JPEG end-of-image marker, FF D9

def decode.facts.id source · line 3111 · 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 3126 · raw

@body:List<&2, U32> -> @kk:Nat -> U32

byte kk of a segment body, 0 past its end

def decode.facts.app source · line 3131 · 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 3149 · 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 3154 · 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 3158 · 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 3167 · 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.body source · line 3183 · raw

@left:Nat -> @+mark:U32 -> @+hi:U32 -> @+byte:U32 -> @+got:List<&2, U32> -> @+acc:U32 -> Fs

a body byte of the facts pass, of a segment of left more bytes

def decode.facts.step source · line 3195 · raw

@+byte: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 3215 · raw

@fs:Fs -> U32

the facts so far

def decode.facts.walk source · line 3222 · 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 3235 · 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 3245 · 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 3253 · 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