def jpeg.scan.decode

gralha-jpeg-decode@0.1.0.1 · jpeg.bend:559

import gralha-jpeg-decode@0.1.0.1/jpeg.bend as Jpeg
import Base
import gralha-text-scan@0.1.0.1/scan.bend as U
import gralha-bytes@0.1.0.1/bytes.bend as Bin

559def jpeg.scan.decode(+src: Bin.Bytes, +at: U32, +end: U32, +frame: JPEGFrame) -> JPEGDecoded:
560  JPEGFrame{w, h, hm, vm, progressive, components, quants, huffs, restart, adobe, valid} = frame
561  +count = Bin.bytes.get(src, at)
562  +cs = jpeg.scan.select(U32.to_nat(U.choose(U32, U32.is_le(count, 4), count, 0)), src, U32.inc(at), components)
563  +single = U32.is_eq(count, 1)
564  +cols = U.choose(U32, single, jpeg.scan.extent(cs, w, h, hm, vm, False{}), jpeg.ceil(w, (hm * 8 : U32)))
565  +rows = U.choose(U32, single, jpeg.scan.extent(cs, w, h, hm, vm, True{}), jpeg.ceil(h, (vm * 8 : U32)))
566  +ss = Bin.bytes.get(src, (end - 3 : U32))
567  +se = Bin.bytes.get(src, (end - 2 : U32))
568  +approx = Bin.bytes.get(src, (end - 1 : U32))
569  +ah = (approx / 16 : U32)
570  +al = (approx % 16 : U32)
571  +sequential = U32.is_eq(ss, 0) && U32.is_eq(se, 63) && U32.is_eq(approx, 0)
572  +spectral = U32.is_le(ss, se) && U32.is_le(se, 63) && U.choose(Bool, U32.is_eq(ss, 0), U32.is_eq(se, 0), single) && U32.is_le(al, 13) && (U32.is_eq(ah, 0) || U32.is_eq(ah, U32.inc(al)))
573  +ok = valid && U.choose(Bool, progressive, spectral, sequential) && U32.is_gt(w, 0) && U32.is_gt(count, 0) && U32.is_le(count, 4) && U32.is_eq((at + 1 + count * 2 + 3 : U32), end)
574  U.branch(JPEGDecoded, ok, _ => U.branch(JPEGDecoded, jpeg.scan.ready(cs, ss, se, ah, quants), _ => jpeg.scan.finish(jpeg.mcus(U32.to_nat((cols * rows : U32)), 0, cols, single, huffs, restart, JPEGScan{jpeg.scan.histories(cs, ss, se, al), JPEGStream{src, end, 0, 0, JPEGSpectral{ss, se, ah, al, 0}, True{}}}), frame), _ => JPEGDecoded{jpeg.frame.invalid(frame), end}), _ => JPEGDecoded{jpeg.frame.invalid(frame), end})

Uses (31)

Used by (1)