use of javax.imageio.metadata.IIOMetadata in project imageio-ext by geosolutions-it.
the class GeoTiffVrtTest method palette.
/**
* Test Read on a Paletted Image
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void palette() throws FileNotFoundException, IOException {
if (!isGDALAvailable) {
return;
}
final File outputFile = TestData.temp(this, "writetest.tif", false);
outputFile.deleteOnExit();
final File inputFile = TestData.file(this, "paletted.tif.vrt");
ImageReader reader = new VRTImageReaderSpi().createReaderInstance();
reader.setInput(inputFile);
final IIOMetadata metadata = reader.getImageMetadata(0);
final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", inputFile);
pbjImageRead.setParameter("reader", reader);
final ImageLayout l = new ImageLayout();
l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(256).setTileWidth(256);
RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image, "Paletted image read");
// ////////////////////////////////////////////////////////////////
// preparing to write
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
ImageWriter writer = new GeoTiffImageWriterSpi().createWriterInstance();
pbjImageWrite.setParameter("Output", outputFile);
pbjImageWrite.setParameter("writer", writer);
pbjImageWrite.setParameter("ImageMetadata", metadata);
pbjImageWrite.setParameter("Transcode", false);
pbjImageWrite.addSource(image);
final RenderedOp op = JAI.create("ImageWrite", pbjImageWrite);
final ImageWriter writer2 = (ImageWriter) op.getProperty(ImageWriteDescriptor.PROPERTY_NAME_IMAGE_WRITER);
writer2.dispose();
// ////////////////////////////////////////////////////////////////
// preparing to read again
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjImageReRead = new ParameterBlockJAI("ImageRead");
pbjImageReRead.setParameter("Input", outputFile);
pbjImageReRead.setParameter("Reader", new GeoTiffImageReaderSpi().createReaderInstance());
final RenderedOp image2 = JAI.create("ImageRead", pbjImageReRead);
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image2, "Paletted image read back after writing");
else
Assert.assertNotNull(image2.getTiles());
ImageIOUtilities.disposeImage(image2);
ImageIOUtilities.disposeImage(image);
}
use of javax.imageio.metadata.IIOMetadata in project imageio-ext by geosolutions-it.
the class GDALImageWriter method write.
/**
* Write the input image to the output.
* <p>
* The output must have been set beforehand using the <code>setOutput</code>
* method.
*
* <p>
* An <code>ImageWriteParam</code> may optionally be supplied to control
* the writing process. If <code>param</code> is <code>null</code>, a
* default write param will be used.
*
* <p>
* If the supplied <code>ImageWriteParam</code> contains optional setting
* values not supported by this writer (<i>e.g.</i> progressive encoding
* or any format-specific settings), they will be ignored.
*
* @param streamMetadata
* an <code>IIOMetadata</code> object representing stream
* metadata, or <code>null</code> to use default values.
* @param image
* an <code>IIOImage</code> object containing an image, and
* metadata to be written. Note that metadata is actually
* supposed to be an instance of
* {@link GDALCommonIIOImageMetadata}.
* {@link GDALWritableCommonIIOImageMetadata} may be used to
* set properties from other type of ImageMetadata to a
* format which is understood by this writer.
* @param param
* an <code>ImageWriteParam</code>, or <code>null</code>
* to use a default <code>ImageWriteParam</code>.
*
* @exception IllegalStateException
* if the output has not been set.
* @exception IllegalArgumentException
* if <code>image</code> is <code>null</code>.
* @exception IOException
* if an error occurs during writing.
*/
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
if (outputFile == null) {
throw new IllegalStateException("the output is null!");
}
if (param == null)
param = getDefaultWriteParam();
// /////////////////////////////////////////////////////////////////////
//
// Initial check on the capabilities of this writer as well as the
// provided parameters.
//
// /////////////////////////////////////////////////////////////////////
final String driverName = (String) ((GDALImageWriterSpi) this.originatingProvider).getSupportedFormats().get(0);
final DriverCreateCapabilities writingCapabilities = GDALUtilities.formatWritingCapabilities(driverName);
if (writingCapabilities == GDALUtilities.DriverCreateCapabilities.READ_ONLY)
throw new IllegalStateException("This writer seems to not support either create or create copy");
if (image == null)
throw new IllegalArgumentException("The provided input image is invalid.");
// //
//
// Getting the source image and its main properties
//
// //
final PlanarImage inputRenderedImage = PlanarImage.wrapRenderedImage(image.getRenderedImage());
final int sourceWidth = inputRenderedImage.getWidth();
final int sourceHeight = inputRenderedImage.getHeight();
final int sourceMinX = inputRenderedImage.getMinX();
final int sourceMinY = inputRenderedImage.getMinY();
final int dataType = GDALUtilities.retrieveGDALDataBufferType(inputRenderedImage.getSampleModel().getDataType());
final int nBands = inputRenderedImage.getNumBands();
// //
//
// Setting regions and sizes and retrieving parameters
//
// //
final int xSubsamplingFactor = param.getSourceXSubsampling();
final int ySubsamplingFactor = param.getSourceYSubsampling();
final Vector<String> myOptions = (Vector<String>) ((GDALImageWriteParam) param).getCreateOptionsHandler().getCreateOptions();
Rectangle imageBounds = new Rectangle(sourceMinX, sourceMinY, sourceWidth, sourceHeight);
Dimension destSize = new Dimension();
computeRegions(imageBounds, destSize, param);
// Destination sizes, needed for Dataset Creation
final int destinationWidth = destSize.width;
final int destinationHeight = destSize.height;
// getting metadata before deciding if Create or CreateCopy will be used
final IIOMetadata metadata = image.getMetadata();
GDALCommonIIOImageMetadata imageMetadata = null;
if (metadata != null) {
if (metadata instanceof GDALCommonIIOImageMetadata) {
imageMetadata = (GDALCommonIIOImageMetadata) metadata;
} else {
// TODO: build a metadata conversion to obtain an understandable
// metadata object. Standard plugin-neutral format does not
// contain really useful fields to be converted.
// imageMetadata = new GDALWritableCommonIIOImageMetadata();
// convertMetadata(IMAGE_METADATA_NAME, metadata,
// imageMetadata);
}
}
// /////////////////////////////////////////////////////////////////////
//
// Some GDAL formats driver support both "Create" and "CreateCopy"
// methods. Some others simply support "CreateCopy" method which only
// allows to create a new File from an existing Dataset.
//
// /////////////////////////////////////////////////////////////////////
Dataset writeDataset = null;
Driver driver = null;
try {
if (writingCapabilities == GDALUtilities.DriverCreateCapabilities.CREATE) {
// /////////////////////////////////////////////////////////////////
//
// Create is supported
// -------------------
//
// /////////////////////////////////////////////////////////////////
// Retrieving the file name.
final String fileName = outputFile.getAbsolutePath();
// //
//
// Dataset creation
//
// //
driver = gdal.GetDriverByName(driverName);
writeDataset = driver.Create(fileName, destinationWidth, destinationHeight, nBands, dataType, myOptions);
// //
//
// Data Writing
//
// //
writeDataset = writeData(writeDataset, inputRenderedImage, imageBounds, nBands, dataType, xSubsamplingFactor, ySubsamplingFactor);
// //
if (imageMetadata != null) {
setMetadata(writeDataset, imageMetadata);
}
} else {
// ////////////////////////////////////////////////////////////////
//
// Only CreateCopy is supported
// ----------------------------------------------------------------
//
// First of all, it is worth to point out that CreateCopy method
// allows to create a File from an existing Dataset.
// ////////////////////////////////////////////////////////////////
driver = gdal.GetDriverByName(driverName);
// //
//
// Temporary Dataset creation from the originating image
//
// //
final File tempFile = File.createTempFile("datasetTemp", ".ds", null);
Dataset tempDataset = null;
try {
tempDataset = createDatasetFromImage(inputRenderedImage, tempFile.getAbsolutePath(), imageBounds, nBands, dataType, destinationWidth, destinationHeight, xSubsamplingFactor, ySubsamplingFactor);
tempDataset.FlushCache();
// //
if (imageMetadata != null) {
setMetadata(tempDataset, imageMetadata);
}
// //
//
// Copy back the temporary dataset to the requested dataset
//
// //
writeDataset = driver.CreateCopy(outputFile.getPath(), tempDataset, 0, myOptions);
} finally {
if (tempDataset != null) {
try {
// Closing the dataset
GDALUtilities.closeDataSet(tempDataset);
} catch (Throwable e) {
if (LOGGER.isLoggable(Level.FINEST))
LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
}
}
}
tempFile.delete();
}
// //
//
// Flushing and closing dataset
//
// //
writeDataset.FlushCache();
} finally {
if (writeDataset != null) {
try {
// Closing the dataset
GDALUtilities.closeDataSet(writeDataset);
} catch (Throwable e) {
if (LOGGER.isLoggable(Level.FINEST))
LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
}
}
if (driver != null) {
try {
// Closing the driver
driver.delete();
} catch (Throwable e) {
if (LOGGER.isLoggable(Level.FINEST))
LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
}
}
}
}
use of javax.imageio.metadata.IIOMetadata in project imageio-ext by geosolutions-it.
the class ImageReadOpImageMT method getProperty.
/**
* Gets a property from the property set of this image. If the property name
* is not recognized, <code>java.awt.Image.UndefinedProperty</code> will
* be returned.
*
* <p>
* This implementation first attempts to retrieve the property using the
* equivalent superclass method. If the returned value is not a valid
* property value, the requested property name is that of the image
* thumbnails property, the stream metadata property, or the image metadata
* property, and there has been no prior attempt to read the corresponding
* property value, then its reads the value and set the property. This
* implementation therefore defers reading of the image thumbnails, stream
* metadata, and image metadata values until the correpsonding property is
* actually requested.
* </p>
*
* @param name
* the name of the property to get, as a <code>String</code>.
*
* @return A reference to the property <code>Object</code>, or the value
* <code>java.awt.Image.UndefinedProperty</code>.
*
* @exception IllegalArgumentException
* if <code>propertyName</code> is <code>null</code>.
*/
public Object getProperty(String name) {
// Attempt to get property from superclass method.
Object property = super.getProperty(name);
// made, then read the thumbnails and set the property.
if ((property == null || property == java.awt.Image.UndefinedProperty)) {
// Thumbnails
if (readThumbnails && name.equalsIgnoreCase(ImageReadDescriptor.PROPERTY_NAME_THUMBNAILS)) {
// and with computeTile().
synchronized (reader) {
// got here first.
if (readThumbnails) {
try {
// Get number of thumbnails.
int numThumbnails = reader.getNumThumbnails(imageIndex);
if (numThumbnails > 0) {
// Read all thumbnails.
BufferedImage[] thumbnails = new BufferedImage[numThumbnails];
for (int i = 0; i < numThumbnails; i++) {
thumbnails[i] = reader.readThumbnail(imageIndex, i);
}
// Set thumbnail property.
setProperty(ImageReadDescriptor.PROPERTY_NAME_THUMBNAILS, thumbnails);
// Update return value.
property = thumbnails;
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
// to UndefinedProperty.
if (property == null) {
property = java.awt.Image.UndefinedProperty;
}
// Unset thumbnail flag to avert subsequent
// reading attempts in case this one failed.
readThumbnails = false;
}
}
}
} else if (!reader.isIgnoringMetadata() && ((!streamMetadataRead && name.equalsIgnoreCase(ImageReadDescriptor.PROPERTY_NAME_METADATA_STREAM)) || (!imageMetadataRead && name.equalsIgnoreCase(ImageReadDescriptor.PROPERTY_NAME_METADATA_IMAGE)))) {
// and with computeTile().
synchronized (reader) {
// Set flag to indicate stream or image metadata.
boolean isStreamMetadata = name.equalsIgnoreCase(ImageReadDescriptor.PROPERTY_NAME_METADATA_STREAM);
// Recheck the appropriate flag.
if (!(isStreamMetadata ? streamMetadataRead : imageMetadataRead)) {
// Set property name.
String propertyName = isStreamMetadata ? ImageReadDescriptor.PROPERTY_NAME_METADATA_STREAM : ImageReadDescriptor.PROPERTY_NAME_METADATA_IMAGE;
IIOMetadata metadata = null;
try {
// Read metadata.
metadata = isStreamMetadata ? reader.getStreamMetadata() : reader.getImageMetadata(imageIndex);
// Set metadata property.
if (metadata != null) {
setProperty(propertyName, metadata);
}
// Update return value.
property = metadata;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
// to UndefinedProperty.
if (property == null) {
property = java.awt.Image.UndefinedProperty;
}
// reading attempts in case this one failed.
if (isStreamMetadata) {
streamMetadataRead = true;
} else {
imageMetadataRead = true;
}
}
}
}
}
}
return property;
}
use of javax.imageio.metadata.IIOMetadata in project imageio-ext by geosolutions-it.
the class GeoTiffTest method write.
/**
* Test Writing capabilities.
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void write() throws IOException, FileNotFoundException {
if (!isGDALAvailable) {
return;
}
final File outputFile = TestData.temp(this, "writetest.tif", false);
outputFile.deleteOnExit();
final File inputFile = TestData.file(this, "utmByte.tif");
ImageReadParam rparam = new ImageReadParam();
rparam.setSourceRegion(new Rectangle(1, 1, 300, 500));
rparam.setSourceSubsampling(1, 2, 0, 0);
ImageReader reader = new GeoTiffImageReaderSpi().createReaderInstance();
reader.setInput(inputFile);
final IIOMetadata metadata = reader.getImageMetadata(0);
final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", inputFile);
pbjImageRead.setParameter("reader", reader);
pbjImageRead.setParameter("readParam", rparam);
final ImageLayout l = new ImageLayout();
l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(256).setTileWidth(256);
RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image, "geotiff");
// ////////////////////////////////////////////////////////////////
// preparing to write
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
ImageWriter writer = new GeoTiffImageWriterSpi().createWriterInstance();
pbjImageWrite.setParameter("Output", outputFile);
pbjImageWrite.setParameter("writer", writer);
pbjImageWrite.setParameter("ImageMetadata", metadata);
pbjImageWrite.setParameter("Transcode", false);
ImageWriteParam param = new ImageWriteParam(Locale.getDefault());
param.setSourceRegion(new Rectangle(10, 10, 100, 100));
param.setSourceSubsampling(2, 1, 0, 0);
pbjImageWrite.setParameter("writeParam", param);
pbjImageWrite.addSource(image);
final RenderedOp op = JAI.create("ImageWrite", pbjImageWrite);
final ImageWriter writer2 = (ImageWriter) op.getProperty(ImageWriteDescriptor.PROPERTY_NAME_IMAGE_WRITER);
writer2.dispose();
// ////////////////////////////////////////////////////////////////
// preparing to read again
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjImageReRead = new ParameterBlockJAI("ImageRead");
pbjImageReRead.setParameter("Input", outputFile);
pbjImageReRead.setParameter("Reader", new GeoTiffImageReaderSpi().createReaderInstance());
final RenderedOp image2 = JAI.create("ImageRead", pbjImageReRead);
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image2, "geotif2");
else
Assert.assertNotNull(image2.getTiles());
}
use of javax.imageio.metadata.IIOMetadata in project imageio-ext by geosolutions-it.
the class GeoTiffTest method palette.
/**
* Test Read on a Paletted Image
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void palette() throws FileNotFoundException, IOException {
if (!isGDALAvailable) {
return;
}
final File outputFile = TestData.temp(this, "writetest.tif", false);
outputFile.deleteOnExit();
final File inputFile = TestData.file(this, "paletted.tif");
ImageReader reader = new GeoTiffImageReaderSpi().createReaderInstance();
reader.setInput(inputFile);
final IIOMetadata metadata = reader.getImageMetadata(0);
final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", inputFile);
pbjImageRead.setParameter("reader", reader);
final ImageLayout l = new ImageLayout();
l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(256).setTileWidth(256);
RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image, "Paletted image read");
// ////////////////////////////////////////////////////////////////
// preparing to write
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
ImageWriter writer = new GeoTiffImageWriterSpi().createWriterInstance();
pbjImageWrite.setParameter("Output", outputFile);
pbjImageWrite.setParameter("writer", writer);
pbjImageWrite.setParameter("ImageMetadata", metadata);
pbjImageWrite.setParameter("Transcode", false);
pbjImageWrite.addSource(image);
final RenderedOp op = JAI.create("ImageWrite", pbjImageWrite);
final ImageWriter writer2 = (ImageWriter) op.getProperty(ImageWriteDescriptor.PROPERTY_NAME_IMAGE_WRITER);
writer2.dispose();
// ////////////////////////////////////////////////////////////////
// preparing to read again
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjImageReRead = new ParameterBlockJAI("ImageRead");
pbjImageReRead.setParameter("Input", outputFile);
pbjImageReRead.setParameter("Reader", new GeoTiffImageReaderSpi().createReaderInstance());
final RenderedOp image2 = JAI.create("ImageRead", pbjImageReRead);
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image2, "Paletted image read back after writing");
else
Assert.assertNotNull(image2.getTiles());
ImageIOUtilities.disposeImage(image2);
ImageIOUtilities.disposeImage(image);
}
Aggregations