1

I have been trying out some basic image processing using java (after a long gap).

Any operation I do on the original image and save it as a new image -> the o/p image always appears DULL (may be an issue with opacity or transparency).

I am pasting the function which I am using to do this job below :

//Returns a blurred java buffered image

public static BufferedImage blurImage(BufferedImage image) 
{
    int w = image.getWidth(); 
    int h = image.getHeight(); 
    int alpha = 0;
    int red, green, blue, newPix;
    int pix[] = null;

    BufferedImage newImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    for(int i=0,j=0; i<w; i++) 
    {
        for(j=0; j<h; j++) 
        {
            pix = getSurroundingPixels(image, i>0?i-1:0, i<w-1?i+1:w-1, j>0?j-1:0, j<h-1?j+1:h-1);
            red = green = blue = 0;
            for(int k=0; k<pix.length; k++) 
            {
                red += (pix[k]>>16) & 0xFF;
                green += (pix[k]>>8) & 0xFF;
                blue += (pix[k]) & 0xFF;
            }
            alpha = (image.getRGB(i,j)>>24) & 0xFF;
            red /= pix.length;
            green /= pix.length;
            blue /= pix.length;
            newPix = ((alpha<<24) | (red<<16) | (green<<8) | blue);
            newImage.setRGB(i,j, newPix);
        }
    }

    return newImage;
}

I would appreciate someone helping me on this issue.

sudhishkr
  • 3,318
  • 5
  • 33
  • 55
  • 1
    Try `ConvolveOp`, as shown in the articles cited [here](http://stackoverflow.com/a/14412962/230513). – trashgod Feb 07 '13 at 09:19
  • I will definitely try ConvolveOp, BUT my intention is NOT to use an alternative. I want to figure out whats going wrong here. Infact, I have written similar functions like rotate, mirror functions. Even those are returning a DULL image. – sudhishkr Feb 07 '13 at 09:58
  • 2
    Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe using a self-contained or otherwise accessible image. – trashgod Feb 07 '13 at 10:03
  • Some related examples are cited [here](http://stackoverflow.com/a/12984566/230513). – trashgod Feb 07 '13 at 10:16
  • In the above function I have pasted. I have now replaced BufferedImage.INT_TYPE_ARGB with BufferedImage.INT_TYPE_RGB , after this the o/p image doesnt appear dull and looks normal. Can you please explain why does this happen? – sudhishkr Feb 08 '13 at 05:12

1 Answers1

2

I have now replaced BufferedImage.INT_TYPE_ARGB with BufferedImage.INT_TYPE_RGB, after this, the [processed] image doesn't appear dull, and [it] looks normal. Can you please explain why does this happen?

TYPE_INT_ARGB has a DirectColorModel with alpha; TYPE_INT_RGB has a DirectColorModel without alpha. Your algorithm scales the RGB, but clones the A. At a guess, your test image is opaque, possibly a .jpg image, requiring BufferedImage.TYPE_INT_RGB. You may want to examine your image using this example that scales A or this example that illustrates the color conversion done by setRGB().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045