Search in sources :

Example 1 with SolidColorPainter

use of org.geotoolkit.display2d.canvas.painter.SolidColorPainter in project geotoolkit by Geomatys.

the class DefaultPortrayalService method prepareCanvas.

public static void prepareCanvas(final J2DCanvas canvas, final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
    final ContextContainer2D renderer = new ContextContainer2D(canvas);
    canvas.setContainer(renderer);
    final Color bgColor = canvasDef.getBackground();
    if (bgColor != null) {
        canvas.setBackgroundPainter(new SolidColorPainter(bgColor));
    }
    final CanvasMonitor monitor = canvasDef.getMonitor();
    if (monitor != null) {
        canvas.setMonitor(monitor);
    }
    final Hints hints = sceneDef.getHints();
    if (hints != null) {
        for (Entry<?, ?> entry : hints.entrySet()) {
            canvas.setRenderingHint((Key) entry.getKey(), entry.getValue());
        }
    }
    final MapLayers context = sceneDef.getContext();
    renderer.setContext(context);
    GridGeometry gridGeometry = canvasDef.getGridGeometry();
    if (gridGeometry != null) {
        try {
            canvas.setGridGeometry(gridGeometry);
        } catch (FactoryException ex) {
            throw new PortrayalException("Could not set objective crs", ex);
        }
    } else {
        final Envelope contextEnv = canvasDef.getEnvelope();
        final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
        try {
            canvas.setObjectiveCRS(crs);
        } catch (TransformException | FactoryException ex) {
            throw new PortrayalException("Could not set objective crs", ex);
        }
        // we specifically say to not repect X/Y proportions
        canvas.setAxisProportions(!canvasDef.isStretchImage());
        // setVisibleArea -> setAxisRange -> setRange.
        if (contextEnv != null) {
            try {
                canvas.setGridGeometry(canvasDef.getOrCreateGridGeometry());
            } catch (Exception e) {
                // Rollback to previous behavior
                try {
                    canvas.setVisibleArea(contextEnv);
                    if (canvasDef.getAzimuth() != 0) {
                        canvas.rotate(-Math.toRadians(canvasDef.getAzimuth()));
                    }
                } catch (NoninvertibleTransformException | TransformException ex) {
                    ex.addSuppressed(e);
                    throw new PortrayalException(ex);
                }
            }
        }
    }
    // paints all extensions
    final List<PortrayalExtension> extensions = sceneDef.extensions();
    if (extensions != null) {
        for (final PortrayalExtension extension : extensions) {
            if (extension != null)
                extension.completeCanvas(canvas);
        }
    }
}
Also used : SolidColorPainter(org.geotoolkit.display2d.canvas.painter.SolidColorPainter) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) Hints(org.geotoolkit.factory.Hints) GO2Hints(org.geotoolkit.display2d.GO2Hints) FactoryException(org.opengis.util.FactoryException) ContextContainer2D(org.geotoolkit.display2d.container.ContextContainer2D) Color(java.awt.Color) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) Envelope(org.opengis.geometry.Envelope) PortrayalException(org.geotoolkit.display.PortrayalException) FactoryException(org.opengis.util.FactoryException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) CanvasMonitor(org.geotoolkit.display.canvas.control.CanvasMonitor) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MapLayers(org.apache.sis.portrayal.MapLayers) PortrayalException(org.geotoolkit.display.PortrayalException)

Example 2 with SolidColorPainter

use of org.geotoolkit.display2d.canvas.painter.SolidColorPainter in project geotoolkit by Geomatys.

the class J2DCanvasBuffered method createBufferedImage.

/**
 * This will try to create the most efficient bufferedImage knowing
 * the different rendering parameters and hints.
 */
