Example usage for java.awt.image BufferedImage getRGB

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

Introduction

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

Prototype

public int getRGB(int x, int y) 

Source Link

Document

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.

Usage

From source file:net.team2xh.crt.gui.util.GUIToolkit.java

/**
 * Provides a ImageIcon from a file name.
 *
 * @param path Picture file name/*from ww  w.jav a  2  s  .  c o  m*/
 * @return     ImageIcon
 */
public static Image getIcon(String path) {
    Image img = null;
    Theme theme = Theme.getTheme();
    try {
        URI uri = theme.getClass().getResource(path).toURI();
        File file = new File(uri);
        img = ImageIO.read(file);

    } catch (IOException ex) {
        Logger.getLogger(GUIToolkit.class.getName()).log(Level.SEVERE, null, ex);
    } catch (URISyntaxException ex) {
        Exceptions.printStackTrace(ex);
    }

    if (img != null) {
        if (theme.getClass() == DarkTheme.class) {
            // If dark theme, invert colors
            // http://stackoverflow.com/a/25425107
            BufferedImage bi = (BufferedImage) img;
            for (int y = 0; y < bi.getHeight(); ++y) {
                for (int x = 0; x < bi.getWidth(); ++x) {
                    int c = bi.getRGB(x, y);
                    bi.setRGB(x, y, 0x00ffffff - (c | 0xff000000) | (c & 0xff000000));
                }
            }
        }
    }

    return img;
}

From source file:org.squidy.designer.util.ImageUtils.java

/**
 * Converts a given image to a grayscale image.
 * //from  ww w .  ja  v  a  2  s .co m
 * @param source
 * @return
 */
public static BufferedImage convertToGrayscaleImage(BufferedImage source) {
    BufferedImage grayImage = new BufferedImage(source.getWidth(), source.getHeight(),
            BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < source.getWidth(); x++)
        for (int y = 0; y < source.getHeight(); y++) {
            int argb = source.getRGB(x, y);

            int a = (argb >> 24) & 0xff;
            int r = (argb >> 16) & 0xff;
            int g = (argb >> 8) & 0xff;
            int b = (argb) & 0xff;

            int l = (int) (.299 * r + .587 * g + .114 * b); // luminance

            grayImage.setRGB(x, y, (a << 24) + (l << 16) + (l << 8) + l);
        }
    return grayImage;
}

From source file:imageprocessingproject.ImageHistogram.java

public static BufferedImage autoContrastImage(BufferedImage image) {
    createContrastLUT(image);/* ww  w  .  ja  v  a2 s .c  o  m*/

    int height = image.getHeight();
    int width = image.getWidth();
    int r, g, b;
    Color c;

    BufferedImage tempImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            r = (int) (255 * contrast_lut[c.getRed()]);
            g = (int) (255 * contrast_lut[c.getGreen()]);
            b = (int) (255 * contrast_lut[c.getBlue()]);
            tempImage.setRGB(i, j, new Color(r, g, b, c.getAlpha()).getRGB());
        }
    }

    return tempImage;
}

From source file:net.tbnr.util.render.ImageToChatBukkitUtil.java

public static Color getColor(BufferedImage image, int x, int y) {
    if (x < 0 || x >= image.getWidth(null)) {
        throw new IndexOutOfBoundsException("x must be between 0 and " + (image.getWidth(null) - 1));
    }/*from   ww  w .j  av a  2  s. c o m*/
    if (y < 0 || y >= image.getHeight(null)) {
        throw new IndexOutOfBoundsException("y must be between 0 and " + (image.getHeight(null) - 1));
    }
    return new Color(image.getRGB(x, y));
}

From source file:com.reydentx.core.common.PhotoUtils.java

public static boolean isBlackJPEG(BufferedImage bi) {
    if (bi != null) {
        int height = bi.getHeight();
        int width = bi.getWidth();

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (bi.getRGB(x, y) != -16777216) {
                    return false;
                }/*from w  w w.j  a  v  a  2s.  c  o  m*/
            }
        }
    }

    return true;
}

From source file:imageprocessingproject.ImageHistogram.java

public static BufferedImage normalizeImage(BufferedImage image) {
    int height = image.getHeight();
    int width = image.getWidth();

    int r, g, b, minr = 255, ming = 255, minb = 255, maxr = 0, maxg = 0, maxb = 0;
    Color c;/*  w w w . j a va 2s . c  o m*/

    BufferedImage tempImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            if (minr > c.getRed()) {
                minr = c.getRed();
            }
            if (ming > c.getGreen()) {
                ming = c.getGreen();
            }
            if (minb > c.getBlue()) {
                minb = c.getBlue();
            }
            if (maxr < c.getRed()) {
                maxr = c.getRed();
            }
            if (maxg < c.getGreen()) {
                maxg = c.getGreen();
            }
            if (maxb < c.getBlue()) {
                maxb = c.getBlue();
            }
        }
    }

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            r = (int) ((c.getRed() - minr) * 255 / (maxr - minr));
            g = (int) ((c.getGreen() - ming) * 255 / (maxg - ming));
            b = (int) ((c.getBlue() - minb) * 255 / (maxb - minb));
            tempImage.setRGB(i, j, new Color(r, g, b, c.getAlpha()).getRGB());
        }
    }

    return tempImage;
}

From source file:ImageProcessing.ImageProcessing.java

public static void copyImage(BufferedImage source_image, BufferedImage target_image) {
    //Copies pixel data from source image to target image.
    //The size will not be copied/adjusted, so keep in mind the size of both images.
    for (int i = 0; i < target_image.getHeight(); i++) {
        for (int j = 0; j < target_image.getWidth(); j++) {
            int rgb = source_image.getRGB(j, i);
            target_image.setRGB(j, i, rgb);
        }//w ww. j  ava  2s .c  om
    }
}

From source file:net.cloudkit.relaxation.CaptchaTest.java

public static BufferedImage removeBlank(BufferedImage img, int whiteThreshold, int white) throws Exception {
    final int width = img.getWidth();
    final int height = img.getHeight();
    int start = 0;
    int end = 0;/*from w w  w  .j a v a  2s .  c o  m*/
    Label1: for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            if (isWhite(img.getRGB(x, y), whiteThreshold) == white) {
                start = y;
                break Label1;
            }
        }
    }
    Label2: for (int y = height - 1; y >= 0; --y) {
        for (int x = 0; x < width; ++x) {
            if (isWhite(img.getRGB(x, y), whiteThreshold) == white) {
                end = y;
                break Label2;
            }
        }
    }
    return img.getSubimage(0, start, width, end - start + 1);
}

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

/**
 * Checks if a screenshot is complete black.
 *
 * @param var the image.//  ww w  . j  a v a  2 s.c om
 * @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: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  .  j  a va 2  s .co  m*/
        }

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