Search in sources :

Example 16 with WritableImage

use of javafx.scene.image.WritableImage in project tokentool by RPTools.

the class ImageUtil method composePreview.

public static Image composePreview(StackPane compositeTokenPane, Color bgColor, ImageView portraitImageView, ImageView maskImageView, ImageView overlayImageView, boolean useAsBase, boolean clipImage) {
    // Process layout as maskImage may have changed size if the overlay was changed
    compositeTokenPane.layout();
    SnapshotParameters parameter = new SnapshotParameters();
    Image finalImage = null;
    Group blend;
    if (clipImage) {
        // We need to clip the portrait image first then blend the overlay image over it
        // We will first get a snapshot of the portrait equal to the mask overlay image width/height
        double x, y, width, height;
        x = maskImageView.getParent().getLayoutX();
        y = maskImageView.getParent().getLayoutY();
        width = maskImageView.getFitWidth();
        height = maskImageView.getFitHeight();
        Rectangle2D viewPort = new Rectangle2D(x, y, width, height);
        Rectangle2D maskViewPort = new Rectangle2D(1, 1, width, height);
        WritableImage newImage = new WritableImage((int) width, (int) height);
        WritableImage newMaskImage = new WritableImage((int) width, (int) height);
        ImageView overlayCopyImageView = new ImageView();
        ImageView clippedImageView = new ImageView();
        parameter.setViewport(viewPort);
        parameter.setFill(bgColor);
        portraitImageView.snapshot(parameter, newImage);
        parameter.setViewport(maskViewPort);
        parameter.setFill(Color.TRANSPARENT);
        maskImageView.setVisible(true);
        maskImageView.snapshot(parameter, newMaskImage);
        maskImageView.setVisible(false);
        clippedImageView.setFitWidth(width);
        clippedImageView.setFitHeight(height);
        clippedImageView.setImage(clipImageWithMask(newImage, newMaskImage));
        // Our masked portrait image is now stored in clippedImageView, lets now blend the overlay image over it
        // We'll create a temporary group to hold our temporary ImageViews's and blend them and take a snapshot
        overlayCopyImageView.setImage(overlayImageView.getImage());
        overlayCopyImageView.setFitWidth(overlayImageView.getFitWidth());
        overlayCopyImageView.setFitHeight(overlayImageView.getFitHeight());
        overlayCopyImageView.setOpacity(overlayImageView.getOpacity());
        if (useAsBase) {
            blend = new Group(overlayCopyImageView, clippedImageView);
        } else {
            blend = new Group(clippedImageView, overlayCopyImageView);
        }
        // Last, we'll clean up any excess transparent edges by cropping it
        finalImage = autoCropImage(blend.snapshot(parameter, null));
    } else {
        parameter.setFill(Color.TRANSPARENT);
        finalImage = autoCropImage(compositeTokenPane.snapshot(parameter, null));
    }
    return finalImage;
}
Also used : Group(javafx.scene.Group) WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) Rectangle2D(javafx.geometry.Rectangle2D) ImageView(javafx.scene.image.ImageView) BufferedImage(java.awt.image.BufferedImage) WritableImage(javafx.scene.image.WritableImage) Image(javafx.scene.image.Image)

Example 17 with WritableImage

use of javafx.scene.image.WritableImage in project org.csstudio.display.builder by kasemir.

the class ImageScaling method start.

@Override
public void start(final Stage stage) {
    // Image with red border
    final WritableImage image = new WritableImage(WIDTH, HEIGHT);
    final PixelWriter writer = image.getPixelWriter();
    for (int x = 0; x < WIDTH; ++x) {
        writer.setColor(x, 0, Color.RED);
        writer.setColor(x, HEIGHT - 1, Color.RED);
    }
    for (int y = 0; y < HEIGHT; ++y) {
        writer.setColor(0, y, Color.RED);
        writer.setColor(WIDTH - 1, y, Color.RED);
    }
    // Draw into canvas, scaling 'up'
    final Canvas canvas = new Canvas(800, 600);
    canvas.getGraphicsContext2D().drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight());
    final Scene scene = new Scene(new Group(canvas), canvas.getWidth(), canvas.getHeight());
    stage.setScene(scene);
    stage.show();
}
Also used : WritableImage(javafx.scene.image.WritableImage) Group(javafx.scene.Group) Canvas(javafx.scene.canvas.Canvas) PixelWriter(javafx.scene.image.PixelWriter) Scene(javafx.scene.Scene)

Example 18 with WritableImage

use of javafx.scene.image.WritableImage in project org.csstudio.display.builder by kasemir.

the class WidgetTransfer method createDragImage.

/**
 * Create a image representing the dragged widget.
 *
 * @param widget The {@link Widget} being dragged.
 * @param image The widget's type image. Can be {@code null}.
 * @return An {@link Image} instance.
 */
