Java 类javafx.scene.image.WritablePixelFormat 实例源码

项目:NinePatch    文件:FxNinePatch.java   
@Override
public int[] getPixels(Image img, int x, int y, int w, int h)
{
    int[] pixels = new int[w * h];

    PixelReader reader = img.getPixelReader();

    PixelFormat.Type type =  reader.getPixelFormat().getType();

    WritablePixelFormat<IntBuffer> format = null;

    if(type == PixelFormat.Type.INT_ARGB_PRE)
    {
        format = PixelFormat.getIntArgbPreInstance();
    }
    else
    {
        format = PixelFormat.getIntArgbInstance();
    }

    reader.getPixels(x, y, w, h, format, pixels, 0, w);

    return pixels;
}
项目:ShootOFF    文件:SwingFXUtils.java   
/**
 * Determine the appropriate {@link WritablePixelFormat} type that can be
 * used to transfer data into the indicated BufferedImage.
 * 
 * @param bimg
 *            the BufferedImage that will be used as a destination for a
 *            {@code PixelReader<IntBuffer>#getPixels()} operation.
 * @return
 */
private static WritablePixelFormat<IntBuffer> getAssociatedPixelFormat(BufferedImage bimg) {
    switch (bimg.getType()) {
    // We lie here for xRGB, but we vetted that the src data was opaque
    // so we can ignore the alpha. We use ArgbPre instead of Argb
    // just to get a loop that does not have divides in it if the
    // PixelReader happens to not know the data is opaque.
    case BufferedImage.TYPE_INT_RGB:
    case BufferedImage.TYPE_INT_ARGB_PRE:
        return PixelFormat.getIntArgbPreInstance();

    case BufferedImage.TYPE_INT_ARGB:
        return PixelFormat.getIntArgbInstance();

    default:
        // Should not happen...
        throw new InternalError("Failed to validate BufferedImage type");
    }
}
项目:tokentool    文件: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;
}
项目:ShootOFF    文件:SwingFXUtils.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. 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;
}
项目:matriximage    文件:Main2.java   
private void pixelate(final DotMatrix MATRIX, final boolean WITH_RED, final boolean WITH_GREEN, final boolean WITH_BLUE) {
    PixelReader                    reader = src.getPixelReader();
    PixelWriter                    writer = dst.getPixelWriter();
    WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance();

    int width      = (int) src.getWidth();
    int height     = (int) src.getHeight();
    int kernelSize = 6;

    for (int y = kernelSize; y < height - kernelSize * 2; y += kernelSize * 2 + 1) {
        for (int x = kernelSize; x < width - kernelSize * 2; x += kernelSize * 2 + 1) {
            int kernelWidth  = kernelSize * 2 + 1;
            int kernelHeight = kernelSize * 2 + 1;

            int[] buffer = new int[kernelWidth * kernelHeight];
            reader.getPixels(x, y, kernelWidth, kernelHeight, format, buffer, 0, kernelWidth);

            int alpha  = 0;
            int red    = 0;
            int green  = 0;
            int blue   = 0;

            for (int color : buffer) {
                alpha += (color >>> 24);
                red   += (color >>> 16 & 0xFF);
                green += (color >>> 8 & 0xFF);
                blue  += (color & 0xFF);
            }
            alpha = alpha / kernelWidth / kernelHeight;
            red   = WITH_RED   ? red   / kernelWidth / kernelHeight : 0;
            green = WITH_GREEN ? green / kernelWidth / kernelHeight : 0;
            blue  = WITH_BLUE  ? blue  / kernelWidth / kernelHeight : 0;

            int pixelateColor = (alpha << 24) + (red << 16) + (green << 8) + blue;
            Arrays.fill(buffer, pixelateColor);
            writer.setPixels(x, y, kernelWidth, kernelHeight, format, buffer, 0, kernelWidth);

            MATRIX.setPixel(x / kernelWidth, y / kernelHeight, pixelateColor);
        }
    }
}
项目:matriximage    文件:Main.java   
private void pixelate() {
    PixelReader                    reader = src.getPixelReader();
    PixelWriter                    writer = dst.getPixelWriter();
    WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance();

    int width      = (int) src.getWidth();
    int height     = (int) src.getHeight();
    // Bird Demo
    // int kernelSize = 4;

    // Han Solo Demo
    int kernelSize = 6;

    for (int y = kernelSize; y < height - kernelSize * 2; y += kernelSize * 2 + 1) {
        for (int x = kernelSize; x < width - kernelSize * 2; x += kernelSize * 2 + 1) {
            int kernelWidth  = kernelSize * 2 + 1;
            int kernelHeight = kernelSize * 2 + 1;

            int[] buffer = new int[kernelWidth * kernelHeight];
            reader.getPixels(x, y, kernelWidth, kernelHeight, format, buffer, 0, kernelWidth);

            int alpha  = 0;
            int red    = 0;
            int green  = 0;
            int blue   = 0;

            for (int color : buffer) {
                alpha += (color >>> 24);
                red   += (color >>> 16 & 0xFF);
                green += (color >>> 8 & 0xFF);
                blue  += (color & 0xFF);
            }
            alpha = alpha / kernelWidth / kernelHeight;
            red   = red   / kernelWidth / kernelHeight;
            green = green / kernelWidth / kernelHeight;
            blue  = blue  / kernelWidth / kernelHeight;

            int pixelateColor = (alpha << 24) + (red << 16) + (green << 8) + blue;
            Arrays.fill(buffer, pixelateColor);
            writer.setPixels(x, y, kernelWidth, kernelHeight, format, buffer, 0, kernelWidth);

            matrix.setPixel(x / kernelWidth, y / kernelHeight, pixelateColor);
        }
    }
    addText();
}
项目:matriximage    文件:Main3.java   
private void pixelate(final DotMatrix MATRIX, final boolean WITH_RED, final boolean WITH_GREEN, final boolean WITH_BLUE) {
    PixelReader                    reader = src.getPixelReader();
    PixelWriter                    writer = dst.getPixelWriter();
    WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance();

    int width      = (int) src.getWidth();
    int height     = (int) src.getHeight();
    int kernelSize = 6;

    for (int y = kernelSize; y < height - kernelSize * 2; y += kernelSize * 2 + 1) {
        for (int x = kernelSize; x < width - kernelSize * 2; x += kernelSize * 2 + 1) {
            int kernelWidth  = kernelSize * 2 + 1;
            int kernelHeight = kernelSize * 2 + 1;

            int[] buffer = new int[kernelWidth * kernelHeight];
            reader.getPixels(x, y, kernelWidth, kernelHeight, format, buffer, 0, kernelWidth);

            int alpha  = 0;
            int red    = 0;
            int green  = 0;
            int blue   = 0;

            for (int color : buffer) {
                alpha += (color >>> 24);
                red   += (color >>> 16 & 0xFF);
                green += (color >>> 8 & 0xFF);
                blue  += (color & 0xFF);
            }
            alpha = alpha / kernelWidth / kernelHeight;
            red   = WITH_RED   ? red   / kernelWidth / kernelHeight : 0;
            green = WITH_GREEN ? green / kernelWidth / kernelHeight : 0;
            blue  = WITH_BLUE  ? blue  / kernelWidth / kernelHeight : 0;

            int pixelateColor = (alpha << 24) + (red << 16) + (green << 8) + blue;
            Arrays.fill(buffer, pixelateColor);
            writer.setPixels(x, y, kernelWidth, kernelHeight, format, buffer, 0, kernelWidth);

            MATRIX.setPixel(x / kernelWidth, y / kernelHeight, pixelateColor);
        }
    }
}