4 >SDL_PixelFormat</TITLE
7 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
10 TITLE="SDL Library Documentation"
11 HREF="index.html"><LINK
14 HREF="video.html"><LINK
17 HREF="sdlpalette.html"><LINK
20 HREF="sdlsurface.html"></HEAD
31 SUMMARY="Header navigation table"
40 >SDL Library Documentation</TH
48 HREF="sdlpalette.html"
62 HREF="sdlsurface.html"
83 >SDL_PixelFormat -- Stores surface format information</DIV
90 >Structure Definition</H2
92 CLASS="PROGRAMLISTING"
93 >typedef struct SDL_PixelFormat {
97 Uint8 Rloss, Gloss, Bloss, Aloss;
98 Uint8 Rshift, Gshift, Bshift, Ashift;
99 Uint32 Rmask, Gmask, Bmask, Amask;
102 } SDL_PixelFormat;</PRE
112 CLASS="INFORMALTABLE"
136 HREF="sdlpalette.html"
161 >The number of bits used to represent each pixel in a surface. Usually 8, 16, 24 or 32.</TD
176 >The number of bytes used to represent each pixel in a surface. Usually one to four.</TD
191 >Binary mask used to retrieve individual color values</TD
206 >Precision loss of each color component (2<SUP
223 >Binary left shift of each color component in the pixel value</TD
238 >Pixel value of transparent pixels</TD
253 >Overall surface alpha value</TD
271 >SDL_PixelFormat</SPAN
272 > describes the format of the pixel data stored at the <TT
278 HREF="sdlsurface.html"
283 >. Every surface stores a <SPAN
285 >SDL_PixelFormat</SPAN
293 >If you wish to do pixel level modifications on a surface, then understanding how SDL stores its color information is essential.</P
295 >8-bit pixel formats are the easiest to understand. Since its an 8-bit format, we have 8 <TT
310 > is 1, all pixels are represented by a Uint8 which contains an index into <TT
320 >. So, to determine the color of a pixel in a 8-bit surface: we read the color index from <SPAN
328 > and we use that index to read the <A
334 > structure from <SPAN
354 CLASS="PROGRAMLISTING"
355 >SDL_Surface *surface;
356 SDL_PixelFormat *fmt;
366 fmt=surface->format;
368 /* Check the bitdepth of the surface */
369 if(fmt->BitsPerPixel!=8){
370 fprintf(stderr, "Not an 8-bit surface.\n");
374 /* Lock the surface */
375 SDL_LockSurface(surface);
377 /* Get the topleft pixel */
378 index=*(Uint8 *)surface->pixels;
379 color=fmt->palette->colors[index];
381 /* Unlock the surface */
382 SDL_UnlockSurface(surface);
383 printf("Pixel Color-> Red: %d, Green: %d, Blue: %d. Index: %d\n",
384 color->r, color->g, color->b, index);
389 >Pixel formats above 8-bit are an entirely different experience. They are
390 considered to be "TrueColor" formats and the color information is stored in the
391 pixels themselves, not in a palette. The mask, shift and loss fields tell us
392 how the color information is encoded. The mask fields allow us to isolate each
393 color component, the shift fields tell us the number of bits to the right of
394 each component in the pixel value and the loss fields tell us the number of
395 bits lost from each component when packing 8-bit color component in a pixel.
397 CLASS="PROGRAMLISTING"
398 >/* Extracting color components from a 32-bit color value */
399 SDL_PixelFormat *fmt;
400 SDL_Surface *surface;
402 Uint8 red, green, blue, alpha;
406 fmt=surface->format;
407 SDL_LockSurface(surface);
408 pixel=*((Uint32*)surface->pixels);
409 SDL_UnlockSurface(surface);
411 /* Get Red component */
412 temp=pixel&fmt->Rmask; /* Isolate red component */
413 temp=temp>>fmt->Rshift;/* Shift it down to 8-bit */
414 temp=temp<<fmt->Rloss; /* Expand to a full 8-bit number */
417 /* Get Green component */
418 temp=pixel&fmt->Gmask; /* Isolate green component */
419 temp=temp>>fmt->Gshift;/* Shift it down to 8-bit */
420 temp=temp<<fmt->Gloss; /* Expand to a full 8-bit number */
423 /* Get Blue component */
424 temp=pixel&fmt->Bmask; /* Isolate blue component */
425 temp=temp>>fmt->Bshift;/* Shift it down to 8-bit */
426 temp=temp<<fmt->Bloss; /* Expand to a full 8-bit number */
429 /* Get Alpha component */
430 temp=pixel&fmt->Amask; /* Isolate alpha component */
431 temp=temp>>fmt->Ashift;/* Shift it down to 8-bit */
432 temp=temp<<fmt->Aloss; /* Expand to a full 8-bit number */
435 printf("Pixel Color -> R: %d, G: %d, B: %d, A: %d\n", red, green, blue, alpha);
450 HREF="sdlsurface.html"
457 HREF="sdlmaprgb.html"
469 SUMMARY="Footer navigation table"
480 HREF="sdlpalette.html"
498 HREF="sdlsurface.html"