/**
* Gets the bounding box for the output of <code>TranslateOpImage</code>.
* This method satisfies the implementation of CRIF.
*/
public Rectangle2D getBounds2D(ParameterBlock paramBlock) {
RenderableImage source = paramBlock.getRenderableSource(0);
float x_center = paramBlock.getFloatParameter(0);
float y_center = paramBlock.getFloatParameter(1);
float angle = paramBlock.getFloatParameter(2);
Interpolation interp = (Interpolation)paramBlock.getObjectParameter(3);
//
// Convert angle to degrees (within some precision) given PI's
// transcendantal nature. All this, to check if we can call
// simpler methods like Copy or Transpose for certain angles
// viz., 0, 90, 180, 270, 360, 450, .....
//
int dangle = 0;
double tmp_angle = 180.0F * angle / Math.PI;
double rnd_angle = Math.round(tmp_angle);
if (Math.abs(rnd_angle - tmp_angle) < 0.0001) {
dangle = (int) rnd_angle;
} else {
dangle = (int) tmp_angle;
}
//
// It's a copy if angle is 0 degrees or multiple of 360 degrees
//
if (dangle % 360 == 0) {
return new Rectangle2D.Float(source.getMinX(),
source.getMinY(),
source.getWidth(),
source.getHeight());
}
//
// It's a transpose if angle is mutiple of 270, 180, 90 degrees
//
float x0 = (float)source.getMinX();
float y0 = (float)source.getMinY();
float s_width = (float)source.getWidth();
float s_height = (float)source.getHeight();
float x1 = x0 + s_width - 1;
float y1 = y0 + s_height - 1;
float tx0 = 0;
float ty0 = 0;
float tx1 = 0;
float ty1 = 0;
if (dangle % 270 == 0) {
if (dangle < 0) {
// -270 degrees
tx0 = s_height - y1 - 1;
ty0 = x0;
tx1 = s_height - y0 - 1;
ty1 = x1;
return new Rectangle2D.Float(tx0,
ty0,
tx1 - tx0 + 1,
ty1 - ty0 + 1);
} else {
// 270 degrees
tx0 = y0;
ty0 = s_width - x1 - 1;
tx1 = y1;
ty1 = s_width - x0 - 1;
return new Rectangle2D.Float(tx0,
ty0,
tx1 - tx0 + 1,
ty1 - ty0 + 1);
}
}
if (dangle % 180 == 0) {
tx0 = s_width - x1 - 1;
ty0 = s_height - y1 - 1;
tx1 = s_width - x0 - 1;
ty1 = s_height - y0 - 1;
// 180 degrees
return new Rectangle2D.Float(tx0,
ty0,
tx1 - tx0 + 1,
ty1 - ty0 + 1);
}
if (dangle % 90 == 0) {
if (dangle < 0) {
// -90 degrees
tx0 = y0;
ty0 = s_width - x1 - 1;
tx1 = y1;
ty1 = s_width - x0 - 1;
return new Rectangle2D.Float(tx0,
ty0,
tx1 - tx0 + 1,
ty1 - ty0 + 1);
} else {
// 90 degrees
tx0 = s_height - y1 - 1;
ty0 = x0;
tx1 = s_height - y0 - 1;
ty1 = x1;
return new Rectangle2D.Float(tx0,
ty0,
tx1 - tx0 + 1,
ty1 - ty0 + 1);
}
}
//
// It's a Affine
//
AffineTransform rotate =
AffineTransform.getRotateInstance(angle, x_center, y_center);
//
// Get sx0,sy0 coordinates and 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)