[SDL] Defect: SDL_getRGB SDL_getRGBA

Greg Smith gpsmith_us at yahoo.com
Thu Dec 12 20:29:01 PST 2002


This is a defect report for SDL_getRGB and
SDL_getRGBA.  If defects should be posted to a
different address please let me know.

Version:       1.2.2 and 1.2.5
Files:         SDL_pixels.c
Functions:     SDL_getRGB and SDL_getRGBA
Description:

SDL_getRGB and SDL_getRGBA do not convert color
components with less than 8-bits per component to full
8-bits where loss bits are filled with most
significant bits of the component.

DOCUMENTATION ON THESE FUNCTIONS

"This function uses the entire 8-bit [0..255] range
when converting color components from pixel formats
with less than 8-bits per RGB component (e.g., a
completely white pixel in 16-bit RGB565 format would
return [0xff, 0xff, 0xff] not [0xf8, 0xfc, 0xf8]).

If the surface has no alpha component, the alpha will
be returned as 0xff (100% opaque)."

DEFECT IN CODE

Example for red component in SDL_getRGB

For RGB565
fmt->Rmask  = 0xF800
fmt->Rshift = 11
fmt->Rloss  = 3

Current code for SDL_getRGB red component

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

if pixel == 0xFFFF then

rv    = (pixel & fmt->Rmask)  >> fmt->Rshift;
rv    = (0x0000FFFF & 0xF800) >> 11;
rv    = (0xF800) >> 11;
rv    = 0x1F;

*r    = (rv << fmt->Rloss) + (rv >> (8 - fmt->Rloss));
*r    = (0x1F << 3)        + (0x1F >> (8-3));
*r    = (0xF8)             + (0x1F >> 5);
*r    = (0xF8)             + (0)
*r    = 0xF8

This is incorrect.  The result should be 0xFF.

The fix can be accomplished several ways.

Fix #1

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

or 

Fix #2

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


These fixes as documented in the original source
require components to be 4-8 bits in size.

EXAMPLE PROGRAM

The attached tar.gz file contains a sample program to
demonstrate the library function vs. one of the fix
#2.

Greg Smith
gpsmith_us at yahoo.com

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SDL_getRGBfix.tar.gz
Type: application/x-gzip-compressed
Size: 1707 bytes
Desc: SDL_getRGBfix.tar.gz
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20021212/15ab6209/attachment.bin>


More information about the SDL mailing list