Skip to content

Add FilterType::Area for area-averaging resize - #3156

Open
varunswaika wants to merge 1 commit into
image-rs:mainfrom
varunswaika:fix-1941
Open

varunswaika wants to merge 1 commit into
image-rs:mainfrom
varunswaika:fix-1941

Conversation

@varunswaika

Copy link
Copy Markdown

Closes #1941.

I used an AI tool to help write the code; I've reviewed and tested it.

The details below are AI-written, quoted per the LLM policy:

What: adds FilterType::Area. Each output pixel is the mean of the input
pixels it covers, weighted by how much of each pixel it covers. Modelled
on OpenCV's INTER_AREA. Not compared against OpenCV itself.

Why: no existing filter does true area averaging. thumbnail averages
whole blocks: 3 to 2 pixels of [0, 90, 180] gives [45, 180]. It also
has no FilterType.

How:

  • imageops::resize builds a filter per axis, since the weights depend on
    the scale ratio. The kernel returns the overlap between an input pixel
    and the output pixel's span.
  • Area weights are never negative, so the output stays in the input's
    range. The final clamp of float output to [0, 1] is skipped for Area,
    so HDR values above 1 are kept. Other filters are unchanged.
  • DynamicImage::resize* uses pic-scale-safe (0.1.12). Its ResamplingFunction::Area
    does not weight partly covered pixels when downscaling by a non-integer
    factor: 3 to 2 pixels of [0, 90, 180] gives [45, 135], not
    [30, 150]. So Area goes through imageops::resize instead, still
    with alpha premultiplied.
  • Targets main (the 1.0 line). Adding a variant breaks exhaustive
    matches, so it is listed under Version Next in CHANGES.md.

Tests:

  • New resize_area (generic) and area (DynamicImage) tests: a 2x
    downscale, a 1.5x downscale and a 2 to 3 upscale, on rows and columns,
    plus an f32 case with values above 1. With a plain box kernel and
    pic-scale-safe's Area, the 1.5x case fails on both paths.
  • Area added to the constant-image and transparency tests.
  • cargo test (default features), cargo test --no-default-features --lib,
    cargo clippy --all-targets --features serde -- -D warnings (stable
    1.100; --all-features did not build locally because of rav1e, and CI
    runs clippy on nightly), cargo fmt --check.

Questions for maintainers:

  • Should FilterType become #[non_exhaustive] at the same time?
  • The generic path is slower than pic-scale-safe. Fixing the weights
    upstream in pic-scale-safe would let Area use the fast path.

Overlaps: #3071 adds a Display impl with an exhaustive match on
FilterType; whichever merges second needs an Area arm. #2937 also
edits vertical_sample.

cc @MeherabHS, who offered a PR for this on 19 November.

Each output pixel is the mean of the input pixels it covers, weighted by
how much of each pixel it covers. Modelled on OpenCV's INTER_AREA.

The generic resize uses a per-axis filter with exact coverage weights.
Area weights are never negative, so float output is not clamped to
[0, 1] for this filter.

pic-scale-safe's Area filter gives whole input pixels full weight when
downscaling by a non-integer factor, so DynamicImage::resize* routes
Area through the generic path.

Closes image-rs#1941

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing resize option INTER_AREA

1 participant