One of the major Graphics Mill features is the ability to combine multiple images into a single one. The basics of combining images, including code snippets, are presented in the Overlaying Images topic. Everything is pretty simple if you just need to place one image on the top of another, but things become much more interesting if you want to blend images. This topic sheds light on how images can be blended in Graphics Mill:
In this topic the source image refers to the base bitmap and the overlaying image refers to the bitmap added atop. The following two images are used throughout the topic to demonstrate combining modes:
Figure 1. Source bitmap
Figure 2. Overlaying bitmap
The overlaying bitmap comes with two levels of transparency. The background is completely transparent and the yellow part comes with an opacity level of 0.7
(70%).
Each overlaying pixel replaces the corresponding source pixel. The opacity value is ignored, as a result the transparent zones become black.
Figure 3. Bitmaps are blended using the Copy mode.
In the Alpha mode, each overlaying pixel is blended with the corresponding source pixel. The opacity value of the overlaying image is taken into account, therefore this mode is useful only when the overlaying bitmap has the alpha channel.
Figure 4. Bitmaps are blended using the Alpha mode.
The AlphaOverlapped mode is almost the same as the Alpha mode, but the resulting alpha channel is calculated as the maximum between the source and overlaying alpha channels. This mode is useful only when both the source and the overlaying bitmaps have alpha channels.
For demonstration purposes the source and overlaying images were modified. In the first case (Figure 5.1) the opacity of the source image was set to 0.3
(30%) and the alpha channel of the overlaying bitmap was multiplied by 0.8
. In the second case (Figure 5.2) the opacity of the source image was set to 0.8
(80%) and the alpha channel of the overlaying bitmap was multiplied by 0.3
.
Figure 5.1. Bitmaps are blended using the AlphaOverlapped mode.
Figure 5.2. Bitmaps are blended using the AlphaOverlapped mode.
The AlphaOverlay mode is an associative version of the over
operator (normal painting operation). Normal painting is one of the simplest solutions to the visibility problem in computer graphics. The computer paints distant objects first, followed by closer objects, and as a result hides the objects, which non-visible to the observer. Thus, the AlphaOverlay mode allows you to set the order of the blending images.
For demonstration purposes the source and overlaying images were modified. In the first case (Figure 6.1) the opacity of the source image was set to 0.3
(30%) and the alpha channel of the overlaying bitmap was multiplied by 0.8
. In the second case (Figure 6.2) the opacity of the source image was set to 0.8
(80%) and the alpha channel of the overlaying bitmap was multiplied by 0.3
.
Figure 6.1. Bitmaps are blended using the AlphaOverlay mode.
Figure 6.2. Bitmaps are blended using the AlphaOverlay mode.
Colors are blended only in the semi-transparent areas of the overlaying bitmap. The opaque areas are ignored.
Figure 7. Bitmaps are blended using the Behind mode.
The Add mode simply sums pixel values of the source and overlaying images taking into account their transparency. In case if the resulting image is RGB and all channels are above 255 (65535), white is displayed.
Figure 8. Bitmaps are blended using the Add mode.
In the Subtract mode, the source and overlaying colors are added, and then the maximum luminosity value is subtracted from the resulting value. The maximum luminosity value is 255 for common pixel formats and 65535 for the extended ones.
Figure 9. Bitmaps are blended using the Subtract mode.
In the Multiply mode, the source color is multiplied by the overlaying one. The resulting value is normalized to the maximum luminosity value. The maximum luminosity value is 255 for common pixel formats and 65535 for extended ones. The resulting color is always a darker color. Multiplying any color with black produces black. Multiplying any color with white leaves the color unchanged.
Figure 10. Bitmaps are blended using the Multiply mode.
In the Divide mode, the source color is divided by the overlaying one.
Figure 11. Bitmaps are blended using the Divide mode.
In the Difference mode, the resulting value is the difference between the source and overlaying colors, whichever has the greater luminosity. Blending any color with white in this mode inverts the color. Blending any color with black in this mode leaves the color unchanged.
Figure 12. Bitmaps are blended using the Difference mode.
In the Hue, Saturation, Luminosity, and Color modes the source and overlaying images are converted to the HSL color space before blending. The resulting image is transformed back to the original color format. If the destination image has alpha channel, then the resulting image will have the same alpha channel. The alpha channel of the overlaying image is considered when calculating the color components of the resulting image.
In the Hue mode, the hue of the resulting color is taken from the overlaying color, and the other components, saturation and luminosity, are taken from the source color.
Figure 13. Bitmaps are blended using the Hue mode.
In the Saturation mode, the saturation of the resulting color is taken from the overlaying color, and the other components are taken from the source color. If the overlaying color has a saturation equal to zero (the overlaying color is gray), then the source color remains unchanged.
Figure 14. Bitmaps are blended using the Saturation mode.
In the Luminosity mode, the luminosity of the resulting color is taken from the overlaying color, and the other components are taken from the source color. Blending in this mode produces the opposite effect to the Color mode.
Figure 15. Bitmaps are blended using the Luminosity mode.
In the Color mode, hue and saturation of the resulting color are taken from the overlaying color, and the luminosity is taken from the source color. This mode is useful for tinting images.
Figure 16. Bitmaps are blended using the Color mode.
In the Screen mode, the overlaying color is inverted and multiplied by the source one. The resulting value is normalized to the maximum luminosity value, which is 255 for common pixel formats and 65535 for extended ones. The resulting color is always a lighter color. Blending any color with black in this mode will leave the color unchanged. Blending any color with white in this mode produces white.
Figure 17. Bitmaps are blended using the Screen mode.
The Overlay mode combines the Screen and Multiply modes. Depending on the source pixel luminosity the Screen (lightening) or the Multiply (darkening) mode is used to calculate the resulting pixel. The lighter the source pixel, the lighter the resulting pixel; and the darker the source pixel, the darker the resulting pixel. As a result, the Overlay mode preserves highlights and shadows.
Figure 18. Bitmaps are blended using the Overlay mode.
In the And mode, the bitwise AND operation is applied to the source and overlaying colors.
Figure 19. Bitmaps are blended using the And mode.
In the Or mode, the bitwise OR operation is applied to the source and overlaying colors.
Figure 20. Bitmaps are blended using the Or mode.
In the Xor mode, the bitwise XOR operation is applied to the source and overlaying colors.
Figure 21. Bitmaps are blended using the Xor mode.
In the IfDarker mode, the resulting color is defined as the darkest between the colors of the source and overlaying images. If a pixel of the source image is darker than the corresponding pixel of the overlaying image, then the source pixel remains untouched. Otherwise, it is replaced by the overlaying pixel.
Figure 22. Bitmaps are blended using the IfDarker mode.
In the IfLighter mode, the resulting color is defined as the lightest between the colors of the source and overlaying images. If a pixel of the source image is lighter than the corresponding pixel of the overlaying image, then the source pixel remains untouched. Otherwise, it is replaced by the overlaying pixel.
Figure 23. Bitmaps are blended using the IfLighter mode.
In the Invert mode, the overlaying image is inverted and blended with the source image in the same way as in the Alpha mode.
Figure 24. Bitmaps are blended using the Invert mode.
In the Texturize mode, the overlaying image is converted to grayscale and blended with the source image in the same way as in the Alpha mode.
Figure 25. Bitmaps are blended using the Texturize mode.