How can I produce a fade-out or fade-in image effect?

Bill Day

You can fade an image using a Java 2D convolve operation akin to the edge detection discussed in my JavaWorld column, "Image processing with Java 2D", at:

The key thing to note is that if your kernel coefficients add up to more than one, you will darken the overall image. If you use a kernel like:

0.0f 0.0f 0.0f
0.0f x 0.0f
0.0f 0.0f 0.0f

where x-1.0f>0 by some small amount, then you apply this kernel by repeatedly convoluting the image, you will see it fade out. You should be able to fade back in by reversing this convolution (1.0f-x>0) while checking each convoluted pixel's RGB values against a copy of the original image and clipping to the original's RGB values when you reach them. If you are using grayscale images (R, G, and B values are the same), it will be all the easier, as you'll only need to check one value instead of three for each pixel and pass.