Example usage for java.awt.image BufferedImage getWidth

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

Introduction

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

Prototype

public int getWidth() 

Source Link

Document

Returns the width of the BufferedImage .

Usage

From source file:com.seleniumtests.util.imaging.ImageProcessor.java

/**
 * Agregate 2 pictures/*from  w w  w . jav  a  2  s  . com*/
 * @param imgf1         first picture to agregate
 * @param imgf2         seconde picture to aggregate
 * @param img2PosX      X coord where second picture will be but, relative to first one 
 * @param img2PosY      Y coord where second picture will be but, relative to first one 
 * @return            the complete picture
 */
public static BufferedImage concat(BufferedImage img1, BufferedImage img2, Integer img2PosX, Integer img2PosY) {

    if (img2PosX < 0 || img2PosY < 0) {
        throw new IllegalArgumentException("relative position must be > 0");
    }

    Integer finalWidth = Math.max(img2.getWidth() + img2PosX, img1.getWidth());
    Integer finalHeight = Math.max(img2.getHeight() + img2PosY, img1.getHeight());

    BufferedImage img = new BufferedImage(finalWidth, finalHeight, BufferedImage.TYPE_INT_RGB);

    img.createGraphics().drawImage(img1, 0, 0, null);
    img.createGraphics().drawImage(img2, img2PosX, img2PosY, null);

    return img;
}

From source file:com.sketchy.utils.image.SketchyImage.java

public static BufferedImage toByteImage(BufferedImage image) {
    BufferedImage newImage = createByteImage(image.getWidth(), image.getHeight());
    Graphics2D graphics = newImage.createGraphics();
    graphics.setBackground(Color.white);
    graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
    graphics.drawImage(image, 0, 0, null);
    return newImage;
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 4-bit colour using the given colour map. No
 * transparency.//  ww w  .  j  ava  2  s  .com
 * 
 * @param src
 *            the source image to convert
 * @param cmap
 *            the colour map, which should contain no more than 16 entries
 *            The entries are in the form RRGGBB (hex).
 * @return a copy of the source image with a 4-bit colour depth, with the
 *         custom colour pallette
 */
public static BufferedImage convert4(BufferedImage src, int[] cmap) {
    IndexColorModel icm = new IndexColorModel(4, cmap.length, cmap, 0, false, Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_BINARY,
            icm);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);

    return dest;
}

From source file:ImageUtil.java

public static BufferedImage scale(BufferedImage src, int width, int height) throws IOException {
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = dest.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance((double) width / src.getWidth(),
            (double) height / src.getHeight());
    g.drawRenderedImage(src, at);//from www .j  a  va 2  s.  co  m
    return dest;
}

From source file:de.fhg.igd.swingrcp.SwingRCPUtilities.java

/**
 * Applies the given transparency mask to a buffered image. Always creates a
 * new buffered image containing an alpha channel. Copies the old image into
 * the new one and then sets the alpha pixels according to the given mask.
 * /*  w  w w. j a v a 2s  . c o m*/
 * @param img the old image
 * @param mask the alpha mask
 * @return the new image with alpha channel applied
 * @throws IllegalArgumentException if the image's size does not match the
 *             mask's size
 */
public static BufferedImage applyTransparencyMask(BufferedImage img, ImageData mask) {
    if (mask.width != img.getWidth() || mask.height != img.getHeight()) {
        throw new IllegalArgumentException("Image size does not match the mask size");
    }

    // copy image and also convert to RGBA
    BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = result.getGraphics();
    g.drawImage(img, 0, 0, null);

    WritableRaster alphaRaster = result.getAlphaRaster();
    int alpha0[] = new int[] { 0 };
    int alpha255[] = new int[] { 255 };
    for (int y = 0; y < img.getHeight(); y++) {
        for (int x = 0; x < img.getWidth(); x++) {
            alphaRaster.setPixel(x, y, mask.getPixel(x, y) == 0 ? alpha0 : alpha255);
        }
    }

    return result;
}

From source file:com.github.zhanhb.ckfinder.connector.utils.ImageUtils.java

/**
 * creates dimension of thumb./*  w  w w . j a  va 2s .com*/
 *
 * @param image original image.
 * @param maxWidth max thumb width
 * @param maxHeight max thumb height
 * @return dimension of thumb image.
 */
