resize_and_pad

Function resize_and_pad 

Source
pub fn resize_and_pad(
    img: &RgbImage,
    vcfg: &VisionConfig,
    operator_max: Option<usize>,
) -> RgbImage
Expand description

Fit img inside the smart_resize canvas preserving aspect, then zero-pad right/bottom to the canvas exactly. Returns the padded canvas.

★ VERIFIED against the reference’s own pixel_values (2026-09-22), not assumed. Two properties were read straight out of the golden for image c (640x360 -> a 644x364 canvas), and both had a plausible alternative:

  1. The pad is BLACK PIXELS, i.e. it precedes normalization. Padded cells in the golden hold exactly (0 - mean)/std = (-1.7926, -1.7520, -1.4801), not 0.0. REFERENCE.md §5 recorded tvF.pad(..., fill=0) without pinning it relative to rescale/normalize; this settles it.
  2. The fit NEVER UPSCALES. The golden’s content occupies rows 0..360 and columns 0..640 — the source dimensions, unresampled — with the remaining 4 rows and 4 columns padded. An aspect-fit that scaled up to fill the canvas (scale 1.00625 here) would have produced a 362x644 content box and only 2 pad rows. Every sampled pixel of the golden matches the raw PNG normalized in place, to 4e-4 (float32 storage).

tests/glm_vision_preprocess_pin.rs pins both against the fixtures.