def jpeg.sample

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

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

670def jpeg.sample(component: JPEGComponent, +w: U32, +h: U32, +hm: U32, +vm: U32, +x: U32, +y: U32) -> F32:
671  JPEGComponent{id, ch, cv, quant, dc, ac, predictor, bw, bh, coefficients, samples} = component
672  +hh = ch
673  +vv = cv
674  +data = samples
675  +stride = (bw * 8 : U32)
676  +width = jpeg.ceil((w * hh : U32), hm)
677  +height = jpeg.ceil((h * vv : U32), vm)
678  # libjpeg's horizontally doubled narrow planes use box reconstruction.
679  +box = U32.is_eq((hh * 2 : U32), hm) && U32.is_le(width, 2) && (U32.is_eq(vv, vm) || U32.is_eq((vv * 2 : U32), vm))
680  +px = U.choose(F32, box, U32.to_f32((x * hh / hm : U32)), U.clamp(((U32.to_f32(x) + 0.5) * U32.to_f32(hh) / U32.to_f32(hm) - 0.5 : F32), 0.0, U32.to_f32((width - 1 : U32))))
681  +py = U.choose(F32, box, U32.to_f32((y * vv / vm : U32)), U.clamp(((U32.to_f32(y) + 0.5) * U32.to_f32(vv) / U32.to_f32(vm) - 0.5 : F32), 0.0, U32.to_f32((height - 1 : U32))))
682  +x0 = F32.to_u32(F32.floor(px))
683  +y0 = F32.to_u32(F32.floor(py))
684  +x1 = U.choose(U32, U32.is_lt(U32.inc(x0), width), U32.inc(x0), x0)
685  +y1 = U.choose(U32, U32.is_lt(U32.inc(y0), height), U32.inc(y0), y0)
686  +fx = (px - U32.to_f32(x0) : F32)
687  +fy = (py - U32.to_f32(y0) : F32)
688  (jpeg.sample.byte(data, stride, x0, y0) * (1.0 - fx) * (1.0 - fy) + jpeg.sample.byte(data, stride, x1, y0) * fx * (1.0 - fy) + jpeg.sample.byte(data, stride, x0, y1) * (1.0 - fx) * fy + jpeg.sample.byte(data, stride, x1, y1) * fx * fy : F32)

Uses (23)

Used by (1)