5131 |
clevermous |
1 |
|
|
|
2 |
> |
|
|
3 |
> |
|
|
4 |
>SDL_PixelFormat |
|
|
5 |
> |
|
|
6 |
NAME="GENERATOR" |
|
|
7 |
CONTENT="Modular DocBook HTML Stylesheet Version 1.64 |
|
|
8 |
"> |
|
|
9 |
REL="HOME" |
|
|
10 |
TITLE="SDL Library Documentation" |
|
|
11 |
HREF="index.html"> |
|
|
12 |
REL="UP" |
|
|
13 |
TITLE="Video" |
|
|
14 |
HREF="video.html"> |
|
|
15 |
REL="PREVIOUS" |
|
|
16 |
TITLE="SDL_Palette" |
|
|
17 |
HREF="sdlpalette.html"> |
|
|
18 |
REL="NEXT" |
|
|
19 |
TITLE="SDL_Surface" |
|
|
20 |
HREF="sdlsurface.html"> |
|
|
21 |
> |
|
|
22 |
CLASS="REFENTRY" |
|
|
23 |
BGCOLOR="#FFF8DC" |
|
|
24 |
TEXT="#000000" |
|
|
25 |
LINK="#0000ee" |
|
|
26 |
VLINK="#551a8b" |
|
|
27 |
ALINK="#ff0000" |
|
|
28 |
> |
|
|
29 |
CLASS="NAVHEADER" |
|
|
30 |
>
|
|
31 |
WIDTH="100%" |
|
|
32 |
BORDER="0" |
|
|
33 |
CELLPADDING="0" |
|
|
34 |
CELLSPACING="0" |
|
|
35 |
>
|
|
36 |
> | |
|
|
37 |
COLSPAN="3" |
|
|
38 |
ALIGN="center" |
|
|
39 |
>SDL Library Documentation |
|
|
40 |
> | |
|
|
41 |
>
|
|
42 |
> |
|
|
|
43 |
WIDTH="10%" |
|
|
44 |
ALIGN="left" |
|
|
45 |
VALIGN="bottom" |
|
|
46 |
> |
|
|
47 |
HREF="sdlpalette.html" |
|
|
48 |
>Prev |
|
|
49 |
> |
|
|
50 |
> |
|
|
|
51 |
WIDTH="80%" |
|
|
52 |
ALIGN="center" |
|
|
53 |
VALIGN="bottom" |
|
|
54 |
> |
|
|
55 |
> |
|
|
|
56 |
WIDTH="10%" |
|
|
57 |
ALIGN="right" |
|
|
58 |
VALIGN="bottom" |
|
|
59 |
> |
|
|
60 |
HREF="sdlsurface.html" |
|
|
61 |
>Next |
|
|
62 |
> |
|
|
63 |
> | |
|
|
64 |
> | |
|
|
65 |
>
|
|
|
66 |
ALIGN="LEFT" |
|
|
67 |
WIDTH="100%"> |
|
|
68 |
> |
|
|
69 |
> |
|
|
70 |
NAME="SDLPIXELFORMAT" |
|
|
71 |
>SDL_PixelFormat |
|
|
72 |
> |
|
|
73 |
> |
|
|
74 |
CLASS="REFNAMEDIV" |
|
|
75 |
> |
|
|
76 |
NAME="AEN2756" |
|
|
77 |
> |
|
|
78 |
> |
|
|
79 |
>Name |
|
|
80 |
>SDL_PixelFormat -- Stores surface format information |
|
|
81 |
> |
|
|
82 |
CLASS="REFSECT1" |
|
|
83 |
> |
|
|
84 |
NAME="AEN2759" |
|
|
85 |
> |
|
|
86 |
> |
|
|
87 |
>Structure Definition |
|
|
88 |
> |
|
|
89 |
CLASS="PROGRAMLISTING" |
|
|
90 |
>typedef struct{ |
|
|
91 |
SDL_Palette *palette; |
|
|
92 |
Uint8 BitsPerPixel; |
|
|
93 |
Uint8 BytesPerPixel; |
|
|
94 |
Uint32 Rmask, Gmask, Bmask, Amask; |
|
|
95 |
Uint8 Rshift, Gshift, Bshift, Ashift; |
|
|
96 |
Uint8 Rloss, Gloss, Bloss, Aloss; |
|
|
97 |
Uint32 colorkey; |
|
|
98 |
Uint8 alpha; |
|
|
99 |
} SDL_PixelFormat; |
|
|
100 |
> |
|
|
101 |
> |
|
|
102 |
CLASS="REFSECT1" |
|
|
103 |
> |
|
|
104 |
NAME="AEN2762" |
|
|
105 |
> |
|
|
106 |
> |
|
|
107 |
>Structure Data |
|
|
108 |
> |
|
|
109 |
CLASS="INFORMALTABLE" |
|
|
110 |
> |
|
|
111 |
NAME="AEN2764" |
|
|
112 |
> |
|
|
113 |
> |
|
|
114 |
> |
|
|
115 |
> |
|
|
254 |
> |
|
|
255 |
> |
|
|
256 |
> |
|
|
257 |
> |
|
|
258 |
> |
|
|
259 |
CLASS="REFSECT1" |
|
|
260 |
> |
|
|
261 |
NAME="AEN2803" |
|
|
262 |
> |
|
|
263 |
> |
|
|
264 |
>Description |
|
|
265 |
> |
|
|
266 |
>A |
|
|
267 |
CLASS="STRUCTNAME" |
|
|
268 |
>SDL_PixelFormat |
|
|
269 |
> describes the format of the pixel data stored at the |
|
|
270 |
CLASS="STRUCTFIELD" |
|
|
271 |
> |
|
|
272 |
>pixels |
|
|
273 |
> |
|
|
274 |
> field of a |
|
|
275 |
HREF="sdlsurface.html" |
|
|
276 |
> |
|
|
277 |
CLASS="STRUCTNAME" |
|
|
278 |
>SDL_Surface |
|
|
279 |
> |
|
|
280 |
>. Every surface stores a |
|
|
281 |
CLASS="STRUCTNAME" |
|
|
282 |
>SDL_PixelFormat |
|
|
283 |
> in the |
|
|
284 |
CLASS="STRUCTFIELD" |
|
|
285 |
> |
|
|
286 |
>format |
|
|
287 |
> |
|
|
288 |
> field. |
|
|
289 |
> |
|
|
290 |
>If you wish to do pixel level modifications on a surface, then understanding how SDL stores its color information is essential. |
|
|
291 |
> |
|
|
292 |
>8-bit pixel formats are the easiest to understand. Since its an 8-bit format, we have 8 |
|
|
293 |
CLASS="STRUCTFIELD" |
|
|
294 |
> |
|
|
295 |
>BitsPerPixel |
|
|
296 |
> |
|
|
297 |
> and 1 |
|
|
298 |
CLASS="STRUCTFIELD" |
|
|
299 |
> |
|
|
300 |
>BytesPerPixel |
|
|
301 |
> |
|
|
302 |
>. Since |
|
|
303 |
CLASS="STRUCTFIELD" |
|
|
304 |
> |
|
|
305 |
>BytesPerPixel |
|
|
306 |
> |
|
|
307 |
> is 1, all pixels are represented by a Uint8 which contains an index into |
|
|
308 |
CLASS="STRUCTFIELD" |
|
|
309 |
> |
|
|
310 |
>palette |
|
|
311 |
> |
|
|
312 |
>-> |
|
|
313 |
CLASS="STRUCTFIELD" |
|
|
314 |
> |
|
|
315 |
>colors |
|
|
316 |
> |
|
|
317 |
>. So, to determine the color of a pixel in a 8-bit surface: we read the color index from |
|
|
318 |
CLASS="STRUCTNAME" |
|
|
319 |
>surface |
|
|
320 |
>-> |
|
|
321 |
CLASS="STRUCTFIELD" |
|
|
322 |
> |
|
|
323 |
>pixels |
|
|
324 |
> |
|
|
325 |
> and we use that index to read the |
|
|
326 |
HREF="sdlcolor.html" |
|
|
327 |
> |
|
|
328 |
CLASS="STRUCTNAME" |
|
|
329 |
>SDL_Color |
|
|
330 |
> |
|
|
331 |
> structure from |
|
|
332 |
CLASS="STRUCTNAME" |
|
|
333 |
>surface |
|
|
334 |
>-> |
|
|
335 |
CLASS="STRUCTFIELD" |
|
|
336 |
> |
|
|
337 |
>format |
|
|
338 |
> |
|
|
339 |
>-> |
|
|
340 |
CLASS="STRUCTFIELD" |
|
|
341 |
> |
|
|
342 |
>palette |
|
|
343 |
> |
|
|
344 |
>-> |
|
|
345 |
CLASS="STRUCTFIELD" |
|
|
346 |
> |
|
|
347 |
>colors |
|
|
348 |
> |
|
|
349 |
>. Like so: |
|
|
350 |
|
|
|
351 |
CLASS="PROGRAMLISTING" |
|
|
352 |
>SDL_Surface *surface; |
|
|
353 |
SDL_PixelFormat *fmt; |
|
|
354 |
SDL_Color *color; |
|
|
355 |
Uint8 index; |
|
|
356 |
|
|
|
357 |
. |
|
|
358 |
. |
|
|
359 |
|
|
|
360 |
/* Create surface */ |
|
|
361 |
. |
|
|
362 |
. |
|
|
363 |
fmt=surface->format; |
|
|
364 |
|
|
|
365 |
/* Check the bitdepth of the surface */ |
|
|
366 |
if(fmt->BitsPerPixel!=8){ |
|
|
367 |
fprintf(stderr, "Not an 8-bit surface.\n"); |
|
|
368 |
return(-1); |
|
|
369 |
} |
|
|
370 |
|
|
|
371 |
/* Lock the surface */ |
|
|
372 |
SDL_LockSurface(surface); |
|
|
373 |
|
|
|
374 |
/* Get the topleft pixel */ |
|
|
375 |
index=*(Uint8 *)surface->pixels; |
|
|
376 |
color=fmt->palette->colors[index]; |
|
|
377 |
|
|
|
378 |
/* Unlock the surface */ |
|
|
379 |
SDL_UnlockSurface(surface); |
|
|
380 |
printf("Pixel Color-> Red: %d, Green: %d, Blue: %d. Index: %d\n", |
|
|
381 |
color->r, color->g, color->b, index); |
|
|
382 |
. |
|
|
383 |
. |
|
|
384 |
> |
|
|
385 |
> |
|
|
386 |
>Pixel formats above 8-bit are an entirely different experience. They are |
|
|
387 |
considered to be "TrueColor" formats and the color information is stored in the |
|
|
388 |
pixels themselves, not in a palette. The mask, shift and loss fields tell us |
|
|
389 |
how the color information is encoded. The mask fields allow us to isolate each |
|
|
390 |
color component, the shift fields tell us the number of bits to the right of |
|
|
391 |
each component in the pixel value and the loss fields tell us the number of |
|
|
392 |
bits lost from each component when packing 8-bit color component in a pixel. |
|
|
393 |
|
|
|
394 |
CLASS="PROGRAMLISTING" |
|
|
395 |
>/* Extracting color components from a 32-bit color value */ |
|
|
396 |
SDL_PixelFormat *fmt; |
|
|
397 |
SDL_Surface *surface; |
|
|
398 |
Uint32 temp, pixel; |
|
|
399 |
Uint8 red, green, blue, alpha; |
|
|
400 |
. |
|
|
401 |
. |
|
|
402 |
. |
|
|
403 |
fmt=surface->format; |
|
|
404 |
SDL_LockSurface(surface); |
|
|
405 |
pixel=*((Uint32*)surface->pixels); |
|
|
406 |
SDL_UnlockSurface(surface); |
|
|
407 |
|
|
|
408 |
/* Get Red component */ |
|
|
409 |
temp=pixel&fmt->Rmask; /* Isolate red component */ |
|
|
410 |
temp=temp>>fmt->Rshift;/* Shift it down to 8-bit */ |
|
|
411 |
temp=temp<<fmt->Rloss; /* Expand to a full 8-bit number */ |
|
|
412 |
red=(Uint8)temp; |
|
|
413 |
|
|
|
414 |
/* Get Green component */ |
|
|
415 |
temp=pixel&fmt->Gmask; /* Isolate green component */ |
|
|
416 |
temp=temp>>fmt->Gshift;/* Shift it down to 8-bit */ |
|
|
417 |
temp=temp<<fmt->Gloss; /* Expand to a full 8-bit number */ |
|
|
418 |
green=(Uint8)temp; |
|
|
419 |
|
|
|
420 |
/* Get Blue component */ |
|
|
421 |
temp=pixel&fmt->Bmask; /* Isolate blue component */ |
|
|
422 |
temp=temp>>fmt->Bshift;/* Shift it down to 8-bit */ |
|
|
423 |
temp=temp<<fmt->Bloss; /* Expand to a full 8-bit number */ |
|
|
424 |
blue=(Uint8)temp; |
|
|
425 |
|
|
|
426 |
/* Get Alpha component */ |
|
|
427 |
temp=pixel&fmt->Amask; /* Isolate alpha component */ |
|
|
428 |
temp=temp>>fmt->Ashift;/* Shift it down to 8-bit */ |
|
|
429 |
temp=temp<<fmt->Aloss; /* Expand to a full 8-bit number */ |
|
|
430 |
alpha=(Uint8)temp; |
|
|
431 |
|
|
|
432 |
printf("Pixel Color -> R: %d, G: %d, B: %d, A: %d\n", red, green, blue, alpha); |
|
|
433 |
. |
|
|
434 |
. |
|
|
435 |
. |
|
|
436 |
> |
|
|
437 |
> |
|
|
438 |
> |
|
|
439 |
CLASS="REFSECT1" |
|
|
440 |
> |
|
|
441 |
NAME="AEN2830" |
|
|
442 |
> |
|
|
443 |
> |
|
|
444 |
>See Also |
|
|
445 |
> |
|
|
446 |
> |
|
|
447 |
HREF="sdlsurface.html" |
|
|
448 |
> |
|
|
449 |
CLASS="STRUCTNAME" |
|
|
450 |
>SDL_Surface |
|
|
451 |
> |
|
|
452 |
>, |
|
|
453 |
|
|
|
454 |
HREF="sdlmaprgb.html" |
|
|
455 |
> |
|
|
456 |
CLASS="FUNCTION" |
|
|
457 |
>SDL_MapRGB |
|
|
458 |
> |
|
|
459 |
> |
|
|
460 |
> |
|
|
461 |
> |
|
|
462 |
CLASS="NAVFOOTER" |
|
|
463 |
>
|
|
|
464 |
ALIGN="LEFT" |
|
|
465 |
WIDTH="100%"> |
|
|
517 |
> |
|
|
518 |
> |
|
|
519 |
> |
|
|
520 |
> |