[SDL] BUG?: RGBA->screen blit has 1-off error with opaque colors
david at olofson.net
Wed Jun 6 05:31:48 PDT 2007
On Wednesday 06 June 2007, Markus Laire wrote:
> The attached program should print "First pixel: 00FFFFFF" but
> instead of that I get "First pixel: 00FEFEFE"
> Am I right in thinking that this is a bug in SDL?
> OS is Linux (Knoppix 4.0.2)
> SDL version is 1.2.11
I think this is the result of the blitters multiplying by the alpha
value, and then dividing by 256 (8 bit right shift) rather than 255.
(Actual division is *very* expensive, compared to shift operations!)
One way around this is to scale the alpha value from [0, 255] to [0,
256]. In the case of full surface alpha, this costs virtually
nothing, but it would probably slow down alpha channel (RGBA) blits
quite a bit - although not nearly as much as actually dividing by 255
for each channel.
One could cheat by scaling the alpha channel to [0, 128] and shifting
7 bits, but obviosuly, that results in a "funny" pixel format, and
less accurate blending... Another hack is to add 1 somewhere, but
then you just get 1 instead of 0 for "fully" transparent.
I'm afraid there is no easy, low cost solution - which is why it is
the way it is, I would think.
//David Olofson - Programmer, Composer, Open Source Advocate
.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
'-- http://www.reologica.se - Rheology instrumentation --'
More information about the SDL