Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
IDXGI opened this issue Apr 2, 2025 · 2 comments
Open

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

IDXGI opened this issue Apr 2, 2025 · 2 comments
Milestone

Comments

@IDXGI
Copy link

IDXGI commented Apr 2, 2025

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?

@dlemstra
Copy link
Owner

dlemstra commented Apr 5, 2025

I pushed a patch yesterday to allow specifying the filter type as an argument for the resize method. This will become available in the next release.

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.

Maybe I should reconsider renaming the CloneAndMutate in the next major release. This method will not create a copy of the image but only a resized copy. This is done to avoid requiring a copy first and then resizing the image, but you probably only want to use this method when you want to create multiple resized images from the same input image.

And resizing part of the image to a smaller size is a specific use case and I haven't seen anyone else talking about this feature. This is currently not possible with ImageMagick and I also don't think we will be going to add that anytime soon.

@dlemstra dlemstra added this to the 14.7.0 milestone Apr 5, 2025
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

No branches or pull requests

3 participants
@dlemstra @IDXGI and others