Java Code Examples for java.awt.image.BufferedImage.getRGB()

The following are Jave code examples for showing how to use getRGB() of the java.awt.image.BufferedImage class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ChessBot   File: ImageUtils.java   View Source Code Vote up 9 votes
public static BufferedImage captureImage(String iii) {
    try {
        Rectangle rec = new Rectangle(CAPTURE_1_X, CAPTURE_1_Y, CAPTURE_2_X, CAPTURE_2_Y);
        Robot robot = new Robot();
        
        BufferedImage img = robot.createScreenCapture(rec);
        
        int[] rawData = new int[DesktopScreenRecorder.FRAME_SIZE];

        img.getRGB(0, 0,
                DesktopScreenRecorder.CAPTURE_2_X,
                DesktopScreenRecorder.CAPTURE_2_Y,
                rawData,
                0,
                DesktopScreenRecorder.CAPTURE_2_X);
        
        return img;
    } catch (Exception e) {
        UIUtils.popupError(e, "ImageUtils::writeImage");
    }
    return null;
}
 
Example 2
Project: openjdk-jdk10   File: ImageTool.java   View Source Code Vote up 6 votes
/**
 * Increases image.
 *
 * @param image an image to enlarge.
 * @param zoom A scale.
 * @return a result image.
 */
public static BufferedImage enlargeImage(BufferedImage image, int zoom) {
    int wight = image.getWidth();
    int height = image.getHeight();
    BufferedImage result = new BufferedImage(wight * zoom,
            height * zoom,
            image.getType());
    int rgb;
    for (int x = 0; x < wight; x++) {
        for (int y = 0; y < height; y++) {
            rgb = image.getRGB(x, y);
            for (int i = 0; i < zoom; i++) {
                for (int j = 0; j < zoom; j++) {
                    result.setRGB(x * zoom + i,
                            y * zoom + j,
                            rgb);
                }
            }
        }
    }
    return result;
}
 
Example 3
Project: jdk8u-jdk   File: BMPSubsamplingTest.java   View Source Code Vote up 6 votes
private void checkTestImage(BufferedImage dst) {
    // NB: do not forget about subsampling factor.
    int x = dx / (2 * srcXSubsampling);
    int y = h / (2 * srcYSubsampling);
    System.out.println("Check result: width=" + dst.getWidth() +
        ", height=" + dst.getHeight());
    for (int i = 0; i < colors.length; i++) {
        System.out.println("\tcheck at: " + x + ", " + y);
        int srcRgb = colors[i].getRGB();
        int dstRgb = dst.getRGB(x, y);
        if (srcRgb != dstRgb) {
            throw new RuntimeException("Test failed due to wrong dst color " +
                Integer.toHexString(dstRgb) + " at " + x + "," + y +
                "instead of " + Integer.toHexString(srcRgb));
        }
        x += dx / srcXSubsampling;
    }
}
 
Example 4
Project: jdk8u-jdk   File: ImageTransferTest.java   View Source Code Vote up 6 votes
private static int[] getImageData(Image image) {
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bimage.createGraphics();
    try {
        g2d.drawImage(image, 0, 0, width, height, null);
    } finally {
        g2d.dispose();
    }
    return bimage.getRGB(0, 0, width, height, null, 0, width);
}
 
Example 5
Project: openjdk-jdk10   File: bug7181438.java   View Source Code Vote up 6 votes
public static void main(final String[] args) {

        final BufferedImage bi = createBufferedImage();
        final VolatileImage vi = createVolatileImage();
        final Graphics s2dVi = vi.getGraphics();

        //sw->texture->surface blit
        s2dVi.drawImage(bi, 0, 0, null);

        final BufferedImage results = vi.getSnapshot();
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                //Image should be opaque: (black color and alpha = 255)
                if (results.getRGB(i, j) != 0xFF000000) {
                    throw new RuntimeException("Failed: Wrong alpha");
                }
            }
        }
        System.out.println("Passed");
    }
 
Example 6
Project: jdk8u-jdk   File: Util.java   View Source Code Vote up 6 votes
/**
 * Compares two bufferedImages pixel-by-pixel.
 * return true if all pixels in the two areas are identical
 */
public static boolean compareBufferedImages(BufferedImage bufferedImage0, BufferedImage bufferedImage1) {
    int width = bufferedImage0.getWidth();
    int height = bufferedImage0.getHeight();

    if (width != bufferedImage1.getWidth() || height != bufferedImage1.getHeight()) {
        return false;
    }

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (bufferedImage0.getRGB(x, y) != bufferedImage1.getRGB(x, y)) {
                return false;
            }
        }
    }

    return true;
}
 
