*/
protected GridCoverage2D deriveGridCoverage(final GridCoverage2D[] sources,
final Parameters parameters)
{
if (NORMALIZE) {
final ParameterList block = parameters.parameters;
KernelJAI mask1 = (KernelJAI) block.getObjectParameter("Mask1");
KernelJAI mask2 = (KernelJAI) block.getObjectParameter("Mask2");
/*
* Normalizes the kernel in such a way that pixel values likes
* [-2 -1 0 +1 +2] will give a gradient of about 1 unit/pixel.
*/
double factor = getNormalizationFactor(mask1, mask2);
if (!(factor > 0)) {
// Do not transform if factor is 0 or NaN.
factor = 1;
}
/*
* Computes a scale factor taking in account the transformation from
* grid to coordinate system. This scale will convert gradient from
* 1 unit/pixel to 1 unit/meters or 1 unit/degrees, depending the
* coordinate reference systems axis unit.
*/
double scaleMask1 = 1;
double scaleMask2 = 1;
if (sources.length != 0) {
final MathTransform2D mtr;
mtr = ((GridGeometry2D) sources[0].getGridGeometry()).getGridToCRS2D();
if (mtr instanceof AffineTransform) {
final AffineTransform tr = (AffineTransform) mtr;
final double scaleX = XAffineTransform.getScaleX0(tr);
final double scaleY = XAffineTransform.getScaleY0(tr);
scaleMask1 = getScaleFactor(mask1, scaleX, scaleY);
scaleMask2 = getScaleFactor(mask2, scaleX, scaleY);
if (!(scaleMask1>0 && scaleMask2>0)) {
// Do not rescale if scale is 0 or NaN.
scaleMask1 = 1;
scaleMask2 = 1;
}
if (DEBUG) {
System.out.print("factor= "); System.out.println(factor);
System.out.print("scaleMask1= "); System.out.println(scaleMask1);
System.out.print("scaleMask1= "); System.out.println(scaleMask2);
}
}
}
block.setParameter("Mask1", divide(mask1, factor*scaleMask1));
block.setParameter("Mask2", divide(mask2, factor*scaleMask2));
}
return super.deriveGridCoverage(sources, parameters);
}