aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ConvolvedOp with / without alpha premultiplication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ConvolvedOp with / without alpha premultiplication" Watch "ConvolvedOp with / without alpha premultiplication" New topic
Author

ConvolvedOp with / without alpha premultiplication

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I'm doing some image effects and wondering if ConvolveOp is working correctly. I was doing some filters on images and was getting dark halos around them. I read somewhere that this might be due to not using pre-calculated alpha (i.e. RGB values do not already include the effect of the alpha channel), so I switched my BufferedImage type to TYPE_INT_ARGB_PRE, and it worked as expected (see below).

However I do not understand why the image does not display correctly when using TYPE_INT_ARGB. According to the ConvolveOp documents
This class operates with BufferedImage data in which color components are premultiplied with the alpha component. If the Source BufferedImage has an alpha component, and the color components are not premultiplied with the alpha component, then the data are premultiplied before being convolved. If the Destination has color components which are not premultiplied, then alpha is divided out before storing into the Destination (if alpha is 0, the color components are set to 0). If the Destination has no alpha component, then the resulting alpha is discarded after first dividing it out of the color components.

Doesn't the bolded part mean that non-premultiplied images should work? Or are ConvolvedOp operations only possible on images with premultiplied alpha?


Above: left: BufferedImage type TYPE_INT_ARGB before and after convolution; right: TYPE_INT_ARGB_PRE
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

This class operates with BufferedImage data in which color components are premultiplied with the alpha component. If the Source BufferedImage has an alpha component, and the color components are not premultiplied with the alpha component, then the data are premultiplied before being convolved. If the Destination has color components which are not premultiplied, then alpha is divided out before storing into the Destination (if alpha is 0, the color components are set to 0). If the Destination has no alpha component, then the resulting alpha is discarded after first dividing it out of the color components.


Hi Luigi. I haven't run any tests, but the highlighted line from the documentation seems to imply that your problem is not caused by not using premultiplied alpha in your source, but in your destination. After the convolve operation, transparent elements may be divided out by simply setting the color to black. Maybe you can try to use a premultiplied source, and filtering to a non-premultiplied destination, and see if you still get the same problem.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Hi Stephan,

I tried what you suggested, and indeed the same problem happens if the source is premultiplied and the destination isn't.

Likewise if the destination is premultiplied and the source isn't, it renders correctly.

Maybe the solution is just to always use TYPE_INT_ARGB_PRE for your destination image, so there wouldn't be any restriction on what you could convolve, but you would be restricted on the type you end up with.

I don't really care what type I end up with, but presumably there are reasons for sometimes needing a particular image type.
 
 
subject: ConvolvedOp with / without alpha premultiplication