Skip to content

Enhance CloneAndMutate(), add Resize() overload for area and filter #1819

@IDXGI

Description

@IDXGI

Is your feature request related to a problem? Please describe

#1577
I handle relatively large images and frequently perform clone, crop, and resize operations to extract a specific area and scale it to screen size for display. The current interface CloneAndMutate has two problems:

  1. It can only copy+resize the entire image at the same time, which means that each resize operation applies to the whole image. When I only need a small or medium area to display, this can lead to performance loss.
  2. It is not possible to set the FilterType (such as Lanczos/Nearest) used during CloneAndMutate(Resize op) without changing the original image.

In ImageSharp, this could be implemented like this:

cropImage = image.Clone(
   new Configuration { PreferContiguousImageBuffers = true },
   context => context.Resize(
      width: pixelWidth,
      height: pixelHeight,
      sampler: KnownResamplers.Lanczos2,
      sourceRectangle: imageCropRect,
      targetRectangle: new Rectangle(0, 0, pixelWidth, pixelHeight),
      compand: false));

Resize() serves a lot of options. In actual testing, when only a small area needs to be extracted, the performance is better.

Describe the solution you'd like

For best performance, Resize could specify source area and target area like ImageSharp, so when use CloneAndMutate, the behavior is:
Read source Image with specified area -> process resize with specified filter -> write to copied image (target area)

This is a little flexible and complicated flow, which is ImageSharp implemented, maybe could learn the benefits.
More advanced and abstract, could support more read source->process->write copied operation, and FilterType is not only for Resize, other operations like Transform Scale also need specify filter when using CloneAndMutate, otherwise we couldn't set filter (maybe set source img FilterType can make influence?)

Describe alternatives you've considered

For small areas, first perform CloneArea() and then Resize() is better for performance, and for large areas CloneAndMutate is better. But there is no baseline which exact size to use CloneAndMutate or CloneArea+Resize for better performance. So the best solution is the one mentioned above.

Additional context

ImageMagick said is not designed for speed, maybe it not easy to do these change?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions