|
TutorialBlendModes
Tutorial on the alpha blending modes upcoming in version 0.11.4
The nightly builds of PulpCore now integrate new alpha blending modes. 1. What is Alpha compositingWikipedia defines "Alpha compositing as : ... the process of combining an image with a background to create the appearance of partial transparency. It is often useful to render image elements in separate passes, and then combine the resulting multiple 2D images into a single, final image in a process called compositing. For example, compositing is used extensively when combining computer rendered image elements with live footage. Basically, a composite mode explains how colors of each pixel of an image will blend together to create the resulting images. Alpha compositing is also known as "Porter and Duff rules" because they first explained how translucent images could be blend together to create various effects. 2. A bit of theory about image representationBefore going further, let's explain how is composed an image. An image is composed of pixels (a colored dot). Each pixel of an image is composed of 4 components :
All components are then added to create a color. For example :
Each component value is stored as a byte (a byte = 8bits allows to have 256 different values for each component). A color, composed of 4 bytes is stored, in a 32 bits Java datatype : primitive type int. How about the alpha component ?Alpha component represents the transparent component of the image. A value of 0 means transparent color, a value of 255 means an opaque color. Values between 0 and 255 means that the color is translucent, it means that this color will react with the underlying color (that'll be explained in next chapter).
Premultiplied representation has an advantage over unpremultiply representation : 3 multiplications are spared for each pixel. Consider big images (800x600) and you'll see that it can spare a lot of operations time. 3. Blending modesThe following blend modes operations are expected to be used with premultiplied colors. They explain how an existing image (a background image or Destination image), DST, will blend with the image that needs to be drawn (the foreground image or Source image) SRC. Ax represents the Alpha component. 2.1 ClearAr = 0 None of the terms are used.
How to use Clear in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.SrcClear());
add(redSprite);2.2 DstAr = Ad Only the terms that contribute destination color are used.
How to use Dst in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.Dst());
add(redSprite);2.3 Dst AtopAr = As * (1 - Ad) + Ad * As The destination that overlaps the source is composited with the source and replaces the destination. How to use Dst Atop in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.DstAtop());
add(redSprite);2.4 Dst InAr = Ad * As The destination that overlaps the source, replaces the source. How to use Dst In in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.DstIn());
add(redSprite);2.5 Dst OutAr = Ad * (1 - As) The destination that does not overlap the source replaces the source. How to use Dst Out in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.DstOut());
add(redSprite);2.6 Dst OverAr = As * (1 - Ad) + Ad The destination that overlaps the source is composited with the source and replaces the destination. How to use Dst Over in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.DstOver());
add(redSprite);2.7 SrcAr = As Only the terms that contribute source color are used. How to use Src in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.Src());
add(redSprite);2.8 Src AtopAr = As * Ad + Ad * (1 - As) The source that overlaps the destination is composited with the destination. How to use Src Atop in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.SrcAtop());
add(redSprite);2.9 Src InAr = As * Ad The source that overlaps the destination, replaces the destination. How to use Src In in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.SrcIn());
add(redSprite);2.10 Src OutAr = As * (1 - Ad) The source that does not overlap the destination replaces the destination. How to use Src Out in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.SrcOut());
add(redSprite);2.11 Src Over (Default)Ar = As + Ad * (1 - As) The source color is placed over the destination color. How to use Src Over in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.SrcOver());
add(redSprite);or... nothing, because that's the default blend mode ! 2.12 XorAr = As * (1 - Ad) + Ad * (1 - As) The non-overlapping regions of source and destination are combined. How to use Xor in PulpCore : ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.Xor());
add(redSprite);3. Applications in PulpCore3.1 Mixing Two images togetherEven if you can add images directly in the Scene2D, you can't change the default blending mode here. You'll have to create a Group and add your images here.
groupA.setBlendMode(BlendMode.DstIn());
ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
add(redSprite);
groupA.setBlendMode(BlendMode.DstIn());
ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.Xor());
add(redSprite);Both options are corrects. You'll have to choose one depending on what you want to do. 3. 2 Using the back bufferYou may notice, if you're using some blend modes that the result is not exactly what you expected. Why do I get this black rectangle ?? Ar = Ad * (1 - As)if the destination image is opaque (that's the case of our background), Ad is 1. Imagine that the source image is also opaque, we have : Ar = 1 * (1 - 1) = 0All the components are 0, the background is painted in black. This effect is explained in more details in filthy rich clients. To avoid this, make sure to apply masks on a back buffer using the createBackBuffer() method : groupA = new Group();
groupA.createBackBuffer();
groupA.setBlendMode(BlendMode.DstIn());
ImageSprite blueSprite = new ImageSprite("blue.png", 10, 10);
add(blueSprite);
ImageSprite redSprite = new ImageSprite("red.png", 60, 60);
redSprite.setBlendMode(BlendMode.Xor());
add(redSprite);
|
Sign in to add a comment