} else {
noData = null;
}
// ROI to use
ROI roi = null;
if (roiUsed) {
roi = roiData;
}
// New array ofr the transformed source images
RenderedOp[] translated = new RenderedOp[sources.length];
List<AffineTransform> transform = new ArrayList<AffineTransform>();
for (int i = 0; i < sources.length; i++) {
// Translation coefficients
int xTrans = (int) (Math.random() * 10);
int yTrans = (int) (Math.random() * 10);
// Translation operation
AffineTransform tr = AffineTransform.getTranslateInstance(xTrans, yTrans);
// Addition to the transformations list
transform.add(tr);
// Translation of the image
translated[i] = TranslateDescriptor.create(sources[i], (float) xTrans, (float) yTrans,
null, null);
}
// Definition of the final image dimensions
ImageLayout layout = new ImageLayout();
layout.setMinX(sources[0].getMinX());
layout.setMinY(sources[0].getMinY());
layout.setWidth(sources[0].getWidth());
layout.setHeight(sources[0].getHeight());
RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
// BandMerge operation
RenderedOp merged = BandMergeDescriptor.create(noData, destNoData, hints, transform, roi,
translated);
Assert.assertNotNull(merged.getTiles());
// Check if the bands number is the same
assertEquals(BAND_NUMBER, merged.getNumBands());
// Upper-Left tile indexes
int minTileX = merged.getMinTileX();
int minTileY = merged.getMinTileY();
// Raster object
Raster upperLeftTile = merged.getTile(minTileX, minTileY);
// Tile bounds
int minX = upperLeftTile.getMinX();
int minY = upperLeftTile.getMinY();
int maxX = upperLeftTile.getWidth() + minX;
int maxY = upperLeftTile.getHeight() + minY;
// Source corners
final int dstMinX = merged.getMinX();
final int dstMinY = merged.getMinY();
final int dstMaxX = merged.getMaxX();
final int dstMaxY = merged.getMaxY();
Point2D ptDst = new Point2D.Double(0, 0);
Point2D ptSrc = new Point2D.Double(0, 0);
// Cycle on all the tile Bands
for (int b = 0; b < BAND_NUMBER; b++) {
RandomIter iter = RandomIterFactory.create(translated[b], null);
// Source corners
final int srcMinX = translated[b].getMinX();
final int srcMinY = translated[b].getMinY();
final int srcMaxX = translated[b].getMaxX();
final int srcMaxY = translated[b].getMaxY();
// Cycle on the y-axis
for (int x = minX; x < maxX; x++) {
// Cycle on the x-axis
for (int y = minY; y < maxY; y++) {
// Calculated value
double value = upperLeftTile.getSampleDouble(x, y, b);
// If the tile pixels are outside the image bounds, then no data is set.
if (x < dstMinX || x >= dstMaxX || y < dstMinY || y >= dstMaxY) {
value = destNoData;
}
// Set the x,y destination pixel location
ptDst.setLocation(x, y);
// Map destination pixel to source pixel
transform.get(b).transform(ptDst, ptSrc);
// Source pixel indexes
int srcX = round(ptSrc.getX());
int srcY = round(ptSrc.getY());
double valueOld = destNoData;
// Check if the pixel is inside the source bounds
if (!(srcX < srcMinX || srcX >= srcMaxX || srcY < srcMinY || srcY >= srcMaxY)) {
// Old band value
valueOld = iter.getSampleDouble(srcX, srcY, 0);
}
// ROI CHECK
boolean contained = true;
if (roiUsed) {
if (!roi.contains(x, y)) {
contained = false;
// Comparison if the final value is not inside a ROI
assertEquals(value, destNoData, TOLERANCE);
}
}