final byte[] array = buffer.getData(0);
for (int i=0; i<array.length; i++) {
array[i] = (byte) random.nextInt(161);
}
final MathTransform identity = IdentityTransform.create(2);
final GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null);
GridCoverage2D coverage;
coverage = factory.create("Test", source, DefaultGeographicCRS.WGS84,
identity, new GridSampleDimension[] {band}, null, null);
/*
* Apply the operation. The SampleTranscoder class is suppose to transform our
* integers into real-world values. Check if the result use floating-points.
*/
final RenderedImage target = coverage.view(ViewType.GEOPHYSICS).getRenderedImage();
assertSame(target, PlanarImage.wrapRenderedImage(target));
assertEquals(DataBuffer.TYPE_BYTE, source.getSampleModel().getDataType());
if (coverage.getRenderedImage() != target) {
assertEquals(DataBuffer.TYPE_FLOAT, target.getSampleModel().getDataType());
}
/*
* Now, gets the data as an array and compare it with the expected values.
*/
double[] sourceData = source.getData().getSamples(0, 0, SIZE, SIZE, 0, (double[]) null);
double[] targetData = target.getData().getSamples(0, 0, SIZE, SIZE, 0, (double[]) null);
band.getSampleToGeophysics().transform(sourceData, 0, sourceData, 0, sourceData.length);
CategoryListTest.compare(sourceData, targetData, EPS);
/*
* Construct a new image with the resulting data, and apply an inverse transformation.
* Compare the resulting values with the original data.
*/
RenderedImage back = PlanarImage.wrapRenderedImage(target).getAsBufferedImage();
coverage = factory.create("Test", back, DefaultGeographicCRS.WGS84,
identity, new GridSampleDimension[]{band.geophysics(true)}, null, null);
back = coverage.view(ViewType.PACKED).getRenderedImage();
assertEquals(DataBuffer.TYPE_BYTE, back.getSampleModel().getDataType());
sourceData = source.getData().getSamples(0, 0, SIZE, SIZE, 0, (double[]) null);