Source

pygame / docs / ref / Surface.html

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<html>
<title>Surface</title>
<body bgcolor=#dddddd text=#333377 link=#7777bb vlink=#7777bb>

<table border=0 width=100% cellpadding=0 cellspacing=0 bgcolor=#f5f5f5><tr valign=top>
<td rowspan=2><table border=0 cellpadding=5 cellspacing=0 bgcolor=#333377>
<tr height=86 align=left><td valign=middle><font color=#ffffff size=+5>
	<a href=../index.html><font size=+5 color=#ffffff><i><b>
      pygame</b></i></font></a>&nbsp;&nbsp;</td>
<td valign=middle><tt><font color=#dddddd><br>
	pygame<br>documentation</font>
</td></tr></table></td><td width=100% align=center valign=middle>

	||&nbsp;
	<a href=http://pygame.seul.org>Home</a> &nbsp;||&nbsp;
	<a href=../index.html>Help Contents</a> &nbsp;||
	<br>&nbsp;<br>

|| <a href=CD.html>CD</a> || 
<a href=Channel.html>Channel</a> || 
<a href=Font.html>Font</a> || 
<a href=Joystick.html>Joystick</a> || 
<a href=Rect.html>Rect</a> || 
<a href=Sound.html>Sound</a> || 
<a href=Surface.html>Surface</a> ||<br>
|| <a href=pygame.html>pygame</a> || 
<a href=pygame_UserRect.html>UserRect</a> || 
<a href=pygame_cdrom.html>cdrom</a> || 
<a href=pygame_constants.html>constants</a> || 
<a href=pygame_cursors.html>cursors</a> || 
<a href=pygame_display.html>display</a> || 
<a href=pygame_event.html>event</a> ||<br>
|| <a href=pygame_font.html>font</a> || 
<a href=pygame_image.html>image</a> || 
<a href=pygame_joystick.html>joystick</a> || 
<a href=pygame_key.html>key</a> || 
<a href=pygame_mixer.html>mixer</a> || 
<a href=pygame_mixer_music.html>mixer_music</a> || 
<a href=pygame_mouse.html>mouse</a> ||<br>
|| <a href=pygame_surfarray.html>surfarray</a> || 
<a href=pygame_time.html>time</a> || 
<a href=pygame_version.html>version</a> ||<br>


