public int lpad, rpad, tpad, bpad;
private static ImageLayout layoutHelper(ImageLayout layout,
RenderedImage source,
AffineTransform forward_tr) {
ImageLayout newLayout;
if (layout != null) {
newLayout = (ImageLayout)layout.clone();
} else {
newLayout = new ImageLayout();
}
//
// Get sx0,sy0 coordinates & width & height of the source.
//
float sx0 = (float)source.getMinX();
float sy0 = (float)source.getMinY();
float sw = (float)source.getWidth();
float sh = (float)source.getHeight();
//
// The 4 points (clockwise order) are
// (sx0, sy0), (sx0+sw, sy0)
// (sx0, sy0+sh), (sx0+sw, sy0+sh)
//
Point2D[] pts = new Point2D[4];
pts[0] = new Point2D.Float(sx0, sy0);
pts[1] = new Point2D.Float((sx0+sw), sy0);
pts[2] = new Point2D.Float((sx0+sw), (sy0+sh));
pts[3] = new Point2D.Float(sx0, (sy0+sh));
// Forward map
forward_tr.transform(pts, 0, pts, 0, 4);
float dx0 = Float.MAX_VALUE;
float dy0 = Float.MAX_VALUE;
float dx1 = -Float.MAX_VALUE;
float dy1 = -Float.MAX_VALUE;
for (int i = 0; i < 4; i++) {
float px = (float)pts[i].getX();
float py = (float)pts[i].getY();
dx0 = Math.min(dx0, px);
dy0 = Math.min(dy0, py);
dx1 = Math.max(dx1, px);
dy1 = Math.max(dy1, py);
}
//
// Get the width & height of the resulting bounding box.
// This is set on the layout
//
int lw = (int)(dx1 - dx0);
int lh = (int)(dy1 - dy0);
//
// Set the starting integral coordinate
// with the following criterion.
// If it's greater than 0.5, set it to the next integral value (ceil)
// else set it to the integral value (floor).
//
int lx0, ly0;
int i_dx0 = (int)Math.floor(dx0);
if (Math.abs(dx0 - i_dx0) <= 0.5) {
lx0 = i_dx0;
} else {
lx0 = (int) Math.ceil(dx0);
}
int i_dy0 = (int)Math.floor(dy0);
if (Math.abs(dy0 - i_dy0) <= 0.5) {
ly0 = i_dy0;
} else {
ly0 = (int) Math.ceil(dy0);
}
//
// Create the layout
//
newLayout.setMinX(lx0);
newLayout.setMinY(ly0);
newLayout.setWidth(lw);
newLayout.setHeight(lh);
return newLayout;
}