JFLib

img_core/yuv.rs#

Source: lib/img_core/yuv.rs

yuv -- YUV -> RGB conversion for HEIC / AVIF (u16 sample planes)       [src/img/yuv.s]

  coef(yp) -> YuvCoef / ERR_UNSUPPORTED      the fixed-point gains (yuv_coef)
  to_argb(yp, sf, dx, dy) -> 0 / ERR_*       YUV image into sf at (dx, dy) (clipped), opaque
  alpha(yp, sf, dx, dy) -> 0                 replace the alpha bytes with yp's Y plane
  prem_clamp(sf)                             limit R, G, B of every pixel to its alpha
  argb_to_yuv() -> ERR_UNSUPPORTED           the inverse (not implemented, as in the assembly)

Conversion (tools/heic_check.py has the same model): the range maps Y to Y' = (Y - yoff) *
255 / ymax and chroma to C' = (C - coff) * 255 / cmax (full range: yoff 0, ymax = cmax =
2^bits - 1, coff 2^(bits - 1); limited: yoff 16 << (bits - 8), ymax 219 << (bits - 8), coff
128 << (bits - 8), cmax 224 << (bits - 8); identity uses the luma range for all three).
YCbCr: R = Y' + (2 - 2Kr) Cr', G = Y' - 2Kb(1 - Kb) / Kg Cb' - 2Kr(1 - Kr) / Kg Cr', B = Y'
+ (2 - 2Kb) Cb'; YCgCo: R = Y' - Cg' + Co', G = Y' + Cg', B = Y' - Cg' - Co'; identity:
G = Y', B = U', R = V'.  The gains are computed in double precision (fixed order of
operations) and rounded once: Ky = floor(255 / ymax * 2^32 + 0.5), Kc = +-floor(255 / cmax
* coef * 2^28 + 0.5).

Chroma upsampling (linear, weights summing to 4 per direction, so upsampled chroma carries
a factor 16): horizontally co-sited (location types 0 / 2 / 4: even x = 4 c[x/2], odd x =
2 c[(x-1)/2] + 2 c[(x+1)/2]) or centred (1 / 3 / 5: 3 c[k] + c[k -+ 1], k = x >> 1);
vertically centred (types 0 / 1 / 4 / 5) or co-sited (2 / 3), the same way; neighbours
clamp at the plane edge.  YPF_NEAREST: c[x / subw][y / subh].

The assembly converts bands of 32 rows on the worker pool; img_core has no thread pool
(no plat dependency), so the bands run one after the other here -- every band depends only
on its rows, the bytes are the same.
YPF_NEAREST: u32 = 1  // nearest-neighbour chroma upsampling (default: linear)
  • struct YuvPlanes<'a> a YUV image (YP_*): u16 samples (also for 8-bit content), strides in samples; the chroma planes are ceil(w / subw) x ceil(h / subh) samples

    • pl: [&'a [u16]; 3] -- Y, U (Cb / Cg / B), V (Cr / Co / R); U empty: monochrome
    • st: [u32; 3]
    • w: u32
    • h: u32
    • subw: u32 -- horizontal chroma subsampling 1 / 2
    • subh: u32 -- vertical chroma subsampling 1 / 2
    • bits: u32 -- luma bit depth 8..16
    • bitsc: u32 -- chroma bit depth 8..16
    • cicp: u32 -- primaries | transfer << 8 | matrix << 16 | full range << 24
    • flags: u32 -- YPF_*
    • chloc: u32 -- chroma sample location type 0..5 (HEVC VUI / H.273; 0 default)
  • struct YuvCoef the gains of coef (YK): out = clamp((y' * Ky + u' * Ku + v' * Kv + 2^31) >> 32) with y' = Y - yoff (Ky Q32) and u' / v' = 16 * upsampled chroma - coff (Q28)

    • r: [i64; 3] -- Ky, Ku, Kv of red
    • g: [i64; 3]
    • b: [i64; 3]
    • yoff: i64 -- luma offset
    • coff: i64 -- 16 * chroma offset
    • ky: i64 -- Q32 luma gain (monochrome: R = G = B)
  • fn coef(yp: &YuvPlanes) -> Result<YuvCoef, i32> yuv_coef: the fixed-point gains of yp's matrix, range and bit depths

  • fn to_argb(yp: &YuvPlanes, sf: &mut Surface, dx: u32, dy: u32) -> i32 yuv_to_argb: convert the YUV image into sf at (dx, dy) (clipped to sf) as opaque BGRA (alpha 255; alpha replaces it) -> 0 / ERR_UNSUPPORTED

  • fn alpha(yp: &YuvPlanes, sf: &mut Surface, dx: u32, dy: u32) -> i32 yuv_alpha: replace the alpha bytes of sf at (dx, dy) (clipped) with yp's Y plane (bit depth bits, always full range: a = floor(Y * 255 / (2^bits - 1) + 1/2) in Q32) -> 0

  • fn prem_clamp(sf: &mut Surface) yuv_prem_clamp: limit R, G, B of every pixel to its alpha (premultiplied sources: the result is valid FMT_PARGB)

  • fn argb_to_yuv() -> i32 argb_to_yuv: the inverse, for future encoders (not implemented yet)

Source: docs/API.md in the JFLib repository.