</td></tr></table>
<br>
<h2 align=center>Surface</h2>
Surface objects represent a simple memory buffer of pixels.
Surface objects can reside in system memory, or in special
hardware memory, which can be hardware accelerated. Surfaces that
are 8 bits per pixel use a colormap to represent their color
values. All Surfaces with higher bits per pixel use a packed
pixels to store their color values.
<br>&nbsp;<br>
Surfaces can have many extra attributes like alpha planes,
colorkeys, source rectangle clipping. These functions mainly
effect how the Surface is blitted to other Surfaces. The blit
routines will attempt to use hardware acceleration when possible,
otherwise will use highly optimized software blitting methods.
<br>&nbsp;<br>
There is support for pixel access for the Surfaces. Pixel access
on hardware surfaces is slow and not recommended. Pixels can be
accessed using the get_at() and set_at() functions. These methods
are fine for simple access, but will be considerably slow when
doing of pixel work with them. If you plan on doing a lot of
pixel level work, it is recommended to use the pygame.surfarray
module, which can treat the surfaces like large multidimensional
arrays (and it's quite quick). Some surfaces need to be locked
before they can be used. Surfaces with flags like HWSURFACE and
RLEACCEL generally require calls to lock() and unlock()
surrounding pixel access. It is safe to lock() and unlock()
surfaces that do not require locking. Nonetheless, you can check
to see if a Surface really needs to be locked with the mustlock()
function.<p>&nbsp;</p>Here is the quick breakdown of how packed pixels work (don't worry if
you don't quite understand this, it is only here for informational
purposes, it is not needed). Each colorplane mask can be used to
isolate the values for a colorplane from the packed pixel color.
Therefore PACKED_COLOR & RED_MASK == REDPLANE. Note that the
REDPLANE is not exactly the red color value, but it is the red
color value bitwise left shifted a certain amount. The losses and
masks can be used to convert back and forth between each
colorplane and the actual color for that plane. Here are the
final formulas used be map and unmap (not exactly, heh).
PACKED_COLOR = RED>>losses[0]<<shifts[0] |
      GREEN>>losses[1]<<shifts[1] | BLUE>>losses[2]<<shifts[2]
RED = PACKED_COLOR & masks[0] >> shifts[0] << losses[0]
GREEN = PACKED_COLOR & masks[1] >> shifts[1] << losses[1]
BLUE = PACKED_COLOR & masks[2] >> shifts[2] << losses[2]
There is also an alpha channel for some Surfaces. The alpha
channel works this same exact way, and the map_rgba() and
unmap_rgba() functions can be used to do the conversion for you.

<hr>

<table>
<tr><td><a href=#blit>blit</a></td><td> -
copy a one Surface to another.</td></tr>


<tr><td><a href=#convert>convert</a></td><td> -
new copy of surface with different format</td></tr>


<tr><td><a href=#convert_alpha>convert_alpha</a></td><td> -
new copy of surface with different format and per pixel alpha</td></tr>


<tr><td><a href=#fill>fill</a></td><td> -
fill areas of a Surface</td></tr>


<tr><td><a href=#get_alpha>get_alpha</a></td><td> -
query alpha information</td></tr>


<tr><td><a href=#get_at>get_at</a></td><td> -
get a pixel color</td></tr>


<tr><td><a href=#get_bitsize>get_bitsize</a></td><td> -
query size of pixel</td></tr>


<tr><td><a href=#get_bytesize>get_bytesize</a></td><td> -
query size of pixel</td></tr>


<tr><td><a href=#get_clip>get_clip</a></td><td> -
query the clipping area</td></tr>


<tr><td><a href=#get_colorkey>get_colorkey</a></td><td> -
query colorkey</td></tr>


<tr><td><a href=#get_flags>get_flags</a></td><td> -
query the surface width</td></tr>


<tr><td><a href=#get_height>get_height</a></td><td> -
query the surface height</td></tr>


<tr><td><a href=#get_losses>get_losses</a></td><td> -
get mapping losses for each colorplane</td></tr>


<tr><td><a href=#get_masks>get_masks</a></td><td> -
get mapping bitmasks for each colorplane</td></tr>


<tr><td><a href=#get_palette>get_palette</a></td><td> -
get the palette</td></tr>


<tr><td><a href=#get_palette_at>get_palette_at</a></td><td> -
get a palette entry</td></tr>


<tr><td><a href=#get_pitch>get_pitch</a></td><td> -
query the surface pitch</td></tr>


<tr><td><a href=#get_rect>get_rect</a></td><td> -
get a rectangle covering the entire surface</td></tr>


<tr><td><a href=#get_shifts>get_shifts</a></td><td> -
alphashift</td></tr>


<tr><td><a href=#get_size>get_size</a></td><td> -
query the surface size</td></tr>


<tr><td><a href=#get_width>get_width</a></td><td> -
query the surface width</td></tr>


<tr><td><a href=#lock>lock</a></td><td> -
locks Surface for pixel access</td></tr>


<tr><td><a href=#map_rgb>map_rgb</a></td><td> -
convert RGB into a mapped color</td></tr>


<tr><td><a href=#mustlock>mustlock</a></td><td> -
check if the surface needs locking</td></tr>


<tr><td><a href=#save>save</a></td><td> -
save surface as BMP data</td></tr>


<tr><td><a href=#set_alpha>set_alpha</a></td><td> -
change alpha information</td></tr>


<tr><td><a href=#set_at>set_at</a></td><td> -
set pixel at given position</td></tr>


<tr><td><a href=#set_clip>set_clip</a></td><td> -
assign destination clipping rectangle</td></tr>


<tr><td><a href=#set_colorkey>set_colorkey</a></td><td> -
change colorkey information</td></tr>


<tr><td><a href=#set_palette>set_palette</a></td><td> -
set the palette</td></tr>


<tr><td><a href=#set_palette_at>set_palette_at</a></td><td> -
set a palette entry</td></tr>


<tr><td><a href=#unlock>unlock</a></td><td> -
locks Surface for pixel access</td></tr>


<tr><td><a href=#unmap_rgb>unmap_rgb</a></td><td> -
convert mapped color into RGB</td></tr>


</table>

<hr>

<a name=blit><font size=+2><b>blit
</b></font><br><font size=+1><tt>
Surface.blit(source, destpos, [sourcerect]) -> Rect
</tt></font><ul>
The blitting will transfer one surface to another. It will
respect any special modes like colorkeying and alpha. If hardware
support is available, it will be used. The given source is the
Surface to copy from. The destoffset is a 2-number-sequence that
specifies where on the destination Surface the blit happens (see below).
When sourcerect isn't supplied, the blit will copy the
entire source surface. If you would like to copy only a portion
of the source, use the sourcerect argument to control
what area is copied.
<br>&nbsp;<br>
The blit is subject to be clipped by the active clipping
rectangle. The return value contains the actual area blitted.
<br>&nbsp;<br>
As a shortcut, the destination position can be passed as a
rectangle. If a rectangle is given, the blit will use the topleft
corner of the rectangle as the blit destination position. The
rectangle sizes will be ignored.
</ul><br>&nbsp;<br>

<a name=convert><font size=+2><b>convert
</b></font><br><font size=+1><tt>
Surface.convert([src_surface]) -> Surface
</tt></font><ul>
Creates a new copy of the surface with the desired pixel format.
Surfaces with the same pixel format will blit much faster than
those with mixed formats. The pixel format of the new surface
will match the format given as the argument. If no surface is
given, the new surface will have the same pixel format as the
current display.
</ul><br>&nbsp;<br>

<a name=convert_alpha><font size=+2><b>convert_alpha
</b></font><br><font size=+1><tt>
Surface.convert_alpha([src_surface]) -> Surface
</tt></font><ul>
Creates a new copy of the surface with the desired pixel format.
The new surface will be in a format suited for quick blitting to
the given format with per pixel alpha. If no surface is given,
the new surface will be optimized for blittint to the current
display.
<br>&nbsp;<br>
Unlike the <a href=#convert>convert()</a> method, the pixel format for the new image
will not be exactly the same as the requested source, but it will
be optimized for fast alpha blitting to the destination.
</ul><br>&nbsp;<br>

<a name=fill><font size=+2><b>fill
</b></font><br><font size=+1><tt>
Surface.fill(color, [rectstyle])) -> Rect
</tt></font><ul>
Fills the specified area of the Surface with the mapped color
value. If no destination rectangle is supplied, it will fill the
entire Surface.
<br>&nbsp;<br>
The fill is subject to be clipped by the active clipping
rectangle. The return value contains the actual area filled.
</ul><br>&nbsp;<br>

