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:baocaoxla.xuly_compare.java

public int chi_square(BufferedImage image, BufferedImage imgcompare) {
    int[] his = new int[256];
    int[] hisnomal = new int[256];
    int[] his1 = new int[256];

    for (int i = 0; i < 256; i++) {
        his[i] = 0;// w w  w.  j  a v a 2s .  co m
        hisnomal[i] = 0;
        his1[i] = 0;

    }
    int width = image.getWidth();
    int height = image.getHeight();
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            Color c = new Color(image.getRGB(j, i));
            his[(c.getRed() + c.getGreen() + c.getBlue()) / 3]++;
            // hisnomal[255-(c.getRed() + c.getGreen() + c.getBlue()) / 3]++;

        }
    }
    int width1 = imgcompare.getWidth();
    int height1 = imgcompare.getHeight();
    for (int i = 0; i < height1; i++) {
        for (int j = 0; j < width1; j++) {
            Color c = new Color(imgcompare.getRGB(j, i));
            his1[(c.getRed() + c.getGreen() + c.getBlue()) / 3]++;
        }
    }
    double sum = 0;
    double sum1 = 0;
    for (int i = 0; i < 256; i++) {
        sum1 = sum1 + Math.pow(hisnomal[i] - his[i], 2) / (hisnomal[i] + his[i]);
    }
    for (int i = 0; i < 256; i++) {
        sum = sum + Math.pow(his1[i] - his[i], 2) / (his1[i] + his[i]);
    }
    int percent = (int) ((1 - sum / sum1) * 100);
    return percent;
}

From source file:VASSAL.tools.image.ImageIOImageLoaderTest.java

@Test
public void testLoadType2tRNSBug() throws IOException {
    final String efile = "test/VASSAL/tools/image/non-type2-tRNS.png";
    final String afile = "test/VASSAL/tools/image/type2-tRNS.png";

    final ImageTypeConverter mconv = new MemoryImageTypeConverter();
    final ImageIOImageLoader loader = new ImageIOImageLoader(mconv);

    final BufferedImage expected = ImageIO.read(new File(efile));
    final BufferedImage actual = read(loader, afile);

    assertEquals(BufferedImage.TYPE_INT_ARGB, actual.getType());

    // We check that:
    // (1) the images have the same fully-transparent pixels, and
    // (2) all other pixels are identical
    final int w = expected.getWidth();
    final int h = expected.getHeight();
    for (int x = 0; x < w; ++x) {
        for (int y = 0; y < h; ++y) {
            final int ep = expected.getRGB(x, y);
            final int ap = actual.getRGB(x, y);

            if ((ep & 0xff000000) == 0 && (ap & 0xff000000) == 0) {
                // color components of fully-transparent pixels don't matter
                continue;
            }/* w ww .  j a  v  a 2 s .c  o  m*/

            assertEquals(ep, ap);
        }
    }
}

From source file:baocaoxla.xuly_compare.java

public int Correlation(BufferedImage image, BufferedImage imgcompare) {
    //BufferedImage img=new BufferedImage(image.getWidth(),get, imageType)
    int[] his = new int[256];
    int[] his1 = new int[256];

    for (int i = 0; i < 256; i++) {
        his[i] = 0;/*from   ww  w . ja v a  2  s  .com*/
        his1[i] = 0;

    }
    int width = image.getWidth();
    int height = image.getHeight();
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            Color c = new Color(image.getRGB(j, i));
            his[(c.getRed() + c.getGreen() + c.getBlue()) / 3]++;

        }
    }
    int width1 = imgcompare.getWidth();
    int height1 = imgcompare.getHeight();
    for (int i = 0; i < height1; i++) {
        for (int j = 0; j < width1; j++) {
            Color c = new Color(imgcompare.getRGB(j, i));
            his1[(c.getRed() + c.getGreen() + c.getBlue()) / 3]++;
        }
    }
    double sumhis = 0;
    double sumhis1 = 0;

    for (int i = 0; i < 256; i++) {
        sumhis = sumhis + his[i];
        sumhis1 = sumhis1 + his1[i];
    }
    double tu = 0;
    double mau1 = 0;
    double mau2 = 0;
    for (int i = 0; i < 256; i++) {
        tu = tu + (his[i] - sumhis / 256) * (his1[i] - sumhis1 / 256);
        mau1 = mau1 + Math.pow((his[i] - sumhis / 256), 2);
        mau2 = mau2 + Math.pow((his1[i] - sumhis1 / 256), 2);
    }
    double mau = Math.sqrt(mau1 * mau2);
    int compare = (int) (((tu / mau) + 1) * 50);

    return compare;
}

