Page 1 of 1

Efficiently crop and copy an image?

Posted: 2010-04-17T14:35:45-07:00
by anotherprogrammer123
Hi,

I am using Magick++ and was wondering what is the most efficient way to crop a Magick::Image and store this cropped region in a new Magick::Image.
The code I am currently using is below, but I think it is inefficient because the whole image is copied:

Code: Select all

Magick::Image img("myImage.jpg");
Magick::Geometry cropRegion(0, 0, 100, 100);
//We need to use a temporary object because "crop" modifies the original image
Magick::Image temp = img;
temp.crop(cropRegion);
temp.display();
Thanks in advance.

Re: Efficiently crop and copy an image?

Posted: 2010-04-17T14:38:49-07:00
by magick
The most efficient crop would be:
  • Magick::Image img("myImage.jpg[100x100+0+0]");

Re: Efficiently crop and copy an image?

Posted: 2010-04-17T14:46:06-07:00
by anotherprogrammer123
Thanks for the quick response! However, it will not work in my particular case. Sorry for not being more clear in the first post.

The image I am using will have crop called to it potentially many times. Also, I will not have access to the file name.
The function is embedded inside another class as such:

Code: Select all

Magick::Image image::crop(int width, int length)
{
    Magick::Geometry cropRegion(0, 0, width, length);
    //We need to use a temporary object because "crop" modifies the original image
    Magick::Image temp = this->img;
    temp.crop(cropRegion);
    return temp;
}

Re: Efficiently crop and copy an image?

Posted: 2010-04-20T12:47:49-07:00
by anotherprogrammer123
*bump*

Re: Efficiently crop and copy an image?

Posted: 2010-04-20T14:52:31-07:00
by magick
The crop() method is the proper way to crop an image. ImageMagick uses reference counted image objects so the source image pixels are not replicated to a new pixel cache. Instead the crop image is created and the source pixels are copied to the crop image pixel container. We suspect you cannot get any more efficient than that other than perhaps using some other API.

Re: Efficiently crop and copy an image?

Posted: 2010-04-20T19:28:44-07:00
by anotherprogrammer123
Okay, thanks for the responses!