Example usage for java.awt.image BufferedImage setRGB

List of usage examples for java.awt.image BufferedImage setRGB

Introduction

In this page you can find the example usage for java.awt.image BufferedImage setRGB.

Prototype

public void setRGB(int x, int y, int rgb) 

Source Link

Document

Sets a pixel in this BufferedImage to the specified RGB value.

Usage

From source file:main.java.whiteSocket.Bloop.java

public static BufferedImage printImgBool(boolean[][] some, String path) throws Exception {
    //      BufferedImage testOut = createWhiteImage();
    BufferedImage testOut = Bloop.loadSketch();
    for (int row = 0; row < Bloop.sketch.getHeight(); row++) {
        for (int col = 0; col < Bloop.sketch.getWidth(); col++) {
            if (some[row][col]) {
                testOut.setRGB(col, row, 0);
            }//from w  ww .j a v  a  2 s .co  m
        }
    }
    saveImg(testOut, path);
    return testOut;
}

From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Write an image showing the heatmap of ssim values, per window
 * @param pValues sequence of SSIM values
 * @param pHeight number of SSIM windows across
 * @param pWidth number of SSIM windows down
 * @param pFilename filename to save the image to (png)
 *//*  w  w  w. j  a v  a2 s  .  c om*/
private static BufferedImage dumpSSIMHeatMap(final double[] pValues, final int pHeight, final int pWidth,
        final String pFilename) {
    BufferedImage heatMap = new BufferedImage(pWidth, pHeight, BufferedImage.TYPE_USHORT_GRAY);

    final int maxPixelValue = (int) Math.pow(2, heatMap.getColorModel().getPixelSize()) - 1;

    int pixel = 0;
    for (int height = 0; height < pHeight; height++) {
        for (int width = 0; width < pWidth; width++) {
            pixel = ((int) (maxPixelValue * pValues[(height * pWidth) + width]));
            if (pixel < 0) {
                pixel = 0;
            }
            heatMap.setRGB(width, height, pixel);
        }
    }

    //only write to file if filename is non-null
    if (pFilename != null) {
        try {
            ImageIO.write(heatMap, "png", new File(pFilename));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    return heatMap;
}

From source file:cn.z.Ocr5.java

public static BufferedImage removeBackgroud(File picFile) throws Exception {

    BufferedImage img = ImageIO.read(picFile);
    final int width = img.getWidth();
    final int height = img.getHeight();

    // int blackThreshold = 300;

    // img.getMinX() img.getMinY()
    for (int x = img.getMinX(); x < width; x++) {
        for (int y = img.getMinY(); y < height; y++) {

            Color color = new Color(img.getRGB(x, y));
            if ((color.getBlue() < 120) || ((color.getRed() + color.getGreen() + color.getBlue()) < 50)) { //
                img.setRGB(x, y, Color.WHITE.getRGB());
            } else if ((color.getRed() + color.getGreen() + color.getBlue()) < 400) {
                img.setRGB(x, y, Color.BLACK.getRGB());
            }/*from   ww w .  j ava 2  s  . c o  m*/

            int nearly = 0;
            int vertical = 0;
            int horizontal = 0;

            if (x > 0) {
                Color leftColor = new Color(img.getRGB(x - 1, y));
                if ((leftColor.getRed() + leftColor.getGreen() + leftColor.getBlue()) < 400) {
                    nearly++;
                    horizontal++;
                }
            }
            if (x < width - 1) {
                Color rightColor = new Color(img.getRGB(x + 1, y));
                if ((rightColor.getRed() + rightColor.getGreen() + rightColor.getBlue()) < 400) {
                    nearly++;
                    horizontal++;
                }
            }
            if (y > 0) {
                Color topColor = new Color(img.getRGB(x, y - 1));
                if ((topColor.getRed() + topColor.getGreen() + topColor.getBlue()) < 400) {
                    nearly++;
                    vertical++;
                }
            }
            if (y < height - 1) {
                Color bottomColor = new Color(img.getRGB(x, y + 1));
                if ((bottomColor.getRed() + bottomColor.getGreen() + bottomColor.getBlue()) < 400) {
                    nearly++;
                    vertical++;
                }
            }

            if (x > 0 && y > 0) {
                Color leftTopColor = new Color(img.getRGB(x - 1, y - 1));
                if ((leftTopColor.getRed() + leftTopColor.getGreen() + leftTopColor.getBlue()) < 400) {
                    nearly++;
                }
            }
            if (x < width - 1 && y < height - 1) {
                Color rightBottomColor = new Color(img.getRGB(x + 1, y + 1));
                if ((rightBottomColor.getRed() + rightBottomColor.getGreen()
                        + rightBottomColor.getBlue()) < 400) {
                    nearly++;
                }
            }
            if (x < width - 1 && y > 0) {
                Color rightTopColor = new Color(img.getRGB(x + 1, y - 1));
                if ((rightTopColor.getRed() + rightTopColor.getGreen() + rightTopColor.getBlue()) < 400) {
                    nearly++;
                }
            }
            if (x > 0 && y < height - 1) {
                Color leftBottomColor = new Color(img.getRGB(x - 1, y + 1));
                if ((leftBottomColor.getRed() + leftBottomColor.getGreen() + leftBottomColor.getBlue()) < 400) {
                    nearly++;
                }
            }

            if (nearly < 2) {
                img.setRGB(x, y, Color.WHITE.getRGB());
            }
            /*
            if (horizontal < 1 && vertical > 0) {
            img.setRGB(x, y, Color.WHITE.getRGB());
            }
            if (horizontal > 0 && vertical < 1) {
            img.setRGB(x, y, Color.WHITE.getRGB());
            }
            */

            /*
            if (isWhite(img.getRGB(x, y), whiteThreshold) == 1) {
            img.setRGB(x, y, Color.WHITE.getRGB());
            } else {
            img.setRGB(x, y, Color.BLACK.getRGB());
            }
                    
                    
            if (getColorBright(img.getRGB(x, y)) < 100) {
            int count = isBlack(img.getRGB(x - 1, y), blackThreshold) + isBlack(img.getRGB(x + 1, y), blackThreshold) + isBlack(img.getRGB(x, y - 1), blackThreshold) + isBlack(img.getRGB(x, y + 1), blackThreshold) + isBlack(img.getRGB(x + 1, y + 1), blackThreshold) + isBlack(img.getRGB(x - 1, y - 1), blackThreshold) + isBlack(img.getRGB(x + 1, y -1 ), blackThreshold) + isBlack(img.getRGB(x - 1, y + 1), blackThreshold);
            System.out.println(count);
            if (count < 2) {
                img.setRGB(x, y, Color.WHITE.getRGB());
            }
            //     img.setRGB(x, y, Color.WHITE.getRGB());
            }
            */

            //                if(getColorBright(img.getRGB(x, y)) > 600) {
            //                    img.setRGB(x, y, Color.WHITE.getRGB());
            //                } else {
            //                    /*
            //                    // ?Graphics2D
            //                    Graphics2D g2d = img.createGraphics();
            //                    // ?
            //                    // 1.0f? 0-1.0????
            //                    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
            //                    // 
            //                    g2d.setColor(new Color(255,0,0));
            //                    g2d.setStroke(new BasicStroke(1));
            //                    // g2d.draw
            //                    // 
            //                    // ? ?
            //                    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            //                    g2d.dispose();
            //                    */
            //
            //                    img.setRGB(x, y, Color.BLACK.getRGB());
            //                    /*
            //                    System.out.println(getColorBright(img.getRGB(x, y)) + ":");
            //                    System.out.println(getColorBright(img.getRGB(x + 1, y)) + "-" + getColorBright(img.getRGB(x + 1, y + 1)) + "-" + getColorBright(img.getRGB(x, y + 1)));
            //                    System.out.println(getColorBright(img.getRGB(x - 1, y)) + "-" + getColorBright(img.getRGB(x - 1, y - 1)) + "-" + getColorBright(img.getRGB(x, y - 1)));
            //                    System.out.println(getColorBright(img.getRGB(x - 1, y + 1)) + "-" + getColorBright(img.getRGB(x + 1, y - 1)));
            //                    */
            //
            //                    /*
            //                    int i = 0;
            //                    i = ((x < width - 1) && getColorBright(img.getRGB(x + 1, y)) < 30)? i + 1 : i;
            //                    i = ((x < width - 1) && (y < height - 1) && getColorBright(img.getRGB(x + 1, y + 1)) < 30)? i + 1 : i;
            //                    i = ((y < height - 1) && getColorBright(img.getRGB(x, y + 1)) < 30)? i + 1 : i;
            //                    i = ((x > 0) && getColorBright(img.getRGB(x - 1, y)) < 30)? i + 1 : i;
            //                    i = ((x > 0) && (y > 0) && getColorBright(img.getRGB(x - 1, y - 1)) < 30)? i + 1 : i;
            //                    i = ((y > 0) && getColorBright(img.getRGB(x, y - 1)) < 30)? i + 1 : i;
            //                    i = ((x < width - 1) && (y > 0) && getColorBright(img.getRGB(x + 1, y - 1)) < 30)? i + 1 : i;
            //                    i = ((x > 0) && (y < height - 1) && getColorBright(img.getRGB(x - 1, y + 1)) < 30)? i + 1 : i;
            //
            //                    if(i > 1) {
            //                        img.setRGB(x, y, Color.BLACK.getRGB());
            //                    } else {
            //                        img.setRGB(x, y, Color.WHITE.getRGB());
            //                    }
            //                    */
            //                }

            /*
            int i = 0;
            i = (getColorBright(img.getRGB(x + 1, y)) == 0)? i + 1 : i;
            i = (getColorBright(img.getRGB(x + 1, y + 1)) == 0)? i + 1 : i;
            i = (getColorBright(img.getRGB(x, y + 1)) == 0)? i + 1 : i;
            i = (getColorBright(img.getRGB(x - 1, y)) == 0)? i + 1 : i;
            i = (getColorBright(img.getRGB(x - 1, y - 1)) == 0)? i + 1 : i;
            i = (getColorBright(img.getRGB(x, y - 1)) == 0)? i + 1 : i;
                    
            System.out.println(getColorBright(img.getRGB(x, y)) + ":");
            System.out.println(getColorBright(img.getRGB(x + 1, y)) + "-" + getColorBright(img.getRGB(x + 1, y + 1)) + "-" + getColorBright(img.getRGB(x, y + 1)));
            System.out.println(getColorBright(img.getRGB(x - 1, y)) + "-" + getColorBright(img.getRGB(x - 1, y - 1)) + "-" + getColorBright(img.getRGB(x, y - 1)));
            System.out.println(getColorBright(img.getRGB(x - 1, y + 1)) + "-" + getColorBright(img.getRGB(x + 1, y - 1)));
            if(getColorBright(img.getRGB(x, y)) == 0 &&  i < 3) {
            img.setRGB(x, y, Color.WHITE.getRGB());
            }
            */

            /*
            // ?for????
            // ??object
            Object data = img.getRaster().getDataElements(x, y, null);
            int red = img.getColorModel().getRed(data);
            int blue = img.getColorModel().getBlue(data);
            int green = img.getColorModel().getGreen(data);
            System.out.println((red + blue + green) + "-" + getColorBright(img.getRGB(x, y)));
            red = (red * 3 + green * 6 + blue * 1)/10;
            green = red;
            blue = green;
                    
            // r?g?b?rgbbufferedImage????rgbrgb8388608?255*255*255?16777216
            int rgb = (red * 256 + green) * 256 + blue;
            if(rgb > 8388608) {
            rgb = rgb - 16777216;
            }
            // rgb
            img.setRGB(x, y, rgb);
            */

        }
    }
    // img = img.getSubimage(1, 1, img.getWidth() - 2, img.getHeight() - 2);
    return img;
}

From source file:net.rptools.lib.image.ImageUtil.java

public static BufferedImage rgbToGrayscale(BufferedImage image) {
    if (image == null) {
        return null;
    }/* w  ww.  j  a v  a  2 s .  c  om*/
    BufferedImage returnImage = new BufferedImage(image.getWidth(), image.getHeight(),
            pickBestTransparency(image));
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            int encodedPixel = image.getRGB(x, y);

            int alpha = (encodedPixel >> 24) & 0xff;
            int red = (encodedPixel >> 16) & 0xff;
            int green = (encodedPixel >> 8) & 0xff;
            int blue = (encodedPixel) & 0xff;

            int average = (int) ((red + blue + green) / 3.0);

            // y = 0.3R + 0.59G + 0.11B luminance formula
            int value = (alpha << 24) + (average << 16) + (average << 8) + average;
            returnImage.setRGB(x, y, value);
        }
    }
    return returnImage;
}

