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:org.sejda.sambox.pdmodel.graphics.image.JPEGFactoryTest.java

@Test
public void testCreateFromImage4BYTE_ABGR() throws IOException {
    // workaround Open JDK bug
    // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7044758
    if (System.getProperty("java.runtime.name").equals("OpenJDK Runtime Environment")
            && (System.getProperty("java.specification.version").equals("1.6")
                    || System.getProperty("java.specification.version").equals("1.7")
                    || System.getProperty("java.specification.version").equals("1.8"))) {
        return;//from  www  .  j  a v a  2 s  .  c  o  m
    }

    PDDocument document = new PDDocument();
    BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));

    // create an ARGB image
    int width = image.getWidth();
    int height = image.getHeight();
    BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics ag = argbImage.getGraphics();
    ag.drawImage(image, 0, 0, null);
    ag.dispose();

    for (int x = 0; x < argbImage.getWidth(); ++x) {
        for (int y = 0; y < argbImage.getHeight(); ++y) {
            argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24));
        }
    }

    PDImageXObject ximage = JPEGFactory.createFromImage(argbImage);
    validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName());
    assertNotNull(ximage.getSoftMask());
    validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName());
    assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10);

    doWritePDF(document, ximage, testResultsDir, "jpeg-4bargb.pdf");
}

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

public boolean isContrastPoint(int x, int y, BufferedImage sharpOCT) {
    return Util.calculateGrayScaleValue(sharpOCT.getRGB(x, y)) == 0
            && Util.calculateGrayScaleValue(sharpOCT.getRGB(x, y + 1)) > 0;
}

From source file:org.geoserver.wms.WMSTestSupport.java

/**
 * Counts the number of non black pixels
 * /*from ww w  . j a  v  a2 s.  co  m*/
 * @param testName
 * @param image
 * @param bgColor
 * @return
 */
protected int countNonBlankPixels(String testName, BufferedImage image, Color bgColor) {
    int pixelsDiffer = 0;

    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            if (image.getRGB(x, y) != bgColor.getRGB()) {
                ++pixelsDiffer;
            }
        }
    }

    LOGGER.fine(testName + ": pixel count=" + (image.getWidth() * image.getHeight()) + " non bg pixels: "
            + pixelsDiffer);
    return pixelsDiffer;
}

From source file:de.ep3.ftpc.view.designer.UIDesigner.java

/**
 * Loads an icon image from the resources directory provided.
 *
 * The icon will be recolored according to the UI default color
 * (with respect to its alpha values).//  w ww .  j  a  v  a2  s .c o m
 *
 * @param dirName The directory name under the resource/drawable directory.
 * @param fileName The file name under the directory name provided before.
 * @return The icom ready to be displayed within the GUI.
 */
public Icon getDefaultIcon(String dirName, String fileName) {
    BufferedImage image = getDefaultImage(dirName, fileName);

    /* Change icon color according to UI color */

    int imageHeight = image.getHeight();
    int imageWidth = image.getWidth();

    int defaultRGBA = getDefaultBorderColor().getRGB();
    int defaultRGB = defaultRGBA & 0x00FFFFFF;

    for (int y = 0; y < imageHeight; y++) {
        for (int x = 0; x < imageWidth; x++) {
            int imageRGBA = image.getRGB(x, y);
            int imageA = imageRGBA & 0xFF000000;

            int newRGBA = defaultRGB | imageA; // I'm quite proud of this little bitwise color calculation ^-^

            image.setRGB(x, y, newRGBA);
        }
    }

    return new ImageIcon(image);
}

From source file:com.xuggle.xuggler.UtilsTest.java

@SuppressWarnings("deprecation")
@Test//  www . ja  v a  2  s.  c om
public void testImageToImageSolidColor() {
    int w = 50;
    int h = 50;
    int gray = Color.GRAY.getRGB();

    // construct an all gray image

    BufferedImage image1 = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
    for (int x = 0; x < w; ++x)
        for (int y = 0; y < h; ++y)
            image1.setRGB(x, y, gray);

    // convert image1 to a picture and then back to image2

    BufferedImage image2 = Utils.videoPictureToImage(Utils.imageToVideoPicture(image1, 0));

    // test that all the pixels in image2 are gray, but not black or
    // white

    for (int x = 0; x < w; ++x)
        for (int y = 0; y < h; ++y) {
            int pixel = image2.getRGB(x, y);
            assertTrue("color value missmatch", pixel == gray);
        }
}

From source file:com.xuggle.xuggler.UtilsTest.java

