I faced some discrepancy between transformations made with help of AffineTransform and AffineTransformOp .
So, this is the way to reproduce it: let's say we have black image 512x512 pixels, I am placing some pixel to (162, 216) point on this image, then construct AffineTransform and with help of AffineTransformOp do transform over this image.
From other side - I have one POI (point of interest) with same coordinates as pixel above, but it stored separately in my application. When I do same AffineTransform to this POI - the result will not match.
Testcase to reproduce it below:
package dp.math.image;import org.testng.annotations.Test;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import static org.junit.Assert.assertEquals;
public class ImageTransformerTest2 {
@Test
public void testAffineTransform() throws InterruptedException {
int L = 512;
int xPos = 162; int yPos = 216;
int color = 0x55667788;
BufferedImage src = new BufferedImage(L, L, BufferedImage.TYPE_INT_ARGB);
src.setRGB(xPos, yPos, color);
AffineTransform at = new AffineTransform();
at.translate(50, 100);
at.rotate(-Math.PI / 2, L / 2 + 0.5, L / 2 + 0.5);
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage dst = new BufferedImage(L, L, BufferedImage.TYPE_INT_ARGB);
op.filter(src, dst);
Point2D srcPoint2D = new Point2D.Float(xPos, yPos);
Point2D dstPoint2D = at.transform(srcPoint2D, null);
assertEquals(color, dst.getRGB(266, 450));
assertEquals(266, (int) Math.round(dstPoint2D.getX()));
assertEquals(450, (int) Math.round(dstPoint2D.getY()));
}
}
this test will fail, since dstPoint2D have coordinates (266.0, 451.0), but not (266.0, 450.0) as expected.
same code with main() method
package dp.math.image;import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
public class ImageTransformerTest2 {
public static void main(String[] args) {
int L = 512;
int xPos = 162; int yPos = 216;
int color = 0x55667788;
BufferedImage src = new BufferedImage(L, L, BufferedImage.TYPE_INT_ARGB);
src.setRGB(xPos, yPos, color);
AffineTransform at = new AffineTransform();
at.translate(50, 100);
at.rotate(-Math.PI / 2, L / 2 + 0.5, L / 2 + 0.5);
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage dst = new BufferedImage(L, L, BufferedImage.TYPE_INT_ARGB);
op.filter(src, dst);
Point2D srcPoint2D = new Point2D.Float(xPos, yPos);
Point2D dstPoint2D = at.transform(srcPoint2D, null);
// check transformation of (162, 216) to (266, 450) - ensure that pixel is on it's place;
System.out.println("color from DST image = " + dst.getRGB(266, 450));
// do same transformation for Point2D
System.out.println("point2D result = " + dstPoint2D.toString());
}
}