private static Dimension createThumbDimension(BufferedImage image, int maxWidth, int maxHeight) {
    log.debug("image(w={},h={}), max w={}, max h={}", image.getWidth(), image.getHeight(), maxWidth, maxHeight);
    int width = image.getWidth(), height = image.getHeight();

    long w = (long) width * maxHeight;
    long h = (long) height * maxWidth;

    if (w > h) {
        if (width > maxWidth) {
            height = (int) Math.round(1.0 * h / width);
            width = maxWidth;
        }
    } else if (height > maxHeight) {
        width = (int) Math.round(1.0 * w / height);
        height = maxHeight;
    }
    log.debug("didmension w={}, h={}", width, height);
    return new Dimension(width, height);
}

From source file:de.bund.bfr.jung.JungUtils.java

private static Paint mixWith(Paint paint, Color mix) {
    if (paint instanceof Color) {
        Color c = (Color) paint;

        return new Color((c.getRed() + mix.getRed()) / 2, (c.getGreen() + mix.getGreen()) / 2,
                (c.getBlue() + mix.getBlue()) / 2, (c.getAlpha() + mix.getAlpha()) / 2);
    } else if (paint instanceof TexturePaint) {
        BufferedImage texture = ((TexturePaint) paint).getImage();
        BufferedImage mixed = new BufferedImage(texture.getWidth(), texture.getHeight(), texture.getType());

        for (int x = 0; x < texture.getWidth(); x++) {
            for (int y = 0; y < texture.getHeight(); y++) {
                mixed.setRGB(x, y, ((Color) mixWith(new Color(texture.getRGB(x, y)), mix)).getRGB());
            }/*from w  w  w  .ja va 2 s  .  c o m*/
        }

        return new TexturePaint(mixed, new Rectangle(mixed.getWidth(), mixed.getHeight()));
    } else {
        return paint;
    }
}

From source file:ImageProcessing.ImageProcessing.java

public static double[] extractRedColor(BufferedImage source) {
    //Extracts the Red value from the RGB value of the source pixels.
    int imageWidth = source.getWidth();
    int imageHeight = source.getHeight();
    double[] values = new double[imageWidth * imageHeight];

    for (int i = 0; i < imageHeight; i++) {
        for (int j = 0; j < imageWidth; j++) {
            int rgbValue = source.getRGB(j, i);
            Color currentPixel = new Color(rgbValue, true);
            int value = currentPixel.getRed();
            values[(i * imageWidth) + j] = value;
        }//  ww w .  j  a v a2  s  . com
    }

    return values;
}

From source file:de.ppi.selenium.util.ScreenshotUtils.java

/**
 * Checks if a screenshot is complete black.
 *
 * @param var the image.//w w  w.j  a va  2 s.  com
 * @return true if it is black.
 */
private static boolean isBlack(BufferedImage var) {
    final int scale = 3;
    double[] varArr = new double[var.getWidth() * var.getHeight() * scale];
    // unroll pixels
    for (int i = 0; i < var.getHeight(); i++) {
        for (int j = 0; j < var.getWidth(); j++) {
            varArr[i * var.getWidth() + j + 0] = new Color(var.getRGB(j, i)).getRed();
            varArr[i * var.getWidth() + j + 1] = new Color(var.getRGB(j, i)).getGreen();
            varArr[i * var.getWidth() + j + 2] = new Color(var.getRGB(j, i)).getBlue();
        }
    }

    // test if all are black
    for (int i = 0; i != varArr.length; i++) {
        if (varArr[i] != 0) {
            return false;
        }
    }
    return true;
}

From source file:ImageProcessing.ImageProcessing.java

public static double[] extractBlueColor(BufferedImage source) {
    //Extracts the Blue value from the RGB value of the source pixels.
    int imageWidth = source.getWidth();
    int imageHeight = source.getHeight();
    double[] values = new double[imageWidth * imageHeight];

    for (int i = 0; i < imageHeight; i++) {
        for (int j = 0; j < imageWidth; j++) {
            int rgbValue = source.getRGB(j, i);
            Color currentPixel = new Color(rgbValue, true);
            int value = currentPixel.getBlue();
            values[(i * imageWidth) + j] = value;
        }//from  w  w w  .j  a  v  a2  s .c  o  m
    }

    return values;
}