MogrifyRegion

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

MogrifyRegion

Post by rmabry »

Essentially, the MogrifyRegion method does bad things with regions that do not lie wholly within the image. At least if the region strays into negative x or y territory, it works improperly. This is ImageMagick 7.0.7-15 Q16 x86_64 2018-01-17 on Centos 6.9 with perl v5.10.1.

My own project produces core dumps (which is how I discovered the trouble), though in trying to write a smaller demo I didn't get core dumps, just weirdness. But that's enough.

I hope I don't make this sound more complicated than it is; a simple standalone script is at the end.

There are two images at the Imgur link below -- one before MogrifyImage is called and one after. I know they're a bit too psychedelic, but I wanted to see where the shards were flying off to, and this helped. Check out the "after" image, in which nine (pairs of) calls to MogrifyImage are made atop the "before" image, each pair being identical except for the first arguments, the region geometry. The "circle" part looks like so, for example:

Code: Select all

$image->MogrifyRegion("120x120-45+325", "draw", primitive=>"circle", points=>"60,60  0,60", fill=> ...


(The actual images here are 400x400. Notice that the above snippet tries to plant a black disk of radius 60 at the center of a 120x120 region.) Each pair of calls should draw an identical diamond atop a circle, except that 8 of them (all but the center one) go partially out of bounds (in symmetric fashion). The ones with positive out-of-bound-ness do fine (or seem to, but who knows what lurks within) -- these are the four diamond/disk figures in the lower right of the image (Center, E, SE, and S, if you like, though I'm not invoking any gravity). The center circle-diamond figure shows the whole thing, the E, SE, and S chunks are "drawn" the same way, but the region code seems to handle it. The remaining four (W, NW, N, NE) each stray into negative territory and these go way wrong, as you can see (on Imgur):

https://imgur.com/a/6CNCq

There is no Region method in PerlMagick, nor can one Set(region=>...), so MogrifyRegion seems like a workaround, but it is quite handy. In particular, you can actually Draw within the region as one would like, as opposed to what I mentioned recently, in a post titled "-region -draw". It would be nice to see it fixed. As a workaround I can add code to my project to avoid core dumping, but that's untidy.

Here's a small standalone script that shows the problem well enough. The two "shards" of circles that result in this case belong to the west and north pieces; there's a clue in there somewhere. If you move those two calls before the east and south you get no shards, but the error is still obvious.

Code: Select all

use Image::Magick;

$image = new Image::Magick;
$image->Set(size=>"200x200");
$image->ReadImage('xc:#888888'); 

# South:
$image->MogrifyRegion("100x100+50+125", 'draw', 
	fill=>"blue", primitive=>"circle", points=>"50,50 50,10");

# East:
$image->MogrifyRegion("100x100+125+50", 'draw', 
	fill=>"red", primitive=>"circle", points=>"50,50 50,10");


# North:
$image->MogrifyRegion("100x100+50-25", 'draw', 
	fill=>"green", primitive=>"circle", points=>"50,50 50,10");

# West:
$image->MogrifyRegion("100x100-25+50", 'draw', 
	fill=>"black", primitive=>"circle", points=>"50,50 50,10");


$image->Write("mogrifyRegion-some.png");

Rick
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: MogrifyRegion

Post by magick »

Thanks for the problem report. We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today. The patch will be available in the beta releases of ImageMagick @ https://www.imagemagick.org/download/beta/ by sometime tomorrow.

Note, the region in IMv7 is relative to the upper left hand corner of the source image unlike IMv6 which is relative to the image region. In IMv7, we utilize a write mask whereas in IMv6 we composite the region on the source image.

Its possible there are further issues concerning regions, feel free to post here if the beta does not behave as expected.
Post Reply