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.cloudkit.relaxation.VerifyImage.java

public Point getFirstPoint(BufferedImage image) {
    int w = image.getWidth();
    int h = image.getHeight();

    for (int x = 0; x < w; ++x) {
        for (int y = 0; y < h; ++y) {
            int rgb = getRed(image.getRGB(x, y));
            if (rgb != 255) {
                return new Point(x, y);
            }//from www . j  a v  a2 s.  com
        }
    }

    return new Point(-1, -1);
}

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

public void getFont(BufferedImage image, int x, int y, BufferedImage draw, BufferedImage outDraw) {
    int w = image.getWidth();
    int h = image.getHeight();
    int c = getRed(image.getRGB(x, y));
    if (c != 255) {
        int c1 = x == 0 ? 255 : getRed(image.getRGB(x - 1, y));
        int c2 = x == w - 1 ? 255 : getRed(image.getRGB(x + 1, y));
        int c3 = y == 0 ? 255 : getRed(image.getRGB(x, y - 1));
        int c4 = y == h - 1 ? 255 : getRed(image.getRGB(x, y + 1));
        int c5 = x != 0 && y != 0 ? getRed(image.getRGB(x - 1, y - 1)) : 255;
        int c6 = x != w - 1 && y != 0 ? getRed(image.getRGB(x + 1, y - 1)) : 255;
        int c7 = x != 0 && y != h - 1 ? getRed(image.getRGB(x - 1, y + 1)) : 255;
        int c8 = x != w - 1 && y != h - 1 ? getRed(image.getRGB(x + 1, y + 1)) : 255;
        outDraw.setRGB(x, y, image.getRGB(x, y));
        draw.setRGB(x, y, Color.WHITE.getRGB());
        ++this.count;
        if (c1 != 255) {
            this.getFont(image, x - 1, y, draw, outDraw);
        }/*w w  w .  j  a va 2 s.co  m*/

        if (c2 != 255) {
            this.getFont(image, x + 1, y, draw, outDraw);
        }

        if (c3 != 255) {
            this.getFont(image, x, y - 1, draw, outDraw);
        }

        if (c4 != 255) {
            this.getFont(image, x, y + 1, draw, outDraw);
        }

        if (c5 != 255) {
            this.getFont(image, x - 1, y - 1, draw, outDraw);
        }

        if (c6 != 255) {
            this.getFont(image, x + 1, y - 1, draw, outDraw);
        }

        if (c7 != 255) {
            this.getFont(image, x - 1, y + 1, draw, outDraw);
        }

        if (c8 != 255) {
            this.getFont(image, x + 1, y + 1, draw, outDraw);
        }

    }
}

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

public static void fadeImages(BufferedImage source1, BufferedImage source2, BufferedImage target, int relX,
        int targetX) {
    int pixel1, pixel2, newPixel;
    double f;//from w ww  .  jav a 2s  . c o m
    int r1, g1, b1, r2, g2, b2;
    byte newR, newG, newB;
    ColorModel cm = source1.getColorModel();

    for (int x = relX; x < source1.getWidth(); x++) {
        f = linearF(x, relX, source1.getWidth());
        for (int y = 0; y < source1.getHeight(); y++) {
            pixel1 = source1.getRGB(x, y);
            pixel2 = source2.getRGB(x - relX, y);

            r1 = cm.getRed(pixel1);
            g1 = cm.getGreen(pixel1);
            b1 = cm.getBlue(pixel1);
            r2 = cm.getRed(pixel2);
            g2 = cm.getGreen(pixel2);
            b2 = cm.getBlue(pixel2);

            int tr = 10;

            if (r1 < tr && g1 < tr && b1 < tr) {
                newPixel = pixel2;
            } else if (r2 < tr && g2 < tr && b2 < tr) {
                newPixel = pixel1;
            } else {
                newR = (byte) Math.round(((double) r1) * (1 - f) + ((double) r2) * f);
                newG = (byte) Math.round(((double) g1) * (1 - f) + ((double) g2) * f);
                newB = (byte) Math.round(((double) b1) * (1 - f) + ((double) b2) * f);
                newPixel = (newR & 0xff) << 16 | (newG & 0xff) << 8 | (newB & 0xff) << 0;
            }
            target.setRGB(x + targetX, y, newPixel);
        }

    }
}

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

