Magick.NET icon indicating copy to clipboard operation
Magick.NET copied to clipboard

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

Open IDXGI opened this issue 8 months ago • 2 comments

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?

IDXGI avatar Apr 02 '25 16:04 IDXGI

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 avatar Apr 05 '25 15:04 dlemstra