[SDL] Dealing with colors...

Mattias Engdegård f91-men at nada.kth.se
Tue Aug 29 07:19:56 PDT 2000


>1) in 8 bbp, does the SDL_MapRGB() searches for an entry into the palette
>similar to the requested color and returns its color number into the
>palette? Writing a number of a color into a pixel seems infact to work...

Yes, this is how it's supposed to work

>2) in 16bpp mode, shouldn't the three components be respectively 5, 6 and 5
>bits wide? if so, does SDL make a conversion, translating them in 8bit
>values? I tried to write 255, 255, 255 to obtain white, and I got, but the
>actual colors written (or, at least, the ones returnet by SDL_GetRGB() )
>were 248, 252, 248( printing the color Uint32 value, i gor 65535, and this
>is right...). How does all this work?

SDL_GetRGB() returns the values rescaled to 8 bits/component. SDL <= 1.1.4
just returns the bit fields shifted left the appropriate amount (which
is why you get (248,252,248) as white). This has been fixed in SDL CVS,
where it does slightly better rescaling (so SDL_GetRGB will return
16-bit white as (255, 255, 255)).
(Martin, how about clarifying this in the docs?)

>3) In a 16bit file, aren't the components stored using 5, 6 and 5 bits for
>the 3 components? How should I deal with is, building my own loading
>functions?

Few file formats support 16 bit pixel formats. BMP and TGA do, and
SDL/SDL_image can load those (but SDL_SaveBMP won't save anything in
that format). Most people use a 24-bit file format instead (PNG, TGA,
JPEG, BMP, PPM, PCX, ...), but if you make your own file format you have
to write your own loader of course. It is usually not worth the trouble.

>4) What are RGB shift, mask and loss, into the format structure, used for?

"mask" is the bitmask for that particular bit field (ones covering the
bit field, zeroes elsewhere). "shift" is number of bits to the right
of the bit field. "loss" is 8 - (bit field size in bits).

Example: RGB565:
Rmask = 0b1111100000000000 = 0xf800, Rshift = 11, Rloss = 3
Gmask = 0b0000011111100000 = 0x07e0, Gshift =  5, Gloss = 2
Bmask = 0b0000000000011111 = 0x001f, Bshift =  0, Bloss = 3

They are of course used for the pixel value <-> RGB values conversion.

>5) Finally, how are the various modes managed? I mean: determining the
>color, the pitch, how the various bits are packet into one ore more bytes
>(i.e: 12bpp mode, 15 bpp mode...), conversions during blits, etc...

Formats that are fully supported are:
8bpp indexed
15, 16bpp (555, 565)
24bpp (not very fast)
32bpp (with or without alpha channel)

Semi-supported (i.e. may work but usually not optimised)
1bpp bitmaps
odd bit field divisions (like ARGB4444 in 16bpp)

>Thanx a lot...  (the docs don't contain anything about this stuff, and
>making experiments and watching code examples is not enough.....)

Then you haven't done enough experiments, and haven't read enough code.
Both SDL's source and other people's code could be a guidance.

But ask away if you have any questions. That's what this list is for. :)




More information about the SDL mailing list