@SuppressWarnings("deprecation")
@Test// w  w  w .java 2 s . co m
public void testPictureToPictureWithRotate() {
    // note that the image is square in this test to make rotation
    // easier to handle

    int size = 50;
    int black = Color.BLACK.getRGB();
    int white = Color.WHITE.getRGB();

    // construct an image with black and white stripped columns

    BufferedImage image1 = new BufferedImage(size, size, BufferedImage.TYPE_3BYTE_BGR);
    for (int x = 0; x < size; ++x)
        for (int y = 0; y < size; ++y) {
            int color = x % 2 == 0 ? black : white;
            image1.setRGB(x, y, color);
        }

    // convert image1 to a picture and then back to image2

    BufferedImage image2 = Utils.videoPictureToImage(Utils.imageToVideoPicture(image1, 0));

    // rotae image2

    AffineTransform t = AffineTransform.getRotateInstance(Math.PI / 2, image2.getWidth() / 2,
            image2.getHeight() / 2);
    AffineTransformOp ato = new AffineTransformOp(t, AffineTransformOp.TYPE_BICUBIC);
    BufferedImage image3 = new BufferedImage(size, size, BufferedImage.TYPE_3BYTE_BGR);
    ato.filter(image2, image3);

    // convert image2 to a picture and then back to image3

    BufferedImage image4 = Utils.videoPictureToImage(Utils.imageToVideoPicture(image3, 0));

    // test that image4 now contains stripped rows (not columns)

    for (int x = 0; x < size; ++x)
        for (int y = 0; y < size; ++y) {
            int pixel = image4.getRGB(x, y);
            int color = y % 2 == 0 ? black : white;
            assertTrue("color value missmatch", pixel == color);
        }
}

From source file:com.seleniumtests.it.driver.TestBrowserSnapshot.java

/**
 * Test page contains fixed header (yellow) and footer (orange) of 5 pixels height. Detect how many
 * pixels of these colors are present in picture
 * Also count red pixels which is a line not to remove when cropping
 * @param picture//from w ww.j  av  a 2  s  .  c o m
 * @return
 * @throws IOException 
 */
private int[] getHeaderAndFooterPixels(File picture) throws IOException {
    BufferedImage image = ImageIO.read(picture);

    int topPixels = 0;
    int bottomPixels = 0;
    int securityLine = 0;
    for (int height = 0; height < image.getHeight(); height++) {
        Color color = new Color(image.getRGB(0, height));
        if (color.equals(Color.YELLOW)) {
            topPixels++;
        } else if (color.equals(Color.ORANGE)) {
            bottomPixels++;
        } else if (color.equals(Color.RED) || color.equals(Color.GREEN)) {
            securityLine++;
        }
    }

    return new int[] { topPixels, bottomPixels, securityLine };
}

From source file:blusunrize.immersiveengineering.client.render.TileRenderAutoWorkbench.java

