How to work with 16 bit surfaces?

In the past…all I have ever worked with have been 8 bit displays. Under
DOS, svgalib, and SDL. I have noticed…however the limitations of 256
colors over time. I know there are differences in the way 8 and 16 bit
surfaces work. One of these has been the absence of a palette in a 16 bit
display.

Is a 16 bit surface blitted slower than an 8 bit surface?

How do you do color fading in a 16 bit display? (e.g. fade in…fade
out…)

Also…how do you work with 16 bit displays? ( pretty vague…huh?)

Open display
Set colors
Write to screen…etc.

16 million colors? Non indexed bitmap files?

So…now would I not have to use a common palette in all the images in my
game if I used a 16 bit display?

I mean…with an 8 bit display…when I make images in GIMP…I always have
to
index them to 255 colors. I’ve made my own palette. But when I convert the
image
from RGB to indexed…I always have to dither it or it will look really
bad.
This dithering sucks. Is there another way? 16 bit displays possibly?

Oh well…

I guess that’s plenty of questions…

Paul Lowe
xpaull at ultraviolet.org

Is a 16 bit surface blitted slower than an 8 bit surface?

Yes, unless the real display is already in 16 bit mode.

How do you do color fading in a 16 bit display? (e.g. fade in…fade
out…)

There are several techniques, most of which involve repeatedly modifying
the pixels in the surface directly.
The most popular involves creating a 64K lookup table for each fade step,
and index into it:

... save surface
for step in fade_steps
do for pixel in pixels
   do pixel = fade_lookup[step][original_pixel]
   done
   update screen
done

The same can be done with 32 bit surfaces, but you need to convert the pixels
to 16-bit on the fly, or the lookup table is prohibitively large.

I’ve never actually done this as it is really slow on older systems.
See the screenlib demo library for another way to do a fade.

Gamma fading is the best solution as it directly affects the monitor
display and doesn’t touch the display surface at all. Unfortunately,
it’s not supported on the vast majority of PC hardware.

Also…how do you work with 16 bit displays? ( pretty vague…huh?)

Open display
Set colors
Write to screen…etc.

You open a 16 bit display by setting the bits-per-pixel argument to 16
when calling SDL_SetVideoMode(). If the actual display format (available
through the SDL_GetDisplayFormat() function) is not greater than 8 bit,
SDL will dither to the “best” available selection of palette colors.
This generally looks terrible. :slight_smile:

16 bit displays and 32 bit displays are almost exactly the same, except
one has 16 bit pixels, and the other has 32 bit pixels. Both pack the
color information directly into the pixel.

for example, say you have a pixel with RGB 255,0,255 (purple?) then
the associated 16 bit pixel might be 0xFC3F. The associated 32 bit pixel
might be 0x00FF00FF. The portable way to do this is to use the color
mask and shift information in the pixel format to generate a pixel from
a given RGB value:

/* Assemble R-G-B values into a specified pixel format and store them */
#define PIXEL_FROM_RGB(pixel, fmt, r, g, b)
{
pixel = ((r>>fmt->Rloss)<Rshift)|
((g>>fmt->Gloss)<Gshift)|
((b>>fmt->Bloss)<Bshift);
}

fmt is the PixelFormat member of the SDL_Surface structure.

Note that this doesn’t work with 24-bit surfaces. This is because the
24-bit surface is a little strange in that it encodes the same number
of colors as a 32-bit surface, but encodes them as a red-byte, a green-byte,
and a blue-byte rather than a bitfield.

So…now would I not have to use a common palette in all the images in my
game if I used a 16 bit display?

That’s absolutely correct.

I mean…with an 8 bit display…when I make images in GIMP…I always have
to
index them to 255 colors. I’ve made my own palette. But when I convert the
image
from RGB to indexed…I always have to dither it or it will look really
bad.
This dithering sucks. Is there another way? 16 bit displays possibly?

Yup. If you save your GIMP images as 24-bit BMP files, then when you load
them with SDL, they should look good using a 16-bit display.

Oh well…

I guess that’s plenty of questions…

Feel free to ask more. There is quite a bit of stored knowledge here
on this list. :slight_smile:

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/