private BufferedImage createBufferedImage(final Dimension dim) {
    // See if a color model has been set, if so use it.
    final ColorModel cm = (ColorModel) getRenderingHint(GO2Hints.KEY_COLOR_MODEL);
    if (cm != null) {
        return new BufferedImage(cm, cm.createCompatibleWritableRaster(dim.width, dim.height), cm.isAlphaPremultiplied(), null);
    }
    // Get the Anti-aliasing value;
    final boolean AA;
    final Object val = getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    if (RenderingHints.VALUE_ANTIALIAS_ON == val) {
        AA = true;
    } else {
        // force AA off, to replace default value.
        setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        AA = false;
    }
    // check background painter.
    if (painter == null) {
        if (AA) {
            // Anti-aliasing enable, unpredictable colors
            return new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
        }
        // check graphic object and see if we can predict colors
        final Set<Integer> colors = extractColors(getContainer().flatten(true));
        if (colors != null) {
            // translucent background
            colors.add(0);
            // we succesfully predicted the colors, makes an index color model
            return createBufferedImage(dim, colors);
        } else {
            // we can't use a index color model, use an ARGB palette
            return new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
        }
    } else {
        if (painter.isOpaque()) {
            // see of we can determinate the background color, only if there is no AA.
            if (!AA && painter instanceof SolidColorPainter) {
                // check graphic object and see if we can predict colors
                final Set<Integer> colors = extractColors(getContainer().flatten(true));
                if (colors != null) {
                    final Color bgColor = ((SolidColorPainter) painter).getColor();
                    colors.add(bgColor.getRGB());
                    // we succesfully predicted the colors, makes an index color model
                    return createBufferedImage(dim, colors);
                } else {
                    // Bug OpenJDK : issue with TYPE_INT_RGB, replace by TYPE_3BYTE_BGR
                    return new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_3BYTE_BGR);
                }
            } else {
                // Bug OpenJDK : issue with TYPE_INT_RGB, replace by TYPE_3BYTE_BGR
                return new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_3BYTE_BGR);
            }
        } else {
            // we can't determinate the background colors, use an ARGB palette
            return new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
        }
    }
}
Also used : SolidColorPainter(org.geotoolkit.display2d.canvas.painter.SolidColorPainter) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage)

Example 3 with SolidColorPainter

use of org.geotoolkit.display2d.canvas.painter.SolidColorPainter in project geotoolkit by Geomatys.

the class Portrayer method portray.

public BufferedImage portray(final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
    final Envelope contextEnv = canvasDef.getEnvelope();
    final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
    canvas.setSize(canvasDef.getDimension());
    canvas.setRenderingHints(sceneDef.getHints());
    final Color bgColor = canvasDef.getBackground();
    if (bgColor != null) {
        canvas.setBackgroundPainter(new SolidColorPainter(bgColor));
    }
    final CanvasMonitor monitor = canvasDef.getMonitor();
    if (monitor != null) {
        canvas.setMonitor(monitor);
    }
    final MapLayers context = sceneDef.getContext();
    container.setContext(context);
    try {
        canvas.setObjectiveCRS(crs);
    } catch (TransformException | FactoryException ex) {
        throw new PortrayalException("Could not set objective crs", ex);
    }
    // we specifically say to not repect X/Y proportions
    canvas.setAxisProportions(!canvasDef.isStretchImage());
    try {
        canvas.setVisibleArea(contextEnv);
        if (canvasDef.getAzimuth() != 0) {
            canvas.rotate(-Math.toRadians(canvasDef.getAzimuth()));
        }
    } catch (NoninvertibleTransformException ex) {
        throw new PortrayalException(ex);
    } catch (TransformException ex) {
        throw new PortrayalException(ex);
    }
    // paints all extensions
    final List<PortrayalExtension> extensions = sceneDef.extensions();
    if (extensions != null) {
        for (final PortrayalExtension extension : extensions) {
            if (extension != null)
                extension.completeCanvas(canvas);
        }
    }
    canvas.repaint();
    final BufferedImage buffer = canvas.getSnapShot();
    container.setContext(EMPTY_CONTEXT);
    return buffer;
}
Also used : SolidColorPainter(org.geotoolkit.display2d.canvas.painter.SolidColorPainter) FactoryException(org.opengis.util.FactoryException) Color(java.awt.Color) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) Envelope(org.opengis.geometry.Envelope) BufferedImage(java.awt.image.BufferedImage) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) CanvasMonitor(org.geotoolkit.display.canvas.control.CanvasMonitor) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MapLayers(org.apache.sis.portrayal.MapLayers) PortrayalException(org.geotoolkit.display.PortrayalException)

Aggregations

Color (java.awt.Color)3 SolidColorPainter (org.geotoolkit.display2d.canvas.painter.SolidColorPainter)3 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)2 BufferedImage (java.awt.image.BufferedImage)2 MapLayers (org.apache.sis.portrayal.MapLayers)2 PortrayalException (org.geotoolkit.display.PortrayalException)2 CanvasMonitor (org.geotoolkit.display.canvas.control.CanvasMonitor)2 Envelope (org.opengis.geometry.Envelope)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 TransformException (org.opengis.referencing.operation.TransformException)2 FactoryException (org.opengis.util.FactoryException)2 ColorModel (java.awt.image.ColorModel)1 IndexColorModel (java.awt.image.IndexColorModel)1 IOException (java.io.IOException)1 IIOException (javax.imageio.IIOException)1 DisjointExtentException (org.apache.sis.coverage.grid.DisjointExtentException)1 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 GO2Hints (org.geotoolkit.display2d.GO2Hints)1 ContextContainer2D (org.geotoolkit.display2d.container.ContextContainer2D)1