BufferedImage: setRGB(getRGB()) sets no data, if the getter is called before

265 Views Asked by At

I'm trying to understand why a

bufferedImg.setRGB(x, y, color.getRGB());

sets no data (white pixels) at all, if I print one immediately before it by

System.out.println(color.getRGB());

as in following java code:

...
    int height = img.getHeight();
    int width = img.getWidth();

    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){

           Color c = new Color(img.getRGB(j, i));

           int red = (int)(c.getRed() * 0.299);
           int green = (int)(c.getGreen() * 0.587);
           int blue = (int)(c.getBlue() *0.114);

           Color newColor = new Color(red + green + blue,
                red + green + blue, red + green + blue);

           System.out.println(newColor.getRGB());         // resets data
           img.setRGB(j, i, newColor.getRGB());
        }
    }

Additional infos:

  • Its an implementation for converting RGB to grayscale
  • it works perfectly fine by removing the print/log line
  • (multiple) calls of println() before or/and after shows correct data
  • buffered image source is an openCV mat
  • i didnt find any specific reasons on the internet

Hoping for a person to give me some insight.

0

There are 0 best solutions below