public static BlueprintLines getBlueprintDrawable(ItemStack stack, World world) {
    if (stack.isEmpty())
        return null;
    EntityPlayer player = ClientUtils.mc().player;
    ArrayList<BufferedImage> images = new ArrayList<>();
    try {/*from w  ww.j av a 2s  .  co  m*/
        IBakedModel ibakedmodel = ClientUtils.mc().getRenderItem().getItemModelWithOverrides(stack, world,
                player);
        HashSet<String> textures = new HashSet();
        Collection<BakedQuad> quads = ibakedmodel.getQuads(null, null, 0);
        for (BakedQuad quad : quads)
            if (quad != null && quad.getSprite() != null)
                textures.add(quad.getSprite().getIconName());
        for (String s : textures) {
            ResourceLocation rl = new ResourceLocation(s);
            rl = new ResourceLocation(rl.getNamespace(),
                    String.format("%s/%s%s", "textures", rl.getPath(), ".png"));
            IResource resource = ClientUtils.mc().getResourceManager().getResource(rl);
            BufferedImage bufferedImage = TextureUtil.readBufferedImage(resource.getInputStream());
            if (bufferedImage != null)
                images.add(bufferedImage);
        }
    } catch (Exception e) {
    }
    if (images.isEmpty())
        return null;
    ArrayList<Pair<TexturePoint, TexturePoint>> lines = new ArrayList();
    HashSet testSet = new HashSet();
    HashMultimap<Integer, TexturePoint> area = HashMultimap.create();
    int wMax = 0;
    for (BufferedImage bufferedImage : images) {
        Set<Pair<TexturePoint, TexturePoint>> temp_lines = new HashSet<>();

        int w = bufferedImage.getWidth();
        int h = bufferedImage.getHeight();

        if (h > w)
            h = w;
        if (w > wMax)
            wMax = w;
        for (int hh = 0; hh < h; hh++)
            for (int ww = 0; ww < w; ww++) {
                int argb = bufferedImage.getRGB(ww, hh);
                float r = (argb >> 16 & 255) / 255f;
                float g = (argb >> 8 & 255) / 255f;
                float b = (argb & 255) / 255f;
                float intesity = (r + b + g) / 3f;
                int alpha = (argb >> 24) & 255;
                if (alpha > 0) {
                    boolean added = false;
                    //Check colour sets for similar colour to shade it later
                    TexturePoint tp = new TexturePoint(ww, hh, w);
                    if (!testSet.contains(tp)) {
                        for (Integer key : area.keySet()) {
                            for (TexturePoint p : area.get(key)) {
                                float mod = w / (float) p.scale;
                                int pColour = bufferedImage.getRGB((int) (p.x * mod), (int) (p.y * mod));
                                float dR = (r - (pColour >> 16 & 255) / 255f);
                                float dG = (g - (pColour >> 8 & 255) / 255f);
                                float dB = (b - (pColour & 255) / 255f);
                                double delta = Math.sqrt(dR * dR + dG * dG + dB * dB);
                                if (delta < .25) {
                                    area.put(key, tp);
                                    added = true;
                                    break;
                                }
                            }
                            if (added)
                                break;
                        }
                        if (!added)
                            area.put(argb, tp);
                        testSet.add(tp);
                    }
                    //Compare to direct neighbour
                    for (int i = 0; i < 4; i++) {
                        int xx = (i == 0 ? -1 : i == 1 ? 1 : 0);
                        int yy = (i == 2 ? -1 : i == 3 ? 1 : 0);
                        int u = ww + xx;
                        int v = hh + yy;

                        int neighbour = 0;
                        float delta = 1;
                        boolean notTransparent = false;
                        if (u >= 0 && u < w && v >= 0 && v < h) {
                            neighbour = bufferedImage.getRGB(u, v);
                            notTransparent = ((neighbour >> 24) & 255) > 0;
                            if (notTransparent) {
                                float neighbourIntesity = ((neighbour >> 16 & 255) + (neighbour >> 8 & 255)
                                        + (neighbour & 255)) / 765f;
                                float intesityDelta = Math.max(0,
                                        Math.min(1, Math.abs(intesity - neighbourIntesity)));
                                float rDelta = Math.max(0,
                                        Math.min(1, Math.abs(r - (neighbour >> 16 & 255) / 255f)));
                                float gDelta = Math.max(0,
                                        Math.min(1, Math.abs(g - (neighbour >> 8 & 255) / 255f)));
                                float bDelta = Math.max(0, Math.min(1, Math.abs(b - (neighbour & 255) / 255f)));
                                delta = Math.max(intesityDelta, Math.max(rDelta, Math.max(gDelta, bDelta)));
                                delta = delta < .25 ? 0 : delta > .4 ? 1 : delta;
                            }
                        }
                        if (delta > 0) {
                            Pair<TexturePoint, TexturePoint> l = Pair.of(
                                    new TexturePoint(ww + (i == 0 ? 0 : i == 1 ? 1 : 0),
                                            hh + (i == 2 ? 0 : i == 3 ? 1 : 0), w),
                                    new TexturePoint(ww + (i == 0 ? 0 : i == 1 ? 1 : 1),
                                            hh + (i == 2 ? 0 : i == 3 ? 1 : 1), w));
                            temp_lines.add(l);
                        }
                    }
                }
            }
        lines.addAll(temp_lines);
    }

    ArrayList<Integer> lumiSort = new ArrayList<>(area.keySet());
    Collections.sort(lumiSort, (rgb1, rgb2) -> Double.compare(getLuminance(rgb1), getLuminance(rgb2)));
    HashMultimap<ShadeStyle, Point> complete_areaMap = HashMultimap.create();
    int lineNumber = 2;
    int lineStyle = 0;
    for (Integer i : lumiSort) {
        complete_areaMap.putAll(new ShadeStyle(lineNumber, lineStyle), area.get(i));
        ++lineStyle;
        lineStyle %= 3;
        if (lineStyle == 0)
            lineNumber += 1;
    }

    Set<Pair<Point, Point>> complete_lines = new HashSet<>();
    for (Pair<TexturePoint, TexturePoint> line : lines) {
        TexturePoint p1 = line.getKey();
        TexturePoint p2 = line.getValue();
        complete_lines.add(Pair.of(
                new Point((int) (p1.x / (float) p1.scale * wMax), (int) (p1.y / (float) p1.scale * wMax)),
                new Point((int) (p2.x / (float) p2.scale * wMax), (int) (p2.y / (float) p2.scale * wMax))));
    }
    return new BlueprintLines(wMax, complete_lines, complete_areaMap);
}

From source file:org.iish.visualmets.services.ImageTransformation.java

/**
 * Returns a 270 degrees rotated image/* w w w.  j  av a  2  s.  c  om*/
 *
 * @param bi image
 * @return a rotated image
 */
private BufferedImage RotateImage270Degrees(BufferedImage bi) {
    int width = bi.getWidth();
    int height = bi.getHeight();

    BufferedImage biFlip = new BufferedImage(height, width, bi.getType());

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            biFlip.setRGB(j, width - 1 - i, bi.getRGB(i, j));
        }

    return biFlip;
}

From source file:org.iish.visualmets.services.ImageTransformation.java

/**
 * Returns a 90 degrees rotated image//from  w ww. j  a v  a  2s. c  o  m
 *
 * @param bi image
 * @return a rotated image
 */
private BufferedImage RotateImage90Degrees(BufferedImage bi) {
    int width = bi.getWidth();
    int height = bi.getHeight();

    BufferedImage biFlip = new BufferedImage(height, width, bi.getType());

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            biFlip.setRGB(height - 1 - j, i, bi.getRGB(i, j));
        }

    return biFlip;
}