docs/man3/SDL_PixelFormat.3
author Sam Lantinga <slouken@libsdl.org>
Wed, 08 Mar 2006 18:30:12 +0000
changeset 1486 9d77fc9d0ace
parent 1279 e867f327aa54
child 2283 546f7c1eb755
permissions -rw-r--r--
Re-added C runtime support for Visual C++ 5,6
slouken@181
     1
.TH "SDL_PixelFormat" "3" "Tue 11 Sep 2001, 23:01" "SDL" "SDL API Reference" 
slouken@0
     2
.SH "NAME"
slouken@0
     3
SDL_PixelFormat\- Stores surface format information
slouken@0
     4
.SH "STRUCTURE DEFINITION"
slouken@0
     5
.PP
slouken@0
     6
.nf
slouken@1279
     7
\f(CWtypedef struct SDL_PixelFormat {
slouken@0
     8
  SDL_Palette *palette;
slouken@0
     9
  Uint8  BitsPerPixel;
slouken@0
    10
  Uint8  BytesPerPixel;
slouken@1279
    11
  Uint8  Rloss, Gloss, Bloss, Aloss;
slouken@1279
    12
  Uint8  Rshift, Gshift, Bshift, Ashift;
slouken@0
    13
  Uint32 Rmask, Gmask, Bmask, Amask;
slouken@0
    14
  Uint32 colorkey;
slouken@0
    15
  Uint8  alpha;
slouken@0
    16
} SDL_PixelFormat;\fR
slouken@0
    17
.fi
slouken@0
    18
.PP
slouken@0
    19
.SH "STRUCTURE DATA"
slouken@0
    20
.TP 20
slouken@0
    21
\fBpalette\fR
slouken@0
    22
Pointer to the \fIpalette\fR, or \fBNULL\fP if the \fBBitsPerPixel\fR>8
slouken@0
    23
.TP 20
slouken@0
    24
\fBBitsPerPixel\fR
slouken@0
    25
The number of bits used to represent each pixel in a surface\&. Usually 8, 16, 24 or 32\&.
slouken@0
    26
.TP 20
slouken@0
    27
\fBBytesPerPixel\fR
slouken@0
    28
The number of bytes used to represent each pixel in a surface\&. Usually one to four\&.
slouken@0
    29
.TP 20
slouken@0
    30
\fB[RGBA]mask\fR
slouken@0
    31
Binary mask used to retrieve individual color values
slouken@0
    32
.TP 20
slouken@0
    33
\fB[RGBA]loss\fR
slouken@0
    34
Precision loss of each color component (2^[RGBA]loss)
slouken@0
    35
.TP 20
slouken@0
    36
\fB[RGBA]shift\fR
slouken@0
    37
Binary left shift of each color component in the pixel value
slouken@0
    38
.TP 20
slouken@0
    39
\fBcolorkey\fR
slouken@0
    40
Pixel value of transparent pixels
slouken@0
    41
.TP 20
slouken@0
    42
\fBalpha\fR
slouken@0
    43
Overall surface alpha value
slouken@0
    44
.SH "DESCRIPTION"
slouken@0
    45
.PP
slouken@0
    46
A \fBSDL_PixelFormat\fR describes the format of the pixel data stored at the \fBpixels\fR field of a \fI\fBSDL_Surface\fR\fR\&. Every surface stores a \fBSDL_PixelFormat\fR in the \fBformat\fR field\&.
slouken@0
    47
.PP
slouken@0
    48
If you wish to do pixel level modifications on a surface, then understanding how SDL stores its color information is essential\&.
slouken@0
    49
.PP
slouken@0
    50
8-bit pixel formats are the easiest to understand\&. Since its an 8-bit format, we have 8 \fBBitsPerPixel\fR and 1 \fBBytesPerPixel\fR\&. Since \fBBytesPerPixel\fR is 1, all pixels are represented by a Uint8 which contains an index into \fBpalette\fR->\fBcolors\fR\&. So, to determine the color of a pixel in a 8-bit surface: we read the color index from \fBsurface\fR->\fBpixels\fR and we use that index to read the \fI\fBSDL_Color\fR\fR structure from \fBsurface\fR->\fBformat\fR->\fBpalette\fR->\fBcolors\fR\&. Like so: 
slouken@0
    51
.PP
slouken@0
    52
.nf
slouken@0
    53
\f(CWSDL_Surface *surface;
slouken@0
    54
SDL_PixelFormat *fmt;
slouken@0
    55
SDL_Color *color;
slouken@0
    56
Uint8 index;
slouken@0
    57
slouken@0
    58
\&.
slouken@0
    59
\&.
slouken@0
    60
slouken@0
    61
/* Create surface */
slouken@0
    62
\&.
slouken@0
    63
\&.
slouken@0
    64
fmt=surface->format;
slouken@0
    65
slouken@0
    66
/* Check the bitdepth of the surface */
slouken@0
    67
if(fmt->BitsPerPixel!=8){
slouken@0
    68
  fprintf(stderr, "Not an 8-bit surface\&.
slouken@0
    69
");
slouken@0
    70
  return(-1);
slouken@0
    71
}
slouken@0
    72
slouken@0
    73
/* Lock the surface */
slouken@0
    74
SDL_LockSurface(surface);
slouken@0
    75
slouken@0
    76
/* Get the topleft pixel */
slouken@0
    77
index=*(Uint8 *)surface->pixels;
slouken@0
    78
color=fmt->palette->colors[index];
slouken@0
    79
slouken@0
    80
/* Unlock the surface */
slouken@0
    81
SDL_UnlockSurface(surface);
slouken@0
    82
printf("Pixel Color-> Red: %d, Green: %d, Blue: %d\&. Index: %d
slouken@0
    83
",
slouken@0
    84
          color->r, color->g, color->b, index);
slouken@0
    85
\&.
slouken@0
    86
\&.\fR
slouken@0
    87
.fi
slouken@0
    88
.PP
slouken@0
    89
.PP
slouken@55
    90
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\&. 
slouken@0
    91
.PP
slouken@0
    92
.nf
slouken@0
    93
\f(CW/* Extracting color components from a 32-bit color value */
slouken@0
    94
SDL_PixelFormat *fmt;
slouken@0
    95
SDL_Surface *surface;
slouken@0
    96
Uint32 temp, pixel;
slouken@0
    97
Uint8 red, green, blue, alpha;
slouken@0
    98
\&.
slouken@0
    99
\&.
slouken@0
   100
\&.
slouken@0
   101
fmt=surface->format;
slouken@0
   102
SDL_LockSurface(surface);
slouken@55
   103
pixel=*((Uint32*)surface->pixels);
slouken@0
   104
SDL_UnlockSurface(surface);
slouken@0
   105
slouken@0
   106
/* Get Red component */
slouken@0
   107
temp=pixel&fmt->Rmask; /* Isolate red component */
slouken@0
   108
temp=temp>>fmt->Rshift;/* Shift it down to 8-bit */
slouken@0
   109
temp=temp<<fmt->Rloss; /* Expand to a full 8-bit number */
slouken@0
   110
red=(Uint8)temp;
slouken@0
   111
slouken@0
   112
/* Get Green component */
slouken@0
   113
temp=pixel&fmt->Gmask; /* Isolate green component */
slouken@0
   114
temp=temp>>fmt->Gshift;/* Shift it down to 8-bit */
slouken@0
   115
temp=temp<<fmt->Gloss; /* Expand to a full 8-bit number */
slouken@0
   116
green=(Uint8)temp;
slouken@0
   117
slouken@0
   118
/* Get Blue component */
slouken@0
   119
temp=pixel&fmt->Bmask; /* Isolate blue component */
slouken@0
   120
temp=temp>>fmt->Bshift;/* Shift it down to 8-bit */
slouken@0
   121
temp=temp<<fmt->Bloss; /* Expand to a full 8-bit number */
slouken@0
   122
blue=(Uint8)temp;
slouken@0
   123
slouken@0
   124
/* Get Alpha component */
slouken@0
   125
temp=pixel&fmt->Amask; /* Isolate alpha component */
slouken@0
   126
temp=temp>>fmt->Ashift;/* Shift it down to 8-bit */
slouken@0
   127
temp=temp<<fmt->Aloss; /* Expand to a full 8-bit number */
slouken@0
   128
alpha=(Uint8)temp;
slouken@0
   129
slouken@0
   130
printf("Pixel Color -> R: %d,  G: %d,  B: %d,  A: %d
slouken@0
   131
", red, green, blue, alpha);
slouken@0
   132
\&.
slouken@0
   133
\&.
slouken@0
   134
\&.\fR
slouken@0
   135
.fi
slouken@0
   136
.PP
slouken@0
   137
.SH "SEE ALSO"
slouken@0
   138
.PP
slouken@0
   139
\fI\fBSDL_Surface\fR\fR, \fI\fBSDL_MapRGB\fP\fR
slouken@181
   140
...\" created by instant / docbook-to-man, Tue 11 Sep 2001, 23:01