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);
}
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));
}
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;
}
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;
}
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();
}
Aggregations