use of org.apache.sis.coverage.grid.GridExtent in project sis by apache.
the class AbstractGridResourceTest method testRangeArgumentForInterleavedModel.
/**
* Tests {@link AbstractGridResource.RangeArgument} for data organized in an interleaved sample model.
* This is the state when the {@code insert} methods are invoked.
*/
@Test
public void testRangeArgumentForInterleavedModel() {
final AbstractGridResource.RangeArgument r = resource.validateRangeArgument(7, new int[] { 4, 6, 2 });
assertEquals(3, r.insertBandDimension(new GridExtent(360, 180), 2).getDimension());
assertArrayEquals(new int[] { 3, 1, 2 }, r.insertSubsampling(new int[] { 3, 1 }, 2));
assertEquals("numBands", 3, r.getNumBands());
assertEquals("first", 4, r.getFirstSpecified());
// Expect sorted source indices: {2, 4, 6}.
assertEquals("source", 2, r.getSourceIndex(0));
assertEquals("source", 4, r.getSourceIndex(1));
assertEquals("source", 6, r.getSourceIndex(2));
// Expect original positions of sorted indices: {2, 0, 1}.
assertEquals("target", 2, r.getTargetIndex(0));
assertEquals("target", 0, r.getTargetIndex(1));
assertEquals("target", 1, r.getTargetIndex(2));
// Expect source indices divided by 2 minus 1.
assertEquals("subsampled", 0, r.getSubsampledIndex(0));
assertEquals("subsampled", 1, r.getSubsampledIndex(1));
assertEquals("subsampled", 2, r.getSubsampledIndex(2));
assertEquals("pixelStride", 3, r.getPixelStride());
}
use of org.apache.sis.coverage.grid.GridExtent in project sis by apache.
the class CoverageReadConsistency method iterator.
/**
* Creates a pixel iterator for a sub-region in a slice of the specified coverage.
* All coordinates given to this method are in the coordinate space of subsampled coverage subset.
* This method returns {@code null} if the arguments are valid but the image can not be created
* because of a restriction in {@code PixelInterleavedSampleModel} constructor.
*
* @param coverage the coverage from which to get the iterator, or {@code null} if unavailable.
* @param sliceMin lower bounds of the <var>n</var>-dimensional region of the coverage for which to get an iterator.
* @param sliceMax upper bounds of the <var>n</var>-dimensional region of the coverage for which to get an iterator.
* @param subsampling subsampling factors to apply on the image.
* @param subOffsets offsets to add after multiplication by subsampling factors.
* @return pixel iterator over requested area, or {@code null} if unavailable.
*/
private static PixelIterator iterator(final GridCoverage coverage, long[] sliceMin, long[] sliceMax, final int[] subsampling, final int[] subOffsets, final boolean allowSubsampling) {
if (coverage == null) {
return null;
}
/*
* Same extent than `areaOfInterest` but in two dimensions and with (0,0) origin.
* We use that for clipping iteration to the area that we requested even if the
* coverage gave us a larger area.
*/
final Rectangle sliceAOI = new Rectangle(StrictMath.toIntExact(sliceMax[0] - sliceMin[0] + 1), StrictMath.toIntExact(sliceMax[1] - sliceMin[1] + 1));
/*
* If the given coordinates were in a subsampled space while the coverage is at full resolution,
* convert the coordinates to full resolution.
*/
if (allowSubsampling) {
sliceMin = sliceMin.clone();
sliceMax = sliceMax.clone();
for (int i = 0; i < sliceMin.length; i++) {
sliceMin[i] = sliceMin[i] * subsampling[i] + subOffsets[i];
sliceMax[i] = sliceMax[i] * subsampling[i] + subOffsets[i];
}
}
RenderedImage image = coverage.render(new GridExtent(null, sliceMin, sliceMax, true));
/*
* The subsampling offsets were included in the extent given to above `render` method call, so in principle
* they should not be given again to `SubsampledImage` constructor. However the `render` method is free to
* return an image with a larger extent, which may result in different offsets. The result can be "too much"
* offset. We want to compensate by subtracting the surplus. But because we can not have negative offsets,
* we shift the whole `sliceAOI` (which is equivalent to subtracting `subX|Y` in full resolution coordinates)
* and set the offset to the complement.
*/
if (allowSubsampling) {
final int subX = subsampling[0];
final int subY = subsampling[1];
if (subX > image.getTileWidth() || subY > image.getTileHeight()) {
// `SubsampledImage` does not support this case.
return null;
}
int offX = StrictMath.floorMod(image.getMinX(), subX);
int offY = StrictMath.floorMod(image.getMinY(), subY);
if (offX != 0) {
sliceAOI.x--;
offX = subX - offX;
}
if (offY != 0) {
sliceAOI.y--;
offY = subY - offY;
}
image = SubsampledImage.create(image, subX, subY, offX, offY);
if (image == null) {
return null;
}
}
return new PixelIterator.Builder().setRegionOfInterest(sliceAOI).create(image);
}
Aggregations