[ENH]Translucent images in 8 and 16 bit screen resolution

Andreas Raab andreas.raab at gmx.de
Sat Dec 16 22:56:37 UTC 2006


karl wrote:
> Images with alpha translucency will now keep the translucency in 8 and 
> 16 bit screen resolution. (Rotated forms will not, that has to do with 
> WarpBlt internals I don't really understand yet.) It would be nice if 
> people could play with this enhancement and report any issues.

Sorry to say that but the proposed fix is incorrect. Form>>blend (the 
operator used by #translucentImage:at:) computes the blend of source and 
destination color using:

    srcAlpha*srcColor + (1-srcAlpha)*dstColor

while rule 34 computes it based on the following formula:

    srcColor + (1-srcAlpha)*dstColor

In other words, rule 34 assumes that the input is premultiplied, i.e., 
the color components for each pixel are in the range of (0..alpha). The 
difference can be seen in the following:

	"Go to 32 bpp for the demonstration"
	Display newDepth: 32.
	World color: Color red.

	"Create a regular alpha (0-255 saturated) form"
	formA := Form extent: 200 at 200 depth: 32.
	formA fillColor: (Color white alpha: 0.5).
	formA displayAt: 0 at 0. "shows as white"

	"Convert formA to pre-multiplied"
	formB := Form extent: formA extent depth: 32.
	formA displayOn: formB at: 0 at 0 rule: Form blend.
	formB displayAt: 200 at 0. "shows as gray since it's premultiplied"

	"Show the both forms using Form>>blend"
	formA displayOn: Display at: 0 at 200 rule: Form blend.
	formB displayOn: Display at: 200 at 200 rule: Form blend.

	"Show the both forms using rule 34"
	formA displayOn: Display at: 0 at 400 rule: 34.
	formB displayOn: Display at: 200 at 400 rule: 34.

Notice how Form>>blend is the correct combination rule for blending a 
50% white with the red background for the regular form and rule 34 is 
the correct combination rule for the pre-multiplied blend. But neither 
is correct for the "other" form. In other words, you can't mix the two; 
you need to know whether to expect pre-multiplied alpha or not, and 
#translucentImage:at: does NOT expect pre-multiplied alpha.

The way to fix that is by either converting the input form into 
pre-multiplied form first (as shown in the above) the cost of which is 
the obvious memory hit or by writing other custom BitBlt methods just 
like for rule 34 (only for using the regular Form>>blend operator).

Cheers,
   - Andreas



More information about the Squeak-dev mailing list