public static BufferedImage autoPanImage(BufferedImage img, Color bgcolor) {
    BufferedImage out = null;/*from  ww  w .j  av a2  s  .c o  m*/
    if (img == null) {
        return null;
    }

    int bgcolorInt = bgcolor.getRGB();
    int w = img.getWidth();
    int h = img.getHeight();
    int up = 0;
    int down = h - 1;
    int left = 0;
    int right = w - 1;
    int tmp;

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h / 2; y++) {
            tmp = img.getRGB(x, y);
            if (tmp == bgcolorInt) {
                if (y > up) {
                    up = y;
                }
            } else {
                break;
            }
        }

        for (int y = h - 1; y > h / 2; y--) {
            tmp = img.getRGB(x, y);
            if (tmp == bgcolorInt) {
                if (y < down) {
                    down = y;
                }
            } else {
                break;
            }
        }

    }

    for (int y = up; y < down; y++) {
        for (int x = 0; x < w / 2; x++) {
            tmp = img.getRGB(x, y);
            if (tmp == bgcolorInt) {
                if (x > left) {
                    left = x;
                }
            } else {
                break;
            }
        }

        for (int x = w - 1; x > w / 2; x--) {
            tmp = img.getRGB(x, y);
            if (tmp == bgcolorInt) {
                if (x < right) {
                    right = x;
                }
            } else {
                break;
            }
        }

    }

    System.out.println("up=" + up);
    System.out.println("down=" + down);
    System.out.println("left=" + left);
    System.out.println("right=" + right);

    out = img.getSubimage(left, up, right - left + 1, down - up + 1);
    return out;
}

From source file:org.stanwood.nwn2.gui.view.UIIconView.java

@Override
public void paintUIObject(Graphics g) {
    int x = getX();
    int y = getY();
    try {/*from   w  w w .  j  ava  2s  .c  o m*/
        BufferedImage img = getIconManager().getIcon(icon.getImg());
        int width = getWidth();
        int height = getHeight();

        if (img.getHeight() != height && img.getWidth() != width) {
            Image newImg = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics ig = img.getGraphics();
            ig.drawImage(newImg, 0, 0, null);
        }

        if (icon.getColor() != null) {
            Color colour = getColor(icon.getColor());
            for (int w = 0; w < img.getWidth(); w++) {
                for (int h = 0; h < img.getHeight(); h++) {
                    Color rgb = ColorUtil.blend(new Color(img.getRGB(w, h)), colour);
                    img.setRGB(w, h, rgb.getRGB());
                }
            }
        }

        g.drawImage(img, x, y, null);
    } catch (Exception e) {
        log.error(e.getMessage());
        drawMissingIcon(x, y, getWidth(), getHeight(), g);
    }
}

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

public BufferedImage autoCrop(BufferedImage image) {
    int iw = image.getWidth();
    int ih = image.getHeight();
    int x = iw;/*  w ww.  java  2 s.co  m*/
    int y = ih;
    int x2 = 0;
    int y2 = 0;

    for (int i = 0; i < iw; ++i) {
        for (int j = 0; j < ih; ++j) {
            int rgb = getRed(image.getRGB(i, j));
            if (rgb != 255) {
                x = Math.min(i, x);
                y = Math.min(j, y);
                x2 = Math.max(i, x2);
                y2 = Math.max(j, y2);
            }
        }
    }

    return image.getSubimage(x, y, x2 - x + 1, y2 - y + 1);
}

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

/**
 * Recursively search for a contour to the left of the supplied starting
 * point. If the contour returned contains the starting point then the
 * contour traced back to the start point rather than towards the edge of
 * the image.//from   w w  w .j  av a 2s.c o  m
 *
 * @param searchPoint point to search from
 * @param startPoint start search point
 * @param contourList list to add the contour points to
 * @param sharpOCT OCT to find the contour in
 * @return the next point in the contour after the search point
 */
