From 095dad2b204f9da4ba32282560635ba36cb42a34 Mon Sep 17 00:00:00 2001 From: HenrikJannsen Date: Sat, 30 Nov 2024 12:40:07 +0700 Subject: [PATCH] Move out cathash methods to new class --- .../common/utils/CatHashImageUtil.java | 108 ++++++++++++++++++ .../bisq/desktop/common/utils/ImageUtil.java | 73 ------------ .../cathash/JavaFxCatHashService.java | 8 +- 3 files changed, 112 insertions(+), 77 deletions(-) create mode 100644 apps/desktop/desktop/src/main/java/bisq/desktop/common/utils/CatHashImageUtil.java diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/common/utils/CatHashImageUtil.java b/apps/desktop/desktop/src/main/java/bisq/desktop/common/utils/CatHashImageUtil.java new file mode 100644 index 0000000000..12066eb510 --- /dev/null +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/common/utils/CatHashImageUtil.java @@ -0,0 +1,108 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.desktop.common.utils; + +import bisq.common.file.FileUtils; +import javafx.scene.SnapshotParameters; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.image.*; +import javafx.scene.paint.Color; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; + +public class CatHashImageUtil { + private static final String BASE_PATH = "images/cathash/"; + + public static Image composeImage(String[] paths, double size) { + return composeImage(paths, size, size); + } + + public static Image composeImage(String[] paths, double width, double height) { + return composeImage(paths, BASE_PATH, width, height); + } + + public static Image composeImage(String[] paths, String basePath, double width, double height) { + Canvas canvas = new Canvas(); + canvas.setWidth(width); + canvas.setHeight(height); + GraphicsContext graphicsContext2D = canvas.getGraphicsContext2D(); + + double radius = Math.min(height, width) / 2d; + double x = width / 2d; + double y = height / 2d; + graphicsContext2D.beginPath(); + graphicsContext2D.moveTo(x - radius, y); + graphicsContext2D.arc(x, y, radius, radius, 180, 360); + graphicsContext2D.closePath(); + graphicsContext2D.clip(); + + for (String path : paths) { + graphicsContext2D.drawImage(new Image(basePath + path), 0, 0, width, height); + } + SnapshotParameters snapshotParameters = new SnapshotParameters(); + snapshotParameters.setFill(Color.TRANSPARENT); + return canvas.snapshot(snapshotParameters, null); + } + + public static Image readRawImage(File file) throws IOException { + byte[] rawData = FileUtils.read(file.getAbsolutePath()); + return byteArrayToImage(rawData); + } + + public static void writeRawImage(Image image, File file) throws IOException { + byte[] rawData = imageToByteArray(image); + FileUtils.write(file.getAbsolutePath(), rawData); + } + + public static Image byteArrayToImage(byte[] data) { + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data); + + int width = byteArrayInputStream.read(); + int height = byteArrayInputStream.read(); + + byte[] pixels = new byte[width * height * 4]; + byteArrayInputStream.read(pixels, 0, pixels.length); + + WritableImage image = new WritableImage(width, height); + PixelWriter pixelWriter = image.getPixelWriter(); + pixelWriter.setPixels(0, 0, width, height, PixelFormat.getByteBgraInstance(), pixels, 0, width * 4); + + return image; + } + + public static byte[] imageToByteArray(Image image) { + int width = (int) image.getWidth(); + int height = (int) image.getHeight(); + PixelReader pixelReader = image.getPixelReader(); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + byteArrayOutputStream.write(width); + byteArrayOutputStream.write(height); + + ByteBuffer buffer = ByteBuffer.allocate(width * height * 4); // 4 bytes per pixel (ARGB) + WritablePixelFormat format = WritablePixelFormat.getByteBgraInstance(); + pixelReader.getPixels(0, 0, width, height, format, buffer, width * 4); + + byteArrayOutputStream.write(buffer.array(), 0, buffer.array().length); + return byteArrayOutputStream.toByteArray(); + } +} diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/common/utils/ImageUtil.java b/apps/desktop/desktop/src/main/java/bisq/desktop/common/utils/ImageUtil.java index 836c33ba18..827f0bb426 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/common/utils/ImageUtil.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/common/utils/ImageUtil.java @@ -17,12 +17,8 @@ package bisq.desktop.common.utils; -import bisq.common.file.FileUtils; import javafx.geometry.Pos; import javafx.scene.Node; -import javafx.scene.SnapshotParameters; -import javafx.scene.canvas.Canvas; -import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.*; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; @@ -33,7 +29,6 @@ import lombok.extern.slf4j.Slf4j; import java.io.*; -import java.nio.ByteBuffer; import java.util.Objects; @Slf4j @@ -87,33 +82,6 @@ public static void addAppIcons(Stage stage) { stage.getIcons().add(ImageUtil.getImageByPath("images/app_window/icon_16.png")); } - public static Image composeImage(String[] paths, double size) { - return composeImage(paths, size, size); - } - - public static Image composeImage(String[] paths, double width, double height) { - Canvas canvas = new Canvas(); - canvas.setWidth(width); - canvas.setHeight(height); - GraphicsContext graphicsContext2D = canvas.getGraphicsContext2D(); - - double radius = Math.min(height, width) / 2d; - double x = width / 2d; - double y = height / 2d; - graphicsContext2D.beginPath(); - graphicsContext2D.moveTo(x - radius, y); - graphicsContext2D.arc(x, y, radius, radius, 180, 360); - graphicsContext2D.closePath(); - graphicsContext2D.clip(); - - for (String path : paths) { - graphicsContext2D.drawImage(new Image("images/cathash/" + path), 0, 0, width, height); - } - SnapshotParameters snapshotParameters = new SnapshotParameters(); - snapshotParameters.setFill(Color.TRANSPARENT); - return canvas.snapshot(snapshotParameters, null); - } - /** * @param size * @param cssStrokeColor E.g. -bisq2-green @@ -163,45 +131,4 @@ public static StackPane getOverlappedIconsPane(String leftIconId, return pane; } - public static Image readRawImage(File file) throws IOException { - byte[] rawData = FileUtils.read(file.getAbsolutePath()); - return byteArrayToImage(rawData); - } - - public static void writeRawImage(Image image, File file) throws IOException { - byte[] rawData = imageToByteArray(image); - FileUtils.write(file.getAbsolutePath(), rawData); - } - - public static Image byteArrayToImage(byte[] data) { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data); - - int width = byteArrayInputStream.read(); - int height = byteArrayInputStream.read(); - - byte[] pixels = new byte[width * height * 4]; - byteArrayInputStream.read(pixels, 0, pixels.length); - - WritableImage image = new WritableImage(width, height); - PixelWriter pixelWriter = image.getPixelWriter(); - pixelWriter.setPixels(0, 0, width, height, PixelFormat.getByteBgraInstance(), pixels, 0, width * 4); - - return image; - } - - public static byte[] imageToByteArray(Image image) { - int width = (int) image.getWidth(); - int height = (int) image.getHeight(); - PixelReader pixelReader = image.getPixelReader(); - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - byteArrayOutputStream.write(width); - byteArrayOutputStream.write(height); - - ByteBuffer buffer = ByteBuffer.allocate(width * height * 4); // 4 bytes per pixel (ARGB) - WritablePixelFormat format = WritablePixelFormat.getByteBgraInstance(); - pixelReader.getPixels(0, 0, width, height, format, buffer, width * 4); - - byteArrayOutputStream.write(buffer.array(), 0, buffer.array().length); - return byteArrayOutputStream.toByteArray(); - } } diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/components/cathash/JavaFxCatHashService.java b/apps/desktop/desktop/src/main/java/bisq/desktop/components/cathash/JavaFxCatHashService.java index 637f69124a..ef49edef38 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/components/cathash/JavaFxCatHashService.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/components/cathash/JavaFxCatHashService.java @@ -17,7 +17,7 @@ package bisq.desktop.components.cathash; -import bisq.desktop.common.utils.ImageUtil; +import bisq.desktop.common.utils.CatHashImageUtil; import bisq.user.cathash.CatHashService; import javafx.scene.image.Image; @@ -32,16 +32,16 @@ public JavaFxCatHashService(Path baseDir) { @Override protected Image composeImage(String[] paths, double size) { - return ImageUtil.composeImage(paths, size); + return CatHashImageUtil.composeImage(paths, size); } @Override protected void writeRawImage(Image image, File iconFile) throws IOException { - ImageUtil.writeRawImage(image, iconFile); + CatHashImageUtil.writeRawImage(image, iconFile); } @Override protected Image readRawImage(File iconFile) throws IOException { - return ImageUtil.readRawImage(iconFile); + return CatHashImageUtil.readRawImage(iconFile); } }