Example usage for java.awt.image BufferedImage TYPE_INT_ARGB_PRE

List of usage examples for java.awt.image BufferedImage TYPE_INT_ARGB_PRE

Introduction

In this page you can find the example usage for java.awt.image BufferedImage TYPE_INT_ARGB_PRE.

Prototype

int TYPE_INT_ARGB_PRE

To view the source code for java.awt.image BufferedImage TYPE_INT_ARGB_PRE.

Click Source Link

Document

Represents an image with 8-bit RGBA color components packed into integer pixels.

Usage

From source file:Main.java

/**
 * Converts a {@link Image} into a {@link BufferedImage} using
 * {@link BufferedImage#TYPE_INT_ARGB_PRE} type. If the image is already a
 * {@link BufferedImage} then the same instance is returned.
 * /*  ww  w  .ja va 2  s  .c  o m*/
 * @param image the image to convert to a buffered image.
 * @return the converted image or the same instance if already a
 * {@link BufferedImage}.
 */
public static BufferedImage toBufferedImage(Image imageIn) {
    return toBufferedImage(imageIn, BufferedImage.TYPE_INT_ARGB_PRE);
}

From source file:Main.java

/**
 * Determine the appropriate {@link WritablePixelFormat} type that can
 * be used to transfer data into the indicated BufferedImage.
 * // ww  w . j a v a2s.  com
 * @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");
    }
}

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  w w .  j a  va2 s .  c om
 * 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:Main.java

/**
 * Determine the optimal BufferedImage type to use for the specified
 * {@code fxFormat} allowing for the specified {@code bimg} to be used
 * as a potential default storage space if it is not null and is compatible.
 * /*  w ww .  ja v a  2  s.co  m*/
 * @param fxFormat the PixelFormat of the source FX Image
 * @param bimg an optional existing {@code BufferedImage} to be used
 *             for storage if it is compatible, or null
 * @return 
 */
private static int getBestBufferedImageType(PixelFormat<?> fxFormat, BufferedImage bimg) {
    if (bimg != null) {
        int bimgType = bimg.getType();
        if (bimgType == BufferedImage.TYPE_INT_ARGB || bimgType == BufferedImage.TYPE_INT_ARGB_PRE) {
            // We will allow the caller to give us a BufferedImage
            // that has an alpha channel, but we might not otherwise
            // construct one ourselves.
            // We will also allow them to choose their own premultiply
            // type which may not match the image.
            // If left to our own devices we might choose a more specific
            // format as indicated by the choices below.
            return bimgType;
        }
    }
    switch (fxFormat.getType()) {
    default:
    case BYTE_BGRA_PRE:
    case INT_ARGB_PRE:
        return BufferedImage.TYPE_INT_ARGB_PRE;
    case BYTE_BGRA:
    case INT_ARGB:
        return BufferedImage.TYPE_INT_ARGB;
    case BYTE_RGB:
        return BufferedImage.TYPE_INT_RGB;
    case BYTE_INDEXED:
        return (fxFormat.isPremultiplied() ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_ARGB);
    }
}

From source file:ImageBouncer.java

public void setImageType(String s) {
    int type = BufferedImage.TYPE_CUSTOM;
    if (s.equals("TYPE_INT_RGB"))
        type = BufferedImage.TYPE_INT_RGB;
    else if (s.equals("TYPE_INT_ARGB"))
        type = BufferedImage.TYPE_INT_ARGB;
    else if (s.equals("TYPE_INT_ARGB_PRE"))
        type = BufferedImage.TYPE_INT_ARGB_PRE;
    else if (s.equals("TYPE_3BYTE_BGR"))
        type = BufferedImage.TYPE_3BYTE_BGR;
    else if (s.equals("TYPE_BYTE_GRAY"))
        type = BufferedImage.TYPE_BYTE_GRAY;
    else if (s.equals("TYPE_USHORT_GRAY"))
        type = BufferedImage.TYPE_USHORT_GRAY;
    else if (s.equals("TYPE_USHORT_555_RGB"))
        type = BufferedImage.TYPE_USHORT_565_RGB;
    else if (s.equals("TYPE_USHORT_565_RGB"))
        type = BufferedImage.TYPE_USHORT_565_RGB;
    else {//  w  ww  .j a  v a 2  s  .c o  m
        System.out.println("Unrecognized type.");
        return;
    }
    image = makeBufferedImage(mOriginalImage, type);
}

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided colors.
 *//*from w w  w .j  a  v a2 s. c  o m*/
private void setupDefaultPainter(Color foreground, Color background) {
    if (foreground == null || background == null) {
        ourPainter = null;
        return;
    }

    BufferedImage buff = new BufferedImage(6, 6, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();

    g2.setColor(background);
    g2.fillRect(0, 0, 6, 6);

    g2.setColor(foreground);
    g2.drawLine(0, 2, 6, 2);
    g2.drawLine(0, 5, 6, 5);

    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, 6, 6));

    g2.dispose();
}

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided colors and texture map.
 *///from  w  ww .  j a  v  a  2s .  c om
private void setupTexturePainter(Color foreground, Color background, boolean[][] texture, int scale) {
    if (texture == null || texture.length < 1 || texture[0].length < 1) {
        setupDefaultPainter(foreground, background);
        return;
    }

    else if (foreground == null || background == null) {
        ourPainter = null;
        return;
    }

    scale = Math.max(1, scale);
    int w = texture[0].length;
    int h = texture.length;

    BufferedImage buff = new BufferedImage(w * scale, h * scale, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();

    g2.setColor(background);
    g2.fillRect(0, 0, w * scale, h * scale);

    g2.setColor(foreground);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            try {
                if (texture[i][j])
                    g2.fillRect(j * scale, i * scale, scale, scale);
            }
            // g2.drawLine(j, i, j, i); }
            catch (ArrayIndexOutOfBoundsException aob) {
            }
        }
    }

    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, w * scale, h * scale));

    g2.dispose();
}

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided image.
 *//*from  w  w  w.  ja  v  a2 s .c o m*/
private void setupImagePainter(Image texture) {
    if (texture == null) {
        ourPainter = null;
        return;
    }

    int w = texture.getWidth(this);
    int h = texture.getHeight(this);

    if (w <= 0 || h <= 0) {
        ourPainter = null;
        return;
    }

    BufferedImage buff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();
    g2.drawImage(texture, 0, 0, this);
    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, w, h));

    g2.dispose();
}

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided icon.
 *///from w  w w.  j av a 2s .  com
private void setupIconPainter(Icon texture) {
    if (texture == null) {
        ourPainter = null;
        return;
    }

    int w = texture.getIconWidth();
    int h = texture.getIconHeight();

    if (w <= 0 || h <= 0) {
        ourPainter = null;
        return;
    }

    BufferedImage buff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();
    texture.paintIcon(this, g2, 0, 0);
    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, w, h));

    g2.dispose();
}

From source file:com.fluidops.iwb.deepzoom.DZConvert.java

static int getType(BufferedImage img) {
    if (img.getType() == BufferedImage.TYPE_CUSTOM)
        return BufferedImage.TYPE_INT_ARGB_PRE;
    else//from  w  w w  .  j ava 2s .co m
        return img.getType();
}