<a name=get_alpha><font size=+2><b>get_alpha
</b></font><br><font size=+1><tt>
Surface.get_alpha() -> alpha
</tt></font><ul>
Returns the current alpha value for the Surface. If transparency
is disabled for the Surface, it returns None.
</ul><br>&nbsp;<br>

<a name=get_at><font size=+2><b>get_at
</b></font><br><font size=+1><tt>
Surface.get_at([x, y]) -> RGBA
</tt></font><ul>
Returns the RGB color values at a given pixel. If the
Surface has no per-pixel alpha, the alpha will be 255 (opaque).
The surface must be locked for this to work correctly.
</ul><br>&nbsp;<br>

<a name=get_bitsize><font size=+2><b>get_bitsize
</b></font><br><font size=+1><tt>
Surface.get_bitsize() -> int
</tt></font><ul>
Returns the number of bits used to represent each pixel. This
value may not exactly fill the number of bytes used per pixel.
For example a 15 bit Surface still requires a full 2 bytes.
</ul><br>&nbsp;<br>

<a name=get_bytesize><font size=+2><b>get_bytesize
</b></font><br><font size=+1><tt>
Surface.get_bytesize() -> int
</tt></font><ul>
Returns the number of bytes used to store each pixel.
</ul><br>&nbsp;<br>

<a name=get_clip><font size=+2><b>get_clip
</b></font><br><font size=+1><tt>
Surface.get_clip() -> rect
</tt></font><ul>
Returns the current destination clipping area being used by the
Surface. If the clipping area is not set, it will return a
rectangle containing the full Surface area.
</ul><br>&nbsp;<br>

<a name=get_colorkey><font size=+2><b>get_colorkey
</b></font><br><font size=+1><tt>
Surface.get_colorkey() -> RGBA
</tt></font><ul>
Returns the current mapped color value being used for
colorkeying. If colorkeying is not enabled for this surface, it
returns None
</ul><br>&nbsp;<br>

<a name=get_flags><font size=+2><b>get_flags
</b></font><br><font size=+1><tt>
Surface.get_flags() -> flags
</tt></font><ul>
Returns the current state flags for the surface.
</ul><br>&nbsp;<br>

<a name=get_height><font size=+2><b>get_height
</b></font><br><font size=+1><tt>
Surface.get_height() -> height
</tt></font><ul>
Returns the height of the Surface.
</ul><br>&nbsp;<br>

