def jpeg.frame.start
gralha-jpeg-decode@0.1.0.1 · jpeg.bend:228
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
228def jpeg.frame.start(+src: Bin.Bytes, +at: U32, +end: U32, +frame: JPEGFrame, +kind: U32) -> JPEGFrame:
229 JPEGFrame{w, h, hm, vm, progressive, components, quants, huffs, restart, adobe, valid} = frame
230 +ww = Bin.bytes.be(src, (at + 3 : U32), 2n, 0)
231 +hh = Bin.bytes.be(src, U32.inc(at), 2n, 0)
232 +count = Bin.bytes.get(src, (at + 5 : U32))
233 +cs = jpeg.components.read(U32.to_nat(U.choose(U32, U32.is_le(count, 4), count, 0)), src, (at + 6 : U32))
234 +mh = jpeg.components.max(cs, False{}, 1)
235 +mv = jpeg.components.max(cs, True{}, 1)
236 +ok = valid && U32.is_eq(w, 0) && U32.is_eq(Bin.bytes.get(src, at), 8) && U32.is_gt(ww, 0) && U32.is_le(ww, 2048) && U32.is_gt(hh, 0) && U32.is_le(hh, 2048) && (U32.is_eq(count, 1) || U32.is_eq(count, 3) || U32.is_eq(count, 4)) && U32.is_eq((at + 6 + count * 3 : U32), end) && jpeg.components.valid(cs) && U32.is_le(jpeg.components.units(cs), 10)
237 U.branch(JPEGFrame, ok, _ => JPEGFrame{ww, hh, mh, mv, U32.is_eq(kind, 194), jpeg.components.allocate(cs, jpeg.ceil(ww, (8 * mh : U32)), jpeg.ceil(hh, (8 * mv : U32))), quants, huffs, restart, adobe, ok}, _ => jpeg.frame.invalid(frame))