Example usage for java.awt.image BufferedImage getType

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

Introduction

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

Prototype

public int getType() 

Source Link

Document

Returns the image type.

Usage

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.
 * //from w  w w.  j  ava  2  s .c o  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: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.ja  v  a  2 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:com.spstudio.common.image.ImageUtils.java

public static BufferedImage zoom(BufferedImage sourceImage, int width, int height) {
    BufferedImage zoomImage = new BufferedImage(width, height, sourceImage.getType());
    Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    Graphics gc = zoomImage.getGraphics();
    gc.setColor(Color.WHITE);//from w w w .  jav a  2 s . c o  m
    gc.drawImage(image, 0, 0, null);
    return zoomImage;
}

From source file:UploadImage.java

public static BufferedImage shrink(BufferedImage image, int n) {

    int w = image.getWidth() / n;
    int h = image.getHeight() / n;

    BufferedImage shrunkImage = new BufferedImage(w, h, image.getType());

    for (int y = 0; y < h; ++y)
        for (int x = 0; x < w; ++x)
            shrunkImage.setRGB(x, y, image.getRGB(x * n, y * n));

    return shrunkImage;
}

From source file:com.vaadin.testbench.screenshot.ImageUtil.java

/**
 * Extract magical image properties used by the getBlock function.
 * /*from   www .  j  a v a 2 s. c o  m*/
 * @param image
 *            a BufferedImage
 * @return an ImageProperties descriptor object
 */
public static final ImageProperties getImageProperties(final BufferedImage image) {
    final int imageType = image.getType();
    ImageProperties p = new ImageProperties();
    p.image = image;
    p.raster = image.getRaster();
    p.alpha = imageType == TYPE_INT_ARGB || imageType == BufferedImage.TYPE_4BYTE_ABGR;
    boolean rgb = imageType == TYPE_INT_ARGB || imageType == TYPE_INT_RGB;
    boolean bgr = imageType == BufferedImage.TYPE_INT_BGR || imageType == BufferedImage.TYPE_3BYTE_BGR
            || imageType == BufferedImage.TYPE_4BYTE_ABGR;
    p.width = image.getWidth();
    p.height = image.getHeight();
    p.fallback = !(rgb || bgr);
    return p;
}

From source file:com.ackpdfbox.app.imageio.TIFFUtil.java

/**
 * Sets the ImageIO parameter compression type based on the given image.
 * @param image buffered image used to decide compression type
 * @param param ImageIO write parameter to update
 *///w w  w .  j  a  v  a2  s  . co  m
public static void setCompressionType(ImageWriteParam param, BufferedImage image) {
    // avoid error: first compression type is RLE, not optimal and incorrect for color images
    // TODO expose this choice to the user?
    if (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1) {
        param.setCompressionType("CCITT T.6");
    } else {
        param.setCompressionType("LZW");
    }
}

From source file:Main.java

/**
 * Quickly crops an image from its source.
 * @param src The source image./*w  w  w.j  av a2  s .  co  m*/
 * @param x The x coordinate to start at.
 * @param y The y coordinate to start at.
 * @param width The width to clip.
 * @param height The height to clip.
 * @return
 */
public static BufferedImage imgUtilFastCrop(BufferedImage src, int x, int y, int width, int height) {
    if (src == null)
        return null;
    if (x == 0 && y == 0 && width == src.getWidth() && height == src.getHeight())
        return imgUtilFastCopy(src);
    else {
        BufferedImage b = new BufferedImage(width, height, src.getType());
        Graphics g = b.getGraphics();
        g.drawImage(src, -x, -y, null);
        g.dispose();
        return b;
    }
}

From source file:com.smash.revolance.ui.model.helper.ImageHelper.java

/**
 * diff = (img2.rgb(x,y) != img1.rgb(x,y)) ? img1.rgb() : 0
 *
 * @param img1//from www. j  a v  a 2  s  .  c  om
 * @param img2
 * @return
 * @throws IOException
 */
public static BufferedImage diffImg(BufferedImage img1, BufferedImage img2) throws IOException {
    int w = img1.getWidth();
    int h = img1.getHeight();
    int t = img1.getType();

    BufferedImage diff = new BufferedImage(w, h, t);

    if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) {
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                int refIntensity = img1.getRGB(x, y);
                int imgIntensity = img2.getRGB(x, y);
                if (refIntensity != imgIntensity) {
                    diff.setRGB(x, y, imgIntensity);
                } else {
                    diff.setRGB(x, y, 0);
                }
            }
        }
    }

    return diff;
}

From source file:com.partagames.imageresizetool.SimpleImageResizeTool.java

/**
 * Scales an image to the desired dimensions.
 *
 * @param img  Original image// w  w w.java2s  . c om
 * @param newW Target width
 * @param newH Target height
 * @return Scaled image
 */
public static BufferedImage scale(BufferedImage img, int newW, int newH) {
    int w = img.getWidth();
    int h = img.getHeight();
    final BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
    final Graphics2D g = dimg.createGraphics();

    // use provided rendering hint, default is bilinear 
    switch (scalingHint) {
    case "n":
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        break;
    case "b":
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        break;
    }

    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
    g.dispose();
    return dimg;
}

From source file:com.smash.revolance.ui.model.helper.ImageHelper.java

public static BufferedImage scaleImage(BufferedImage image, double widthPerCent, double heightPerCent) {
    int w = (int) (image.getWidth() * widthPerCent);
    int h = (int) (image.getHeight() * heightPerCent);
    int t = image.getType();

    Image scaledImage = image.getScaledInstance(w, h, Image.SCALE_SMOOTH);

    BufferedImage newImg = new BufferedImage(w, h, t);
    newImg.getGraphics().drawImage(scaledImage, 0, 0, null);

    return newImg;
}