<a name=get_losses><font size=+2><b>get_losses
</b></font><br><font size=+1><tt>
Surface.get_losses() -> redloss, greenloss, blueloss, alphaloss
</tt></font><ul>
Returns the bitloss for each color plane. The loss is the number
of bits removed for each colorplane from a full 8 bits of
resolution. A value of 8 usually indicates that colorplane is not
used (like the alpha)
</ul><br>&nbsp;<br>

<a name=get_masks><font size=+2><b>get_masks
</b></font><br><font size=+1><tt>
Surface.get_masks() -> redmask, greenmask, bluemask, alphamask
</tt></font><ul>
Returns the bitmasks for each color plane. The bitmask is used to
isolate each colorplane value from a mapped color value. A value
of zero means that colorplane is not used (like alpha)
</ul><br>&nbsp;<br>

<a name=get_palette><font size=+2><b>get_palette
</b></font><br><font size=+1><tt>
Surface.get_palette() -> [[r, g, b], ...]
</tt></font><ul>
This will return the an array of all the color indexes in the
Surface's palette.
</ul><br>&nbsp;<br>

<a name=get_palette_at><font size=+2><b>get_palette_at
</b></font><br><font size=+1><tt>
Surface.get_palette_at(index) -> r, g, b
</tt></font><ul>
This will retreive an individual color entry from the Surface's
palette.
</ul><br>&nbsp;<br>

<a name=get_pitch><font size=+2><b>get_pitch
</b></font><br><font size=+1><tt>
Surface.get_pitch() -> pitch
</tt></font><ul>
The surface pitch is the number of bytes used in each
scanline. This function should rarely needed, mainly for
any special-case debugging.
</ul><br>&nbsp;<br>

<a name=get_rect><font size=+2><b>get_rect
</b></font><br><font size=+1><tt>
Surface.get_rect() -> rect
</tt></font><ul>
Returns a new rectangle covering the entire surface.
This rectangle will always start at 0, 0 with a width.
and height the same size as the image.
</ul><br>&nbsp;<br>

<a name=get_shifts><font size=+2><b>get_shifts
</b></font><br><font size=+1><tt>
Surface.get_shifts() -> redshift, greenshift, blueshift,
</tt></font><ul>
<br>&nbsp;<br>
Returns the bitshifts used for each color plane. The shift is
determine how many bits left-shifted a colorplane value is in a
mapped color value.
</ul><br>&nbsp;<br>

<a name=get_size><font size=+2><b>get_size
</b></font><br><font size=+1><tt>
Surface.get_size() -> x, y
</tt></font><ul>
Returns the width and height of the Surface.
</ul><br>&nbsp;<br>

<a name=get_width><font size=+2><b>get_width
</b></font><br><font size=+1><tt>
Surface.get_width() -> width
</tt></font><ul>
Returns the width of the Surface.
</ul><br>&nbsp;<br>

<a name=lock><font size=+2><b>lock
</b></font><br><font size=+1><tt>
Surface.lock() -> None
</tt></font><ul>
On accelerated surfaces, it is usually required to lock the
surface before you can access the pixel values. To be safe, it is
always a good idea to lock the surface before entering a block of
code that changes or accesses the pixel values. The surface must
not be locked when performing other pyGame functions on it like
fill and blit.
<br>&nbsp;<br>
You can doublecheck to really make sure a lock is needed by
calling the <a href=#mustlock>mustlock()</a> member. This should not be needed, since
it is usually recommended to lock anyways and work with all
surface types. If the surface does not need to be locked, the
operation will return quickly with minute overhead.
<br>&nbsp;<br>
On some platforms a necessary lock can shut off some parts of the
system. This is not a problem unless you leave surfaces locked
for long periouds of time. Only keep the surface locked when you
need the pixel access. At the same time, it is not a good too
repeatedly lock and unlock the surface inside tight loops. It is
fine to leave the surface locked while needed, just don't be
lazy.
</ul><br>&nbsp;<br>

<a name=map_rgb><font size=+2><b>map_rgb
</b></font><br><font size=+1><tt>
Surface.map_rgb(RGBA) -> int
</tt></font><ul>
Uses the Surface format to convert RGBA into a mapped color value.
<br>&nbsp;<br>
This function is not as needed as normal C code using SDL. The pygame
functions do not used mapped colors, so there is no need to map them.
</ul><br>&nbsp;<br>

