Search in sources :

Example 31 with WritableImage

use of javafx.scene.image.WritableImage in project FXyzLib by Birdasaur.

the class NormalMap method buildNormalMap.

private void buildNormalMap(double scale, double scaleFactor, boolean invert) {
    pReader = srcImage.getPixelReader();
    final int w = (int) srcImage.getWidth();
    final int h = (int) srcImage.getHeight();
    WritableImage gray = new WritableImage(w, h);
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            gray.getPixelWriter().setColor(x, y, pReader.getColor(x, y).grayscale());
        }
    }
    final byte[] heightPixels = new byte[w * h * 4];
    final byte[] normalPixels = new byte[w * h * 4];
    // get pixels
    pReader = gray.getPixelReader();
    pReader.getPixels(0, 0, w, h, PixelFormat.getByteBgraInstance(), heightPixels, 0, w * 4);
    if (invert) {
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                final int pixelIndex = (y * w * 4) + (x * 4);
                heightPixels[pixelIndex + 0] = (byte) (255 - Byte.toUnsignedInt(heightPixels[pixelIndex]));
                heightPixels[pixelIndex + 1] = (byte) (255 - Byte.toUnsignedInt(heightPixels[pixelIndex + 1]));
                heightPixels[pixelIndex + 2] = (byte) (255 - Byte.toUnsignedInt(heightPixels[pixelIndex + 2]));
                heightPixels[pixelIndex + 3] = (byte) (heightPixels[pixelIndex + 3]);
            }
        }
    }
    // generate normal map
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            final int yAbove = Math.max(0, y - 1);
            final int yBelow = Math.min(h - 1, y + 1);
            final int xLeft = Math.max(0, x - 1);
            final int xRight = Math.min(w - 1, x + 1);
            final int pixelIndex = (y * w * 4) + (x * 4);
            final int pixelAboveIndex = (yAbove * w * 4) + (x * 4);
            final int pixelBelowIndex = (yBelow * w * 4) + (x * 4);
            final int pixelLeftIndex = (y * w * 4) + (xLeft * 4);
            final int pixelRightIndex = (y * w * 4) + (xRight * 4);
            final int pixelAboveHeight = Byte.toUnsignedInt(heightPixels[pixelAboveIndex]);
            final int pixelBelowHeight = Byte.toUnsignedInt(heightPixels[pixelBelowIndex]);
            final int pixelLeftHeight = Byte.toUnsignedInt(heightPixels[pixelLeftIndex]);
            final int pixelRightHeight = Byte.toUnsignedInt(heightPixels[pixelRightIndex]);
            Point3D pixelAbove = new Point3D(x, yAbove, pixelAboveHeight);
            Point3D pixelBelow = new Point3D(x, yBelow, pixelBelowHeight);
            Point3D pixelLeft = new Point3D(xLeft, y, pixelLeftHeight);
            Point3D pixelRight = new Point3D(xRight, y, pixelRightHeight);
            Point3D H = pixelLeft.subtract(pixelRight);
            Point3D V = pixelAbove.subtract(pixelBelow);
            Point3D normal = H.crossProduct(V);
            normal = new Point3D((normal.getX() / w), (normal.getY() / h), (1 / normal.getZ()) / (Math.max(1.0, scale) / Math.min(10, scaleFactor))).normalize();
            // Blue
            normalPixels[pixelIndex + 0] = (byte) (255 - (normal.getZ()));
            // Green
            normalPixels[pixelIndex + 1] = (byte) (128 + (normal.getY() * 128.0));
            // Red
            normalPixels[pixelIndex + 2] = (byte) (128 + (normal.getX() * 128.0));
            // alpha
            normalPixels[pixelIndex + 3] = (byte) (255);
        }
    }
    // create output image
    pWriter.setPixels(0, 0, w, h, PixelFormat.getByteBgraPreInstance(), normalPixels, 0, w * 4);
}
Also used : WritableImage(javafx.scene.image.WritableImage) Point3D(javafx.geometry.Point3D)

Example 32 with WritableImage

use of javafx.scene.image.WritableImage in project intellij-plugins by StepicOrg.

the class AuthDialog method createButtonWithImage.

@NotNull
private Button createButtonWithImage(@NotNull Icon icon) {
    BufferedImage bImg = UIUtil.createImage(icon.getIconWidth(), icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bImg.createGraphics();
    icon.paintIcon(null, graphics, 0, 0);
    graphics.dispose();
    WritableImage image = SwingFXUtils.toFXImage(bImg, null);
    return new Button(null, new ImageView(image));
}
Also used : WritableImage(javafx.scene.image.WritableImage) Button(javafx.scene.control.Button) ImageView(javafx.scene.image.ImageView) BufferedImage(java.awt.image.BufferedImage) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with WritableImage

use of javafx.scene.image.WritableImage in project JFoenix by jfoenixadmin.

the class JFXColorPickerUI method getSLCricle.

private Image getSLCricle(int width, int height, List<Stop> stops) {
    WritableImage raster = new WritableImage(width, height);
    PixelWriter pixelWriter = raster.getPixelWriter();
    Point2D center = new Point2D((double) width / 2, (double) height / 2);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            double dy = x - center.getX();
            double dx = y - center.getY();
            pixelWriter.setColor(x, y, getColor(dx, dy));
        }
    }
    return raster;
}
Also used : WritableImage(javafx.scene.image.WritableImage) Point2D(javafx.geometry.Point2D) PixelWriter(javafx.scene.image.PixelWriter)

Example 34 with WritableImage

use of javafx.scene.image.WritableImage in project JFoenix by jfoenixadmin.

the class JFXColorPickerUI method getHuesCircle.

private Image getHuesCircle(int width, int height, List<Stop> stops) {
    WritableImage raster = new WritableImage(width, height);
    PixelWriter pixelWriter = raster.getPixelWriter();
    Point2D center = new Point2D((double) width / 2, (double) height / 2);
    double rsmall = 0.8 * width / 2;
    double rbig = (double) width / 2;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            double dx = x - center.getX();
            double dy = y - center.getY();
            double distance = Math.sqrt((dx * dx) + (dy * dy));
            double o = Math.atan2(dy, dx);
            if (distance > rsmall && distance < rbig) {
                double H = map(o, -Math.PI, Math.PI, 0, 255);
                double S = 255;
                double L = 152;
                pixelWriter.setColor(x, y, HSL2RGB(H, S, L));
            }
        }
    }
    return raster;
}
Also used : WritableImage(javafx.scene.image.WritableImage) Point2D(javafx.geometry.Point2D) PixelWriter(javafx.scene.image.PixelWriter)

Example 35 with WritableImage

use of javafx.scene.image.WritableImage in project JFoenix by jfoenixadmin.

the class AnimatedFlowContainer method updatePlaceholder.

private void updatePlaceholder(Node newView) {
    if (root.getWidth() > 0 && root.getHeight() > 0) {
        SnapshotParameters parameters = new SnapshotParameters();
        parameters.setFill(Color.TRANSPARENT);
        Image placeholderImage = root.snapshot(parameters, new WritableImage((int) root.getWidth(), (int) root.getHeight()));
        placeholder.setImage(placeholderImage);
        placeholder.setFitWidth(placeholderImage.getWidth());
        placeholder.setFitHeight(placeholderImage.getHeight());
    } else {
        placeholder.setImage(null);
    }
    placeholder.setVisible(true);
    placeholder.setOpacity(1.0);
    root.getChildren().setAll(placeholder, newView);
    placeholder.toFront();
}
Also used : WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) WritableImage(javafx.scene.image.WritableImage) Image(javafx.scene.image.Image)

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