Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
>
116
BORDER="0"
117
CLASS="CALSTABLE"
118
>
119
>
120
>
121
ALIGN="LEFT"
122
VALIGN="TOP"
123
>
124
CLASS="STRUCTFIELD"
125
>
126
>palette
127
>
128
>
129
>
130
ALIGN="LEFT"
131
VALIGN="TOP"
132
>Pointer to the 
133
HREF="sdlpalette.html"
134
>palette
135
>, or 
136
CLASS="LITERAL"
137
>NULL
138
> if the 
139
CLASS="STRUCTFIELD"
140
>
141
>BitsPerPixel
142
>
143
>>8
144
>
145
>
146
>
147
ALIGN="LEFT"
148
VALIGN="TOP"
149
>
150
CLASS="STRUCTFIELD"
151
>
152
>BitsPerPixel
153
>
154
>
155
>
156
ALIGN="LEFT"
157
VALIGN="TOP"
158
>The number of bits used to represent each pixel in a surface. Usually 8, 16, 24 or 32.
159
>
160
>
161
>
162
ALIGN="LEFT"
163
VALIGN="TOP"
164
>
165
CLASS="STRUCTFIELD"
166
>
167
>BytesPerPixel
168
>
169
>
170
>
171
ALIGN="LEFT"
172
VALIGN="TOP"
173
>The number of bytes used to represent each pixel in a surface. Usually one to four.
174
>
175
>
176
>
177
ALIGN="LEFT"
178
VALIGN="TOP"
179
>
180
CLASS="STRUCTFIELD"
181
>
182
>[RGBA]mask
183
>
184
>
185
>
186
ALIGN="LEFT"
187
VALIGN="TOP"
188
>Binary mask used to retrieve individual color values
189
>
190
>
191
>
192
ALIGN="LEFT"
193
VALIGN="TOP"
194
>
195
CLASS="STRUCTFIELD"
196
>
197
>[RGBA]loss
198
>
199
>
200
>
201
ALIGN="LEFT"
202
VALIGN="TOP"
203
>Precision loss of each color component (2
204
>[RGBA]loss
205
>)
206
>
207
>
208
>
209
ALIGN="LEFT"
210
VALIGN="TOP"
211
>
212
CLASS="STRUCTFIELD"
213
>
214
>[RGBA]shift
215
>
216
>
217
>
218
ALIGN="LEFT"
219
VALIGN="TOP"
220
>Binary left shift of each color component in the pixel value
221
>
222
>
223
>
224
ALIGN="LEFT"
225
VALIGN="TOP"
226
>
227
CLASS="STRUCTFIELD"
228
>
229
>colorkey
230
>
231
>
232
>
233
ALIGN="LEFT"
234
VALIGN="TOP"
235
>Pixel value of transparent pixels
236
>
237
>
238
>
239
ALIGN="LEFT"
240
VALIGN="TOP"
241
>
242
CLASS="STRUCTFIELD"
243
>
244
>alpha
245
>
246
>
247
>
248
ALIGN="LEFT"
249
VALIGN="TOP"
250
>Overall surface alpha value
251
>
252
>
253
>
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%">
466
WIDTH="100%"
467
BORDER="0"
468
CELLPADDING="0"
469
CELLSPACING="0"
470
>
471
>
472
WIDTH="33%"
473
ALIGN="left"
474
VALIGN="top"
475
>
476
HREF="sdlpalette.html"
477
>Prev
478
>
479
>
480
WIDTH="34%"
481
ALIGN="center"
482
VALIGN="top"
483
>
484
HREF="index.html"
485
>Home
486
>
487
>
488
WIDTH="33%"
489
ALIGN="right"
490
VALIGN="top"
491
>
492
HREF="sdlsurface.html"
493
>Next
494
>
495
>
496
>
497
>
498
WIDTH="33%"
499
ALIGN="left"
500
VALIGN="top"
501
>SDL_Palette
502
>
503
WIDTH="34%"
504
ALIGN="center"
505
VALIGN="top"
506
>
507
HREF="video.html"
508
>Up
509
>
510
>
511
WIDTH="33%"
512
ALIGN="right"
513
VALIGN="top"
514
>SDL_Surface
515
>
516
>
517
>
518
>
519
>
520
>