ImageConvertorFactory.getInstance().
getImageConvertor(ImageRule.TRUECOLOUR);
watermark = cnv.convert(watermark, params);
params.setObject(ParameterNames.WATERMARK_IMAGE, watermark);
} catch (Exception e) {
throw new ToolException(
EXCEPTION_LOCALIZER.format("loading-watermark-failure"));
}
//We use image to transform watermark (scale, add transparent border).
//To improve performance we suppose all the frames have the same size.
//So the parameters for watermark transformation is retrieved from the
// first frams.
RenderedOp image = ops[0];
//Scale the watermark to fit into the source image.
float xFactor = (float) image.getWidth() / watermark.getWidth();
float yFactor = (float) image.getHeight() / watermark.getHeight();
float factor;
int leftPad = 0;
int bottomPad = 0;
// Get the smallest factor to guarantee that watermark will fit
// into image entirely.
if (xFactor > yFactor) {
factor = yFactor;
leftPad = (int) ((xFactor - yFactor) * watermark.getWidth() / 2.0);
} else {
factor = xFactor;
bottomPad = (int) ((yFactor - xFactor) * watermark.getHeight()/ 2.0);
}
ParameterBlockJAI scalePb = new ParameterBlockJAI("scale");
scalePb.addSource(watermark);
// X scale factor
scalePb.setParameter("xScale", new Float(factor));
// Y scale factor
scalePb.setParameter("yScale", new Float(factor));
// Interpolation method (should really use something better then
// NN here)
scalePb.setParameter("interpolation",
Interpolation.getInstance(Interpolation.INTERP_NEAREST));
try {
watermark = JAI.create("scale",
scalePb,
null);
} catch (Exception e) {
LOGGER.error("jai-operation-failure", new String[]{ "scale", e.toString() });
throw new ToolException(e);
}
if (leftPad != 0 || bottomPad != 0) {
ParameterBlockJAI boarderPb = new ParameterBlockJAI("border");
boarderPb.addSource(watermark);
if (leftPad != 0) {
Integer padding = leftPad;
boarderPb.setParameter("leftPad", padding);
boarderPb.setParameter("rightPad", padding);
} else { // means bottomPad != 0
Integer padding = bottomPad;
boarderPb.setParameter("bottomPad", padding);
boarderPb.setParameter("topPad", padding);
}
//Transparent border.
boarderPb.setParameter("type", new BorderExtenderConstant(new double[]{1}));
try {
watermark = JAI.create("border", boarderPb);
} catch (Exception e) {
LOGGER.error("jai-operation-failure", new String[]{ "border", e.toString() });
throw new ToolException(e);
}
// perform nearest neighbour interpolation as these should only be integer
// translations
watermark = TranslateDescriptor.create(watermark,