Example usage for javafx.scene.image WritableImage getPixelWriter

List of usage examples for javafx.scene.image WritableImage getPixelWriter

Introduction

In this page you can find the example usage for javafx.scene.image WritableImage getPixelWriter.

Prototype

public final PixelWriter getPixelWriter() 

Source Link

Document

This method returns a PixelWriter that provides access to write the pixels of the image.

Usage

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: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 .jav a 2  s.co m*/
                pixelWriter.setColor(readX, readY, pixelColor);

        }
    }

    return outputImage;
}

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));
        }//w w w  .  ja v a 2s .co m
    }

    return outputImage;
}

From source file:Main.java

/**
 * Snapshots the specified {@link BufferedImage} and stores a copy of
 * its pixels into a JavaFX {@link Image} object, creating a new
 * object if needed.//from   w ww  .jav  a  2 s. co  m
 * The returned {@code Image} will be a static snapshot of the state
 * of the pixels in the {@code BufferedImage} at the time the method
 * completes.  Further changes to the {@code BufferedImage} will not
 * be reflected in the {@code Image}.
 * <p>
 * The optional JavaFX {@link WritableImage} parameter may be reused
 * to store the copy of the pixels.
 * A new {@code Image} 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 bimg the {@code BufferedImage} object to be converted
 * @param wimg an optional {@code WritableImage} object that can be
 *        used to store the returned pixel data
 * @return an {@code Image} object representing a snapshot of the
 *         current pixels in the {@code BufferedImage}.
 * @since JavaFX 2.2
 */
public static WritableImage toFXImage(BufferedImage bimg, WritableImage wimg) {
    int bw = bimg.getWidth();
    int bh = bimg.getHeight();
    switch (bimg.getType()) {
    case BufferedImage.TYPE_INT_ARGB:
    case BufferedImage.TYPE_INT_ARGB_PRE:
        break;
    default:
        BufferedImage converted = new BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB_PRE);
        Graphics2D g2d = converted.createGraphics();
        g2d.drawImage(bimg, 0, 0, null);
        g2d.dispose();
        bimg = converted;
        break;
    }
    // assert(bimg.getType == TYPE_INT_ARGB[_PRE]);
    if (wimg != null) {
        int iw = (int) wimg.getWidth();
        int ih = (int) wimg.getHeight();
        if (iw < bw || ih < bh) {
            wimg = null;
        } else if (bw < iw || bh < ih) {
            int empty[] = new int[iw];
            PixelWriter pw = wimg.getPixelWriter();
            PixelFormat<IntBuffer> pf = PixelFormat.getIntArgbPreInstance();
            if (bw < iw) {
                pw.setPixels(bw, 0, iw - bw, bh, pf, empty, 0, 0);
            }
            if (bh < ih) {
                pw.setPixels(0, bh, iw, ih - bh, pf, empty, 0, 0);
            }
        }
    }
    if (wimg == null) {
        wimg = new WritableImage(bw, bh);
    }
    PixelWriter pw = wimg.getPixelWriter();
    IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster();
    int data[] = icr.getDataStorage();
    int offset = icr.getDataOffset(0);
    int scan = icr.getScanlineStride();
    PixelFormat<IntBuffer> pf = (bimg.isAlphaPremultiplied() ? PixelFormat.getIntArgbPreInstance()
            : PixelFormat.getIntArgbInstance());
    pw.setPixels(0, 0, bw, bh, pf, data, offset, scan);
    return wimg;
}

From source file:ijfx.core.image.DefaultDatasetUtilsService.java

public <T extends RealType<T>> Image datasetToImage(RandomAccessibleInterval<T> dataset, ColorTable colorTable,
        double min, double max, WritableImage image) {

    //int width = (int) dataset.dimension(0);
    //int height = (int) dataset.dimension(1);
    //WritableImage image = new WritableImage(width, height);
    RealLUTConverter<T> converter = new RealLUTConverter<T>(min, max, colorTable);
    ARGBType argb = new ARGBType();
    RandomAccess<T> ra = dataset.randomAccess();

    Cursor<T> cursor = Views.iterable(dataset).cursor();
    cursor.reset();//from  w  ww.  j a  va2 s .  c o  m
    int[] position = new int[dataset.numDimensions()];
    while (cursor.hasNext()) {
        cursor.fwd();
        cursor.localize(position);
        converter.convert(cursor.get(), argb);
        image.getPixelWriter().setArgb(position[0], position[1], argb.get());
    }

    return image;
}

From source file:editeurpanovisu.EditeurPanovisu.java

/**
 *
 * @param imgRect/*  w w  w  .  j a v a 2  s.c om*/
 * @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;
}