Page 1 of 1

Magick++: Why does image.crop() not respect the new image origin after rotation?

Posted: 2015-09-25T09:29:20-07:00
by H2SO4
Hi,

I am trying to understand why image.crop() does not seem to respect the new origin which should be one of the effects of an image.rotate().

Platform: ImageMagick 6.9.2 Q16 x86 DLL on a Win10 x64 workstation. Code compiled with Visual Studio 2015.

In the sample below, I am creating a 141x141 blue square, then rotating the image through 45 degrees to arrive at "AfterRotation.JPG" which is 200x200. If I then crop() the image using "Geometry(100, 100, 0, 0)" the outcome is not what I expect. "AfterCrop.JPG" has the anticipated 100x100 dimensions, but instead of starting at (0, 0) of the rotated image its definition of the origin seems to correspond to the pre-rotation coordinate system. I do not understand that apparent discrepancy.

Code: Select all

#include <string> 
#include <Magick++.h> 
using namespace std;
using namespace Magick;
#pragma comment( lib, "CORE_RL_Magick++_" )	

void main() {
	InitializeMagick("");
	Image image(Geometry("141x141"), Color("blue"));
	image.rotate(45);
	image.write("AfterRotate.jpg");
	image.crop(Geometry(100, 100, 0, 0));
	image.write("AfterCrop.jpg");
}
When the code is run, this is what "AfterRotate.JPG" looks like:

Image

I expect "AfterCrop.JPG" to start at the origin (upper-left corner) of "AfterRotate.JPG". In other words, I expect "AfterCrop.JPG" to be the top-left quadrant of the "AfterRotate.JPG" image. Instead, "AfterCrop.JPG" looks like this:

Image

Even though it is clearly operating on post-rotation buffer contents (the square edges are at a 45 degree angle), crop()'s definition of the origin does not correspond to the top-left corner of "AfterRotation.JPG".

What am I doing wrong?

Thanks in advance for all info and assistance.

Re: Magick++: Why does image.crop() not respect the new image origin after rotation?

Posted: 2015-09-25T09:50:45-07:00
by snibgo
Rotate creates image offsets. AfterRotate.jpg does not contain image offsets. You are not cropping AfterRotate.jpg, but the in-memory structure that was used to make AfterRotate.jpg. That structure does contain offsets.

At the command level, use "+repage" to remove offsets. Sorry, I don't know how this is done in Magick++.

Re: Magick++: Why does image.crop() not respect the new image origin after rotation?

Posted: 2015-09-25T14:35:49-07:00
by dlemstra
It's called repage:

Code: Select all

image.crop(Geometry(100, 100, 0, 0));
image.repage();

Re: Magick++: Why does image.crop() not respect the new image origin after rotation?

Posted: 2015-09-25T18:34:48-07:00
by H2SO4
Thank you both, that works. I figured I was missing something simple.