Java Code Examples for java.awt.image.BufferedImage#getRGB()
The following examples show how to use
java.awt.image.BufferedImage#getRGB() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TopDownTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 7 votes |
private static void verify(BufferedImage dst) { int top_rgb = dst.getRGB(50, 25); System.out.printf("top_rgb: %x\n", top_rgb); int bot_rgb = dst.getRGB(50, 75); System.out.printf("bot_rgb: %x\n", bot_rgb); // expect to see blue color on the top of image if (top_rgb != 0xff0000ff) { throw new RuntimeException("Invaid top color: " + Integer.toHexString(bot_rgb)); } if (bot_rgb != 0xffff0000) { throw new RuntimeException("Invalid bottom color: " + Integer.toHexString(bot_rgb)); } }
Example 2
Source File: MutableColorTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
private static void testResult(final String desc, final BufferedImage snapshot, final int evilColor) { for (int y = 0; y < snapshot.getHeight(); y++) { for (int x = 0; x < snapshot.getWidth(); x++) { int snapRGB = snapshot.getRGB(x, y); if (!isSameColor(snapRGB, evilColor)) { System.err.printf("Wrong RGB for %s at (%d,%d): 0x%x " + "instead of 0x%x\n", desc, x, y, snapRGB, evilColor); String fileName = "MutableColorTest_"+desc+".png"; try { ImageIO.write(snapshot, "png", new File(fileName)); System.err.println("Dumped snapshot to "+fileName); } catch (IOException ex) {} throw new RuntimeException("Test FAILED."); } } } }
Example 3
Source File: bug7181438.java From jdk8u_jdk with GNU General Public License v2.0 | 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 4
Source File: Images.java From lams with GNU General Public License v2.0 | 6 votes |
public static double distance(BufferedImage imgA, BufferedImage imgB) { // The images must be the same size. if (imgA.getWidth() == imgB.getWidth() && imgA.getHeight() == imgB.getHeight()) { int width = imgA.getWidth(); int height = imgA.getHeight(); double mse = 0; // Loop over every pixel. for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color ca = new Color(imgA.getRGB(x, y)); Color cb = new Color(imgB.getRGB(x, y)); double variance = sqr(ca.getRed() - cb.getRed()) // + sqr(ca.getBlue() - cb.getBlue()) // + sqr(ca.getGreen() - cb.getGreen()) // + sqr(ca.getAlpha() - cb.getAlpha()); mse += variance; } } return Math.sqrt(mse / height / width); } else { return -1; } }
Example 5
Source File: ASCII.java From VileBot with MIT License | 6 votes |
public String convert( final BufferedImage image ) { StringBuilder sb = new StringBuilder( ( image.getWidth() + 1 ) * image.getHeight() ); for ( int y = 0; y < image.getHeight(); y++ ) { if ( sb.length() != 0 ) sb.append( "\n" ); for ( int x = 0; x < image.getWidth(); x++ ) { Color pixelColor = new Color( image.getRGB( x, y ) ); double gValue = (double) pixelColor.getRed() * 0.2989 + (double) pixelColor.getBlue() * 0.5870 + (double) pixelColor.getGreen() * 0.1140; final char s = negative ? returnStrNeg( gValue ) : returnStrPos( gValue ); sb.append( s ); } } return sb.toString(); }
Example 6
Source File: LUTCompareTest.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private static void checkResults(Image image) { BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g = buf.createGraphics(); g.setColor(Color.pink); g.fillRect(0, 0, w, h); g.drawImage(image, 0, 0, null); g.dispose(); int rgb = buf.getRGB(w/2, h/2); System.out.printf("Result color: %x\n", rgb); /* Buffered image should be the same as the last frame * of animated sequence (which is filled with blue). * Any other color indicates the problem. */ if (rgb != 0xff0000ff) { throw new RuntimeException("Test FAILED!"); } System.out.println("Test PASSED."); }
Example 7
Source File: PcxWriter.java From commons-imaging with Apache License 2.0 | 6 votes |
private void writePixels32(final BufferedImage src, final int bytesPerLine, final BinaryOutputStream bos) throws IOException, ImageWriteException { final int[] rgbs = new int[src.getWidth()]; final byte[] plane = new byte[4 * bytesPerLine]; for (int y = 0; y < src.getHeight(); y++) { src.getRGB(0, y, src.getWidth(), 1, rgbs, 0, src.getWidth()); for (int x = 0; x < rgbs.length; x++) { plane[4 * x + 0] = (byte) rgbs[x]; plane[4 * x + 1] = (byte) (rgbs[x] >> 8); plane[4 * x + 2] = (byte) (rgbs[x] >> 16); plane[4 * x + 3] = 0; } rleWriter.write(bos, plane); } rleWriter.flush(bos); }
Example 8
Source File: bug7181438.java From dragonwell8_jdk with GNU General Public License v2.0 | 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 9
Source File: GraphicC2Translator.java From GraphicCR with Apache License 2.0 | 6 votes |
/** * 将训练元字符装在一起 * * @param trainImg * @param smallImg * @param ch * @return */ private boolean addTrainImg(BufferedImage trainImg, BufferedImage smallImg, char ch) { int which = Arrays.binarySearch(TRAIN_CHARS, ch); int x = -1; int y = -1; for (int i = 0; i < TRAIN_NUM; ++i) { if (trainImg.getRGB(i * UNIT_W, which * (UNIT_H + 1) + UNIT_H) != TARGET_COLOR) { x = i * UNIT_W; y = which * (UNIT_H + 1); break; } } if (x == -1 || y == -1) { return false; } for (int i = 0; i < UNIT_W; ++i) { for (int j = 0; j < UNIT_H; ++j) { trainImg.setRGB(x + i, y + j, smallImg.getRGB(i, j)); } } trainImg.setRGB(x, y + UNIT_H, TARGET_COLOR); return true; }
Example 10
Source File: Skin.java From Nemisys with GNU General Public License v3.0 | 5 votes |
private static byte[] parseBufferedImage(BufferedImage image) { FastByteArrayOutputStream outputStream = new FastByteArrayOutputStream(); for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { Color color = new Color(image.getRGB(x, y), true); outputStream.write(color.getRed()); outputStream.write(color.getGreen()); outputStream.write(color.getBlue()); outputStream.write(color.getAlpha()); } } image.flush(); return outputStream.toByteArray(); }
Example 11
Source File: OpaqueImageToSurfaceBlitTest.java From hottub with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); VolatileImage vi = gc.createCompatibleVolatileImage(16, 16); vi.validate(gc); BufferedImage bi = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB); int data[] = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData(); data[0] = 0x0000007f; data[1] = 0x0000007f; data[2] = 0xff00007f; data[3] = 0xff00007f; Graphics2D g = vi.createGraphics(); g.setComposite(AlphaComposite.SrcOver.derive(0.999f)); g.drawImage(bi, 0, 0, null); bi = vi.getSnapshot(); if (bi.getRGB(0, 0) != bi.getRGB(1, 1)) { throw new RuntimeException("Test FAILED: color at 0x0 ="+ Integer.toHexString(bi.getRGB(0, 0))+" differs from 1x1 ="+ Integer.toHexString(bi.getRGB(1,1))); } System.out.println("Test PASSED."); }
Example 12
Source File: StrokeShapeTest.java From jdk8u-jdk with GNU General Public License v2.0 | 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.translate(25, 100); Stroke stroke = new BasicStroke(200, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); Shape shape = new Polygon(new int[] {0, 1500, 0}, new int[] {750, 0, -750}, 3); g.scale(.1, .1); g.setPaint(Color.BLACK); g.setStroke(stroke); g.draw(shape); g.setPaint(Color.RED); g.fill(stroke.createStrokedShape(shape)); // To visually check it //ImageIO.write(image, "PNG", new File(args[0])); boolean blackPixelFound = false; outer: for (int x = 0; x < 200; ++x) { for (int y = 0; y < 200; ++y) { if (image.getRGB(x, y) == Color.BLACK.getRGB()) { blackPixelFound = true; break outer; } } } if (blackPixelFound) { throw new RuntimeException("The shape hasn't been filled in red."); } }
Example 13
Source File: DiacriticsDrawingTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
private static boolean imagesAreEqual(BufferedImage i1, BufferedImage i2) { if (i1.getWidth() != i2.getWidth() || i1.getHeight() != i2.getHeight()) return false; for (int i = 0; i < i1.getWidth(); i++) { for (int j = 0; j < i1.getHeight(); j++) { if (i1.getRGB(i, j) != i2.getRGB(i, j)) { return false; } } } return true; }
Example 14
Source File: ImageBlend.java From MyBox with Apache License 2.0 | 5 votes |
public static BufferedImage blendImagesFinB(BufferedImage foreImage, BufferedImage backImage, int x, int y, ImagesBlendMode blendMode, float alpha) { try { if (foreImage == null || backImage == null || blendMode == null) { return null; } int imageType = BufferedImage.TYPE_INT_ARGB; BufferedImage target = new BufferedImage(backImage.getWidth(), backImage.getHeight(), imageType); for (int j = 0; j < backImage.getHeight(); ++j) { for (int i = 0; i < backImage.getWidth(); ++i) { target.setRGB(i, j, backImage.getRGB(i, j)); } } int areaWidth = Math.min(backImage.getWidth() - x, foreImage.getWidth()); int areaHeight = Math.min(backImage.getHeight() - y, foreImage.getHeight()); PixelBlend colorBlend = PixelBlend.newColorBlend(blendMode, alpha); for (int j = 0; j < areaHeight; ++j) { for (int i = 0; i < areaWidth; ++i) { int pixelFore = foreImage.getRGB(i, j); int pixelBack = backImage.getRGB(i + x, j + y); target.setRGB(i + x, j + y, colorBlend.blend(pixelFore, pixelBack)); } } return target; } catch (Exception e) { logger.error(e.toString()); return null; } }
Example 15
Source File: DrawImageCoordsTest.java From hottub with GNU General Public License v2.0 | 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 16
Source File: TIFFDirectoryWriteReadTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
private void readAndCheckImage() throws Exception { ImageReader reader = getTIFFReader(); ImageInputStream s = ImageIO.createImageInputStream(new File(FILENAME)); reader.setInput(s); int ni = reader.getNumImages(true); check(ni == 1, "invalid number of images"); // check image BufferedImage img = reader.read(0); check(img.getWidth() == SZ && img.getHeight() == SZ, "invalid image size"); Color c = new Color(img.getRGB(SZ / 2, SZ / 2)); check(C.equals(c), "invalid image color"); IIOMetadata metadata = reader.readAll(0, null).getMetadata(); TIFFDirectory dir = TIFFDirectory.createFromMetadata(metadata); reader.dispose(); s.close(); // ===== perform tag checks ===== checkASCIIField(dir, "copyright", COPYRIGHT, BaselineTIFFTagSet.TAG_COPYRIGHT); checkASCIIField(dir, "description", DESCRIPTION, BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION); checkASCIIField(dir, "software", SOFTWARE, BaselineTIFFTagSet.TAG_SOFTWARE); TIFFField f = dir.getTIFFField(BaselineTIFFTagSet.TAG_IMAGE_WIDTH); check(f.getCount() == 1, "invalid width field count"); int w = f.getAsInt(0); check(w == SZ, "invalid width"); f = dir.getTIFFField(BaselineTIFFTagSet.TAG_IMAGE_LENGTH); check(f.getCount() == 1, "invalid height field count"); int h = f.getAsInt(0); check(h == SZ, "invalid height"); f = dir.getTIFFField(BaselineTIFFTagSet.TAG_BITS_PER_SAMPLE); // RGB: 3 x 8 bits for R, G and B components int bps[] = f.getAsInts(); check((f.getCount() == 3) && (bps.length == 3), "invalid BPS count"); for (int b: bps) { check(b == 8, "invalid bits per sample"); } // RGB: PhotometricInterpretation = 2 f = dir.getTIFFField(BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION); check(f.getCount() == 1, "invalid count"); check(f.getAsInt(0) == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_RGB, "invalid photometric interpretation value"); String rat = " resolution must be rational"; f = dir.getTIFFField(BaselineTIFFTagSet.TAG_X_RESOLUTION); check(f.getType() == TIFFTag.TIFF_RATIONAL, "x" + rat); check(f.getCount() == 1 && f.getAsInt(0) == (int) (RES_X[0][0] / RES_X[0][1]), "invalid x resolution"); f = dir.getTIFFField(BaselineTIFFTagSet.TAG_Y_RESOLUTION); check(f.getType() == TIFFTag.TIFF_RATIONAL, "y" + rat); check(f.getCount() == 1 && f.getAsInt(0) == (int) (RES_Y[0][0] / RES_Y[0][1]), "invalid y resolution"); f = dir.getTIFFField(BaselineTIFFTagSet.TAG_ICC_PROFILE); check(f.getType() == TIFFTag.TIFF_UNDEFINED, "invalid ICC profile field type"); int cnt = f.getCount(); byte icc[] = f.getAsBytes(); check((cnt == ICC_PROFILE.length) && (cnt == icc.length), "invalid ICC profile"); for (int i = 0; i < cnt; i++) { check(icc[i] == ICC_PROFILE[i], "invalid ICC profile"); } }
Example 17
Source File: Utils.java From netbeans-mmd-plugin with Apache License 2.0 | 4 votes |
@Nonnull public static Image renderWithTransparency(final float opacity, @Nonnull final AbstractElement element, @Nonnull final MindMapPanelConfig config, @Nonnull final RenderQuality quality) { final AbstractElement cloned = element.makeCopy(); final Rectangle2D bounds = cloned.getBounds(); final float increase = config.safeScaleFloatValue(config.getElementBorderWidth() + config.getShadowOffset(), 0.0f); final int imageWidth = (int) Math.round(bounds.getWidth() + increase); final int imageHeight = (int) Math.round(bounds.getHeight() + increase); bounds.setRect(0.0d, 0.0d, bounds.getWidth(), bounds.getHeight()); final BufferedImage result = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB); for (int y = 0; y < imageHeight; y++) { for (int x = 0; x < imageWidth; x++) { result.setRGB(x, y, 0); } } final Graphics2D g = result.createGraphics(); final MMGraphics gfx = new MMGraphics2DWrapper(g); try { quality.prepare(g); cloned.doPaint(gfx, config, false); } finally { gfx.dispose(); } int alpha; if (opacity <= 0.0f) { alpha = 0x00; } else if (opacity >= 1.0f) { alpha = 0xFF; } else { alpha = Math.round(0xFF * opacity); } alpha <<= 24; for (int y = 0; y < imageHeight; y++) { for (int x = 0; x < imageWidth; x++) { final int curAlpha = result.getRGB(x, y) >>> 24; if (curAlpha == 0xFF) { result.setRGB(x, y, (result.getRGB(x, y) & 0xFFFFFF) | alpha); } else if (curAlpha != 0x00) { final int calculated = Math.round(curAlpha * opacity) << 24; result.setRGB(x, y, (result.getRGB(x, y) & 0xFFFFFF) | calculated); } } } return result; }
Example 18
Source File: Variables.java From The-5zig-Mod with MIT License | 4 votes |
@Override public void fillDynamicImage(Object dynamicImage, BufferedImage image) { image.getRGB(0, 0, image.getWidth(), image.getHeight(), ((blz) dynamicImage).e(), 0, image.getWidth()); ((blz) dynamicImage).d(); }
Example 19
Source File: WebDriverScreenDiffer.java From nomulus with Apache License 2.0 | 4 votes |
private ComparisonResult compareScreenshots(ActualScreenshot screenshot) { int totalPixels = screenshot.getWidth() * screenshot.getHeight(); Optional<BufferedImage> maybeGoldenImage = loadGoldenImageByName(screenshot.getImageName()); ComparisonResult.Builder commonBuilder = ComparisonResult.builder() .setActualScreenshot(screenshot) .setIsConsideredSimilar(false) .setIsMissingGoldenImage(false) .setIsSizeDifferent(false) .setNumDiffPixels(totalPixels); if (!maybeGoldenImage.isPresent()) { return commonBuilder.setIsMissingGoldenImage(true).build(); } BufferedImage goldenImage = maybeGoldenImage.get(); if ((screenshot.getWidth() != goldenImage.getWidth()) || (screenshot.getHeight() != goldenImage.getHeight())) { return commonBuilder.setIsSizeDifferent(true).build(); } int currPixelDiff = 0; for (int x = 0; x < screenshot.getWidth(); x++) { for (int y = 0; y < screenshot.getHeight(); y++) { Color screenshotColor = new Color(screenshot.getRGB(x, y)); Color goldenImageColor = new Color(goldenImage.getRGB(x, y)); int currColorDiff = IntStream.of( abs(screenshotColor.getRed() - goldenImageColor.getRed()), abs(screenshotColor.getGreen() - goldenImageColor.getGreen()), abs(screenshotColor.getBlue() - goldenImageColor.getBlue())) .max() .getAsInt(); if (currColorDiff > maxColorDiff) { currPixelDiff++; } } } commonBuilder.setNumDiffPixels(currPixelDiff); if (currPixelDiff <= maxPixelDiff) { commonBuilder.setIsConsideredSimilar(true); } return commonBuilder.build(); }
Example 20
Source File: RSLContextInvalidationTest.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); VolatileImage vi = gc.createCompatibleVolatileImage(100, 100); vi.validate(gc); VolatileImage vi1 = gc.createCompatibleVolatileImage(100, 100); vi1.validate(gc); if (!(vi instanceof DestSurfaceProvider)) { System.out.println("Test considered PASSED: no HW acceleration"); return; } DestSurfaceProvider p = (DestSurfaceProvider)vi; Surface s = p.getDestSurface(); if (!(s instanceof AccelSurface)) { System.out.println("Test considered PASSED: no HW acceleration"); return; } AccelSurface dst = (AccelSurface)s; Graphics g = vi.createGraphics(); g.drawImage(vi1, 95, 95, null); g.setColor(Color.red); g.fillRect(0, 0, 100, 100); g.setColor(Color.black); g.fillRect(0, 0, 100, 100); // after this the validated context color is black RenderQueue rq = dst.getContext().getRenderQueue(); rq.lock(); try { dst.getContext().saveState(); dst.getContext().restoreState(); } finally { rq.unlock(); } // this will cause ResetPaint (it will set color to extended EA=ff, // which is ffffffff==Color.white) g.drawImage(vi1, 95, 95, null); // now try filling with black again, but it will come up as white // because this fill rect won't validate the color properly g.setColor(Color.black); g.fillRect(0, 0, 100, 100); BufferedImage bi = vi.getSnapshot(); if (bi.getRGB(50, 50) != Color.black.getRGB()) { throw new RuntimeException("Test FAILED: found color="+ Integer.toHexString(bi.getRGB(50, 50))+" instead of "+ Integer.toHexString(Color.black.getRGB())); } System.out.println("Test PASSED."); }