Image processing in Java using BufferedImage

70 Views Asked by At

I have encountered a problem while trying to apply a color tint to an animation in java. My code iterates through all the pixels of all the images in the animation (I know it is inefficient, but I want to get it to a working state first). So what it does is, it gets the average of the RGB values of a particular pixel and the tint. It then sets that pixel to the resulting RGB values.

Manually calculating the values result in a pretty good tint(Doing the calculations using a calculator and seeing the color using Google). But unfortunately, when setting the pixels to the calculated RGB. It does not work(Terrible color and far from what should show). I have noticed that when I tried calculating the color and then using that to paint an oval, the results are pretty good. I think that the problem lies the setting the pixel.

public void transform(Color color){
//This part just gets the buffered images
        getBuffered();
        for(int i=0;i<32;i++){
            BufferedImage image=catFrames[i];
            for (int x = 0; x < image.getWidth(); x++) {
                for (int y = 0; y < image.getHeight(); y++) {
                    Color pixelColor = new Color(image.getRGB(x, y), true);
               
                    
                    if(pixelColor.getRGB()==Color.white.getRGB())continue;
                    Color col=new Color((pixelColor.getRed() + color.getRed()) / 2,(pixelColor.getGreen() + color.getGreen()) / 2,(pixelColor.getBlue() + color.getBlue()) / 2,255);
                    
                    image.setRGB(x, y, col.getRGB());
                }
            }
            catFrames[i]=image;
        }

This is the method that transforms the individual images into its tinted version. What it looks like The oval is what the color should be and the cat is what I'm trying to tint. Something that might be noteworthy is that coloring when tinting the cat red, it works fine. It does not work when tinting with green and blue.

Also I will not be able to go deeply into image processing, I would appreciate a lot if the solution that would be within the bounds of complexity of what is shown here.

0

There are 0 best solutions below