Affine matrix from rotation and translation

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
swami
Posts: 5
Joined: 2010-10-05T10:44:07-07:00
Authentication code: 8675308

Affine matrix from rotation and translation

Post by swami »

Hi,

I am using Magick++ library version 6.6.3 on Windows7. The Magick++ tutorial contains the following lines:

A very hilarious example is the DrawableRotation and DrawableTranslation objects that require 'angle/2' (instead of 'angle') and 'displacement/2' (instead of 'displacement') as arguments. If this could be regarded as some hacker joke made by one of the library implementers, more serious problems seem to exist: for example, the above two coordinate system transformations don't work together at all (they give nonsensical effects when drawing objects on a canvas).

According to this, there are two bugs:
1. Combining DrawableRotation and DrawableTranslation gives incorrect results.
2. The arguments specified to these objects need to be angle/2 (instead of angle) and displacement/2 (instead of displacement).

Although the document refers to library version 6.2.5.4, both these bugs still exist on version 6.6.3. I have discovered the cause of both these bugs as described below:

1. Combining multiple affine transforms is achieved by multiplying the affine matrices for each transform. The order of transformations is important since matrix multiplication is not commutative. For example, if first transform is a rotation R, and second is translation T, then the combined transform should be T*R. However, the code is doing R*T. Look at magick\draw.c, function DrawImage. The matrix multiplication is doing current * affine, where current is the existing affine matrix, and affine is the new matrix. The correct multiplication should be affine * current. Same situation exists also in wand\draw-wand.c, function AdjustAffine.

2. The above affine transformations are being applied twice - first time by calling AdjustAffine each time a drawable transformation is found in the drawing list, and second time in DrawImage mentioned above. That's why the arguments given to the drawable objects have to be half of their correct value.

I hope these bugs will get fixed in the next version of the library.

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

Re: Affine matrix from rotation and translation

Post by magick »

Can you post some code snippets that illustrates the bug? Two to three examples would be useful so we can test it against any patches we apply.
swami
Posts: 5
Joined: 2010-10-05T10:44:07-07:00
Authentication code: 8675308

Re: Affine matrix from rotation and translation

Post by swami »

Here's a snippet from my code. This is supposed to draw an ellipse at the center of the image, rotated by 45 degrees. It achieves that by applying 3 transformation - translate the center of the ellipse to the origin of the image, rotate the ellipse by 45 degrees, then translate the center of the ellipse back to the original position. More examples can be created on similar lines.

Code: Select all

Magick::Image img("100x100", "white");
Magick::Coordinate center(50, 50);
Magick::DrawableEllipse ell(center.x(), center.y(), 30, 15, 0, 360);

std::list<Magick::Drawable> drawList;
drawList.push_back(Magick::DrawableStrokeColor("red")); // Outline color
drawList.push_back(Magick::DrawableStrokeWidth("2")); // Stroke width
drawList.push_back(Magick::DrawableFillOpacity(0));
drawList.push_back(Magick::DrawableTranslation(-center.x(), -center.y()));
drawList.push_back(Magick::DrawableRotation(45));
drawList.push_back(Magick::DrawableTranslation(center.x(), center.y()));
drawList.push_back(ell);
img.draw(drawList);
Hope this helps. :)
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Affine matrix from rotation and translation

Post by magick »

Try ImageMagick 6.6.5-0 Beta (ftp://ftp.imagemagick.org/pub/ImageMagick/beta). It renders a red ellipse at 45 degrees on a white background.
swami
Posts: 5
Joined: 2010-10-05T10:44:07-07:00
Authentication code: 8675308

Re: Affine matrix from rotation and translation

Post by swami »

magick wrote:Try ImageMagick 6.6.5-0 Beta (ftp://ftp.imagemagick.org/pub/ImageMagick/beta). It renders a red ellipse at 45 degrees on a white background.
Tried it out, and it works fine. Both the bugs are fixed. Thanks for taking care of it so promptly! :)
tsr001
Posts: 1
Joined: 2011-06-21T07:00:49-07:00
Authentication code: 8675308

Re: Affine matrix from rotation and translation

Post by tsr001 »

I'm having the same problem w/6.7.0-8. Code snippet below does not yield a rotated ellipse. Any help would be greatly appreciated.


// Draw a rotated ellipse
Image test_image2(Geometry(cols,rows),"white");
test_image2.strokeColor("black"); // Outline color
test_image2.fillColor("none"); // Fill color
test_image2.strokeWidth(5);
//test_image2.draw(DrawableTranslation(-cols/2,-rows/2));
test_image2.draw(DrawableRotation(45));
//test_image2.draw(DrawableTranslation(cols/2,rows/2));
test_image2.draw(DrawableEllipse(cols/2,rows/2,50,20,0,360));
test_image2.write(argv[2]);
Post Reply