2

I want to filter my BufferedImage in java.
Currently, I have this piece of code:

private void changeRGB(BufferedImage image, int colour) {
    int width = image.getWidth();
    int height = image.getHeight();
    int newColor = 0;

    for(int y = 0; y < height; y++){
      for(int x = 0; x < width; x++){
          Color color = new Color(image.getRGB(x,y));

          int red = color.getRed();
          int green = color.getGreen();
          int blue = color.getBlue();

          if (colour == 1) {
              newColor = new Color(red, 0, 0).getRGB();
          } else if (colour == 2) {
              newColor = new Color(0, green, 0).getRGB();
          } else if (colour == 3) {
              newColor = new Color(0, 0, blue).getRGB();
          }

          image.setRGB(x, y, newColor);
      }
    }
    icon = new ImageIcon(image);
    this.lblFilteredImage.setIcon(icon);
}

and if I press e.g. the red, green or blue button I do it this way:

choice = 2; changeRGB(img, choice);

This code works one time and if I press one of these buttons the 2nd time it doesn't work anymore

mafioso
  • 1,630
  • 4
  • 25
  • 45
  • `color` and `colour` variables in the same scope? You should definetly change one of those variable names... Furthermore if applied more than once to the same image, the image either does not change, since you extract the same channel you already extracted or the image becomes black, since you extract a color that was set to 0 before... – fabian Sep 24 '16 at 08:55
  • Ok but how can I solve that issue so the image doesn't get black? – mafioso Sep 24 '16 at 09:15
  • Use 2 Images, one with the source and the other with the filtered data. Applying different filters does not make sense in with those filters; so I guess you want to apply only one of these filters at once. – fabian Sep 24 '16 at 09:23

2 Answers2

1

it seems you are modifying the "source" image stored in the BufferedImage, use a new BufferedImage to store the result and thus avoid destroying your source image.

you can try it this way, so the "source" image is left unaltered.

private void changeRGB(BufferedImage image,int colour)
{
    int width=image.getWidth();
    int height=image.getHeight();
    int newColor=0;
    BufferedImage tempBuffer=new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
    for(int y=0;y<height;y++)
    {
        for(int x=0;x<width;x++)
        {
            Color color=new Color(image.getRGB(x, y));

            int red=color.getRed();
            int green=color.getGreen();
            int blue=color.getBlue();

            if(colour==1)
            {
                newColor=new Color(red, 0, 0).getRGB();
            }
            else if(colour==2)
            {
                newColor=new Color(0, green, 0).getRGB();
            }
            else if(colour==3)
            {
                newColor=new Color(0, 0, blue).getRGB();
            }

            tempBuffer.setRGB(x, y, newColor);
        }
    }
    icon=new ImageIcon(tempBuffer);
    this.lblFilteredImage.setIcon(icon);
}
Alex
  • 189
  • 1
  • 7
0

I think Alex is right. BufferedImage is mutable, so changeRGB changes the "original" BufferedImage.
Clone the BufferedImage and process the clone.

Community
  • 1
  • 1
c0der
  • 18,467
  • 6
  • 33
  • 65