List of usage examples for javafx.scene.image Image getPixelReader
public final PixelReader getPixelReader()
From source file:Main.java
/** * Returns writable image object that can then be played during gameplay. * @param image the image object * @param x x-coordinate/*ww w . ja v a2 s .c o m*/ * @param y y-coordinate * @param width Width of the window * @param height Height of the window * @return Sub image of the image object */ public static WritableImage getSubImage(Image image, int x, int y, int width, int height) { return new WritableImage(image.getPixelReader(), x, y, width, height); }
From source file:Main.java
/** * Snapshots the specified JavaFX {@link Image} object and stores a * copy of its pixels into a {@link BufferedImage} object, creating * a new object if needed./*from w ww . j a va 2 s .c o m*/ * The method will only convert a JavaFX {@code Image} that is readable * as per the conditions on the * {@link Image#getPixelReader() Image.getPixelReader()} * method. * If the {@code Image} is not readable, as determined by its * {@code getPixelReader()} method, then this method will return null. * If the {@code Image} is a writable, or other dynamic image, then * the {@code BufferedImage} will only be set to the current state of * the pixels in the image as determined by its {@link PixelReader}. * Further changes to the pixels of the {@code Image} will not be * reflected in the returned {@code BufferedImage}. * <p> * The optional {@code BufferedImage} parameter may be reused to store * the copy of the pixels. * A new {@code BufferedImage} will be created if the supplied object * is null, is too small or of a type which the image pixels cannot * be easily converted into. * * @param img the JavaFX {@code Image} to be converted * @param bimg an optional {@code BufferedImage} object that may be * used to store the returned pixel data * @return a {@code BufferedImage} containing a snapshot of the JavaFX * {@code Image}, or null if the {@code Image} is not readable. * @since JavaFX 2.2 */ public static BufferedImage fromFXImage(Image img, BufferedImage bimg) { PixelReader pr = img.getPixelReader(); if (pr == null) { return null; } int iw = (int) img.getWidth(); int ih = (int) img.getHeight(); int prefBimgType = getBestBufferedImageType(pr.getPixelFormat(), bimg); if (bimg != null) { int bw = bimg.getWidth(); int bh = bimg.getHeight(); if (bw < iw || bh < ih || bimg.getType() != prefBimgType) { bimg = null; } else if (iw < bw || ih < bh) { Graphics2D g2d = bimg.createGraphics(); g2d.setComposite(AlphaComposite.Clear); g2d.fillRect(0, 0, bw, bh); g2d.dispose(); } } if (bimg == null) { bimg = new BufferedImage(iw, ih, prefBimgType); } IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster(); int offset = icr.getDataOffset(0); int scan = icr.getScanlineStride(); int data[] = icr.getDataStorage(); WritablePixelFormat<IntBuffer> pf = getAssociatedPixelFormat(bimg); pr.getPixels(0, 0, iw, ih, pf, data, offset, scan); return bimg; }
From source file:net.rptools.tokentool.util.ImageUtil.java
private static Image autoCropImage(Image imageSource) { 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;/*from ww w.j a va2 s.c o m*/ 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(Color.TRANSPARENT); return croppedImageView.snapshot(parameter, null); }
From source file:net.rptools.tokentool.util.ImageUtil.java
private static Image clipImageWithMask(Image imageSource, Image imageMask) { int imageWidth = (int) imageMask.getWidth(); int imageHeight = (int) imageMask.getHeight(); WritableImage outputImage = new WritableImage(imageWidth, imageHeight); PixelReader pixelReader_Mask = imageMask.getPixelReader(); PixelReader pixelReader_Source = imageSource.getPixelReader(); PixelWriter pixelWriter = outputImage.getPixelWriter(); for (int readY = 0; readY < imageHeight; readY++) { for (int readX = 0; readX < imageWidth; readX++) { Color pixelColor = pixelReader_Mask.getColor(readX, readY); if (pixelColor.equals(Color.TRANSPARENT)) pixelWriter.setColor(readX, readY, pixelReader_Source.getColor(readX, readY)); }//from ww w .ja va2s .c o m } return outputImage; }
From source file:net.rptools.tokentool.util.ImageUtil.java
private static Image processMagenta(Image inputImage, int colorThreshold, boolean overlayWanted) { int imageWidth = (int) inputImage.getWidth(); int imageHeight = (int) inputImage.getHeight(); WritableImage outputImage = new WritableImage(imageWidth, imageHeight); PixelReader pixelReader = inputImage.getPixelReader(); PixelWriter pixelWriter = outputImage.getPixelWriter(); for (int readY = 0; readY < imageHeight; readY++) { for (int readX = 0; readX < imageWidth; readX++) { Color pixelColor = pixelReader.getColor(readX, readY); if (isMagenta(pixelColor, COLOR_THRESHOLD) == overlayWanted) pixelWriter.setColor(readX, readY, Color.TRANSPARENT); else/*w w w . j a v a2 s. com*/ pixelWriter.setColor(readX, readY, pixelColor); } } return outputImage; }
From source file:net.rptools.tokentool.util.ImageUtil.java
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; }
From source file:editeurpanovisu.EditeurPanovisu.java
/** * * @param imgRect//w ww.j a v a 2s.co m * @param iRapport * @return image transforme Mercator */ private static Image imgTransformationImage(Image imgRect, int iRapport) { int iLargeur = (int) imgRect.getWidth() / iRapport; int iHauteur = iLargeur / 2 / iRapport; WritableImage imgMercator = new WritableImage(iLargeur, iHauteur); PixelReader prRect = imgRect.getPixelReader(); PixelWriter pwMercator = imgMercator.getPixelWriter(); for (int i = 0; i < iLargeur; i++) { for (int j = 0; j < iHauteur; j++) { double phi = Math.asin(2.d * (iHauteur / 2.d - j) / iHauteur); int y2 = (int) (iHauteur * iRapport * (0.5d - phi / Math.PI)); if (y2 >= iHauteur * iRapport) { y2 = iHauteur * iRapport - 1; } Color clPixel = prRect.getColor(i * iRapport, y2 * iRapport); pwMercator.setColor(i, j, clPixel); } } return imgMercator; }