Example 7
Project: openjdk-jdk10   File: DrawRotatedStringUsingRotatedFont.java   View Source Code Vote up 6 votes
/**
 * Checks an image color. RED and GREEN are allowed only.
 */
private static void checkColors(final BufferedImage bi1,
                                final BufferedImage bi2)
        throws IOException {
    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            final int rgb1 = bi1.getRGB(i, j);
            final int rgb2 = bi2.getRGB(i, j);
            if (rgb1 != rgb2 || rgb1 != 0xFFFF0000 && rgb1 != 0xFF00FF00) {
                ImageIO.write(bi1, "png", new File("image1.png"));
                ImageIO.write(bi2, "png", new File("image2.png"));
                throw new RuntimeException("Failed: wrong text location");
            }
        }
    }
}
 
Example 8
Project: BaseClient   File: ShadersTex.java   View Source Code Vote up 6 votes
public static void loadNSMap1(IResourceManager manager, ResourceLocation location, int width, int height, int[] aint, int offset, int defaultColor)
{
    boolean flag = false;

    try
    {
        IResource iresource = manager.getResource(location);
        BufferedImage bufferedimage = ImageIO.read(iresource.getInputStream());

        if (bufferedimage != null && bufferedimage.getWidth() == width && bufferedimage.getHeight() == height)
        {
            bufferedimage.getRGB(0, 0, width, height, aint, offset, width);
            flag = true;
        }
    }
    catch (IOException var10)
    {
        ;
    }

    if (!flag)
    {
        Arrays.fill(aint, offset, offset + width * height, defaultColor);
    }
}
 
Example 9
Project: jdk8u-jdk   File: IncorrectAlphaConversionBicubic.java   View Source Code Vote up 5 votes
public static void main(final String[] args) {
    final GraphicsEnvironment ge =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
    final GraphicsDevice gd = ge.getDefaultScreenDevice();
    final GraphicsConfiguration gc = gd.getDefaultConfiguration();
    final VolatileImage vi =
            gc.createCompatibleVolatileImage(SIZE, SIZE, TRANSLUCENT);
    final BufferedImage bi = makeUnmanagedBI(gc, TRANSLUCENT);
    final int expected = bi.getRGB(2, 2);

    int attempt = 0;
    BufferedImage snapshot;
    while (true) {
        if (++attempt > 10) {
            throw new RuntimeException("Too many attempts: " + attempt);
        }
        vi.validate(gc);
        final Graphics2D g2d = vi.createGraphics();
        g2d.setComposite(AlphaComposite.Src);
        g2d.scale(2, 2);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                             RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.drawImage(bi, 0, 0, null);
        g2d.dispose();

        snapshot = vi.getSnapshot();
        if (vi.contentsLost()) {
            continue;
        }
        break;
    }
    final int actual = snapshot.getRGB(2, 2);
    if (actual != expected) {
        System.err.println("Actual: " + Integer.toHexString(actual));
        System.err.println("Expected: " + Integer.toHexString(expected));
        throw new RuntimeException("Test failed");
    }
}
 
Example 10
Project: openjdk-jdk10   File: AnimationTest.java   View Source Code Vote up 5 votes
protected void checkFrame(int i, BufferedImage f) {
    int x = 2 * i + 1;
    for (int y = 0; y < h; y++) {
        int argb = f.getRGB(x, y);
        if (argb != 0xffffffff && !(argb == 0xff0000ff && y == 2 * i)) {
            throw new RuntimeException("Test failed - bad frame");
        }
        argb = f.getRGB(y, x);
        if (argb != 0xffffffff && !(argb == 0xffff0000 && y == 2 * i)) {
            throw new RuntimeException("Test failed - bad frame");
        }
    }
}
 
Example 11
Project: jdk8u-jdk   File: DrawImageCoordsTest.java   View Source Code Vote up 5 votes
public static void main(String[] args) {

        /* Create an image to draw, filled in solid red. */
        BufferedImage srcImg =
             new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        Graphics srcG = srcImg.createGraphics();
        srcG.setColor(Color.red);
        int w = srcImg.getWidth(null);
        int h = srcImg.getHeight(null);
        srcG.fillRect(0, 0, w, h);

        /* Create a destination image */
        BufferedImage dstImage =
           new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        Graphics2D dstG = dstImage.createGraphics();
        /* draw image under a scaling transform that overflows int */
        AffineTransform tx = new AffineTransform(0.5, 0, 0, 0.5,
                                                  0, 5.8658460197478485E9);
        dstG.setTransform(tx);
        dstG.drawImage(srcImg, 0, 0, null );
        /* draw image under the same overflowing transform, cancelling
         * out the 0.5 scale on the graphics
         */
        dstG.drawImage(srcImg, 0, 0, 2*w, 2*h, null);
        if (Color.red.getRGB() == dstImage.getRGB(w/2, h/2)) {
             throw new RuntimeException("Unexpected color: clipping failed.");
        }
        System.out.println("Test Thread Completed");
    }
 