public Point findContourLeft(Point searchPoint, Cardinality searchDirection, Point startPoint,
        Cardinality startDirection, LinkedList<Point> contourList, BufferedImage sharpOCT)
        throws InterruptedException {
    if (debug) {
        publish(searchPoint);
        Thread.sleep(debug_sleep);
    }
    Point nextPoint;
    Cardinality nextDirection;
    switch (searchDirection) {
    case SOUTH:
        if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x - 1, searchPoint.y)) > 0) {
            nextPoint = new Point(searchPoint);
            nextDirection = Cardinality.WEST;
        } else if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x - 1, searchPoint.y + 1)) == 0) {
            nextPoint = new Point(searchPoint.x - 1, searchPoint.y + 1);
            nextDirection = Cardinality.EAST;
        } else {
            nextPoint = new Point(searchPoint.x - 1, searchPoint.y);
            nextDirection = Cardinality.SOUTH;
        }
        break;
    case EAST:
        if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x, searchPoint.y + 1)) > 0) {
            nextPoint = new Point(searchPoint);
            nextDirection = Cardinality.SOUTH;
        } else if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x + 1, searchPoint.y + 1)) == 0) {
            nextPoint = new Point(searchPoint.x + 1, searchPoint.y + 1);
            nextDirection = Cardinality.NORTH;
        } else {
            nextPoint = new Point(searchPoint.x, searchPoint.y + 1);
            nextDirection = Cardinality.EAST;
        }
        break;
    case WEST:
        if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x, searchPoint.y - 1)) > 0) {
            nextPoint = new Point(searchPoint);
            nextDirection = Cardinality.NORTH;
        } else if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x - 1, searchPoint.y - 1)) == 0) {
            nextPoint = new Point(searchPoint.x - 1, searchPoint.y - 1);
            nextDirection = Cardinality.SOUTH;
        } else {
            nextPoint = new Point(searchPoint.x, searchPoint.y - 1);
            nextDirection = Cardinality.WEST;
        }
        break;
    case NORTH:
        if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x + 1, searchPoint.y)) > 0) {
            nextPoint = new Point(searchPoint);
            nextDirection = Cardinality.EAST;
        } else if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x + 1, searchPoint.y - 1)) == 0) {
            nextPoint = new Point(searchPoint.x + 1, searchPoint.y - 1);
            nextDirection = Cardinality.WEST;
        } else {
            nextPoint = new Point(searchPoint.x + 1, searchPoint.y);
            nextDirection = Cardinality.NORTH;
        }
        break;
    default:
        //will never happen, just placed in to make code compile
        nextPoint = new Point(searchPoint);
        nextDirection = Cardinality.EAST;
        break;
    }
    if (!((nextPoint.equals(startPoint) && nextDirection == startDirection) || nextPoint.y < 100
            || nextPoint.y > sharpOCT.getHeight() - 20 || nextPoint.x <= 20
            || nextPoint.x >= sharpOCT.getWidth() - 20)) {
        contourList.add(
                findContourLeft(nextPoint, nextDirection, startPoint, startDirection, contourList, sharpOCT));
    }
    return nextPoint;
}

From source file:pack1.test.java

@WebMethod(operationName = "convert")
public String convert(@WebParam(name = "encodedImageStr") String encodedImageStr,
        @WebParam(name = "fileName") String fileName, @WebParam(name = "AOR") String AOR,
        @WebParam(name = "val") int value) {
    System.out.println("Value" + value);
    FileOutputStream imageOutFile = null;
    try {/*from   w w w  .j  a v  a 2s  . c om*/
        Connection con, con1;
        Statement stmtnew = null;
        area = Double.parseDouble(AOR);
        //int val=Integer.parseInt(value);
        System.out.println("connecting");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        System.out.println("connected");
        con = DriverManager.getConnection("jdbc:odbc:test");
        System.out.println(" driver loaded in connection.jsp");
        stmtnew = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        // Decode String using Base64 Class
        byte[] imageByteArray = encodedImageStr.getBytes();
        // Write Image into File system - Make sure you update the path
        imageOutFile = new FileOutputStream(
                "C:/Users/Kushagr Jolly/Documents/NetBeansProjects/webservice/IASRI/cropped" + fileName);
        imageOutFile.write(imageByteArray);
        imageOutFile.close();
        System.out.println("Image Successfully Stored");
        FileInputStream leafPicPath = new FileInputStream(
                "C:/Users/Kushagr Jolly/Documents/NetBeansProjects/webservice/IASRI/cropped" + fileName);
        BufferedImage cat;
        int height, width;
        cat = ImageIO.read(leafPicPath);
        height = cat.getHeight();
        width = cat.getWidth();
        for (int w = 0; w < cat.getWidth(); w++) {

            for (int h = 0; h < cat.getHeight(); h++) {
                // BufferedImage.getRGB() saves the colour of the pixel as a single integer.
                // use Color(int) to grab the RGB values individually.

                Color color = new Color(cat.getRGB(w, h));

                // use the RGB values to get their average.

                int averageColor = ((color.getRed() + color.getGreen() + color.getBlue()) / 3);
                // create a new Color object using the average colour as the red, green and blue

                // colour values

                Color avg = new Color(averageColor, averageColor, averageColor);

                // set the pixel at that position to the new Color object using Color.getRGB().            

                cat.setRGB(w, h, avg.getRGB());

            }

        }
        String greyPicPath = "C:/Users/Kushagr Jolly/Documents/NetBeansProjects/webservice/IASRI/greyscale"
                + fileName;
        greyPicPath = greyPicPath.trim();
        File outputfile = new File(greyPicPath);
        ImageIO.write(cat, "jpg", outputfile);
        System.out.println("Image is successfully converted to grayscale");

        String binPicPath = "C:/Users/Kushagr Jolly/Documents/NetBeansProjects/webservice/IASRI/binary"
                + fileName;
        File f2 = new File(binPicPath);
        System.out.println("1");
        ImageProcessor ip;
        ImagePlus imp = new ImagePlus(greyPicPath);
        System.out.println("2");
        ip = imp.getProcessor();
        ip.invertLut();
        ip.autoThreshold();
        System.out.println("3");
        BufferedImage bf = ip.getBufferedImage();
        System.out.println("4");
        ImageIO.write(bf, "jpg", f2);
        System.out.println("Image is successfully converted to binary image");
        if (choice == 1) {
            String Inserted1 = "insert into test (PhyAOR) values ('" + area + "')";
            System.out.println("InsertedQuery" + Inserted1);
            stmtnew.executeUpdate(Inserted1);
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            String ID = "select MAX(id) as id from test";
            System.out.println("Query" + ID);
            rs = stmt.executeQuery(ID);
            while (rs.next()) {
                id = rs.getInt("id");
            }
            String Inserted2 = "update test set fileName0=? where id=?";
            PreparedStatement ps = con.prepareStatement(Inserted2);
            ps.setString(1, fileName);
            ps.setDouble(2, id);
            int rt = ps.executeUpdate();
            choice++;
        }
        System.out.println(choice);
        if (value == 1) {

            int count = countblackpixel(binPicPath);
            calculatepixA(count, area);
            //returnVal=value+"/"+count+"/"+OnepixArea;

        } else if (value == 2) {

            int flag = countblackpixel(binPicPath);
            // calculatepixA(flag, area);
            leafarea0(flag, area);
            //returnVal=value+"/"+flag+"/"+OnepixArea+"/"+leafArea0;
            //System.out.println(returnVal);
        } else if (value == 3) {
            String Inserted3 = "update test set fileName180=? where id=?";
            PreparedStatement ps1 = con.prepareStatement(Inserted3);
            ps1.setString(1, fileName);
            ps1.setDouble(2, id);
            int rt = ps1.executeUpdate();
            int black = countblackpixel(binPicPath);
            leafarea180(area, black);
            finalarea();
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            String finalans = "select PhyAOR,onePixA,leafarea0,leafarea180,finalleafarea from test where id="
                    + id + "";
            rs = stmt.executeQuery(finalans);
            while (rs.next()) {
                returnVal = rs.getString("PhyAOR") + "/" + rs.getString("onePixA") + "/"
                        + rs.getString("leafarea0") + "/" + rs.getString("leafarea180") + "/"
                        + rs.getString("finalleafarea");
            }
            //returnVal=value+"/"+black+"/"+OnepixArea+"/"+leafArea180+"/"+Leaf_Area;
        } else if (value == 4) {
            finalarea();

        }
        imageOutFile.close();
    } catch (Exception ex) {
        Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println(ex);

    }
    return returnVal;

}