From source file:com.t3.image.ImageUtil.java

public static BufferedImage rgbToGrayscale(BufferedImage image) {

    if (image == null) {
        return null;
    }/* www.  j  a va  2  s .  c  o  m*/

    BufferedImage returnImage = new BufferedImage(image.getWidth(), image.getHeight(),
            pickBestTransparency(image));

    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {

            int encodedPixel = image.getRGB(x, y);

            int alpha = (encodedPixel >> 24) & 0xff;
            int red = (encodedPixel >> 16) & 0xff;
            int green = (encodedPixel >> 8) & 0xff;
            int blue = (encodedPixel) & 0xff;

            int average = (int) ((red + blue + green) / 3.0);

            // y = 0.3R + 0.59G + 0.11B luminance formula
            int value = (alpha << 24) + (average << 16) + (average << 8) + average;
            returnImage.setRGB(x, y, value);
        }
    }

    return returnImage;
}

From source file:brut.androlib.res.decoder.Res9patchStreamDecoder.java

private void drawHLine(BufferedImage im, int y, int x1, int x2) {
    for (int x = x1; x <= x2; x++) {
        im.setRGB(x, y, NP_COLOR);
    }//from w  w w. ja  va 2 s  .  c o m
}

From source file:brut.androlib.res.decoder.Res9patchStreamDecoder.java

