use of org.apache.sis.internal.coverage.j2d.WritableTiledImage in project sis by apache.
the class ImageRenderer method createImage.
/**
* Creates an image with the data specified by the last call to a {@code setData(…)} method.
* The image upper-left corner is located at the position given by {@link #getBounds()}.
* The two-dimensional {@linkplain #getImageGeometry(int) image geometry} is stored as
* a property associated to the {@value org.apache.sis.image.PlanarImage#GRID_GEOMETRY_KEY} key.
*
* <p>The default implementation returns an instance of {@link java.awt.image.WritableRenderedImage}
* if the {@link #createRaster()} return value is an instance of {@link WritableRaster}, or a read-only
* {@link RenderedImage} otherwise.</p>
*
* @return the image.
* @throws IllegalStateException if no {@code setData(…)} method has been invoked before this method call.
* @throws RasterFormatException if a call to a {@link Raster} factory method failed.
* @throws ArithmeticException if a property of the image to construct exceeds the capacity of 32 bits integers.
*
* @since 1.1
*/
@SuppressWarnings("UseOfObsoleteCollectionType")
public RenderedImage createImage() {
final Raster raster = createRaster();
final Colorizer colorizer = new Colorizer(colors);
final ColorModel colors;
if (colorizer.initialize(bands[visibleBand]) || colorizer.initialize(raster.getSampleModel(), visibleBand)) {
colors = colorizer.createColorModel(buffer.getDataType(), bands.length, visibleBand);
} else {
colors = Colorizer.NULL_COLOR_MODEL;
}
SliceGeometry supplier = null;
if (imageGeometry == null) {
if (isSameGeometry(GridCoverage2D.BIDIMENSIONAL)) {
imageGeometry = geometry;
} else {
supplier = new SliceGeometry(geometry, sliceExtent, gridDimensions, mtFactory);
}
}
final WritableRaster wr = (raster instanceof WritableRaster) ? (WritableRaster) raster : null;
if (wr != null && colors != null && (imageX | imageY) == 0) {
return new Untiled(colors, wr, properties, imageGeometry, supplier);
}
if (properties == null) {
properties = new Hashtable<>();
}
properties.putIfAbsent(GRID_GEOMETRY_KEY, (supplier != null) ? new DeferredProperty(supplier) : imageGeometry);
if (wr != null) {
return new WritableTiledImage(properties, colors, width, height, 0, 0, wr);
} else {
return new TiledImage(properties, colors, width, height, 0, 0, raster);
}
}
use of org.apache.sis.internal.coverage.j2d.WritableTiledImage in project sis by apache.
the class GridCoverageBuilder method build.
/**
* Creates the grid coverage from the domain, ranges and values given to setter methods.
* The returned coverage is often a {@link GridCoverage2D} instance, but not necessarily.
*
* @return grid coverage created from specified domain, ranges and sample values.
* @throws IllegalStateException if some properties are inconsistent, for example
* {@linkplain GridGeometry#getExtent() grid extent} not matching image size or
* {@linkplain #setRanges(SampleDimension...) number of sample dimensions} not matching
* the number of bands. This exception often wraps an {@link IllegalGridGeometryException},
* {@link IllegalArgumentException} or {@link org.apache.sis.util.NullArgumentException}.
*/
public GridCoverage build() throws IllegalStateException {
// May be replaced by an instance with extent.
GridGeometry grid = domain;
// May be replaced by a non-null value.
List<? extends SampleDimension> bands = ranges;
/*
* If not already done, create the image from the raster. We try to create the most standard objects
* when possible: a BufferedImage (from Java2D), then later a GridCoverage2D (from SIS public API).
* An exception to this rule is the DataBuffer case: we use a dedicated BufferedGridCoverage class
* instead.
*/
try {
if (image == null) {
if (raster == null) {
if (buffer == null) {
throw new IllegalStateException(missingProperty("values"));
}
if (size != null) {
grid = GridCoverage2D.addExtentIfAbsent(grid, new Rectangle(size));
verifyGridExtent(grid.getExtent(), size.width, size.height);
} else if (grid == null) {
throw new IncompleteGridGeometryException(missingProperty("size"));
}
bands = GridCoverage2D.defaultIfAbsent(bands, null, buffer.getNumBanks());
return new BufferedGridCoverage(domainWithAxisFlips(grid), bands, buffer);
}
/*
* If the band list is null, create a default list of bands because we need
* them for creating the color model. Note that we shall not do that when a
* RenderedImage has been specified because GridCoverage2D constructor will
* will infer better names.
*/
bands = GridCoverage2D.defaultIfAbsent(bands, null, raster.getNumBands());
final SampleModel sm = raster.getSampleModel();
final Colorizer colorizer = new Colorizer(Colorizer.GRAYSCALE);
final ColorModel colors;
if (colorizer.initialize(bands.get(visibleBand)) || colorizer.initialize(sm, visibleBand)) {
colors = colorizer.createColorModel(ImageUtilities.getBandType(sm), bands.size(), visibleBand);
} else {
colors = Colorizer.NULL_COLOR_MODEL;
}
/*
* Create an image from the raster. We favor BufferedImage instance when possible,
* and fallback on TiledImage only if the BufferedImage can not be created.
*/
if (raster instanceof WritableRaster) {
final WritableRaster wr = (WritableRaster) raster;
if (colors != null && (wr.getMinX() | wr.getMinY()) == 0) {
image = new BufferedImage(colors, wr, false, properties);
} else {
image = new WritableTiledImage(properties, colors, wr.getWidth(), wr.getHeight(), 0, 0, wr);
}
} else {
image = new TiledImage(properties, colors, raster.getWidth(), raster.getHeight(), 0, 0, raster);
}
}
/*
* At this point `image` shall be non-null but `bands` may still be null (it is okay).
*/
return new GridCoverage2D(domainWithAxisFlips(grid), bands, image);
} catch (TransformException | NullPointerException | IllegalArgumentException | ArithmeticException e) {
throw new IllegalStateException(Resources.format(Resources.Keys.CanNotBuildGridCoverage), e);
}
}
Aggregations