use of it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam in project imageio-ext by geosolutions-it.
the class ECWTest method imageRead.
// ecwp
/**
* Test reading of a RGB image
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void imageRead() throws FileNotFoundException, IOException {
if (!isECWAvailable)
return;
final ParameterBlockJAI pbjImageRead;
final EnhancedImageReadParam irp = new EnhancedImageReadParam();
final String fileName = "sample.ecw";
final File file = TestData.file(this, fileName);
irp.setSourceSubsampling(2, 2, 0, 0);
pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", file);
pbjImageRead.setParameter("readParam", irp);
final ImageLayout l = new ImageLayout();
l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(512).setTileWidth(512);
RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image, fileName);
else
image.getTiles();
Assert.assertEquals(200, image.getWidth());
Assert.assertEquals(100, image.getHeight());
ImageIOUtilities.disposeImage(image);
}
use of it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam in project imageio-ext by geosolutions-it.
the class ECWTest method manualReadDestination.
@Test
public void manualReadDestination() throws FileNotFoundException, IOException {
if (!isECWAvailable)
return;
final ECWImageReaderSpi spi = new ECWImageReaderSpi();
final ECWImageReader mReader = new ECWImageReader(spi);
final String fileName = "sample.ecw";
final File file = TestData.file(this, fileName);
final EnhancedImageReadParam param = new EnhancedImageReadParam();
param.setDestinationRegion(new Rectangle(0, 0, 200, 100));
final int imageIndex = 0;
mReader.setInput(file);
final RenderedImage image = mReader.readAsRenderedImage(imageIndex, param);
if (TestData.isInteractiveTest())
ImageIOUtilities.visualize(image, fileName);
Assert.assertEquals(200, image.getWidth());
Assert.assertEquals(100, image.getHeight());
mReader.dispose();
}
use of it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam in project imageio-ext by geosolutions-it.
the class GDALImageReader method read.
/**
* Read the raster and returns a <code>BufferedImage</code>
*
* @param imageIndex
* the index of the image to be retrieved.
* @param param
* an <code>ImageReadParam</code> used to control the
* reading process, or <code>null</code>. Actually,
* setting a destinationType allows to specify the number of
* bands in the destination image.
*
* @return the desired portion of the image as a <code>BufferedImage</code>
* @throws IllegalArgumentException
* if <code>param</code> contains an invalid specification
* of a source and/or destination band subset or of a
* destination image.
* @throws IOException
* if an error occurs when acquiring access to the
* underlying datasource
*/
public BufferedImage read(final int imageIndex, final ImageReadParam param) throws IOException {
// //
//
// Retrieving the requested dataset
//
// //
final GDALCommonIIOImageMetadata item = getDatasetMetadata(imageIndex);
int width = item.getWidth();
int height = item.getHeight();
final Dataset originalDataset = datasetsMap.get(item.getDatasetName());
if (originalDataset == null)
throw new IOException("Error while acquiring the input dataset " + item.getDatasetName());
Dataset dataset = originalDataset;
Dataset warpedDataset = null;
try {
if (param instanceof GDALImageReadParam) {
GDALImageReadParam gdalImageReadParam = (GDALImageReadParam) param;
// Check for source != destination
String sourceWkt = dataset.GetProjection();
String destinationWkt = gdalImageReadParam.getDestinationWkt();
SpatialReference sourceReference = new SpatialReference(sourceWkt);
SpatialReference destinationReference = new SpatialReference(destinationWkt);
// lets warp the image using GDAL.
if (sourceReference.IsSame(destinationReference) == 0) {
dataset = gdal.AutoCreateWarpedVRT(dataset, dataset.GetProjection(), destinationWkt, gdalImageReadParam.getResampleAlgorithm().getGDALResampleAlgorithm(), gdalImageReadParam.getMaxError());
warpedDataset = dataset;
// width and height may have changed from original metadata
width = warpedDataset.getRasterXSize();
height = warpedDataset.getRasterYSize();
}
}
final SampleModel itemSampleModel = item.getSampleModel();
int itemNBands = itemSampleModel.getNumBands();
int nDestBands;
BufferedImage bi = null;
final ImageReadParam imageReadParam;
if (param == null)
imageReadParam = getDefaultReadParam();
else
imageReadParam = param;
// //
//
// First, check for a specified ImageTypeSpecifier
//
// //
ImageTypeSpecifier imageType = imageReadParam.getDestinationType();
SampleModel destSampleModel = null;
if (imageType != null) {
destSampleModel = imageType.getSampleModel();
nDestBands = destSampleModel.getNumBands();
} else {
bi = imageReadParam.getDestination();
if (bi != null)
nDestBands = bi.getSampleModel().getNumBands();
else
nDestBands = itemNBands;
}
// //
//
// Second, bands settings check
//
// //
checkReadParamBandSettings(imageReadParam, itemNBands, nDestBands);
int[] srcBands = imageReadParam.getSourceBands();
// int[] destBands = imageReadParam.getDestinationBands();
//
// //
//
// Third, destination image check
//
// //
// if (bi != null && imageType == null) {
// if ((srcBands == null) && (destBands == null)) {
// SampleModel biSampleModel = bi.getSampleModel();
// if (!bi.getColorModel().equals(item.getColorModel())
// || biSampleModel.getDataType() != itemSampleModel
// .getDataType())
// throw new IllegalArgumentException(
// "Provided destination image does not have a valid ColorModel or
// SampleModel");
// }
// }
// //
//
// Computing regions of interest
//
// //
Rectangle srcRegion = new Rectangle(0, 0, 0, 0);
Rectangle destRegion = new Rectangle(0, 0, 0, 0);
computeRegions(imageReadParam, width, height, bi, srcRegion, destRegion);
if (imageReadParam != null) {
if (imageReadParam instanceof EnhancedImageReadParam) {
final EnhancedImageReadParam eparam = (EnhancedImageReadParam) imageReadParam;
final Rectangle dstRegion = eparam.getDestinationRegion();
if (dstRegion != null) {
destRegion.height = dstRegion.height;
destRegion.width = dstRegion.width;
}
}
}
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Source Region = " + srcRegion.toString());
LOGGER.fine("Destination Region = " + destRegion.toString());
}
//
if (bi == null) {
// //
//
// No destination image has been specified.
// Creating a new BufferedImage
//
// //
ColorModel cm;
if (imageType == null) {
cm = item.getColorModel();
bi = new BufferedImage(cm, (WritableRaster) readDatasetRaster(item.getSampleModel(), dataset, srcRegion, destRegion, srcBands), false, null);
} else {
cm = imageType.getColorModel();
bi = new BufferedImage(cm, (WritableRaster) readDatasetRaster(destSampleModel, dataset, srcRegion, destRegion, srcBands), false, null);
}
} else {
// //
//
// the destination image has been specified.
//
// //
// Rectangle destSize = (Rectangle) destRegion.clone();
// destSize.setLocation(0, 0);
Raster readRaster = readDatasetRaster(item.getSampleModel(), dataset, srcRegion, destRegion, srcBands);
WritableRaster raster = bi.getRaster().createWritableChild(0, 0, bi.getWidth(), bi.getHeight(), 0, 0, null);
// TODO: Work directly on a Databuffer avoiding setRect?
raster.setRect(destRegion.x, destRegion.y, readRaster);
// Raster readRaster = readDatasetRaster(item, srcRegion,
// destRegion,
// srcBands);
// WritableRaster raster = bi.getRaster().createWritableChild(
// destRegion.x, destRegion.y, destRegion.width,
// destRegion.height, destRegion.x, destRegion.y, null);
// //TODO: Work directly on a Databuffer avoiding setRect?
// raster.setRect(readRaster);
}
return bi;
} finally {
if (warpedDataset != null) {
GDALUtilities.closeDataSet(warpedDataset);
}
}
}
Aggregations