SDL_PixelFormat

Name

SDL_PixelFormat -- Stores surface format information

Structure Definition

   1 typedef struct {
   2   SDL_Palette *palette;
   3   Uint8  BitsPerPixel;
   4   Uint8  BytesPerPixel;
   5   Uint8  Rloss, Gloss, Bloss, Aloss;
   6   Uint8  Rshift, Gshift, Bshift, Ashift;
   7   Uint32 Rmask, Gmask, Bmask, Amask;
   8   Uint32 colorkey;
   9   Uint8  alpha;
  10 } SDL_PixelFormat;

Structure Data

palette

Pointer to the palette, or NULL if the BitsPerPixel>8

BitsPerPixel

The number of bits used to represent each pixel in a surface. Usually 8, 16, 24 or 32. (1 to 7 are not allowed when creating a surface or open a video mode)

BytesPerPixel

The number of bytes used to represent each pixel in a surface. Usually one to four.

[RGBA]mask

Binary mask used to retrieve individual color values

[RGBA]loss

Precision loss of each color component (2[RGBA]loss)

[RGBA]shift

Binary left shift of each color component in the pixel value

colorkey

Pixel value of transparent pixels

alpha

Overall surface alpha value

Description

A SDL_PixelFormat describes the format of the pixel data stored at the pixels field of a SDL_Surface. Every surface stores a SDL_PixelFormat in the format field.

If you wish to do pixel level modifications on a surface, then understanding how SDL stores its color information is essential.

8-bit pixel formats are the easiest to understand. Since its an 8-bit format, we have 8 BitsPerPixel and 1 BytesPerPixel. Since BytesPerPixel is 1, all pixels are represented by a Uint8 which contains an index into palette->colors. So, to determine the color of a pixel in a 8-bit surface: we read the color index from surface->pixels and we use that index to read the SDL_Color structure from surface->format->palette->colors. Like so:

   1 SDL_Surface *surface;
   2 SDL_PixelFormat *fmt;
   3 SDL_Color *color;
   4 Uint8 index;
   5 
   6 .
   7 .
   8 
   9 /* Create surface */
  10 .
  11 .
  12 fmt=surface->format;
  13 
  14 /* Check the bitdepth of the surface */
  15 if(fmt->BitsPerPixel!=8){
  16   fprintf(stderr, "Not an 8-bit surface.\n");
  17   return(-1);
  18 }
  19 
  20 /* Lock the surface */
  21 SDL_LockSurface(surface);
  22 
  23 /* Get the topleft pixel */
  24 index=*(Uint8 *)surface->pixels;
  25 color=fmt->palette->colors[index];
  26 
  27 /* Unlock the surface */
  28 SDL_UnlockSurface(surface);
  29 printf("Pixel Color-> Red: %d, Green: %d, Blue: %d. Index: %d\n",
  30           color->r, color->g, color->b, index);
  31 .
  32 .

Pixel formats above 8-bit are an entirely different experience. They are considered to be "TrueColor" formats and the color information is stored in the pixels themselves, not in a palette. The mask, shift and loss fields tell us how the color information is encoded. The mask fields allow us to isolate each color component, the shift fields tell us the number of bits to the right of each component in the pixel value and the loss fields tell us the number of bits lost from each component when packing 8-bit color component in a pixel.

   1 /* Extracting color components from a 32-bit color value */
   2 SDL_PixelFormat *fmt;
   3 SDL_Surface *surface;
   4 Uint32 temp, pixel;
   5 Uint8 red, green, blue, alpha;
   6 .
   7 .
   8 .
   9 fmt = surface->format;
  10 SDL_LockSurface(surface);
  11 pixel = *((Uint32*)surface->pixels);
  12 SDL_UnlockSurface(surface);
  13 
  14 /* Get Red component */
  15 temp = pixel & fmt->Rmask;  /* Isolate red component */
  16 temp = temp >> fmt->Rshift; /* Shift it down to 8-bit */
  17 temp = temp << fmt->Rloss;  /* Expand to a full 8-bit number */
  18 red = (Uint8)temp;
  19 
  20 /* Get Green component */
  21 temp = pixel & fmt->Gmask;  /* Isolate green component */
  22 temp = temp >> fmt->Gshift; /* Shift it down to 8-bit */
  23 temp = temp << fmt->Gloss;  /* Expand to a full 8-bit number */
  24 green = (Uint8)temp;
  25 
  26 /* Get Blue component */
  27 temp = pixel & fmt->Bmask;  /* Isolate blue component */
  28 temp = temp >> fmt->Bshift; /* Shift it down to 8-bit */
  29 temp = temp << fmt->Bloss;  /* Expand to a full 8-bit number */
  30 blue = (Uint8)temp;
  31 
  32 /* Get Alpha component */
  33 temp = pixel & fmt->Amask;  /* Isolate alpha component */
  34 temp = temp >> fmt->Ashift; /* Shift it down to 8-bit */
  35 temp = temp << fmt->Aloss;  /* Expand to a full 8-bit number */
  36 alpha = (Uint8)temp;
  37 
  38 printf("Pixel Color -> R: %d,  G: %d,  B: %d,  A: %d\n", red, green, blue, alpha);
  39 .
  40 .
  41 .

See Also

SDL_Surface, SDL_MapRGB, SDL_Palette

SDL_PixelFormat (last edited 2008-10-06 09:03:11 by cust)