Search in sources :

Example 6 with PixelReader

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;
}
Also used : WritableImage(javafx.scene.image.WritableImage) PixelReader(javafx.scene.image.PixelReader) IntBuffer(java.nio.IntBuffer) PixelWriter(javafx.scene.image.PixelWriter)

Example 7 with PixelReader

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);
    }
}
Also used : WritableImage(javafx.scene.image.WritableImage) Group(javafx.scene.Group) PixelReader(javafx.scene.image.PixelReader) SnapshotParameters(javafx.scene.SnapshotParameters) Color(javafx.scene.paint.Color) Rectangle2D(javafx.geometry.Rectangle2D) ImageView(javafx.scene.image.ImageView)

Example 8 with PixelReader

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;
}
Also used : WritableImage(javafx.scene.image.WritableImage) PixelReader(javafx.scene.image.PixelReader) PixelWriter(javafx.scene.image.PixelWriter)

Example 9 with PixelReader

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);
}
Also used : PixelReader(javafx.scene.image.PixelReader) BoundsLocatorImpl(org.testfx.service.locator.impl.BoundsLocatorImpl) Bounds(javafx.geometry.Bounds) Color(javafx.scene.paint.Color) Rectangle2D(javafx.geometry.Rectangle2D) Image(javafx.scene.image.Image)

Example 10 with PixelReader

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;
}
Also used : IntStream(java.util.stream.IntStream) PixelReader(javafx.scene.image.PixelReader) Node(javafx.scene.Node) WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) Assertions.within(org.assertj.core.api.Assertions.within) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ArrayList(java.util.ArrayList) WaitForAsyncUtils(org.testfx.util.WaitForAsyncUtils) Platform(javafx.application.Platform) CmdFXVoid(net.sf.latexdraw.instrument.CmdFXVoid) BadaboomCollector(net.sf.latexdraw.util.BadaboomCollector) List(java.util.List) Image(javafx.scene.image.Image) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) TimeoutTransition(io.github.interacto.fsm.TimeoutTransition) Bounds(javafx.geometry.Bounds) PixelReader(javafx.scene.image.PixelReader)

Aggregations

PixelReader (javafx.scene.image.PixelReader)20 WritableImage (javafx.scene.image.WritableImage)18 PixelWriter (javafx.scene.image.PixelWriter)12 SnapshotParameters (javafx.scene.SnapshotParameters)6 Image (javafx.scene.image.Image)5 Color (javafx.scene.paint.Color)5 IntStream (java.util.stream.IntStream)4 Rectangle2D (javafx.geometry.Rectangle2D)3 Node (javafx.scene.Node)3 Platform (javafx.application.Platform)2 Bounds (javafx.geometry.Bounds)2 ImageView (javafx.scene.image.ImageView)2 Pair (javafx.util.Pair)2 WaitForAsyncUtils (org.testfx.util.WaitForAsyncUtils)2 TimeoutTransition (io.github.interacto.fsm.TimeoutTransition)1 File (java.io.File)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 URISyntaxException (java.net.URISyntaxException)1 IntBuffer (java.nio.IntBuffer)1