use of org.geotoolkit.display2d.service.SceneDef in project geotoolkit by Geomatys.
the class ColorModelTest method testReprojectionCoverageRGB.
@Test
public void testReprojectionCoverageRGB() throws TransformException, PortrayalException, NoSuchAuthorityCodeException, FactoryException {
// create a test coverage
final BufferedImage img = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
final Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, 500, 500);
final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
env.setRange(0, 0, 20);
env.setRange(1, 0, 20);
final GridCoverage coverage = new GridCoverage2D(new GridGeometry(null, env, GridOrientation.HOMOTHETY), null, img);
// display it
final MapLayers context = MapBuilder.createContext();
final MapLayer cl = MapBuilder.createCoverageLayer(coverage, SF.style(StyleConstants.DEFAULT_RASTER_SYMBOLIZER), "coverage");
context.getComponents().add(cl);
final Envelope envelope = Envelopes.transform(env, CRS.forCode("EPSG:3031"));
final CanvasDef cdef = new CanvasDef(new Dimension(800, 600), envelope);
cdef.setBackground(Color.WHITE);
final BufferedImage result = DefaultPortrayalService.portray(cdef, new SceneDef(context));
// background is opaque we should obtain an RGB color model since raster styles
// are unpredictable
assertTrue(!(result.getColorModel() instanceof IndexColorModel));
assertEquals(ColorSpace.TYPE_RGB, result.getColorModel().getColorSpace().getType());
assertEquals(3, result.getColorModel().getNumComponents());
assertEquals(3, result.getColorModel().getNumColorComponents());
// check we don't have any black reprojection pixels
int[] buffer = new int[4];
final Raster raster = result.getData();
for (int x = 0; x < raster.getWidth(); x++) {
for (int y = 0; y < raster.getHeight(); y++) {
raster.getPixel(x, y, buffer);
if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0) {
// black pixel
fail("reprojection should not have generated black pixels.");
}
}
}
}
use of org.geotoolkit.display2d.service.SceneDef in project geotoolkit by Geomatys.
the class GeometryExpressionTest method bufferTest.
/**
* Test a buffer expression around geometry.
*/
@Test
public void bufferTest() throws PortrayalException, IOException {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Point.class).setName("geom").setCRS(crs).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType type = ftb.build();
final Point point = GO2Utilities.JTS_FACTORY.createPoint(new Coordinate(0, 0));
JTS.setCRS(point, crs);
final Feature feature = type.newInstance();
feature.setPropertyValue("geom", point);
final Expression geomExp = FF.function("buffer", FF.property("geom"), FF.literal(10));
final Fill fill = SF.fill(Color.RED);
final PolygonSymbolizer symbolizer = SF.polygonSymbolizer("", geomExp, DEFAULT_DESCRIPTION, DEFAULT_UOM, null, fill, DEFAULT_DISPLACEMENT, LITERAL_ZERO_FLOAT);
final MutableStyle style = SF.style(symbolizer);
final MapLayers context = MapBuilder.createContext();
final MapLayer fml = MapBuilder.createLayer(FeatureStoreUtilities.collection(feature));
fml.setStyle(style);
context.getComponents().add(fml);
final GeneralEnvelope env = new GeneralEnvelope(crs);
env.setRange(0, -20, +20);
env.setRange(1, -20, +20);
final CanvasDef canvasDef = new CanvasDef();
canvasDef.setEnvelope(env);
canvasDef.setDimension(new Dimension(40, 40));
canvasDef.setBackground(Color.WHITE);
final SceneDef sceneDef = new SceneDef(context);
final BufferedImage image = DefaultPortrayalService.portray(canvasDef, sceneDef);
// we must obtain a red point of 10pixel width at image center
final int red = Color.RED.getRGB();
final int white = Color.WHITE.getRGB();
Assert.assertEquals(white, image.getRGB(20, 9));
Assert.assertEquals(red, image.getRGB(20, 11));
Assert.assertEquals(red, image.getRGB(20, 20));
Assert.assertEquals(red, image.getRGB(20, 29));
Assert.assertEquals(white, image.getRGB(20, 31));
Assert.assertEquals(white, image.getRGB(9, 20));
Assert.assertEquals(red, image.getRGB(11, 20));
Assert.assertEquals(red, image.getRGB(20, 20));
Assert.assertEquals(red, image.getRGB(29, 20));
Assert.assertEquals(white, image.getRGB(31, 20));
}
use of org.geotoolkit.display2d.service.SceneDef in project geotoolkit by Geomatys.
the class RasterSymbolizerTest method UTM32632Test.
/**
* Check proper image reprojection in UTM
*/
@Ignore
@Test
public void UTM32632Test() throws Exception {
final BufferedImage img = new BufferedImage(120, 90, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, 120, 90);
// set it's envelope
final GeneralEnvelope gridEnv = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
gridEnv.setRange(0, 0, 120);
gridEnv.setRange(1, 0, 90);
// create the coverage
final GridCoverage coverage = new GridCoverage2D(new GridGeometry(null, gridEnv, GridOrientation.HOMOTHETY), null, img);
final MapLayers context = MapBuilder.createContext();
final MapLayer cl = MapBuilder.createCoverageLayer(coverage, SF.style(StyleConstants.DEFAULT_RASTER_SYMBOLIZER), "coverage");
context.getComponents().add(cl);
final GeneralEnvelope env = new GeneralEnvelope(CRS.forCode("EPSG:32632"));
env.setRange(0, -2574823.6832217844, 5487970.783439655);
env.setRange(1, 4289777.45228916, 1.0491927042028729E7);
final Hints hints = new Hints();
final SceneDef scenedef = new SceneDef(context, hints);
final CanvasDef canvasdef = new CanvasDef(new Dimension(800, 800), env);
canvasdef.setBackground(Color.WHITE);
final BufferedImage buffer = DefaultPortrayalService.portray(canvasdef, scenedef);
ImageIO.write(buffer, "PNG", new File("test.png"));
// We should obtain a green triangle crossing the image looking like this :
//
// |\
// |_\
// we can't test the shape so we test we found more and more green pixels on each line
// we expect to have a blue label at the center of the image
final int[] pixel = new int[4];
final int[] green = new int[] { 0, 255, 0, 255 };
int nbGreen = 0;
final Raster raster = buffer.getData();
for (int y = 0; y < 800; y++) {
int nb = 0;
for (int x = 0; x < 800; x++) {
raster.getPixel(x, y, pixel);
if (Arrays.equals(green, pixel)) {
nb++;
}
}
assertTrue("expected at least one green pixel", nb > 0);
assertTrue(nb >= nbGreen);
nbGreen = nb;
}
}
use of org.geotoolkit.display2d.service.SceneDef in project geotoolkit by Geomatys.
the class RasterSymbolizerTest method coverage_whose_grid_origin_is_lower_left_should_be_flipped.
/**
* Source coverage will be a matrix <em>with origin lower-left</em>:
* <table>
* <tr><td>4</td><td>3</td></tr>
* <tr><td>1</td><td>2</td></tr>
* </table>
* @throws PortrayalException
*/
@Test
public void coverage_whose_grid_origin_is_lower_left_should_be_flipped() throws PortrayalException {
final BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setSample(0, 0, 0, 1);
image.getRaster().setSample(1, 0, 0, 2);
image.getRaster().setSample(1, 1, 0, 3);
image.getRaster().setSample(0, 1, 0, 4);
final GridGeometry geom = new GridGeometry(new GridExtent(2, 2), PixelInCell.CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 10, 10), CommonCRS.defaultGeographic());
final GridCoverage baseData = new GridCoverage2D(geom, null, image);
MapLayer layer = MapBuilder.createLayer(new InMemoryGridCoverageResource(baseData));
final MapLayers ctx = MapBuilder.createContext();
ctx.getComponents().add(layer);
BufferedImage rendering = DefaultPortrayalService.portray(new CanvasDef(new Dimension(2, 2), geom.getEnvelope()), new SceneDef(ctx, new Hints(GO2Hints.KEY_INTERPOLATION, InterpolationCase.NEIGHBOR, RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)));
// As display is oriented upper-left, output should be flipped on y axis. Also, the renderer will stretch values
// along 256 colors, so we have to adapt comparison.
final int[] pixels = rendering.getRaster().getPixels(0, 0, 2, 2, (int[]) null);
final int[] expected = { 255, 255, 255, 255, 165, 165, 165, 255, 0, 0, 0, 255, 88, 88, 88, 255 };
assertArrayEquals(expected, pixels);
final ColorMap colorMap = SF.colorMap(SF.interpolateFunction(null, Arrays.asList(SF.interpolationPoint(1, FF.literal(Color.RED)), SF.interpolationPoint(2, FF.literal(Color.GREEN)), SF.interpolationPoint(3, FF.literal(Color.BLUE)), SF.interpolationPoint(4, FF.literal(Color.WHITE))), null, null, FF.literal(Color.BLACK)));
final RasterSymbolizer symbol = SF.rasterSymbolizer(null, null, null, null, colorMap, null, null, null);
ctx.getComponents().set(0, MapBuilder.createCoverageLayer(baseData, SF.style(symbol), "test"));
rendering = DefaultPortrayalService.portray(new CanvasDef(new Dimension(2, 2), geom.getEnvelope()), new SceneDef(ctx, new Hints(GO2Hints.KEY_INTERPOLATION, InterpolationCase.NEIGHBOR, RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)));
assertEquals(Color.WHITE.getRGB(), rendering.getRGB(0, 0));
assertEquals(Color.BLUE.getRGB(), rendering.getRGB(1, 0));
assertEquals(Color.RED.getRGB(), rendering.getRGB(0, 1));
assertEquals(Color.GREEN.getRGB(), rendering.getRGB(1, 1));
}
use of org.geotoolkit.display2d.service.SceneDef in project geotoolkit by Geomatys.
the class RasterSymbolizerTest method renderInterpolationCoverage.
/**
* Render a coverage with nearest and lanczos interpolation.
*/
@Test
public void renderInterpolationCoverage() throws FactoryException, PortrayalException, IOException {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
final BufferedImage image = new BufferedImage(36, 18, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, 18, 9);
g.setColor(Color.GREEN);
g.fillRect(18, 0, 18, 9);
g.setColor(Color.BLUE);
g.fillRect(0, 9, 18, 9);
g.setColor(Color.YELLOW);
g.fillRect(18, 9, 18, 9);
g.dispose();
final GridExtent extent = new GridExtent(36, 18);
final AffineTransform2D gridToCrs = new AffineTransform2D(10, 0, 0, -10, -180, 90);
final GridGeometry grid = new GridGeometry(extent, PixelInCell.CELL_CORNER, gridToCrs, crs);
final SampleDimension red = new SampleDimension.Builder().setName("1").build();
final SampleDimension green = new SampleDimension.Builder().setName("2").build();
final SampleDimension blue = new SampleDimension.Builder().setName("3").build();
final GridCoverage2D coverage = new GridCoverage2D(grid, Arrays.asList(red, green, blue), image);
final GridExtent queryextent = new GridExtent(360, 180);
final GeneralEnvelope queryenv = new GeneralEnvelope(crs);
queryenv.setRange(0, -180, 180);
queryenv.setRange(1, -90, 90);
final GridGeometry querygrid = new GridGeometry(queryextent, queryenv, GridOrientation.HOMOTHETY);
final MapLayers context = MapBuilder.createContext();
context.getComponents().add(MapBuilder.createCoverageLayer(new InMemoryGridCoverageResource(coverage)));
final BufferedImage nearest;
final BufferedImage bicubic;
final BufferedImage lanczos;
{
final Hints hints = new Hints();
hints.put(GO2Hints.KEY_INTERPOLATION, InterpolationCase.NEIGHBOR);
final CanvasDef cdef = new CanvasDef(querygrid);
final SceneDef sdef = new SceneDef(context, hints);
nearest = DefaultPortrayalService.portray(cdef, sdef);
}
{
final Hints hints = new Hints();
hints.put(GO2Hints.KEY_INTERPOLATION, InterpolationCase.BICUBIC2);
final CanvasDef cdef = new CanvasDef(querygrid);
final SceneDef sdef = new SceneDef(context, hints);
bicubic = DefaultPortrayalService.portray(cdef, sdef);
}
{
final Hints hints = new Hints();
hints.put(GO2Hints.KEY_INTERPOLATION, InterpolationCase.LANCZOS);
final CanvasDef cdef = new CanvasDef(querygrid);
final SceneDef sdef = new SceneDef(context, hints);
lanczos = DefaultPortrayalService.portray(cdef, sdef);
}
int nearestRgb = nearest.getRGB(179, 0);
int bicubicRgb = bicubic.getRGB(179, 0);
int naczosRgb = lanczos.getRGB(179, 0);
assertTrue(nearestRgb != bicubicRgb);
assertTrue(bicubicRgb != naczosRgb);
}
Aggregations