Search in sources :

Example 41 with WritableImage

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

the class Canvas3 method thread_main.

private void thread_main() {
    final Semaphore done = new Semaphore(0);
    try {
        final double lambda = 50.0;
        int to_update = 0;
        while (true) {
            // Draw image off-screen
            long start = System.currentTimeMillis();
            final WritableImage active = image[to_update];
            // PixelWriter is very limited, but might be just what's needed
            // for a display.builder "IntensityGraph".
            final PixelWriter writer = active.getPixelWriter();
            final double phase = System.currentTimeMillis() / 2000.0;
            // Different regions of image could be prepared on parallel threads
            for (int x = 0; x < IMAGE_WIDTH; ++x) {
                final int dx = (x - IMAGE_WIDTH / 2);
                final long dx2 = dx * dx;
                for (int y = 0; y < IMAGE_HEIGHT; ++y) {
                    final long dy = (y - IMAGE_HEIGHT / 2);
                    final double dist = Math.sqrt(dx2 + dy * dy);
                    final double level = 0.5 + 0.5 * Math.cos(2 * Math.PI * (dist / lambda - phase));
                    // final Color c = Color.hsb(level*360.0, 1.0, 1.0);
                    final Color c = Color.gray(level);
                    writer.setColor(x, y, c);
                }
            }
            long ms = System.currentTimeMillis() - start;
            if (draw_ms < 0)
                draw_ms = ms;
            else
                draw_ms = (draw_ms * 9 + ms) / 10;
            counter.incrementAndGet();
            // Show image in canvas on UI thread
            start = System.currentTimeMillis();
            Platform.runLater(() -> {
                final GraphicsContext gc = canvas.getGraphicsContext2D();
                gc.drawImage(active, 0, 0, canvas.getWidth(), canvas.getHeight());
                updates.setText(Long.toString(counter.get()));
                done.release();
            });
            // Wait for UI thread
            done.acquire();
            ms = System.currentTimeMillis() - start;
            if (update_ms < 0)
                update_ms = ms;
            else
                update_ms = (update_ms * 9 + ms) / 10;
            to_update = 1 - to_update;
            Thread.sleep(20);
            if ((counter.get() % 50) == 0) {
                System.out.println("Drawing: " + draw_ms + " ms");
                System.out.println("Update : " + update_ms + " ms");
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : WritableImage(javafx.scene.image.WritableImage) GraphicsContext(javafx.scene.canvas.GraphicsContext) Color(javafx.scene.paint.Color) PixelWriter(javafx.scene.image.PixelWriter) Semaphore(java.util.concurrent.Semaphore)

Example 42 with WritableImage

use of javafx.scene.image.WritableImage in project contentment by GeePawHill.

the class JfxUtility method capture.

public static void capture(Node node) {
    WritableImage image = node.snapshot(new SnapshotParameters(), null);
    // TODO: probably use a file chooser here
    File file = new File("output_0.png");
    int i = 1;
    while (file.exists()) file = new File("output_" + i++ + ".png");
    try {
        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
    } catch (IOException e) {
    }
}
Also used : WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) IOException(java.io.IOException) File(java.io.File) Paint(javafx.scene.paint.Paint)

Example 43 with WritableImage

use of javafx.scene.image.WritableImage in project StegMeister by GaleForce89.

the class BasicEncoder method encodeImage.

public Image encodeImage(Image image, String message) {
    int width = (int) image.getWidth();
    int height = (int) image.getHeight();
    WritableImage copy = new WritableImage(image.getPixelReader(), width, height);
    PixelWriter writer = copy.getPixelWriter();
    PixelReader reader = image.getPixelReader();
    boolean[] bits = encodeImage(message);
    IntStream.range(0, bits.length).mapToObj(i -> new Pair<>(i, reader.getArgb(i % width, i / width))).map(pair -> new Pair<>(pair.getKey(), bits[pair.getKey()] ? pair.getValue() | 1 : pair.getValue() & ~1)).forEach(pair -> {
        int x = pair.getKey() % width;
        int y = pair.getKey() / width;
        writer.setArgb(x, y, pair.getValue());
    });
    return copy;
}
Also used : IntStream(java.util.stream.IntStream) PixelReader(javafx.scene.image.PixelReader) Pair(javafx.util.Pair) PixelWriter(javafx.scene.image.PixelWriter) WritableImage(javafx.scene.image.WritableImage) Image(javafx.scene.image.Image) WritableImage(javafx.scene.image.WritableImage) PixelReader(javafx.scene.image.PixelReader) PixelWriter(javafx.scene.image.PixelWriter) Pair(javafx.util.Pair)

Example 44 with WritableImage

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

the class ImageUtil method processMagenta.

/*
	 * This is for Legacy support but can cause magenta bleed on edges if there is transparency overlap. The preferred overlay storage is now PhotoShop PSD format with layer 1 containing the mask and
	 * layer 2 containing the image
	 */
private static Image processMagenta(Image inputImage, int colorThreshold, boolean overlayWanted) {
    int imageWidth = (int) inputImage.getWidth();
    int imageHeight = (int) inputImage.getHeight();
    WritableImage outputImage = new WritableImage(imageWidth, imageHeight);
    PixelReader pixelReader = inputImage.getPixelReader();
    PixelWriter pixelWriter = outputImage.getPixelWriter();
    for (int readY = 0; readY < imageHeight; readY++) {
        for (int readX = 0; readX < imageWidth; readX++) {
            Color pixelColor = pixelReader.getColor(readX, readY);
            if (isMagenta(pixelColor, COLOR_THRESHOLD) == overlayWanted)
                pixelWriter.setColor(readX, readY, Color.TRANSPARENT);
            else
                pixelWriter.setColor(readX, readY, pixelColor);
        }
    }
    return outputImage;
}
Also used : WritableImage(javafx.scene.image.WritableImage) PixelReader(javafx.scene.image.PixelReader) Color(javafx.scene.paint.Color) PixelWriter(javafx.scene.image.PixelWriter)

Example 45 with WritableImage

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

the class ImageUtil method clipImageWithMask.

/*
	 * Return the intersection between the source image and the mask. Note, the mask does not need to be magenta anymore, any non-transparent pixel is considering a mask
	 */
private static Image clipImageWithMask(Image imageSource, Image imageMask) {
    int imageWidth = (int) imageMask.getWidth();
    int imageHeight = (int) imageMask.getHeight();
    WritableImage outputImage = new WritableImage(imageWidth, imageHeight);
    PixelReader pixelReader_Mask = imageMask.getPixelReader();
    PixelReader pixelReader_Source = imageSource.getPixelReader();
    PixelWriter pixelWriter = outputImage.getPixelWriter();
    for (int readY = 0; readY < imageHeight; readY++) {
        for (int readX = 0; readX < imageWidth; readX++) {
            Color pixelColor = pixelReader_Mask.getColor(readX, readY);
            if (pixelColor.equals(Color.TRANSPARENT))
                pixelWriter.setColor(readX, readY, pixelReader_Source.getColor(readX, readY));
        }
    }
    return outputImage;
}
Also used : WritableImage(javafx.scene.image.WritableImage) PixelReader(javafx.scene.image.PixelReader) Color(javafx.scene.paint.Color) PixelWriter(javafx.scene.image.PixelWriter)

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