- A First Technique for Blending Images
- A Better Technique for Blending Images
- Blending Slides
- Conclusion
A Better Technique for Blending Images
The problem with the previous technique for blending images is that it's slow. In addition to executing three method calls for each row of pixels, the algorithm performs multiple operations (including six floating-point operations) on each pixel. Even if we eliminate the floating-point operations, the algorithm is still slow. Fortunately, we can take advantage of alpha compositing to blend images very quickly.
Wikipedia's alpha compositing entry states: "[A]lpha compositing is the process of combining an image with a background to create the appearance of partial transparency." The combining operation takes advantage of an alpha channel, which basically determines how much of a source pixel's color information covers a destination pixel's color information.
In their 1984 paper "Compositing Digital Images," computer scientists Thomas Porter and Tom Duff defined several combining operations that work with the alpha channel. One of these operations, A over B (also known as source over destination), copies source pixels over destination pixels. However, a pixel's alpha channel value determines how much of the destination pixel's color information shows through: 1.0 means no color information, whereas 0.0 means all color information.
The java.awt.AlphaComposite class implements most of the Porter-Duff compositing rules, including source over destination. Prior to drawing an image onto a graphics context, you obtain an instance of this class that corresponds to a specific alpha value, and then install this object into the current graphics context via java.awt.Graphics2D's public void setComposite(Composite comp) method.
Listing 3 excerpts the blend() method from an equivalent Blender2 application. After creating a BufferedImage to hold the blended image, this method draws the first image via the context's default source over destination with alpha set to 1.0 (no destination color information shows through) rule, installs a composite whose alpha value specifies how much of the second image to combine, and draws this image.
Listing 3 The blend() method from Blender2.java.
public BufferedImage blend (BufferedImage bi1, BufferedImage bi2, double weight) { if (bi1 == null) throw new NullPointerException ("bi1 is null"); if (bi2 == null) throw new NullPointerException ("bi2 is null"); int width = bi1.getWidth (); if (width != bi2.getWidth ()) throw new IllegalArgumentException ("widths not equal"); int height = bi1.getHeight (); if (height != bi2.getHeight ()) throw new IllegalArgumentException ("heights not equal"); BufferedImage bi3 = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bi3.createGraphics (); g2d.drawImage (bi1, null, 0, 0); g2d.setComposite (AlphaComposite.getInstance (AlphaComposite.SRC_OVER, (float) (1.0-weight))); g2d.drawImage (bi2, null, 0, 0); g2d.dispose (); return bi3; }