use of javafx.scene.image.PixelReader in project tokentool by RPTools.
the class ImageUtil method resizeCanvas.
/*
* Resize the overall image width/height without scaling the actual image, eg resize the canvas
*/
public static Image resizeCanvas(Image imageSource, int newWidth, int newHeight, int offsetX, int offsetY) {
int sourceWidth = (int) imageSource.getWidth();
int sourceHeight = (int) imageSource.getHeight();
// No work needed here...
if (sourceWidth == newWidth && sourceHeight == newHeight)
return imageSource;
WritableImage outputImage = new WritableImage(newWidth, newHeight);
PixelReader pixelReader = imageSource.getPixelReader();
PixelWriter pixelWriter = outputImage.getPixelWriter();
WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance();
int[] buffer = new int[sourceWidth * sourceHeight];
pixelReader.getPixels(0, 0, sourceWidth, sourceHeight, format, buffer, 0, sourceWidth);
pixelWriter.setPixels(offsetX, offsetY, sourceWidth, sourceHeight, format, buffer, 0, sourceWidth);
return outputImage;
}
use of javafx.scene.image.PixelReader in project tokentool by RPTools.
the class ImageUtil method autoCropImage.
public static Image autoCropImage(Image imageSource, Color backgroundColor, Image backgroundImage) {
ImageView croppedImageView = new ImageView(imageSource);
PixelReader pixelReader = imageSource.getPixelReader();
int imageWidth = (int) imageSource.getWidth();
int imageHeight = (int) imageSource.getHeight();
int minX = imageWidth, minY = imageHeight, maxX = 0, maxY = 0;
// Find the first and last pixels that are not transparent to create a bounding viewport
for (int readY = 0; readY < imageHeight; readY++) {
for (int readX = 0; readX < imageWidth; readX++) {
Color pixelColor = pixelReader.getColor(readX, readY);
if (!pixelColor.equals(Color.TRANSPARENT)) {
if (readX < minX)
minX = readX;
if (readX > maxX)
maxX = readX;
if (readY < minY)
minY = readY;
if (readY > maxY)
maxY = readY;
}
}
}
if (maxX - minX <= 0 || maxY - minY <= 0)
return new WritableImage(1, 1);
// Create a viewport to clip the image using snapshot
Rectangle2D viewPort = new Rectangle2D(minX, minY, maxX - minX, maxY - minY);
SnapshotParameters parameter = new SnapshotParameters();
parameter.setViewport(viewPort);
parameter.setFill(backgroundColor);
if (backgroundImage != null) {
return new Group(new ImageView(backgroundImage), croppedImageView).snapshot(parameter, null);
} else {
return croppedImageView.snapshot(parameter, null);
}
}
use of javafx.scene.image.PixelReader in project Krothium-Launcher by DarkLBP.
the class Kernel method getProfileIcon.
/**
* Gets a profile icon from the icons map
*
* @param profileIcon The desired profile icon
* @return The image with the profile icon
*/
public Image getProfileIcon(String profileIcon) {
if (iconCache.containsKey(profileIcon)) {
return iconCache.get(profileIcon);
} else if (icons.isEmpty()) {
loadIcons();
}
int[] coords = icons.get(profileIcon);
if (coords == null) {
if (iconCache.containsKey("Furnace")) {
return iconCache.get("Furnace");
}
coords = icons.get("Furnace");
}
WritableImage wi = new WritableImage(68, 68);
PixelWriter pw = wi.getPixelWriter();
PixelReader pr = profileIcons.getPixelReader();
pw.setPixels(0, 0, 68, 68, pr, coords[0] * 68, coords[1] * 68);
iconCache.put(profileIcon, wi);
return wi;
}
use of javafx.scene.image.PixelReader in project TestFX by TestFX.
the class HDPIContractTest method captureRegionAdapterTest.
/**
* Just verifies, that grabPixelColor also uses the right coordinates.
* defaultOffsetTest must work. Does only a approximate test. Uses Black, to
* minimize error in case of color profiles. And uses a high threshold.
*/
public void captureRegionAdapterTest(RobotAdapter<?> adapter) {
sleep(1000);
// Checking that bounds are in JavaFx-Coordinates
BoundsLocatorImpl locator = new BoundsLocatorImpl();
Bounds bounds = locator.boundsOnScreenFor(root);
log("CaptureRegion window bounds for " + adapter.getClass().getSimpleName() + " is " + bounds);
// checking correct bounds (x,y) is unknown...
assertThat("Width doesn't match", bounds.getWidth(), equalTo(100.0));
assertThat("Height doesn't match", bounds.getHeight(), equalTo(100.0));
// not really required, as Robot coordinates are verified in nullOffsetTest, but
// for completeness...
// checking Robot clicks on the JavaFx coordinates (if coordinates above are
// correct)
Image image = adapter.getCaptureRegion(new Rectangle2D(bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), bounds.getHeight()));
PixelReader img = image.getPixelReader();
// verify correctness of dimensions
assertTrue("Width of image incorrect. Is=" + image.getWidth() + " expected " + bounds.getWidth(), Math.abs(image.getWidth() - bounds.getWidth()) < 1.0);
assertTrue("Height of image incorrect. Is=" + image.getHeight() + " expected " + bounds.getHeight(), Math.abs(image.getHeight() - bounds.getHeight()) < 1.0);
// verify correctness of content
Color c = img.getColor(2, 2);
assertTrue("TopLeft color doesn't match " + c, c.getRed() < 16.0 / 255.0);
assertTrue("TopLeft color doesn't match " + c, c.getGreen() < 16.0 / 255.0);
assertTrue("TopLeft color doesn't match " + c, c.getBlue() < 16.0 / 255.0);
c = img.getColor((int) bounds.getWidth() - 2, 2);
assertTrue("TopRight color doesn't match " + c, c.getRed() < 16.0 / 255.0);
assertTrue("TopRight color doesn't match " + c, c.getGreen() < 16.0 / 255.0);
assertTrue("TopRight color doesn't match " + c, c.getBlue() < 16.0 / 255.0);
c = img.getColor(2, (int) bounds.getHeight() - 2);
assertTrue("BottomLeft color doesn't match " + c, c.getRed() < 16.0 / 255.0);
assertTrue("BottomLeft color doesn't match " + c, c.getGreen() < 16.0 / 255.0);
assertTrue("BottomLeft color doesn't match " + c, c.getBlue() < 16.0 / 255.0);
c = img.getColor((int) bounds.getWidth() - 2, (int) bounds.getHeight() - 2);
assertTrue("BottomRight color doesn't match " + c, c.getRed() < 16.0 / 255.0);
assertTrue("BottomRight color doesn't match " + c, c.getGreen() < 16.0 / 255.0);
assertTrue("BottomRight color doesn't match " + c, c.getBlue() < 16.0 / 255.0);
}
use of javafx.scene.image.PixelReader in project latexdraw by arnobl.
the class HelperTest method computeSnapshotSimilarity.
/**
* Compute the similarity of two JavaFX images.
* @param image1 The first image to test.
* @param image2 The second image to test.
* @return A double value in [0;100] corresponding to the similarity between the two images (pixel comparison).
* @throws NullPointerException If image1 or image2 is null.
*/
default double computeSnapshotSimilarity(final Image image1, final Image image2) {
final int width = (int) image1.getWidth();
final int height = (int) image1.getHeight();
final PixelReader reader1 = image1.getPixelReader();
final PixelReader reader2 = image2.getPixelReader();
final double nbNonSimilarPixels = IntStream.range(0, width).parallel().mapToLong(i -> IntStream.range(0, height).parallel().filter(j -> reader1.getArgb(i, j) != reader2.getArgb(i, j)).count()).sum();
return 100d - nbNonSimilarPixels / (width * height) * 100d;
}
Aggregations