<a name=mustlock><font size=+2><b>mustlock
</b></font><br><font size=+1><tt>
Surface.mustlock() -> bool
</tt></font><ul>
Returns true if the surface really does need locking to gain
pixel access. Usually the overhead of checking before locking
outweight the overhead of just locking any surface before access.
</ul><br>&nbsp;<br>

<a name=save><font size=+2><b>save
</b></font><br><font size=+1><tt>
Surface.save(file) -> None
</tt></font><ul>
This will save your surface in the BMP format. The given file
argument can be either a filename or a python file-like object
to save the BMP image to.
</ul><br>&nbsp;<br>

<a name=set_alpha><font size=+2><b>set_alpha
</b></font><br><font size=+1><tt>
Surface.set_alpha([alpha, [flags]]) -> None
</tt></font><ul>
Set the overall transparency for the surface. If no alpha is
passed, alpha blending is disabled for the surface. An alpha of 0
is fully transparent, an alpha of 255 is fully opaque.
<br>&nbsp;<br>
If your surface has a pixel alpha channel, it will override the
overall surface transparency. You'll need to change the actual
pixel transparency to make changes.
<br>&nbsp;<br>
If your image is nonchanging and will be used repeatedly, you
will probably want to pass the RLEACCEL flag to the call. This
will take a short time to compile your surface, and increase the
blitting speed.
</ul><br>&nbsp;<br>

<a name=set_at><font size=+2><b>set_at
</b></font><br><font size=+1><tt>
Surface.set_at([x, y], RGBA) -> None
</tt></font><ul>
Assigns RGBA color to the image at the give position.
The surface must be locked for this to work correctly.
<br>&nbsp;<br>
In many situations just using the <a href=#fill>fill()</a> function with a one-pixel
sized rectangle will be quicker. Also the fill function does not
requirethe surface to be locked.
</ul><br>&nbsp;<br>

<a name=set_clip><font size=+2><b>set_clip
</b></font><br><font size=+1><tt>
Surface.set_clip([rectstyle])) -> None
</tt></font><ul>
Assigns the destination clipping rectangle for the Surface. When
blit or fill operations are performed on the Surface, they are
restricted to the inside of the clipping rectangle. If no
rectangle is passed, the clipping region is set to the entire
Surface area. The rectangle you pass will be clipped to the area of
the Surface.
</ul><br>&nbsp;<br>

<a name=set_colorkey><font size=+2><b>set_colorkey
</b></font><br><font size=+1><tt>
Surface.set_colorkey([RGBA, [flags]]) -> None
</tt></font><ul>
Set the colorkey for the surface by passing a mapped color value
as the color argument. If no arguments are passed, colorkeying
will be disabled for this surface.
<br>&nbsp;<br>
If your image is nonchanging and will be used repeatedly, you
will probably want to pass the RLEACCEL flag to the call. This
will take a short time to compile your surface, and increase the
blitting speed.
</ul><br>&nbsp;<br>

<a name=set_palette><font size=+2><b>set_palette
</b></font><br><font size=+1><tt>
Surface.set_palette([[r, g, b], ...]) -> None
</tt></font><ul>
This will replace the entire palette with color
information you provide.
</ul><br>&nbsp;<br>

<a name=set_palette_at><font size=+2><b>set_palette_at
</b></font><br><font size=+1><tt>
Surface.set_palette_at(index, [r, g, b]) -> None
</tt></font><ul>
This function sets the palette color at a specific entry.
</ul><br>&nbsp;<br>

<a name=unlock><font size=+2><b>unlock
</b></font><br><font size=+1><tt>
Surface.unlock() -> None
</tt></font><ul>
After a surface has been locked, you will need to unlock it when
you are done.
<br>&nbsp;<br>
You can doublecheck to really make sure a lock is needed by
calling the <a href=#mustlock>mustlock()</a> member. This should not be needed, since
it is usually recommended to lock anyways and work with all
surface types. If the surface does not need to be locked, the
operation will return quickly with minute overhead.
</ul><br>&nbsp;<br>

<a name=unmap_rgb><font size=+2><b>unmap_rgb
</b></font><br><font size=+1><tt>
Surface.unmap_rgb(color) -> RGBA
</tt></font><ul>
This function returns the RGBA components for a mapped color
value. If Surface has no per-pixel alpha, alpha will be 255 (opaque).
<br>&nbsp;<br>
This function is not as needed as normal C code using SDL. The pygame
functions do not used mapped colors, so there is no need to unmap them.
</ul><br>&nbsp;<br>


<hr>

</body></html>