From source file:edworld.pdfreader4humans.PDFReaderTest.java

private void assertImagesAreSimilar(InputStream expectedOutputStream, BufferedImage outputImage)
        throws IOException {
    try {/*from  w w  w  .  j  a  va2 s.c om*/
        BufferedImage expectedOutputImage = ImageIO.read(expectedOutputStream);
        assertEquals(expectedOutputImage.getType(), outputImage.getType());
        assertEquals(expectedOutputImage.getWidth(), outputImage.getWidth());
        assertEquals(expectedOutputImage.getHeight(), outputImage.getHeight());
        assertEquals(expectedOutputImage.getTransparency(), outputImage.getTransparency());
        for (int k = 0; k < Math.max(outputImage.getWidth(), outputImage.getHeight()); k++) {
            int kX = k % outputImage.getWidth();
            int kY = k % outputImage.getHeight();
            int expectedColor = expectedOutputImage.getRGB(kX, kY);
            int actualColor = outputImage.getRGB(kX, kY);
            if ((expectedColor ^ 0xFFFFFF) == actualColor)
                expectedColor ^= 0xFFFFFF;
            assertEquals("Color should be the same at (" + k + "," + k + ").", expectedColor, actualColor);
        }
    } finally {
        expectedOutputStream.close();
    }
}

From source file:org.nuxeo.diff.pictures.test.DiffPicturesTest.java