From source file:org.broad.igv.batch.CommandExecutorTest.java

@Test
public void testSnapshotsize() throws Exception {
    String filePath = TestUtils.DATA_DIR + "bam/NA12878.SLX.sample.bam";
    int numLoads = 1;

    for (int ii = 0; ii < numLoads; ii++) {
        IGV.getInstance().loadResources(Arrays.asList(new ResourceLocator(filePath)));
    }/*w w  w. ja  va2 s  . c o  m*/
    exec.execute("goto chr1:9,713,386-9,733,865");

    int minHeight = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 150;
    int maxHeight = minHeight + 200;

    String outFileName = minHeight + ".png";

    exec.execute("maxpanelheight " + maxHeight);
    exec.execute("snapshot " + outFileName);

    File outputFile = new File(snapshotDir, outFileName);
    BufferedImage image = ImageIO.read(outputFile);

    assertTrue("Output image height " + image.getHeight() + " is not at least " + minHeight,
            image.getHeight() > minHeight);

    int remAlphaMask = 0x00ffffff;

    int numBlackPix = 0;
    for (int yy = image.getMinY(); yy < image.getHeight(); yy++) {
        for (int xx = image.getMinX(); xx < image.getWidth(); xx++) {
            int color = image.getRGB(xx, yy) & remAlphaMask;
            numBlackPix += color == 0 ? 1 : 0;
        }
    }

    //Just making sure we don't trivially satisfy the problem
    assertTrue(numBlackPix > 100);

    int totalPix = image.getHeight() * image.getWidth();
    assertTrue("Too much of the snapshot is black", numBlackPix < totalPix * 0.1);
}

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

@SuppressWarnings("deprecation")
@Test//from  w  w  w. ja  v a  2  s  .  co m
public void testImageToImageRandomColor() {
    int w = 50;
    int h = 50;
    Random rnd = new Random();

    // construct an image of random colors

    BufferedImage image1 = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
    for (int x = 0; x < w; ++x)
        for (int y = 0; y < h; ++y) {
            Color c = new Color(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255));
            image1.setRGB(x, y, c.getRGB());
        }

    // 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 the same as image1

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