forked from pygame/pygame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
surface_test.py
2779 lines (2371 loc) · 106 KB
/
surface_test.py
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import math
import unittest
from pygame.tests import test_utils
from pygame.tests.test_utils import (
example_path,
AssertRaisesRegexMixin,
SurfaceSubclass,
)
try:
from pygame.tests.test_utils.arrinter import *
except (ImportError, NameError):
pass
import pygame
from pygame.locals import *
from pygame.compat import xrange_, as_bytes, as_unicode
from pygame.bufferproxy import BufferProxy
import platform
import gc
import weakref
import ctypes
IS_PYPY = "PyPy" == platform.python_implementation()
SDL1 = pygame.get_sdl_version()[0] < 2
def intify(i):
"""If i is a long, cast to an int while preserving the bits"""
if 0x80000000 & i:
return int((0xFFFFFFFF & i))
return i
def longify(i):
"""If i is an int, cast to a long while preserving the bits"""
if i < 0:
return 0xFFFFFFFF & i
return long(i)
class SurfaceTypeTest(AssertRaisesRegexMixin, unittest.TestCase):
def test_surface__pixel_format_as_surface_subclass(self):
"""Ensure a subclassed surface can be used for pixel format
when creating a new surface."""
expected_depth = 16
expected_flags = SRCALPHA
expected_size = (13, 37)
depth_surface = SurfaceSubclass((11, 21), expected_flags, expected_depth)
surface = pygame.Surface(expected_size, 0, depth_surface)
self.assertIsNot(surface, depth_surface)
self.assertIsInstance(surface, pygame.Surface)
self.assertNotIsInstance(surface, SurfaceSubclass)
self.assertEqual(surface.get_size(), expected_size)
self.assertEqual(surface.get_flags(), expected_flags)
self.assertEqual(surface.get_bitsize(), expected_depth)
def test_set_clip(self):
""" see if surface.set_clip(None) works correctly.
"""
s = pygame.Surface((800, 600))
r = pygame.Rect(10, 10, 10, 10)
s.set_clip(r)
r.move_ip(10, 0)
s.set_clip(None)
res = s.get_clip()
# this was garbled before.
self.assertEqual(res[0], 0)
self.assertEqual(res[2], 800)
def test_print(self):
surf = pygame.Surface((70, 70), 0, 32)
self.assertEqual(repr(surf), "<Surface(70x70x32 SW)>")
def test_keyword_arguments(self):
surf = pygame.Surface((70, 70), flags=SRCALPHA, depth=32)
self.assertEqual(surf.get_flags() & SRCALPHA, SRCALPHA)
self.assertEqual(surf.get_bitsize(), 32)
# sanity check to make sure the check below is valid
surf_16 = pygame.Surface((70, 70), 0, 16)
self.assertEqual(surf_16.get_bytesize(), 2)
# try again with an argument list
surf_16 = pygame.Surface((70, 70), depth=16)
self.assertEqual(surf_16.get_bytesize(), 2)
def test_set_at(self):
# 24bit surfaces
s = pygame.Surface((100, 100), 0, 24)
s.fill((0, 0, 0))
# set it with a tuple.
s.set_at((0, 0), (10, 10, 10, 255))
r = s.get_at((0, 0))
self.assertIsInstance(r, pygame.Color)
self.assertEqual(r, (10, 10, 10, 255))
# try setting a color with a single integer.
s.fill((0, 0, 0, 255))
s.set_at((10, 1), 0x0000FF)
r = s.get_at((10, 1))
self.assertEqual(r, (0, 0, 255, 255))
def test_SRCALPHA(self):
# has the flag been passed in ok?
surf = pygame.Surface((70, 70), SRCALPHA, 32)
self.assertEqual(surf.get_flags() & SRCALPHA, SRCALPHA)
# 24bit surfaces can not have SRCALPHA.
self.assertRaises(ValueError, pygame.Surface, (100, 100), pygame.SRCALPHA, 24)
# if we have a 32 bit surface, the SRCALPHA should have worked too.
surf2 = pygame.Surface((70, 70), SRCALPHA)
if surf2.get_bitsize() == 32:
self.assertEqual(surf2.get_flags() & SRCALPHA, SRCALPHA)
def test_masks(self):
def make_surf(bpp, flags, masks):
pygame.Surface((10, 10), flags, bpp, masks)
# With some masks SDL_CreateRGBSurface does not work properly.
masks = (0xFF000000, 0xFF0000, 0xFF00, 0)
self.assertEqual(make_surf(32, 0, masks), None)
# For 24 and 32 bit surfaces Pygame assumes no losses.
masks = (0x7F0000, 0xFF00, 0xFF, 0)
self.assertRaises(ValueError, make_surf, 24, 0, masks)
self.assertRaises(ValueError, make_surf, 32, 0, masks)
# What contiguous bits in a mask.
masks = (0x6F0000, 0xFF00, 0xFF, 0)
self.assertRaises(ValueError, make_surf, 32, 0, masks)
def test_get_bounding_rect(self):
surf = pygame.Surface((70, 70), SRCALPHA, 32)
surf.fill((0, 0, 0, 0))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.width, 0)
self.assertEqual(bound_rect.height, 0)
surf.set_at((30, 30), (255, 255, 255, 1))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.left, 30)
self.assertEqual(bound_rect.top, 30)
self.assertEqual(bound_rect.width, 1)
self.assertEqual(bound_rect.height, 1)
surf.set_at((29, 29), (255, 255, 255, 1))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.left, 29)
self.assertEqual(bound_rect.top, 29)
self.assertEqual(bound_rect.width, 2)
self.assertEqual(bound_rect.height, 2)
surf = pygame.Surface((70, 70), 0, 24)
surf.fill((0, 0, 0))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.width, surf.get_width())
self.assertEqual(bound_rect.height, surf.get_height())
surf.set_colorkey((0, 0, 0))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.width, 0)
self.assertEqual(bound_rect.height, 0)
surf.set_at((30, 30), (255, 255, 255))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.left, 30)
self.assertEqual(bound_rect.top, 30)
self.assertEqual(bound_rect.width, 1)
self.assertEqual(bound_rect.height, 1)
surf.set_at((60, 60), (255, 255, 255))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.left, 30)
self.assertEqual(bound_rect.top, 30)
self.assertEqual(bound_rect.width, 31)
self.assertEqual(bound_rect.height, 31)
# Issue #180
pygame.display.init()
try:
surf = pygame.Surface((4, 1), 0, 8)
surf.fill((255, 255, 255))
surf.get_bounding_rect() # Segfault.
finally:
pygame.display.quit()
def test_copy(self):
"""Ensure a surface can be copied."""
color = (25, 25, 25, 25)
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
s1.fill(color)
s2 = s1.copy()
s1rect = s1.get_rect()
s2rect = s2.get_rect()
self.assertEqual(s1rect.size, s2rect.size)
self.assertEqual(s2.get_at((10, 10)), color)
def test_fill(self):
"""Ensure a surface can be filled."""
color = (25, 25, 25, 25)
fill_rect = pygame.Rect(0, 0, 16, 16)
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
s1.fill(color, fill_rect)
for pt in test_utils.rect_area_pts(fill_rect):
self.assertEqual(s1.get_at(pt), color)
for pt in test_utils.rect_outer_bounds(fill_rect):
self.assertNotEqual(s1.get_at(pt), color)
def test_fill_rle(self):
color = (250, 25, 25, 255)
color2 = (200, 200, 250, 255)
sub_rect = pygame.Rect(16, 16, 16, 16)
s0 = pygame.Surface((32, 32), 24)
s1 = pygame.Surface((32, 32), 24)
s1.set_colorkey((255,0,255), pygame.RLEACCEL)
s0.blit(s1, (0,0))
s1.fill(color)
self.assertTrue(s1.get_flags()&pygame.RLEACCEL)
@unittest.expectedFailure
def test_copy_rle(self):
color = (250, 25, 25, 255)
color2 = (200, 200, 250, 255)
sub_rect = pygame.Rect(16, 16, 16, 16)
s0 = pygame.Surface((32, 32), 24)
s1 = pygame.Surface((32, 32), 24)
s1.set_colorkey((255,0,255), pygame.RLEACCEL)
s0.blit(s1, (0,0))
s1.copy()
self.assertTrue(s1.get_flags()&pygame.RLEACCEL)
def test_subsurface_rle(self):
"""Ensure an RLE sub-surface works independently of its parent."""
color = (250, 25, 25, 255)
color2 = (200, 200, 250, 255)
sub_rect = pygame.Rect(16, 16, 16, 16)
s0 = pygame.Surface((32, 32), 24)
s1 = pygame.Surface((32, 32), 24)
s1.set_colorkey((255,0,255), pygame.RLEACCEL)
s1.fill(color)
s2 = s1.subsurface(sub_rect)
s2.fill(color2)
s0.blit(s1, (0,0))
self.assertTrue(s1.get_flags()&pygame.RLEACCEL)
self.assertTrue(not s2.get_flags()&pygame.RLEACCEL)
@unittest.expectedFailure
def test_subsurface_rle2(self):
color = (250, 25, 25, 255)
color2 = (200, 200, 250, 255)
sub_rect = pygame.Rect(16, 16, 16, 16)
s0 = pygame.Surface((32, 32), 24)
s1 = pygame.Surface((32, 32), 24)
s1.set_colorkey((255,0,255), pygame.RLEACCEL)
s1.fill(color)
s2 = s1.subsurface(sub_rect)
s2.fill(color2)
s0.blit(s2, (0,0))
self.assertTrue(s1.get_flags()&pygame.RLEACCEL)
self.assertTrue(not s2.get_flags()&pygame.RLEACCEL)
def test_fill_negative_coordinates(self):
# negative coordinates should be clipped by fill, and not draw outside the surface.
color = (25, 25, 25, 25)
color2 = (20, 20, 20, 25)
fill_rect = pygame.Rect(-10, -10, 16, 16)
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
r1 = s1.fill(color, fill_rect)
c = s1.get_at((0, 0))
self.assertEqual(c, color)
# make subsurface in the middle to test it doesn't over write.
s2 = s1.subsurface((5, 5, 5, 5))
r2 = s2.fill(color2, (-3, -3, 5, 5))
c2 = s1.get_at((4, 4))
self.assertEqual(c, color)
# rect returns the area we actually fill.
r3 = s2.fill(color2, (-30, -30, 5, 5))
# since we are using negative coords, it should be an zero sized rect.
self.assertEqual(tuple(r3), (0, 0, 0, 0))
def test_fill_keyword_args(self):
"""Ensure fill() accepts keyword arguments."""
color = (1, 2, 3, 255)
area = (1, 1, 2, 2)
s1 = pygame.Surface((4, 4), 0, 32)
s1.fill(special_flags=pygame.BLEND_ADD, color=color, rect=area)
self.assertEqual(s1.get_at((0, 0)), (0, 0, 0, 255))
self.assertEqual(s1.get_at((1, 1)), color)
########################################################################
def test_get_alpha(self):
"""Ensure a surface's alpha value can be retrieved."""
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
self.assertEqual(s1.get_alpha(), 255)
for alpha in (0, 32, 127, 255):
s1.set_alpha(alpha)
for t in range(4):
s1.set_alpha(s1.get_alpha())
self.assertEqual(s1.get_alpha(), alpha)
########################################################################
def test_get_bytesize(self):
"""Ensure a surface's bit and byte sizes can be retrieved."""
depth = 32
depth_bytes = 4
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, depth)
self.assertEqual(s1.get_bytesize(), depth_bytes)
self.assertEqual(s1.get_bitsize(), depth)
########################################################################
def test_get_flags(self):
"""Ensure a surface's flags can be retrieved."""
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
self.assertEqual(s1.get_flags(), pygame.SRCALPHA)
########################################################################
def test_get_parent(self):
"""Ensure a surface's parent can be retrieved."""
parent = pygame.Surface((16, 16))
child = parent.subsurface((0, 0, 5, 5))
self.assertIs(child.get_parent(), parent)
########################################################################
def test_get_rect(self):
"""Ensure a surface's rect can be retrieved."""
size = (16, 16)
surf = pygame.Surface(size)
rect = surf.get_rect()
self.assertEqual(rect.size, size)
########################################################################
def test_get_width__size_and_height(self):
"""Ensure a surface's size, width and height can be retrieved."""
for w in xrange_(0, 255, 32):
for h in xrange_(0, 127, 15):
s = pygame.Surface((w, h))
self.assertEqual(s.get_width(), w)
self.assertEqual(s.get_height(), h)
self.assertEqual(s.get_size(), (w, h))
def test_get_view(self):
"""Ensure a buffer view of the surface's pixels can be retrieved."""
# Check that BufferProxys are returned when array depth is supported,
# ValueErrors returned otherwise.
Error = ValueError
s = pygame.Surface((5, 7), 0, 8)
v2 = s.get_view("2")
self.assertRaises(Error, s.get_view, "0")
self.assertRaises(Error, s.get_view, "1")
self.assertIsInstance(v2, BufferProxy)
self.assertRaises(Error, s.get_view, "3")
s = pygame.Surface((8, 7), 0, 8)
length = s.get_bytesize() * s.get_width() * s.get_height()
v0 = s.get_view("0")
v1 = s.get_view("1")
self.assertIsInstance(v0, BufferProxy)
self.assertEqual(v0.length, length)
self.assertIsInstance(v1, BufferProxy)
self.assertEqual(v1.length, length)
s = pygame.Surface((5, 7), 0, 16)
v2 = s.get_view("2")
self.assertRaises(Error, s.get_view, "0")
self.assertRaises(Error, s.get_view, "1")
self.assertIsInstance(v2, BufferProxy)
self.assertRaises(Error, s.get_view, "3")
s = pygame.Surface((8, 7), 0, 16)
length = s.get_bytesize() * s.get_width() * s.get_height()
v0 = s.get_view("0")
v1 = s.get_view("1")
self.assertIsInstance(v0, BufferProxy)
self.assertEqual(v0.length, length)
self.assertIsInstance(v1, BufferProxy)
self.assertEqual(v1.length, length)
s = pygame.Surface((5, 7), pygame.SRCALPHA, 16)
v2 = s.get_view("2")
self.assertIsInstance(v2, BufferProxy)
self.assertRaises(Error, s.get_view, "3")
s = pygame.Surface((5, 7), 0, 24)
v2 = s.get_view("2")
v3 = s.get_view("3")
self.assertRaises(Error, s.get_view, "0")
self.assertRaises(Error, s.get_view, "1")
self.assertIsInstance(v2, BufferProxy)
self.assertIsInstance(v3, BufferProxy)
s = pygame.Surface((8, 7), 0, 24)
length = s.get_bytesize() * s.get_width() * s.get_height()
v0 = s.get_view("0")
v1 = s.get_view("1")
self.assertIsInstance(v0, BufferProxy)
self.assertEqual(v0.length, length)
self.assertIsInstance(v1, BufferProxy)
self.assertEqual(v1.length, length)
s = pygame.Surface((5, 7), 0, 32)
length = s.get_bytesize() * s.get_width() * s.get_height()
v0 = s.get_view("0")
v1 = s.get_view("1")
v2 = s.get_view("2")
v3 = s.get_view("3")
self.assertIsInstance(v0, BufferProxy)
self.assertEqual(v0.length, length)
self.assertIsInstance(v1, BufferProxy)
self.assertEqual(v1.length, length)
self.assertIsInstance(v2, BufferProxy)
self.assertIsInstance(v3, BufferProxy)
s2 = s.subsurface((0, 0, 4, 7))
self.assertRaises(Error, s2.get_view, "0")
self.assertRaises(Error, s2.get_view, "1")
s2 = None
s = pygame.Surface((5, 7), pygame.SRCALPHA, 32)
for kind in ("2", "3", "a", "A", "r", "R", "g", "G", "b", "B"):
self.assertIsInstance(s.get_view(kind), BufferProxy)
# Check default argument value: '2'
s = pygame.Surface((2, 4), 0, 32)
v = s.get_view()
if not IS_PYPY:
ai = ArrayInterface(v)
self.assertEqual(ai.nd, 2)
# Check locking.
s = pygame.Surface((2, 4), 0, 32)
self.assertFalse(s.get_locked())
v = s.get_view("2")
self.assertFalse(s.get_locked())
c = v.__array_interface__
self.assertTrue(s.get_locked())
c = None
gc.collect()
self.assertTrue(s.get_locked())
v = None
gc.collect()
self.assertFalse(s.get_locked())
# Check invalid view kind values.
s = pygame.Surface((2, 4), pygame.SRCALPHA, 32)
self.assertRaises(TypeError, s.get_view, "")
self.assertRaises(TypeError, s.get_view, "9")
self.assertRaises(TypeError, s.get_view, "RGBA")
self.assertRaises(TypeError, s.get_view, 2)
# Both unicode and bytes strings are allowed for kind.
s = pygame.Surface((2, 4), 0, 32)
s.get_view(as_unicode("2"))
s.get_view(as_bytes("2"))
# Garbage collection
s = pygame.Surface((2, 4), 0, 32)
weak_s = weakref.ref(s)
v = s.get_view("3")
weak_v = weakref.ref(v)
gc.collect()
self.assertTrue(weak_s() is s)
self.assertTrue(weak_v() is v)
del v
gc.collect()
self.assertTrue(weak_s() is s)
self.assertTrue(weak_v() is None)
del s
gc.collect()
self.assertTrue(weak_s() is None)
def test_get_buffer(self):
# Check that get_buffer works for all pixel sizes and for a subsurface.
# Check for all pixel sizes
for bitsize in [8, 16, 24, 32]:
s = pygame.Surface((5, 7), 0, bitsize)
length = s.get_pitch() * s.get_height()
v = s.get_buffer()
self.assertIsInstance(v, BufferProxy)
self.assertEqual(v.length, length)
self.assertEqual(repr(v), "<BufferProxy(" + str(length) + ")>")
# Check for a subsurface (not contiguous)
s = pygame.Surface((7, 10), 0, 32)
s2 = s.subsurface((1, 2, 5, 7))
length = s2.get_pitch() * s2.get_height()
v = s2.get_buffer()
self.assertIsInstance(v, BufferProxy)
self.assertEqual(v.length, length)
# Check locking.
s = pygame.Surface((2, 4), 0, 32)
v = s.get_buffer()
self.assertTrue(s.get_locked())
v = None
gc.collect()
self.assertFalse(s.get_locked())
OLDBUF = hasattr(pygame.bufferproxy, "get_segcount")
@unittest.skipIf(not OLDBUF, "old buffer not available")
def test_get_buffer_oldbuf(self):
from pygame.bufferproxy import get_segcount, get_write_buffer
s = pygame.Surface((2, 4), pygame.SRCALPHA, 32)
v = s.get_buffer()
segcount, buflen = get_segcount(v)
self.assertEqual(segcount, 1)
self.assertEqual(buflen, s.get_pitch() * s.get_height())
seglen, segaddr = get_write_buffer(v, 0)
self.assertEqual(segaddr, s._pixels_address)
self.assertEqual(seglen, buflen)
@unittest.skipIf(not OLDBUF, "old buffer not available")
def test_get_view_oldbuf(self):
from pygame.bufferproxy import get_segcount, get_write_buffer
s = pygame.Surface((2, 4), pygame.SRCALPHA, 32)
v = s.get_view("1")
segcount, buflen = get_segcount(v)
self.assertEqual(segcount, 8)
self.assertEqual(buflen, s.get_pitch() * s.get_height())
seglen, segaddr = get_write_buffer(v, 7)
self.assertEqual(segaddr, s._pixels_address + s.get_bytesize() * 7)
self.assertEqual(seglen, s.get_bytesize())
def test_set_colorkey(self):
# __doc__ (as of 2008-06-25) for pygame.surface.Surface.set_colorkey:
# Surface.set_colorkey(Color, flags=0): return None
# Surface.set_colorkey(None): return None
# Set the transparent colorkey
s = pygame.Surface((16, 16), pygame.SRCALPHA, 32)
colorkeys = ((20, 189, 20, 255), (128, 50, 50, 255), (23, 21, 255, 255))
for colorkey in colorkeys:
s.set_colorkey(colorkey)
for t in range(4):
s.set_colorkey(s.get_colorkey())
self.assertEqual(s.get_colorkey(), colorkey)
def test_set_masks(self):
s = pygame.Surface((32, 32))
r, g, b, a = s.get_masks()
if pygame.get_sdl_version()[0] == 1:
s.set_masks((b, g, r, a))
r2, g2, b2, a2 = s.get_masks()
self.assertEqual((r, g, b, a), (b2, g2, r2, a2))
else:
self.assertRaises(TypeError, s.set_masks, (b, g, r, a))
def test_set_shifts(self):
s = pygame.Surface((32, 32))
r, g, b, a = s.get_shifts()
if pygame.get_sdl_version()[0] == 1:
s.set_shifts((b, g, r, a))
r2, g2, b2, a2 = s.get_shifts()
self.assertEqual((r, g, b, a), (b2, g2, r2, a2))
else:
self.assertRaises(TypeError, s.set_shifts, (b, g, r, a))
def test_blit_keyword_args(self):
color = (1, 2, 3, 255)
s1 = pygame.Surface((4, 4), 0, 32)
s2 = pygame.Surface((2, 2), 0, 32)
s2.fill((1, 2, 3))
s1.blit(special_flags=BLEND_ADD, source=s2, dest=(1, 1), area=s2.get_rect())
self.assertEqual(s1.get_at((0, 0)), (0, 0, 0, 255))
self.assertEqual(s1.get_at((1, 1)), color)
def todo_test_blit(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.blit:
# Surface.blit(source, dest, area=None, special_flags = 0): return Rect
# draw one image onto another
#
# Draws a source Surface onto this Surface. The draw can be positioned
# with the dest argument. Dest can either be pair of coordinates
# representing the upper left corner of the source. A Rect can also be
# passed as the destination and the topleft corner of the rectangle
# will be used as the position for the blit. The size of the
# destination rectangle does not effect the blit.
#
# An optional area rectangle can be passed as well. This represents a
# smaller portion of the source Surface to draw.
#
# An optional special flags is for passing in new in 1.8.0: BLEND_ADD,
# BLEND_SUB, BLEND_MULT, BLEND_MIN, BLEND_MAX new in 1.8.1:
# BLEND_RGBA_ADD, BLEND_RGBA_SUB, BLEND_RGBA_MULT, BLEND_RGBA_MIN,
# BLEND_RGBA_MAX BLEND_RGB_ADD, BLEND_RGB_SUB, BLEND_RGB_MULT,
# BLEND_RGB_MIN, BLEND_RGB_MAX With other special blitting flags
# perhaps added in the future.
#
# The return rectangle is the area of the affected pixels, excluding
# any pixels outside the destination Surface, or outside the clipping
# area.
#
# Pixel alphas will be ignored when blitting to an 8 bit Surface.
# special_flags new in pygame 1.8.
self.fail()
def test_blit__SRCALPHA_opaque_source(self):
src = pygame.Surface((256, 256), SRCALPHA, 32)
dst = src.copy()
for i, j in test_utils.rect_area_pts(src.get_rect()):
dst.set_at((i, j), (i, 0, 0, j))
src.set_at((i, j), (0, i, 0, 255))
dst.blit(src, (0, 0))
for pt in test_utils.rect_area_pts(src.get_rect()):
self.assertEqual(dst.get_at(pt)[1], src.get_at(pt)[1])
def todo_test_blit__blit_to_self(self): # TODO
src = pygame.Surface((256, 256), SRCALPHA, 32)
rect = src.get_rect()
for pt, color in test_utils.gradient(rect.width, rect.height):
src.set_at(pt, color)
src.blit(src, (0, 0))
def todo_test_blit__SRCALPHA_to_SRCALPHA_non_zero(self): # TODO
# " There is no unit test for blitting a SRCALPHA source with non-zero
# alpha to a SRCALPHA destination with non-zero alpha " LL
w, h = size = 32, 32
s = pygame.Surface(size, pygame.SRCALPHA, 32)
s2 = s.copy()
s.fill((32, 32, 32, 111))
s2.fill((32, 32, 32, 31))
s.blit(s2, (0, 0))
# TODO:
# what is the correct behaviour ?? should it blend? what algorithm?
self.assertEqual(s.get_at((0, 0)), (32, 32, 32, 31))
def test_blit__SRCALPHA32_to_8(self):
# Bug: fatal
# SDL_DisplayConvert segfaults when video is uninitialized.
target = pygame.Surface((11, 8), 0, 8)
color = target.get_palette_at(2)
source = pygame.Surface((1, 1), pygame.SRCALPHA, 32)
source.set_at((0, 0), color)
target.blit(source, (0, 0))
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == "dummy",
'requires a non-"dummy" SDL_VIDEODRIVER',
)
def test_image_convert_bug_131(self):
# Bitbucket bug #131: Unable to Surface.convert(32) some 1-bit images.
# https://bitbucket.org/pygame/pygame/issue/131/unable-to-surfaceconvert-32-some-1-bit
pygame.display.init()
try:
pygame.display.set_mode((640, 480))
im = pygame.image.load(example_path(os.path.join("data", "city.png")))
im2 = pygame.image.load(example_path(os.path.join("data", "brick.png")))
self.assertEqual(im.get_palette(), ((0, 0, 0, 255), (255, 255, 255, 255)))
self.assertEqual(im2.get_palette(), ((0, 0, 0, 255), (0, 0, 0, 255)))
self.assertEqual(repr(im.convert(32)), "<Surface(24x24x32 SW)>")
self.assertEqual(repr(im2.convert(32)), "<Surface(469x137x32 SW)>")
# Ensure a palette format to palette format works.
im3 = im.convert(8)
self.assertEqual(repr(im3), "<Surface(24x24x8 SW)>")
self.assertEqual(im3.get_palette(), im.get_palette())
finally:
pygame.display.quit()
def test_convert_init(self):
""" Ensure initialization exceptions are raised
for surf.convert()."""
pygame.display.quit()
surf = pygame.Surface((1, 1))
self.assertRaisesRegex(pygame.error, "display initialized", surf.convert)
pygame.display.init()
try:
if os.environ.get("SDL_VIDEODRIVER") != "dummy":
try:
surf.convert(32)
surf.convert(pygame.Surface((1, 1)))
except pygame.error:
self.fail("convert() should not raise an exception here.")
self.assertRaisesRegex(pygame.error, "No video mode", surf.convert)
pygame.display.set_mode((640, 480))
try:
surf.convert()
except pygame.error:
self.fail("convert() should not raise an exception here.")
finally:
pygame.display.quit()
def test_convert_alpha_init(self):
""" Ensure initialization exceptions are raised
for surf.convert_alpha()."""
pygame.display.quit()
surf = pygame.Surface((1, 1))
self.assertRaisesRegex(pygame.error, "display initialized", surf.convert_alpha)
pygame.display.init()
try:
self.assertRaisesRegex(pygame.error, "No video mode", surf.convert_alpha)
pygame.display.set_mode((640, 480))
try:
surf.convert_alpha()
except pygame.error:
self.fail("convert_alpha() should not raise an exception here.")
finally:
pygame.display.quit()
def test_convert_alpha_SRCALPHA(self):
""" Ensure that the surface returned by surf.convert_alpha()
has alpha blending enabled"""
pygame.display.init()
try:
pygame.display.set_mode((640, 480))
s1 = pygame.Surface((100, 100), 0, 32)
# s2=pygame.Surface((100,100), pygame.SRCALPHA, 32)
s1_alpha = s1.convert_alpha()
self.assertEqual(s1_alpha.get_flags() & SRCALPHA, SRCALPHA)
self.assertEqual(s1_alpha.get_alpha(), 255)
finally:
pygame.display.quit()
@unittest.skip("causes failures in other tests if run, so skip")
def test_src_alpha_issue_1289(self):
""" blit should be white.
"""
surf1 = pygame.Surface((1, 1), pygame.SRCALPHA, 32)
surf1.fill((255, 255, 255, 100))
surf2 = pygame.Surface((1, 1), pygame.SRCALPHA, 32)
self.assertEqual(surf2.get_at((0, 0)), (0, 0, 0, 0))
surf2.blit(surf1, (0, 0))
self.assertEqual(surf1.get_at((0, 0)), (255, 255, 255, 100))
self.assertEqual(surf2.get_at((0, 0)), (255, 255, 255, 100))
def todo_test_convert(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.convert:
# Surface.convert(Surface): return Surface
# Surface.convert(depth, flags=0): return Surface
# Surface.convert(masks, flags=0): return Surface
# Surface.convert(): return Surface
# change the pixel format of an image
#
# Creates a new copy of the Surface with the pixel format changed. The
# new pixel format can be determined from another existing Surface.
# Otherwise depth, flags, and masks arguments can be used, similar to
# the pygame.Surface() call.
#
# If no arguments are passed the new Surface will have the same pixel
# format as the display Surface. This is always the fastest format for
# blitting. It is a good idea to convert all Surfaces before they are
# blitted many times.
#
# The converted Surface will have no pixel alphas. They will be
# stripped if the original had them. See Surface.convert_alpha() for
# preserving or creating per-pixel alphas.
#
self.fail()
def test_convert__pixel_format_as_surface_subclass(self):
"""Ensure convert accepts a Surface subclass argument."""
expected_size = (23, 17)
convert_surface = SurfaceSubclass(expected_size, 0, 32)
depth_surface = SurfaceSubclass((31, 61), 0, 32)
pygame.display.init()
try:
surface = convert_surface.convert(depth_surface)
self.assertIsNot(surface, depth_surface)
self.assertIsNot(surface, convert_surface)
self.assertIsInstance(surface, pygame.Surface)
self.assertIsInstance(surface, SurfaceSubclass)
self.assertEqual(surface.get_size(), expected_size)
finally:
pygame.display.quit()
def todo_test_convert_alpha(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.convert_alpha:
# Surface.convert_alpha(Surface): return Surface
# Surface.convert_alpha(): return Surface
# change the pixel format of an image including per pixel alphas
#
# 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 blitting to the current display.
#
# Unlike the Surface.convert() 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.
#
self.fail()
def test_convert_alpha__pixel_format_as_surface_subclass(self):
"""Ensure convert_alpha accepts a Surface subclass argument."""
expected_size = (23, 17)
convert_surface = SurfaceSubclass(expected_size, SRCALPHA, 32)
depth_surface = SurfaceSubclass((31, 57), SRCALPHA, 32)
pygame.display.init()
try:
pygame.display.set_mode((60, 60))
# This is accepted as an argument, but its values are ignored.
# See issue #599.
surface = convert_surface.convert_alpha(depth_surface)
self.assertIsNot(surface, depth_surface)
self.assertIsNot(surface, convert_surface)
self.assertIsInstance(surface, pygame.Surface)
self.assertIsInstance(surface, SurfaceSubclass)
self.assertEqual(surface.get_size(), expected_size)
finally:
pygame.display.quit()
def todo_test_get_abs_offset(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.get_abs_offset:
# Surface.get_abs_offset(): return (x, y)
# find the absolute position of a child subsurface inside its top level parent
#
# Get the offset position of a child subsurface inside of its top
# level parent Surface. If the Surface is not a subsurface this will
# return (0, 0).
#
self.fail()
def todo_test_get_abs_parent(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.get_abs_parent:
# Surface.get_abs_parent(): return Surface
# find the top level parent of a subsurface
#
# Returns the parent Surface of a subsurface. If this is not a
# subsurface then this surface will be returned.
#
self.fail()
def test_get_at(self):
surf = pygame.Surface((2, 2), 0, 24)
c00 = pygame.Color(1, 2, 3)
c01 = pygame.Color(5, 10, 15)
c10 = pygame.Color(100, 50, 0)
c11 = pygame.Color(4, 5, 6)
surf.set_at((0, 0), c00)
surf.set_at((0, 1), c01)
surf.set_at((1, 0), c10)
surf.set_at((1, 1), c11)
c = surf.get_at((0, 0))
self.assertIsInstance(c, pygame.Color)
self.assertEqual(c, c00)
self.assertEqual(surf.get_at((0, 1)), c01)
self.assertEqual(surf.get_at((1, 0)), c10)
self.assertEqual(surf.get_at((1, 1)), c11)
for p in [(-1, 0), (0, -1), (2, 0), (0, 2)]:
self.assertRaises(IndexError, surf.get_at, p)
def test_get_at_mapped(self):
color = pygame.Color(10, 20, 30)
for bitsize in [8, 16, 24, 32]:
surf = pygame.Surface((2, 2), 0, bitsize)
surf.fill(color)
pixel = surf.get_at_mapped((0, 0))
self.assertEqual(
pixel,
surf.map_rgb(color),
"%i != %i, bitsize: %i" % (pixel, surf.map_rgb(color), bitsize),
)
def todo_test_get_bitsize(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.get_bitsize:
# Surface.get_bitsize(): return int
# get the bit depth of the Surface pixel format
#
# 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.
#
self.fail()
def todo_test_get_clip(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.get_clip:
# Surface.get_clip(): return Rect
# get the current clipping area of the Surface
#
# Return a rectangle of the current clipping area. The Surface will
# always return a valid rectangle that will never be outside the
# bounds of the image. If the Surface has had None set for the
# clipping area, the Surface will return a rectangle with the full
# area of the Surface.
#
self.fail()
def todo_test_get_colorkey(self):
surf = pygame.surface((2, 2), 0, 24)
self.assertIsNone(surf.get_colorykey())
colorkey = pygame.Color(20, 40, 60)
surf.set_colorkey(colorkey)
ck = surf.get_colorkey()
self.assertIsInstance(ck, pygame.Color)
self.assertEqual(ck, colorkey)
def todo_test_get_height(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.get_height:
# Surface.get_height(): return height
# get the height of the Surface
#