JFLib

img_core/xform.rs#

Source: lib/img_core/xform.rs

xform -- affine resampling (move / rotate / scale / skew) and 2 x 3 matrix helpers
[src/img/xform.s]

  affine(src, dst, m, filter, flags, clip) -> 0 / ERR_*                         [rs_affine]
       dst(x, y) = src sampled at m * (x + 0.5, y + 0.5) for every dst pixel inside clip
       ([x0, y0, x1, y1] exclusive, or None = all of dst); pixels outside clip are untouched
  Affine::prepare(src, dst, m, filter, flags, clip) -> Result<Affine, i32>
       validate; dst is only measured                                 [rs_affine_prepare]
  xa.rows(band, y0, y1) -> 0           dst rows y0 .. y1-1 (clipped) into a Band of dst;
                                       disjoint bands may run on several threads
                                                                         [rs_affine_rows]
  Matrix, Matrix::IDENTITY, m.apply(x, y), sincos(angle)    (gfx::xform: xf_identity,
                                       xf_apply, xf_sincos)
  mul(a, b) -> Matrix                  a * b (apply b first, then a)              [xf_mul]
  invert(m) -> Option<Matrix>          None: singular or not finite            [xf_invert]
  translate(&mut m, tx, ty)            m = m * T(tx, ty)                      [xf_translate]
  scale(&mut m, sx, sy)                m = m * S(sx, sy)                          [xf_scale]
  rotate(&mut m, angle, cx, cy)        m = m * R(angle about cx, cy)             [xf_rotate]
  CUBIC_LUT                            Catmull-Rom taps -1, 0, 1, 2 for phase p / 256 (i16,
                                       sum 16384)                            [xf_cubic_lut]

Matrix {a, b, c, d, e, f}: x' = a x + b y + c, y' = d x + e y + f.  affine maps DESTINATION
pixel centres to continuous SOURCE coordinates (source pixel (i, j) covers [i, i + 1) x
[j, j + 1)); to draw an image transformed by a forward matrix F, pass invert(F).  Angles in
radians; with y pointing down a positive angle turns clockwise on screen: R = {cos, -sin,
0, sin, cos, 0}.

Sampling (per destination pixel, xc = x + 0.5, yc = y + 0.5, all f64 in this order):
  qx = a xc + (b yc + c),  qy = d xc + (e yc + f)
  RS_NEAREST     pixel (floor(clamp(qx)), floor(clamp(qy))), copied (format converted with
                 gfx::straight premul / unpremul when src and dst formats differ)
  RS_BILINEAR    U = floor(clamp((qx - 0.5) * 256 + 0.5)) (24.8 fixed point, same for V);
                 x0 = U >> 8, phase fx = U & 255.  Phase 0 in both axes: the pixel (x0, y0)
                 is copied exactly (like nearest).  Else the working values of the 2 x 2
                 pixels (resample::to_work: premultiplied, linear light with RS_GAMMA) are
                 mixed: wx = fx * 64: T = (P00 (16384 - wx) + P10 wx + 8192) >> 14, B
                 likewise, P = (T (16384 - wy) + B wy + 8192) >> 14, then from_work.
  RS_BICUBIC     same phases, 4 x 4 Catmull-Rom taps x0 - 1 .. x0 + 2 with the weights
                 CUBIC_LUT[phase] (exact copy at phase 0); rows first:
                 R = sat16((sum w P + 8192) >> 14), P = sat16((sum w R + 8192) >> 14)
  RS_MULTISAMPLE 16 bilinear samples at xc = x + o_s, yc = y + o_t, o = 1/8, 3/8, 5/8, 7/8
                 (no exact-copy shortcut), P = (sum of the 16 sample P + 8) >> 4
  clamp(v) limits v to +-2^30 (NaN -> -2^30).  An integer translation (a = e = 1, b = d = 0,
  c and f integers below 2^30) with NEAREST / BILINEAR / BICUBIC gives exactly the copies
  above and takes a row-copy path.  Edge modes (flags bits 8..9): outside pixels are
  transparent 0 (XF_TRANSPARENT), the nearest edge pixel (XF_CLAMP), j mod n (XF_WRAP) or
  mirrored with period 2n, edge pixels repeated (XF_REFLECT).

Threads: no global state; rows() allocates about 17 KB of scratch per call.  The 2 x 2 /
4 x 4 mixing keeps the assembly's SSE2 layout (one output per register).

Re-exports: gfx::xform::{Matrix, sincos}

XF_TRANSPARENT: u32 = 0  // outside the source = transparent black
XF_CLAMP: u32 = 1  // repeat the edge pixels
XF_WRAP: u32 = 2  // tile
XF_REFLECT: u32 = 3  // mirror (edge pixels duplicated)
XF_EDGE_SHIFT: u32 = 8
XF_EDGE_MASK: u32 = 0x300
  • fn mul(a: &Matrix, b: &Matrix) -> Matrix a * b (b applied first) [xf_mul]

  • fn invert(m: &Matrix) -> Option<Matrix> the inverse of m; None when the determinant is 0 or not finite [xf_invert]

  • fn translate(m: &mut Matrix, tx: f64, ty: f64) m = m * T(tx, ty): c += a tx + b ty, f += d tx + e ty [xf_translate]

  • fn scale(m: &mut Matrix, sx: f64, sy: f64) m = m * S(sx, sy) [xf_scale]

  • fn rotate(m: &mut Matrix, angle: f64, cx: f64, cy: f64) m = m * T(cx, cy) * R(angle) * T(-cx, -cy) [xf_rotate]

  • struct Affine<'a> a prepared affine resampling of src into a destination of the measured size (the XFORM record); read-only while rows() runs

impl<'a> Affine<'a>

  • fn prepare(src: &'a Surface, dst: &Surface, m: &Matrix, filter: u32, flags: u32, clip: Option<[i32; 4]>) -> Result<Affine<'a>, i32> validate: ERR_UNSUPPORTED (filter, formats), ERR_DIMS (sizes outside 1 .. 65535) [rs_affine_prepare]

  • fn prepare_projective(src: &'a Surface, dst: &Surface, m: &[f64; 9], filter: u32, flags: u32, clip: Option<[i32; 4]>) -> Result<Affine<'a>, i32> Prepare an inverse 3x3 homography, in row-major order. Uses the same alpha-aware filters and edge modes as affine resampling; no intermediate image is allocated.

  • fn rows(&self, band: &mut Band, y0: i32, y1: i32) -> i32 dst rows y0 .. y1-1 inside the clip into band, which must hold them [rs_affine_rows]

  • fn affine(src: &Surface, dst: &mut Surface, m: &Matrix, filter: u32, flags: u32, clip: Option<[i32; 4]>) -> i32 dst = src resampled through m inside clip (None = all of dst) [rs_affine]

  • static CUBIC_LUT: [[i16; 4]; 256] Catmull-Rom taps -1, 0, 1, 2 for phase p / 256 (i16, sum 16384) [xf_cubic_lut]

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