private void drawVLine(BufferedImage im, int x, int y1, int y2) {
    for (int y = y1; y <= y2; y++) {
        im.setRGB(x, y, NP_COLOR);
    }//from ww w  .java2  s  .  c  om
}

From source file:gr.iti.mklab.reveal.forensics.util.Util.java

public static BufferedImage getBufferedIm(int[][][] rgbValues) {
    int ImW = rgbValues[0].length;
    int ImH = rgbValues[0][0].length;
    BufferedImage ImageOut = new BufferedImage(ImW, ImH, 5); // 5 for PNG;
    Color tmpColor;/* w  w  w . ja v  a 2  s .  com*/
    for (int ii = 0; ii < ImW; ii++) {
        for (int jj = 0; jj < ImH; jj++) {
            tmpColor = new Color((int) Math.round(rgbValues[0][ii][jj]), (int) Math.round(rgbValues[1][ii][jj]),
                    (int) Math.round(rgbValues[2][ii][jj]));
            ImageOut.setRGB(ii, jj, tmpColor.getRGB());
        }
    }
    return ImageOut;
}

From source file:org.elfinder.servlets.commands.ResizeCommand.java

/**
 * Rotation function: this could be better!!! Rotation point by point not so
 * fast but work well.// ww  w .  ja va  2  s  .  c  o m
 *
 * @param BufferedImage
 */
private BufferedImage rotateCw(BufferedImage img) {
    int width = img.getWidth();
    int height = img.getHeight();
    BufferedImage newImage = new BufferedImage(height, width, img.getType());

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

From source file:jurls.core.utils.MatrixImage.java

protected void pixel(BufferedImage image, int j, int i, double value) {
    image.setRGB(j, i, getColor(value));
}