Example 12
Project: BaseClient   File: Config.java   View Source Code Vote up 5 votes
private static ByteBuffer readIconImage(File p_readIconImage_0_) throws IOException
{
    BufferedImage bufferedimage = ImageIO.read(p_readIconImage_0_);
    int[] aint = bufferedimage.getRGB(0, 0, bufferedimage.getWidth(), bufferedimage.getHeight(), (int[])null, 0, bufferedimage.getWidth());
    ByteBuffer bytebuffer = ByteBuffer.allocate(4 * aint.length);

    for (int i : aint)
    {
        bytebuffer.putInt(i << 8 | i >> 24 & 255);
    }

    bytebuffer.flip();
    return bytebuffer;
}
 
Example 13
Project: GIFKR   File: PixelSortFilter.java   View Source Code Vote up 5 votes
protected void lineFill(BufferedImage img, int x0, int x1, int y) {

		int c = img.getRGB(x0, y);	

		for(int x = x0+1; x <= x1; x++) {
			img.setRGB(x, y, c);
		}
	}
 
Example 14
Project: jdk8u-jdk   File: ThinLineTest.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
  BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
  Graphics2D g = image.createGraphics();

  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g.setPaint(Color.WHITE);
  g.fill(new Rectangle(image.getWidth(), image.getHeight()));

  g.scale(0.5 / PIXEL, 0.5 / PIXEL);
  g.setPaint(Color.BLACK);
  g.setStroke(new BasicStroke(PIXEL));
  g.draw(new Ellipse2D.Double(PIXEL * 50, PIXEL * 50, PIXEL * 300, PIXEL * 300));

  // To visually check it
  //ImageIO.write(image, "PNG", new File(args[0]));

  boolean nonWhitePixelFound = false;
  for (int x = 0; x < 200; ++x) {
    if (image.getRGB(x, 100) != Color.WHITE.getRGB()) {
      nonWhitePixelFound = true;
      break;
    }
  }
  if (!nonWhitePixelFound) {
    throw new RuntimeException("The thin line disappeared.");
  }
}
 
Example 15
Project: jdk8u-jdk   File: TransformedPaintTest.java   View Source Code Vote up 5 votes
private void checkBI(BufferedImage bi) {
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            if (bi.getRGB(x, y) == Color.blue.getRGB()) {
                try {
                    String fileName = "TransformedPaintTest_res.png";
                    ImageIO.write(bi, "png", new File(fileName));
                    System.err.println("Dumped image to: " + fileName);
                } catch (IOException ex) {}
                throw new RuntimeException("Test failed, blue color found");
            }
        }
    }
}
 
Example 16
Project: openjdk-jdk10   File: EncodeSubImageTest.java   View Source Code Vote up 5 votes
private void checkPixel(BufferedImage dst, int x, int y,
                        boolean isSubsampled)
{
    int dx = isSubsampled ? x / subSampleX : x;
    int dy = isSubsampled ? y / subSampleY : y;
    int src_rgb = img.getRGB(x, y);
    System.out.printf("src_rgb: %x\n", src_rgb);

    int dst_rgb = dst.getRGB(dx, dy);
    System.out.printf("dst_rgb: %x\n", dst_rgb);

    if (src_rgb != dst_rgb) {
        throw new RuntimeException("Test FAILED: invalid color in dst");
    }
}
 
Example 17
Project: jdk8u-jdk   File: JpegImageColorSpaceTest.java   View Source Code Vote up 5 votes
public static void main(String args[]) throws Exception {

        String fileName = "nomarkers.jpg";
        String sep = System.getProperty("file.separator");
        String dir = System.getProperty("test.src", ".");
        String filePath = dir+sep+fileName;
        System.out.println("Test file: " + filePath);
        File imageFile = new File(filePath);

        BufferedImage bufferedImage = ImageIO.read(imageFile);
        int imageWidth = bufferedImage.getWidth();
        int imageHeight = bufferedImage.getHeight();

        for (int i = 0; i < imageWidth; i++) {
            for(int j = 0; j < imageHeight; j++) {
                /*
                * Since image is white we check individual pixel values from
                * BufferedImage to verify if ImageIO.read() is done with proper
                * color space or not.
                */
                if (bufferedImage.getRGB(i, j) != Color.white.getRGB()) {
                    // color space is not proper
                    throw new RuntimeException("ColorSpace is not determined "
                            + "properly by ImageIO");
               }
            }
        }
    }
 