@Test
public void testSamePicture() throws Exception {

    File img1 = FileUtils.getResourceFileFromContext(ISLAND_PNG);
    File img2 = FileUtils.getResourceFileFromContext(ISLAND_PNG);

    FileBlob blob1 = new FileBlob(img1);
    FileBlob blob2 = new FileBlob(img2);

    Blob result;/*from w w w.  j  a v  a  2 s  .c  o  m*/
    HashMap<String, Serializable> params;
    DiffPictures dp = new DiffPictures(blob1, blob2);

    // We make the whole thing red
    params = new HashMap<String, Serializable>();
    params.put("highlightColor", "Red");
    params.put("lowlightColor", "Red");
    result = dp.compare(null, params);

    BufferedImage bi = checkIsImage(result);
    assertEquals(bi.getWidth(), ISLAND_W);
    assertEquals(bi.getHeight(), ISLAND_H);

    // Test a 40x40 rectangle in the middle, where every pixel should be red
    int start_i = (ISLAND_W / 2) - 20;
    int max_i = (ISLAND_W / 2) + 20;
    int start_j = (ISLAND_H / 2) - 20;
    int max_j = (ISLAND_H / 2) + 20;

    for (int i = start_i; i < max_i; i++) {
        for (int j = start_j; j < max_j; j++) {
            int pixel = bi.getRGB(i, j);
            int r = pixel >> 16 & 0xff;
            int g = pixel >> 8 & 0xff;
            int b = pixel & 0xff;

            assertTrue("r should be 255", r == 255);
            assertTrue("g should be 0", g == 0);
            assertTrue("b should be 0", b == 0);
        }
    }

    deleteFile(result);

}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage flipImageHorizontal(BufferedImage img) {
    if (img == null) {
        return null;
    }/*from  ww w.j a  v  a 2 s  . c  o m*/

    if (img.getType() == 0) {
        img = convertToARGB(img);
    }
    BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
    for (int y = 0; y < out.getHeight(); y++) {
        for (int x = 0; x < out.getWidth(); x++) {
            out.setRGB(x, y, img.getRGB(img.getWidth() - x - 1, y));
        }
    }
    return out;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage flipImageVertical(BufferedImage img) {
    if (img == null) {
        return null;
    }/*from w w w  .j  a  v a 2s .com*/

    if (img.getType() == 0) {
        img = convertToARGB(img);
    }
    BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
    for (int y = 0; y < out.getHeight(); y++) {
        for (int x = 0; x < out.getWidth(); x++) {
            out.setRGB(x, y, img.getRGB(x, img.getHeight() - y - 1));
        }
    }
    return out;
}

From source file:oct.analysis.application.comp.EZWorker.java

/**
 * Determine if the supplied coordinate (excluding itself) in the supplied
 * black and white image is surrounded by white pixels on all sides.
 *
 * @param xStart//w w w  .j  av  a  2 s  .c om
 * @param yStart
 * @param sharpOCT
 * @return
 */
public boolean isSurroundedByWhite(int xStart, int yStart, BufferedImage sharpOCT) {
    boolean allWhite = true;
    for (int x = -1; x < 2; x++) {
        for (int y = -1; y < 2; y++) {
            if (x != 0 && y != 0) {
                allWhite &= Util.calculateGrayScaleValue(sharpOCT.getRGB(xStart + x, yStart + y)) > 0;
            }
        }
    }
    return allWhite;
}

From source file:org.apache.pdfbox.tools.imageio.TestImageIOUtils.java

/**
 * Checks whether file image size and content are identical
 *
 * @param filename the filename where we just wrote to
 * @param image the image that is to be checked
 * @throws IOException if something goes wrong
 *//* www .j a  v a2s. c  o  m*/
private void checkImageFileSizeAndContent(String filename, BufferedImage image) throws IOException {
    BufferedImage newImage = ImageIO.read(new File(filename));
    assertNotNull("File '" + filename + "' could not be read", newImage);
    checkNotBlank(filename, newImage);
    checkBufferedImageSize(filename, image, newImage);
    for (int x = 0; x < image.getWidth(); ++x) {
        for (int y = 0; y < image.getHeight(); ++y) {
            if (image.getRGB(x, y) != newImage.getRGB(x, y)) {
                assertEquals("\"File '" + filename + "' has different pixel at (" + x + "," + y + ")",
                        new Color(image.getRGB(x, y)), new Color(newImage.getRGB(x, y)));
            }
        }
    }
}

From source file:org.springframework.boot.ImageBanner.java

private void printBanner(BufferedImage image, int margin, boolean invert, PrintStream out) {
    AnsiElement background = (invert ? AnsiBackground.BLACK : AnsiBackground.DEFAULT);
    out.print(AnsiOutput.encode(AnsiColor.DEFAULT));
    out.print(AnsiOutput.encode(background));
    out.println();//from   ww  w  .  j a va  2  s. co  m
    out.println();
    AnsiColor lastColor = AnsiColor.DEFAULT;
    for (int y = 0; y < image.getHeight(); y++) {
        for (int i = 0; i < margin; i++) {
            out.print(" ");
        }
        for (int x = 0; x < image.getWidth(); x++) {
            Color color = new Color(image.getRGB(x, y), false);
            AnsiColor ansiColor = AnsiColors.getClosest(color);
            if (ansiColor != lastColor) {
                out.print(AnsiOutput.encode(ansiColor));
                lastColor = ansiColor;
            }
            out.print(getAsciiPixel(color, invert));
        }
        out.println();
    }
    out.print(AnsiOutput.encode(AnsiColor.DEFAULT));
    out.print(AnsiOutput.encode(AnsiBackground.DEFAULT));
    out.println();
}