private static Image createDragImage(final Widget widget, final Image image, final int width, final int height) {
    final BufferedImage bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g2d = bImage.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2d.setBackground(TRANSPARENT);
    g2d.clearRect(0, 0, width, height);
    g2d.setColor(Color.ORANGE);
    g2d.setStroke(OUTLINE_STROKE);
    g2d.drawRect(0, 0, width, height);
    if (image != null) {
        int w = (int) image.getWidth();
        int h = (int) image.getHeight();
        BufferedImage bbImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        SwingFXUtils.fromFXImage(image, bbImage);
        g2d.drawImage(bbImage, (int) ((width - w) / 2.0), (int) ((height - h) / 2.0), null);
    }
    final WritableImage dImage = new WritableImage(width, height);
    SwingFXUtils.toFXImage(bImage, dImage);
    return dImage;
}
Also used : WritableImage(javafx.scene.image.WritableImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 19 with WritableImage

use of javafx.scene.image.WritableImage in project org.csstudio.display.builder by kasemir.

the class Screenshot method fromNode.

public static BufferedImage fromNode(Node node) {
    final WritableImage jfx = node.snapshot(null, null);
    final BufferedImage img = new BufferedImage((int) jfx.getWidth(), (int) jfx.getHeight(), BufferedImage.TYPE_INT_ARGB);
    SwingFXUtils.fromFXImage(jfx, img);
    return img;
}
Also used : WritableImage(javafx.scene.image.WritableImage) BufferedImage(java.awt.image.BufferedImage)

Example 20 with WritableImage

use of javafx.scene.image.WritableImage in project org.csstudio.display.builder by kasemir.

the class JFXRepresentation method updateBackground.

/**
 * Update background, using background color and grid information from model
 */
private void updateBackground() {
    final WidgetColor background = model.propBackgroundColor().getValue();
    // Setting the "-fx-background:" of the root node propagates
    // to all child nodes in the scene graph.
    // 
    // if (isEditMode())
    // model_root.setStyle("-fx-background: linear-gradient(from 0px 0px to 10px 10px, reflect, #D2A2A2 48%, #D2A2A2 2%, #D2D2A2 48% #D2D2A2 2%)");
    // else
    // model_root.setStyle("-fx-background: " + JFXUtil.webRGB(background));
    // 
    // In edit mode, this results in error messages because the linear-gradient doesn't "work" for all nodes:
    // 
    // javafx.scene.CssStyleHelper (calculateValue)
    // Caught java.lang.ClassCastException: javafx.scene.paint.LinearGradient cannot be cast to javafx.scene.paint.Color
    // while converting value for
    // '-fx-background-color' from rule '*.text-input' in stylesheet ..jfxrt.jar!/com/sun/javafx/scene/control/skin/modena/modena.bss
    // '-fx-effect' from rule '*.scroll-bar:vertical>*.increment-button>*.increment-arrow' in StyleSheet ...  jfxrt.jar!/com/sun/javafx/scene/control/skin/modena/modena.bss
    // '-fx-effect' from rule '*.scroll-bar:vertical>*.decrement-button>*.decrement-arrow' in stylesheet ... modena.bss
    // '-fx-effect' from rule '*.scroll-bar:horizontal>*.increment-button>*.increment-arrow' in stylesheet ... modena.bss
    // 
    // In the runtime, the background color style is applied to for example the TextEntryRepresentation,
    // overriding its jfx_node.setBackground(..) setting.
    // Setting just the scroll body background to a plain color or grid image provides basic color control.
    // In edit mode, the horiz_bound, vert_bound lines and grid provide sufficient
    // visual indication of the display size.
    final Color backgroundColor = new Color(background.getRed(), background.getGreen(), background.getBlue());
    final boolean gridVisible = isEditMode() ? model.propGridVisible().getValue() : false;
    final int gridStepX = model.propGridStepX().getValue(), gridStepY = model.propGridStepY().getValue();
    final WidgetColor grid_rgb = model.propGridColor().getValue();
    final Color gridColor = new Color(grid_rgb.getRed(), grid_rgb.getGreen(), grid_rgb.getBlue());
    final BufferedImage image = new BufferedImage(gridStepX, gridStepY, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2d.setBackground(backgroundColor);
    g2d.clearRect(0, 0, gridStepX, gridStepY);
    if (gridVisible) {
        g2d.setColor(gridColor);
        g2d.setStroke(new BasicStroke(GRID_LINE_WIDTH));
        g2d.drawLine(0, 0, gridStepX, 0);
        g2d.drawLine(0, 0, 0, gridStepY);
    }
    final WritableImage wimage = new WritableImage(gridStepX, gridStepY);
    SwingFXUtils.toFXImage(image, wimage);
    final ImagePattern pattern = new ImagePattern(wimage, 0, 0, gridStepX, gridStepY, false);
    widget_parent.setBackground(new Background(new BackgroundFill(pattern, CornerRadii.EMPTY, Insets.EMPTY)));
}
Also used : BasicStroke(java.awt.BasicStroke) WritableImage(javafx.scene.image.WritableImage) Background(javafx.scene.layout.Background) WidgetColor(org.csstudio.display.builder.model.properties.WidgetColor) Color(java.awt.Color) WidgetColor(org.csstudio.display.builder.model.properties.WidgetColor) BackgroundFill(javafx.scene.layout.BackgroundFill) ImagePattern(javafx.scene.paint.ImagePattern) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

WritableImage (javafx.scene.image.WritableImage)70 SnapshotParameters (javafx.scene.SnapshotParameters)24 PixelWriter (javafx.scene.image.PixelWriter)21 PixelReader (javafx.scene.image.PixelReader)13 IOException (java.io.IOException)12 Color (javafx.scene.paint.Color)11 BufferedImage (java.awt.image.BufferedImage)9 File (java.io.File)8 Image (javafx.scene.image.Image)6 ImageView (javafx.scene.image.ImageView)6 Group (javafx.scene.Group)5 Random (java.util.Random)4 Canvas (javafx.scene.canvas.Canvas)4 GraphicsContext (javafx.scene.canvas.GraphicsContext)4 Texture (com.almasb.fxgl.texture.Texture)3 OutputStream (java.io.OutputStream)3 Point2D (javafx.geometry.Point2D)3 Scene (javafx.scene.Scene)3 Background (javafx.scene.layout.Background)3 Scale (javafx.scene.transform.Scale)3