Example 18
Project: BaseClient   File: DynamicTexture.java   View Source Code Vote up 4 votes
public DynamicTexture(BufferedImage bufferedImage)
{
    this(bufferedImage.getWidth(), bufferedImage.getHeight());
    bufferedImage.getRGB(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), this.dynamicTextureData, 0, bufferedImage.getWidth());
    this.updateDynamicTexture();
}
 
Example 19
Project: Backmemed   File: ServerListEntryNormal.java   View Source Code Vote up 4 votes
private void prepareServerIcon()
{
    if (this.server.getBase64EncodedIconData() == null)
    {
        this.mc.getTextureManager().deleteTexture(this.serverIcon);
        this.icon = null;
    }
    else
    {
        ByteBuf bytebuf = Unpooled.copiedBuffer((CharSequence)this.server.getBase64EncodedIconData(), Charsets.UTF_8);
        ByteBuf bytebuf1 = null;
        BufferedImage bufferedimage;
        label103:
        {
            try
            {
                bytebuf1 = Base64.decode(bytebuf);
                bufferedimage = TextureUtil.readBufferedImage(new ByteBufInputStream(bytebuf1));
                Validate.validState(bufferedimage.getWidth() == 64, "Must be 64 pixels wide", new Object[0]);
                Validate.validState(bufferedimage.getHeight() == 64, "Must be 64 pixels high", new Object[0]);
                break label103;
            }
            catch (Throwable throwable)
            {
                LOGGER.error("Invalid icon for server {} ({})", new Object[] {this.server.serverName, this.server.serverIP, throwable});
                this.server.setBase64EncodedIconData((String)null);
            }
            finally
            {
                bytebuf.release();

                if (bytebuf1 != null)
                {
                    bytebuf1.release();
                }
            }

            return;
        }

        if (this.icon == null)
        {
            this.icon = new DynamicTexture(bufferedimage.getWidth(), bufferedimage.getHeight());
            this.mc.getTextureManager().loadTexture(this.serverIcon, this.icon);
        }

        bufferedimage.getRGB(0, 0, bufferedimage.getWidth(), bufferedimage.getHeight(), this.icon.getTextureData(), 0, bufferedimage.getWidth());
        this.icon.updateDynamicTexture();
    }
}
 
Example 20
Project: jdk8u-jdk   File: DrawImageBgTest.java   View Source Code Vote up 4 votes
public static void main(String[] args) {
    GraphicsConfiguration gc =
        GraphicsEnvironment.getLocalGraphicsEnvironment().
            getDefaultScreenDevice().getDefaultConfiguration();

    if (gc.getColorModel().getPixelSize() <= 8) {
        System.out.println("8-bit color model, test considered passed");
        return;
    }

    /*
     * Set up images:
     * 1.) VolatileImge for rendering to,
     * 2.) BufferedImage for reading back the contents of the VI
     * 3.) The image triggering the problem
     */
    VolatileImage vImg = null;
    BufferedImage readBackBImg;

    // create a BITMASK ICM such that the transparent color is
    // tr. black (and it's the first in the color map so a buffered image
    // created with this ICM is transparent
    byte r[] = { 0x00, (byte)0xff};
    byte g[] = { 0x00, (byte)0xff};
    byte b[] = { 0x00, (byte)0xff};
    IndexColorModel icm = new IndexColorModel(8, 2, r, g, b, 0);
    WritableRaster wr = icm.createCompatibleWritableRaster(25, 25);
    BufferedImage tImg = new BufferedImage(icm, wr, false, null);

    do {
        if (vImg == null ||
            vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE)
        {
            vImg = gc.createCompatibleVolatileImage(tImg.getWidth(),
                                                    tImg.getHeight());
        }

        Graphics viG = vImg.getGraphics();
        viG.setColor(Color.red);
        viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());

        viG.drawImage(tImg, 0, 0, Color.green, null);
        viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
        viG.drawImage(tImg, 0, 0, Color.white, null);

        readBackBImg = vImg.getSnapshot();
    } while (vImg.contentsLost());

    for (int x = 0; x < readBackBImg.getWidth(); x++) {
        for (int y = 0; y < readBackBImg.getHeight(); y++) {
            int currPixel = readBackBImg.getRGB(x, y);
            if (currPixel != Color.white.getRGB()) {
                String fileName = "DrawImageBgTest.png";
                try {
                    ImageIO.write(readBackBImg, "png", new File(fileName));
                    System.err.println("Dumped image to " + fileName);
                } catch (IOException ex) {}
                throw new
                    RuntimeException("Test Failed: found wrong color: 0x"+
                                     Integer.toHexString(currPixel));
            }
        }
    }
    System.out.println("Test Passed.");
}