use of org.apache.sis.internal.coverage.j2d.TiledImage 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.TiledImage in project sis by apache.
the class ResampledGridCoverageTest method createCoverageND.
/**
* Creates a coverage in {@linkplain HardCodedCRS#WGS84_3D OGC:CRS:84 + elevation} reference system.
* If the {@code withTime} argument is {@code true}, then the coverage will also include a temporal
* dimension. The grid coverage characteristics are:
* <ul>
* <li>Dimension is 6×6.</li>
* <li>Grid extent starts at arbitrary non-zero low values.</li>
* <li>Envelope is arbitrary but stable (no random values).</li>
* <li>Display oriented (origin is in upper-left corner).</li>
* <li>3 byte bands for RGB coloration.</li>
* <li>Each quarter of the overall image is filled with a plain color:
* <table style="color:white;border-collapse:collapse;">
* <tbody style="border:none">
* <tr>
* <td style="width:50%; background-color:black">Black</td>
* <td style="width:50%; background-color:red">Red</td>
* </tr>
* <tr>
* <td style="width:50%; background-color:green">Green</td>
* <td style="width:50%; background-color:blue">Blue</td>
* </tr>
* </tbody>
* </table>
* </li>
* </ul>
*
* @param withTime {@code false} for a three-dimensional coverage, or {@code true} for adding a temporal dimension.
* @return a new three- or four-dimensional RGB Grid Coverage.
*/
private GridCoverage createCoverageND(final boolean withTime) {
random = TestUtilities.createRandomNumberGenerator();
final BufferedImage image = new BufferedImage(2 * QS, 2 * QS, BufferedImage.TYPE_3BYTE_BGR);
final int[] color = new int[QS * QS];
/* Upper-left quarter */
// Keep default value, which is black.
/* Upper-right quarter */
Arrays.fill(color, Color.RED.getRGB());
image.setRGB(QS, 0, QS, QS, color, 0, QS);
/* Lower-left quarter */
Arrays.fill(color, Color.GREEN.getRGB());
image.setRGB(0, QS, QS, QS, color, 0, QS);
/* Lower-right quarter */
Arrays.fill(color, Color.BLUE.getRGB());
image.setRGB(QS, QS, QS, QS, color, 0, QS);
/*
* Create an image with origin between -2 and +2. We use a random image location for more
* complete testing, but actually the tests in this class are independent of image origin.
* Note that grid extent origin does not need to be the same than image origin.
*/
final int minX = random.nextInt(5) - 2;
final int minY = random.nextInt(5) - 2;
GridGeometry gg = createGridGeometryND(withTime ? HardCodedCRS.WGS84_4D : HardCodedCRS.WGS84_3D, 0, 1, 2, 3, false);
final TiledImage shiftedImage = new TiledImage(null, image.getColorModel(), // Image size
image.getWidth(), // Image size
image.getHeight(), // minTileX
random.nextInt(32) - 10, // minTileY
random.nextInt(32) - 10, image.getRaster().createTranslatedChild(minX, minY));
return new GridCoverage2D(gg, null, shiftedImage);
}
use of org.apache.sis.internal.coverage.j2d.TiledImage in project sis by apache.
the class InterpolationTest method createImage.
/**
* Creates an iterator over a simple 3×3 image. Image values are:
*
* {@preformat text
* 0 1 2
* 3 4 5
* 6 7 8
* }
*
* @param support number of pixels needed for interpolations:
* 1 for nearest, 2 for bilinear, 4 for bicubic.
*/
private void createImage(final int support) {
final WritableRaster raster = WritableRaster.createWritableRaster(new BandedSampleModel(DataBuffer.TYPE_FLOAT, XUP - XMIN, YUP - YMIN, 1), new Point(XMIN, YMIN));
float value = 0;
for (int y = YMIN; y < YUP; y++) {
for (int x = XMIN; x < XUP; x++) {
raster.setSample(x, y, 0, value++);
}
}
final TiledImage source = new TiledImage(null, null, XUP - XMIN, YUP - YMIN, 0, 0, raster);
assertNull(source.verify());
iterator = new PixelIterator.Builder().setWindowSize(new Dimension(support, support)).create(source);
window = iterator.createWindow(TransferType.DOUBLE);
}
use of org.apache.sis.internal.coverage.j2d.TiledImage in project sis by apache.
the class MaskedImageTest method multiTile.
/**
* Creates an image with a 4 tiles and the given number of values.
* Image size is {@value #WIDTH}×{@value #HEIGHT} pixels.
*/
private static RenderedImage multiTile() {
final TiledImage image = new TiledImage(null, colorPalette(), WIDTH, HEIGHT, 0, 0, tile(0, 0, 0), tile(TILE_WIDTH, 0, 1), tile(0, TILE_HEIGHT, 3), tile(TILE_WIDTH, TILE_HEIGHT, 2));
assertNull(image.verify());
return image;
}
use of org.apache.sis.internal.coverage.j2d.TiledImage 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