Date: Tue, 12 Aug 2003 14:26:19 +0200 (MEST)
authorSam Lantinga <slouken@libsdl.org>
Tue, 12 Aug 2003 15:17:20 +0000
changeset 688c0522010bb6d
parent 687 91400ecf307d
child 689 5bb080d35049
Date: Tue, 12 Aug 2003 14:26:19 +0200 (MEST)
From: "Mattias Engdeg?rd"
Subject: bug in SDL_GetRGB/GetRGBA

There's an embarrassing bug in GetRGB/GetRGBA which apparently has been there
for years. It incorrectly converts colours with < 8 bits/channel.
It came to my attention today in #sdl.

What it does now is (for each channel):

rv = (pixel & fmt->Rmask) >> fmt->Rshift;
*r = (rv << fmt->Rloss) + (rv >> (8 - fmt->Rloss));

which is wrong; the last line should be

*r = (rv << fmt->Rloss) + (rv >> (8 - (fmt->Rloss << 1)));
src/video/SDL_pixels.c
     1.1 --- a/src/video/SDL_pixels.c	Mon Aug 11 22:28:13 2003 +0000
     1.2 +++ b/src/video/SDL_pixels.c	Tue Aug 12 15:17:20 2003 +0000
     1.3 @@ -381,14 +381,14 @@
     1.4  		 */
     1.5  	        unsigned v;
     1.6  		v = (pixel & fmt->Rmask) >> fmt->Rshift;
     1.7 -		*r = (v << fmt->Rloss) + (v >> (8 - fmt->Rloss));
     1.8 +		*r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1)));
     1.9  		v = (pixel & fmt->Gmask) >> fmt->Gshift;
    1.10 -		*g = (v << fmt->Gloss) + (v >> (8 - fmt->Gloss));
    1.11 +		*g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1)));
    1.12  		v = (pixel & fmt->Bmask) >> fmt->Bshift;
    1.13 -		*b = (v << fmt->Bloss) + (v >> (8 - fmt->Bloss));
    1.14 +		*b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1)));
    1.15  		if(fmt->Amask) {
    1.16  		        v = (pixel & fmt->Amask) >> fmt->Ashift;
    1.17 -			*a = (v << fmt->Aloss) + (v >> (8 - fmt->Aloss));
    1.18 +			*a = (v << fmt->Aloss) + (v >> (8 - (fmt->Aloss << 1)));
    1.19  		} else
    1.20  		        *a = SDL_ALPHA_OPAQUE;
    1.21  	} else {
    1.22 @@ -405,11 +405,11 @@
    1.23  	        /* the note for SDL_GetRGBA above applies here too */
    1.24  	        unsigned v;
    1.25  		v = (pixel & fmt->Rmask) >> fmt->Rshift;
    1.26 -		*r = (v << fmt->Rloss) + (v >> (8 - fmt->Rloss));
    1.27 +		*r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1)));
    1.28  		v = (pixel & fmt->Gmask) >> fmt->Gshift;
    1.29 -		*g = (v << fmt->Gloss) + (v >> (8 - fmt->Gloss));
    1.30 +		*g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1)));
    1.31  		v = (pixel & fmt->Bmask) >> fmt->Bshift;
    1.32 -		*b = (v << fmt->Bloss) + (v >> (8 - fmt->Bloss));
    1.33 +		*b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1)));
    1.34  	} else {
    1.35  		*r = fmt->palette->colors[pixel].r;
    1.36  		*g = fmt->palette->colors[pixel].g;