1import unittest
2import os
3import platform
4
5from pygame.tests import test_utils
6from pygame.tests.test_utils import example_path
7
8import pygame
9import pygame.transform
10from pygame.locals import *
11
12
13def show_image(s, images=[]):
14    # pygame.display.init()
15    size = s.get_rect()[2:]
16    screen = pygame.display.set_mode(size)
17    screen.blit(s, (0, 0))
18    pygame.display.flip()
19    pygame.event.pump()
20    going = True
21    idx = 0
22    while going:
23        events = pygame.event.get()
24        for e in events:
25            if e.type == QUIT:
26                going = False
27            if e.type == KEYDOWN:
28                if e.key in [K_s, K_a]:
29                    if e.key == K_s:
30                        idx += 1
31                    if e.key == K_a:
32                        idx -= 1
33                    s = images[idx]
34                    screen.blit(s, (0, 0))
35                    pygame.display.flip()
36                    pygame.event.pump()
37                elif e.key in [K_ESCAPE]:
38                    going = False
39    pygame.display.quit()
40    pygame.display.init()
41
42
43def threshold(
44    return_surf,
45    surf,
46    color,
47    threshold=(0, 0, 0),
48    diff_color=(0, 0, 0),
49    change_return=True,
50):
51    """ given the color it makes return_surf only have areas with the given colour.
52    """
53
54    width, height = surf.get_width(), surf.get_height()
55
56    if change_return:
57        return_surf.fill(diff_color)
58
59    try:
60        r, g, b = color
61    except ValueError:
62        r, g, b, a = color
63
64    try:
65        tr, tg, tb = color
66    except ValueError:
67        tr, tg, tb, ta = color
68
69    similar = 0
70    for y in xrange(height):
71        for x in xrange(width):
72            c1 = surf.get_at((x, y))
73
74            if (abs(c1[0] - r) < tr) & (abs(c1[1] - g) < tg) & (abs(c1[2] - b) < tb):
75                # this pixel is within the threshold.
76                if change_return:
77                    return_surf.set_at((x, y), c1)
78                similar += 1
79            # else:
80            #    print c1, c2
81
82    return similar
83
84
85class TransformModuleTest(unittest.TestCase):
86    def test_scale__alpha(self):
87        """ see if set_alpha information is kept.
88        """
89
90        s = pygame.Surface((32, 32))
91        s.set_alpha(55)
92        self.assertEqual(s.get_alpha(), 55)
93
94        s = pygame.Surface((32, 32))
95        s.set_alpha(55)
96        s2 = pygame.transform.scale(s, (64, 64))
97        s3 = s.copy()
98        self.assertEqual(s.get_alpha(), s3.get_alpha())
99        self.assertEqual(s.get_alpha(), s2.get_alpha())
100
101    def test_scale__destination(self):
102        """ see if the destination surface can be passed in to use.
103        """
104
105        s = pygame.Surface((32, 32))
106        s2 = pygame.transform.scale(s, (64, 64))
107        s3 = s2.copy()
108
109        # Also validate keyword arguments
110        s3 = pygame.transform.scale(surface=s,
111                                    size=(64, 64),
112                                    dest_surface=s3)
113        pygame.transform.scale(s, (64, 64), s2)
114
115        # the wrong size surface is past in.  Should raise an error.
116        self.assertRaises(ValueError, pygame.transform.scale, s, (33, 64), s3)
117
118        s = pygame.Surface((32, 32))
119        s2 = pygame.transform.smoothscale(s, (64, 64))
120        s3 = s2.copy()
121
122        # Also validate keyword arguments
123        s3 = pygame.transform.smoothscale(surface=s,
124                                          size=(64, 64),
125                                          dest_surface=s3)
126
127        # the wrong size surface is past in.  Should raise an error.
128        self.assertRaises(ValueError, pygame.transform.smoothscale, s, (33, 64), s3)
129
130    def test_scale__vector2(self):
131        s = pygame.Surface((32, 32))
132        s2 = pygame.transform.scale(s, pygame.Vector2(64, 64))
133        s3 = pygame.transform.smoothscale(s, pygame.Vector2(64, 64))
134
135        self.assertEqual((64,64), s2.get_size())
136        self.assertEqual((64,64), s3.get_size())
137
138    def test_scale__zero_surface_transform(self):
139        tmp_surface = pygame.transform.scale(pygame.Surface((128, 128)), (0, 0))
140        self.assertEqual(tmp_surface.get_size(), (0, 0))
141        tmp_surface = pygame.transform.scale(tmp_surface, (128, 128))
142        self.assertEqual(tmp_surface.get_size(), (128, 128))
143
144    def test_threshold__honors_third_surface(self):
145        # __doc__ for threshold as of Tue 07/15/2008
146
147        # pygame.transform.threshold(DestSurface, Surface, color, threshold =
148        # (0,0,0,0), diff_color = (0,0,0,0), change_return = True, Surface =
149        # None): return num_threshold_pixels
150
151        # When given the optional third
152        # surface, it would use the colors in that rather than the "color"
153        # specified in the function to check against.
154
155        # New in pygame 1.8
156
157        ################################################################
158        # Sizes
159        (w, h) = size = (32, 32)
160
161        # the original_color is within the threshold of the threshold_color
162        threshold = (20, 20, 20, 20)
163
164        original_color = (25, 25, 25, 25)
165        threshold_color = (10, 10, 10, 10)
166
167        # Surfaces
168        original_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
169        dest_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
170
171        # Third surface is used in lieu of 3rd position arg color
172        third_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
173
174        # Color filling
175        original_surface.fill(original_color)
176        third_surface.fill(threshold_color)
177
178        ################################################################
179        # All pixels for color should be within threshold
180        #
181        pixels_within_threshold = pygame.transform.threshold(
182            dest_surface=None,
183            surface=original_surface,
184            search_color=threshold_color,
185            threshold=threshold,
186            set_color=None,
187            set_behavior=0,
188        )
189
190        self.assertEqual(w * h, pixels_within_threshold)
191
192        ################################################################
193        # This should respect third_surface colors in place of 3rd arg
194        # color Should be the same as: surface.fill(threshold_color)
195        # all within threshold
196
197        pixels_within_threshold = pygame.transform.threshold(
198            dest_surface=None,
199            surface=original_surface,
200            search_color=None,
201            threshold=threshold,
202            set_color=None,
203            set_behavior=0,
204            search_surf=third_surface,
205        )
206        self.assertEqual(w * h, pixels_within_threshold)
207
208    def test_threshold_dest_surf_not_change(self):
209        """ the pixels within the threshold.
210
211        All pixels not within threshold are changed to set_color.
212        So there should be none changed in this test.
213        """
214        (w, h) = size = (32, 32)
215        threshold = (20, 20, 20, 20)
216        original_color = (25, 25, 25, 25)
217        original_dest_color = (65, 65, 65, 55)
218        threshold_color = (10, 10, 10, 10)
219        set_color = (255, 10, 10, 10)
220
221        surf = pygame.Surface(size, pygame.SRCALPHA, 32)
222        dest_surf = pygame.Surface(size, pygame.SRCALPHA, 32)
223        search_surf = pygame.Surface(size, pygame.SRCALPHA, 32)
224
225        surf.fill(original_color)
226        search_surf.fill(threshold_color)
227        dest_surf.fill(original_dest_color)
228
229        # set_behavior=1, set dest_surface from set_color.
230        # all within threshold of third_surface, so no color is set.
231
232        THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR = 1
233        pixels_within_threshold = pygame.transform.threshold(
234            dest_surface=dest_surf,
235            surface=surf,
236            search_color=None,
237            threshold=threshold,
238            set_color=set_color,
239            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR,
240            search_surf=search_surf,
241        )
242
243        # # Return, of pixels within threshold is correct
244        self.assertEqual(w * h, pixels_within_threshold)
245
246        # # Size of dest surface is correct
247        dest_rect = dest_surf.get_rect()
248        dest_size = dest_rect.size
249        self.assertEqual(size, dest_size)
250
251        # The color is not the change_color specified for every pixel As all
252        # pixels are within threshold
253
254        for pt in test_utils.rect_area_pts(dest_rect):
255            self.assertNotEqual(dest_surf.get_at(pt), set_color)
256            self.assertEqual(dest_surf.get_at(pt), original_dest_color)
257
258    def test_threshold_dest_surf_all_changed(self):
259        """ Lowering the threshold, expecting changed surface
260        """
261
262        (w, h) = size = (32, 32)
263        threshold = (20, 20, 20, 20)
264        original_color = (25, 25, 25, 25)
265        original_dest_color = (65, 65, 65, 55)
266        threshold_color = (10, 10, 10, 10)
267        set_color = (255, 10, 10, 10)
268
269        surf = pygame.Surface(size, pygame.SRCALPHA, 32)
270        dest_surf = pygame.Surface(size, pygame.SRCALPHA, 32)
271        search_surf = pygame.Surface(size, pygame.SRCALPHA, 32)
272
273        surf.fill(original_color)
274        search_surf.fill(threshold_color)
275        dest_surf.fill(original_dest_color)
276
277        THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR = 1
278        pixels_within_threshold = pygame.transform.threshold(
279            dest_surf,
280            surf,
281            search_color=None,
282            set_color=set_color,
283            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR,
284            search_surf=search_surf,
285        )
286
287        self.assertEqual(0, pixels_within_threshold)
288
289        dest_rect = dest_surf.get_rect()
290        dest_size = dest_rect.size
291        self.assertEqual(size, dest_size)
292
293        # The color is the set_color specified for every pixel As all
294        # pixels are not within threshold
295        for pt in test_utils.rect_area_pts(dest_rect):
296            self.assertEqual(dest_surf.get_at(pt), set_color)
297
298    def test_threshold_count(self):
299        """ counts the colors, and not changes them.
300        """
301        surf_size = (32, 32)
302        surf = pygame.Surface(surf_size, pygame.SRCALPHA, 32)
303        search_surf = pygame.Surface(surf_size, pygame.SRCALPHA, 32)
304        search_color = (55, 55, 55, 255)
305        original_color = (10, 10, 10, 255)
306
307        surf.fill(original_color)
308        # set 2 pixels to the color we are searching for.
309        surf.set_at((0, 0), search_color)
310        surf.set_at((12, 5), search_color)
311
312        # There is no destination surface, but we ask to change it.
313        #   This should be an error.
314        self.assertRaises(
315            TypeError, pygame.transform.threshold, None, surf, search_color
316        )
317        # from pygame.transform import THRESHOLD_BEHAVIOR_COUNT
318        THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF = 2
319        self.assertRaises(
320            TypeError,
321            pygame.transform.threshold,
322            None,
323            surf,
324            search_color,
325            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF,
326        )
327
328        THRESHOLD_BEHAVIOR_COUNT = 0
329        num_threshold_pixels = pygame.transform.threshold(
330            dest_surface=None,
331            surface=surf,
332            search_color=search_color,
333            set_behavior=THRESHOLD_BEHAVIOR_COUNT,
334        )
335        self.assertEqual(num_threshold_pixels, 2)
336
337    def test_threshold_search_surf(self):
338        surf_size = (32, 32)
339        surf = pygame.Surface(surf_size, pygame.SRCALPHA, 32)
340        search_surf = pygame.Surface(surf_size, pygame.SRCALPHA, 32)
341        dest_surf = pygame.Surface(surf_size, pygame.SRCALPHA, 32)
342
343        original_color = (10, 10, 10, 255)
344        search_color = (55, 55, 55, 255)
345
346        surf.fill(original_color)
347        dest_surf.fill(original_color)
348        # set 2 pixels to the color we are searching for.
349        surf.set_at((0, 0), search_color)
350        surf.set_at((12, 5), search_color)
351
352        search_surf.fill(search_color)
353
354        # We look in the other surface for matching colors.
355        #  Change it in dest_surf
356        THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF = 2
357
358        # TypeError: if search_surf is used, search_color should be None
359        self.assertRaises(
360            TypeError,
361            pygame.transform.threshold,
362            dest_surf,
363            surf,
364            search_color,
365            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF,
366            search_surf=search_surf,
367        )
368
369        # surf, dest_surf, and search_surf should all be the same size.
370        # Check surface sizes are the same size.
371        different_sized_surf = pygame.Surface((22, 33), pygame.SRCALPHA, 32)
372        self.assertRaises(
373            TypeError,
374            pygame.transform.threshold,
375            different_sized_surf,
376            surf,
377            search_color=None,
378            set_color=None,
379            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF,
380            search_surf=search_surf,
381        )
382
383        self.assertRaises(
384            TypeError,
385            pygame.transform.threshold,
386            dest_surf,
387            surf,
388            search_color=None,
389            set_color=None,
390            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF,
391            search_surf=different_sized_surf,
392        )
393
394        # We look to see if colors in search_surf are in surf.
395        num_threshold_pixels = pygame.transform.threshold(
396            dest_surface=dest_surf,
397            surface=surf,
398            search_color=None,
399            set_color=None,
400            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF,
401            search_surf=search_surf,
402        )
403
404        num_pixels_within = 2
405        self.assertEqual(num_threshold_pixels, num_pixels_within)
406
407        dest_surf.fill(original_color)
408        num_threshold_pixels = pygame.transform.threshold(
409            dest_surf,
410            surf,
411            search_color=None,
412            set_color=None,
413            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF,
414            search_surf=search_surf,
415            inverse_set=True,
416        )
417
418        self.assertEqual(num_threshold_pixels, 2)
419
420    def test_threshold_inverse_set(self):
421        """ changes the pixels within the threshold, and not outside.
422        """
423        surf_size = (32, 32)
424        _dest_surf = pygame.Surface(surf_size, pygame.SRCALPHA, 32)
425        _surf = pygame.Surface(surf_size, pygame.SRCALPHA, 32)
426
427        dest_surf = _dest_surf  # surface we are changing.
428        surf = _surf  # surface we are looking at
429        search_color = (55, 55, 55, 255)  # color we are searching for.
430        threshold = (0, 0, 0, 0)  # within this distance from search_color.
431        set_color = (245, 245, 245, 255)  # color we set.
432        inverse_set = 1  # pixels within threshold are changed to 'set_color'
433
434        original_color = (10, 10, 10, 255)
435        surf.fill(original_color)
436        # set 2 pixels to the color we are searching for.
437        surf.set_at((0, 0), search_color)
438        surf.set_at((12, 5), search_color)
439
440        dest_surf.fill(original_color)
441        # set 2 pixels to the color we are searching for.
442        dest_surf.set_at((0, 0), search_color)
443        dest_surf.set_at((12, 5), search_color)
444
445        THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR = 1
446        num_threshold_pixels = pygame.transform.threshold(
447            dest_surf,
448            surf,
449            search_color=search_color,
450            threshold=threshold,
451            set_color=set_color,
452            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR,
453            inverse_set=1,
454        )
455
456        self.assertEqual(num_threshold_pixels, 2)
457        # only two pixels changed to diff_color.
458        self.assertEqual(dest_surf.get_at((0, 0)), set_color)
459        self.assertEqual(dest_surf.get_at((12, 5)), set_color)
460
461        # other pixels should be the same as they were before.
462        # We just check one other pixel, not all of them.
463        self.assertEqual(dest_surf.get_at((2, 2)), original_color)
464
465    # XXX
466    def test_threshold_non_src_alpha(self):
467
468        result = pygame.Surface((10, 10))
469        s1 = pygame.Surface((10, 10))
470        s2 = pygame.Surface((10, 10))
471        s3 = pygame.Surface((10, 10))
472        s4 = pygame.Surface((10, 10))
473
474        x = s1.fill((0, 0, 0))
475        s1.set_at((0, 0), (32, 20, 0))
476
477        x = s2.fill((0, 20, 0))
478        x = s3.fill((0, 0, 0))
479        x = s4.fill((0, 0, 0))
480        s2.set_at((0, 0), (33, 21, 0))
481        s2.set_at((3, 0), (63, 61, 0))
482        s3.set_at((0, 0), (112, 31, 0))
483        s4.set_at((0, 0), (11, 31, 0))
484        s4.set_at((1, 1), (12, 31, 0))
485
486        self.assertEqual(s1.get_at((0, 0)), (32, 20, 0, 255))
487        self.assertEqual(s2.get_at((0, 0)), (33, 21, 0, 255))
488        self.assertEqual((0, 0), (s1.get_flags(), s2.get_flags()))
489
490        similar_color = (255, 255, 255, 255)
491        diff_color = (222, 0, 0, 255)
492        threshold_color = (20, 20, 20, 255)
493
494        THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR = 1
495        num_threshold_pixels = pygame.transform.threshold(
496            dest_surface=result,
497            surface=s1,
498            search_color=similar_color,
499            threshold=threshold_color,
500            set_color=diff_color,
501            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR,
502        )
503        self.assertEqual(num_threshold_pixels, 0)
504
505        num_threshold_pixels = pygame.transform.threshold(
506            dest_surface=result,
507            surface=s1,
508            search_color=(40, 40, 0),
509            threshold=threshold_color,
510            set_color=diff_color,
511            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR,
512        )
513        self.assertEqual(num_threshold_pixels, 1)
514
515        self.assertEqual(result.get_at((0, 0)), diff_color)
516
517    def test_threshold__uneven_colors(self):
518        (w, h) = size = (16, 16)
519
520        original_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
521        dest_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
522
523        original_surface.fill(0)
524
525        threshold_color_template = [5, 5, 5, 5]
526        threshold_template = [6, 6, 6, 6]
527
528        ################################################################
529
530        for pos in range(len("rgb")):
531            threshold_color = threshold_color_template[:]
532            threshold = threshold_template[:]
533
534            threshold_color[pos] = 45
535            threshold[pos] = 50
536
537            pixels_within_threshold = pygame.transform.threshold(
538                None,
539                original_surface,
540                threshold_color,
541                threshold,
542                set_color=None,
543                set_behavior=0,
544            )
545
546            self.assertEqual(w * h, pixels_within_threshold)
547
548        ################################################################
549
550    def test_threshold_set_behavior2(self):
551        """ raises an error when set_behavior=2 and set_color is not None.
552        """
553        from pygame.transform import threshold
554
555        s1 = pygame.Surface((32, 32), SRCALPHA, 32)
556        s2 = pygame.Surface((32, 32), SRCALPHA, 32)
557        THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF = 2
558        self.assertRaises(
559            TypeError,
560            threshold,
561            dest_surface=s2,
562            surface=s1,
563            search_color=(30, 30, 30),
564            threshold=(11, 11, 11),
565            set_color=(255, 0, 0),
566            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF,
567        )
568
569    def test_threshold_set_behavior0(self):
570        """ raises an error when set_behavior=1
571                and set_color is not None,
572                and dest_surf is not None.
573        """
574        from pygame.transform import threshold
575
576        s1 = pygame.Surface((32, 32), SRCALPHA, 32)
577        s2 = pygame.Surface((32, 32), SRCALPHA, 32)
578        THRESHOLD_BEHAVIOR_COUNT = 0
579
580        self.assertRaises(
581            TypeError,
582            threshold,
583            dest_surface=None,
584            surface=s2,
585            search_color=(30, 30, 30),
586            threshold=(11, 11, 11),
587            set_color=(0, 0, 0),
588            set_behavior=THRESHOLD_BEHAVIOR_COUNT,
589        )
590
591        self.assertRaises(
592            TypeError,
593            threshold,
594            dest_surface=s1,
595            surface=s2,
596            search_color=(30, 30, 30),
597            threshold=(11, 11, 11),
598            set_color=None,
599            set_behavior=THRESHOLD_BEHAVIOR_COUNT,
600        )
601
602        threshold(
603            dest_surface=None,
604            surface=s2,
605            search_color=(30, 30, 30),
606            threshold=(11, 11, 11),
607            set_color=None,
608            set_behavior=THRESHOLD_BEHAVIOR_COUNT,
609        )
610
611    def test_threshold_from_surface(self):
612        """ Set similar pixels in 'dest_surf' to color in the 'surf'.
613        """
614        from pygame.transform import threshold
615
616        surf = pygame.Surface((32, 32), SRCALPHA, 32)
617        dest_surf = pygame.Surface((32, 32), SRCALPHA, 32)
618        surf_color = (40, 40, 40, 255)
619        dest_color = (255, 255, 255)
620        surf.fill(surf_color)
621        dest_surf.fill(dest_color)
622        THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF = 2
623
624        num_threshold_pixels = threshold(
625            dest_surface=dest_surf,
626            surface=surf,
627            search_color=(30, 30, 30),
628            threshold=(11, 11, 11),
629            set_color=None,
630            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF,
631            inverse_set=1,
632        )
633
634        self.assertEqual(
635            num_threshold_pixels, dest_surf.get_height() * dest_surf.get_width()
636        )
637        self.assertEqual(dest_surf.get_at((0, 0)), surf_color)
638
639    def test_threshold__surface(self):
640        """
641        """
642        from pygame.transform import threshold
643
644        s1 = pygame.Surface((32, 32), SRCALPHA, 32)
645        s2 = pygame.Surface((32, 32), SRCALPHA, 32)
646        s3 = pygame.Surface((1, 1), SRCALPHA, 32)
647        THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF = 2
648
649        #     # only one pixel should not be changed.
650        #     s1.fill((40,40,40))
651        #     s2.fill((255,255,255))
652        #     s1.set_at( (0,0), (170, 170, 170) )
653        #     # set the similar pixels in destination surface to the color
654        #     #     in the first surface.
655        #     num_threshold_pixels = threshold(
656        #         dest_surface=s2,
657        #         surface=s1,
658        #         search_color=(30,30,30),
659        #         threshold=(11,11,11),
660        #         set_color=None,
661        #         set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_SURF)
662
663        #     #num_threshold_pixels = threshold(s2, s1, (30,30,30))
664        #     self.assertEqual(num_threshold_pixels, (s1.get_height() * s1.get_width()) -1)
665        #     self.assertEqual(s2.get_at((0,0)), (0,0,0, 255))
666        #     self.assertEqual(s2.get_at((0,1)), (40, 40, 40, 255))
667        #     self.assertEqual(s2.get_at((17,1)), (40, 40, 40, 255))
668
669        # # abs(40 - 255) < 100
670        # #(abs(c1[0] - r) < tr)
671
672        #     s1.fill((160,160,160))
673        #     s2.fill((255,255,255))
674        #     num_threshold_pixels = threshold(s2, s1, (255,255,255), (100,100,100), (0,0,0), True)
675
676        #     self.assertEqual(num_threshold_pixels, (s1.get_height() * s1.get_width()))
677
678        # only one pixel should not be changed.
679        s1.fill((40, 40, 40))
680        s1.set_at((0, 0), (170, 170, 170))
681        THRESHOLD_BEHAVIOR_COUNT = 0
682
683        num_threshold_pixels = threshold(
684            dest_surface=None,
685            surface=s1,
686            search_color=(30, 30, 30),
687            threshold=(11, 11, 11),
688            set_color=None,
689            set_behavior=THRESHOLD_BEHAVIOR_COUNT,
690        )
691
692        # num_threshold_pixels = threshold(s2, s1, (30,30,30))
693        self.assertEqual(num_threshold_pixels, (s1.get_height() * s1.get_width()) - 1)
694
695        # test end markers.  0, and 255
696
697        # the pixels are different by 1.
698        s1.fill((254, 254, 254))
699        s2.fill((255, 255, 255))
700        s3.fill((255, 255, 255))
701        s1.set_at((0, 0), (170, 170, 170))
702        num_threshold_pixels = threshold(
703            None, s1, (254, 254, 254), (1, 1, 1), None, THRESHOLD_BEHAVIOR_COUNT
704        )
705        self.assertEqual(num_threshold_pixels, (s1.get_height() * s1.get_width()) - 1)
706
707        # compare the two surfaces.  Should be all but one matching.
708        num_threshold_pixels = threshold(
709            None, s1, None, (1, 1, 1), None, THRESHOLD_BEHAVIOR_COUNT, s2
710        )
711        self.assertEqual(num_threshold_pixels, (s1.get_height() * s1.get_width()) - 1)
712
713        # within (0,0,0) threshold?  Should match no pixels.
714        num_threshold_pixels = threshold(
715            None, s1, (253, 253, 253), (0, 0, 0), None, THRESHOLD_BEHAVIOR_COUNT
716        )
717        self.assertEqual(num_threshold_pixels, 0)
718
719        # other surface within (0,0,0) threshold?  Should match no pixels.
720        num_threshold_pixels = threshold(
721            None, s1, None, (0, 0, 0), None, THRESHOLD_BEHAVIOR_COUNT, s2
722        )
723        self.assertEqual(num_threshold_pixels, 0)
724
725    def test_threshold__subclassed_surface(self):
726        """Ensure threshold accepts subclassed surfaces."""
727        expected_size = (13, 11)
728        expected_flags = 0
729        expected_depth = 32
730        expected_color = (90, 80, 70, 255)
731        expected_count = 0
732        surface = test_utils.SurfaceSubclass(
733            expected_size, expected_flags, expected_depth
734        )
735        dest_surface = test_utils.SurfaceSubclass(
736            expected_size, expected_flags, expected_depth
737        )
738        search_surface = test_utils.SurfaceSubclass(
739            expected_size, expected_flags, expected_depth
740        )
741        surface.fill((10, 10, 10))
742        dest_surface.fill((255, 255, 255))
743        search_surface.fill((20, 20, 20))
744
745        count = pygame.transform.threshold(
746            dest_surface=dest_surface,
747            surface=surface,
748            threshold=(1, 1, 1),
749            set_color=expected_color,
750            search_color=None,
751            search_surf=search_surface,
752        )
753
754        self.assertIsInstance(dest_surface, pygame.Surface)
755        self.assertIsInstance(dest_surface, test_utils.SurfaceSubclass)
756        self.assertEqual(count, expected_count)
757        self.assertEqual(dest_surface.get_at((0, 0)), expected_color)
758        self.assertEqual(dest_surface.get_bitsize(), expected_depth)
759        self.assertEqual(dest_surface.get_size(), expected_size)
760        self.assertEqual(dest_surface.get_flags(), expected_flags)
761
762    def test_laplacian(self):
763        """
764        """
765
766        SIZE = 32
767        s1 = pygame.Surface((SIZE, SIZE))
768        s2 = pygame.Surface((SIZE, SIZE))
769        s1.fill((10, 10, 70))
770        pygame.draw.line(s1, (255, 0, 0), (3, 10), (20, 20))
771
772        # a line at the last row of the image.
773        pygame.draw.line(s1, (255, 0, 0), (0, 31), (31, 31))
774
775        pygame.transform.laplacian(s1, s2)
776
777        # show_image(s1)
778        # show_image(s2)
779
780        self.assertEqual(s2.get_at((0, 0)), (0, 0, 0, 255))
781        self.assertEqual(s2.get_at((3, 10)), (255, 0, 0, 255))
782        self.assertEqual(s2.get_at((0, 31)), (255, 0, 0, 255))
783        self.assertEqual(s2.get_at((31, 31)), (255, 0, 0, 255))
784
785        # here we create the return surface.
786        s2 = pygame.transform.laplacian(s1)
787
788        self.assertEqual(s2.get_at((0, 0)), (0, 0, 0, 255))
789        self.assertEqual(s2.get_at((3, 10)), (255, 0, 0, 255))
790        self.assertEqual(s2.get_at((0, 31)), (255, 0, 0, 255))
791        self.assertEqual(s2.get_at((31, 31)), (255, 0, 0, 255))
792
793    def test_laplacian__24_big_endian(self):
794        """
795        """
796        pygame.display.init()
797        try:
798            surf_1 = pygame.image.load(
799                example_path(os.path.join("data", "laplacian.png")))
800            SIZE = 32
801            surf_2 = pygame.Surface((SIZE, SIZE), 0, 24)
802            # s1.fill((10, 10, 70))
803            # pygame.draw.line(s1, (255, 0, 0), (3, 10), (20, 20))
804
805            # a line at the last row of the image.
806            # pygame.draw.line(s1, (255, 0, 0), (0, 31), (31, 31))
807
808            # Also validate keyword arguments
809            pygame.transform.laplacian(surface=surf_1, dest_surface=surf_2)
810
811            # show_image(s1)
812            # show_image(s2)
813
814            self.assertEqual(surf_2.get_at((0, 0)), (0, 0, 0, 255))
815            self.assertEqual(surf_2.get_at((3, 10)), (255, 0, 0, 255))
816            self.assertEqual(surf_2.get_at((0, 31)), (255, 0, 0, 255))
817            self.assertEqual(surf_2.get_at((31, 31)), (255, 0, 0, 255))
818
819            # here we create the return surface.
820            surf_2 = pygame.transform.laplacian(surf_1)
821
822            self.assertEqual(surf_2.get_at((0, 0)), (0, 0, 0, 255))
823            self.assertEqual(surf_2.get_at((3, 10)), (255, 0, 0, 255))
824            self.assertEqual(surf_2.get_at((0, 31)), (255, 0, 0, 255))
825            self.assertEqual(surf_2.get_at((31, 31)), (255, 0, 0, 255))
826        finally:
827            pygame.display.quit()
828
829    def test_average_surfaces(self):
830        """
831        """
832
833        SIZE = 32
834        s1 = pygame.Surface((SIZE, SIZE))
835        s2 = pygame.Surface((SIZE, SIZE))
836        s3 = pygame.Surface((SIZE, SIZE))
837        s1.fill((10, 10, 70))
838        s2.fill((10, 20, 70))
839        s3.fill((10, 130, 10))
840
841        surfaces = [s1, s2, s3]
842        surfaces = [s1, s2]
843        sr = pygame.transform.average_surfaces(surfaces)
844
845        self.assertEqual(sr.get_at((0, 0)), (10, 15, 70, 255))
846
847        self.assertRaises(TypeError, pygame.transform.average_surfaces, 1)
848        self.assertRaises(TypeError, pygame.transform.average_surfaces, [])
849
850        self.assertRaises(TypeError, pygame.transform.average_surfaces, [1])
851        self.assertRaises(TypeError, pygame.transform.average_surfaces, [s1, 1])
852        self.assertRaises(TypeError, pygame.transform.average_surfaces, [1, s1])
853        self.assertRaises(TypeError, pygame.transform.average_surfaces, [s1, s2, 1])
854
855        self.assertRaises(
856            TypeError, pygame.transform.average_surfaces, (s for s in [s1, s2, s3])
857        )
858
859    def test_average_surfaces__24(self):
860
861        SIZE = 32
862        depth = 24
863        s1 = pygame.Surface((SIZE, SIZE), 0, depth)
864        s2 = pygame.Surface((SIZE, SIZE), 0, depth)
865        s3 = pygame.Surface((SIZE, SIZE), 0, depth)
866        s1.fill((10, 10, 70, 255))
867        s2.fill((10, 20, 70, 255))
868        s3.fill((10, 130, 10, 255))
869
870        surfaces = [s1, s2, s3]
871        sr = pygame.transform.average_surfaces(surfaces)
872        self.assertEqual(sr.get_masks(), s1.get_masks())
873        self.assertEqual(sr.get_flags(), s1.get_flags())
874        self.assertEqual(sr.get_losses(), s1.get_losses())
875
876        if 0:
877            print(sr, s1)
878            print(sr.get_masks(), s1.get_masks())
879            print(sr.get_flags(), s1.get_flags())
880            print(sr.get_losses(), s1.get_losses())
881            print(sr.get_shifts(), s1.get_shifts())
882
883        self.assertEqual(sr.get_at((0, 0)), (10, 53, 50, 255))
884
885    def test_average_surfaces__24_big_endian(self):
886        pygame.display.init()
887        try:
888            surf_1 = pygame.image.load(
889                example_path(os.path.join("data", "BGR.png")))
890
891            surf_2 = surf_1.copy()
892
893            surfaces = [surf_1, surf_2]
894            self.assertEqual(surf_1.get_at((0, 0)), (255, 0, 0, 255))
895            self.assertEqual(surf_2.get_at((0, 0)), (255, 0, 0, 255))
896
897            surf_av = pygame.transform.average_surfaces(surfaces)
898            self.assertEqual(surf_av.get_masks(), surf_1.get_masks())
899            self.assertEqual(surf_av.get_flags(), surf_1.get_flags())
900            self.assertEqual(surf_av.get_losses(), surf_1.get_losses())
901
902            self.assertEqual(surf_av.get_at((0, 0)), (255, 0, 0, 255))
903        finally:
904            pygame.display.quit()
905
906    def test_average_surfaces__subclassed_surfaces(self):
907        """Ensure average_surfaces accepts subclassed surfaces."""
908        expected_size = (23, 17)
909        expected_flags = 0
910        expected_depth = 32
911        expected_color = (50, 50, 50, 255)
912        surfaces = []
913
914        for color in ((40, 60, 40), (60, 40, 60)):
915            s = test_utils.SurfaceSubclass(
916                expected_size, expected_flags, expected_depth
917            )
918            s.fill(color)
919            surfaces.append(s)
920
921        surface = pygame.transform.average_surfaces(surfaces)
922
923        self.assertIsInstance(surface, pygame.Surface)
924        self.assertNotIsInstance(surface, test_utils.SurfaceSubclass)
925        self.assertEqual(surface.get_at((0, 0)), expected_color)
926        self.assertEqual(surface.get_bitsize(), expected_depth)
927        self.assertEqual(surface.get_size(), expected_size)
928        self.assertEqual(surface.get_flags(), expected_flags)
929
930    def test_average_surfaces__subclassed_destination_surface(self):
931        """Ensure average_surfaces accepts a destination subclassed surface."""
932        expected_size = (13, 27)
933        expected_flags = 0
934        expected_depth = 32
935        expected_color = (15, 15, 15, 255)
936        surfaces = []
937
938        for color in ((10, 10, 20), (20, 20, 10), (30, 30, 30)):
939            s = test_utils.SurfaceSubclass(
940                expected_size, expected_flags, expected_depth
941            )
942            s.fill(color)
943            surfaces.append(s)
944        expected_dest_surface = surfaces.pop()
945
946        # Also validate keyword arguments
947        dest_surface = pygame.transform.average_surfaces(
948            surfaces=surfaces, dest_surface=expected_dest_surface
949        )
950
951        self.assertIsInstance(dest_surface, pygame.Surface)
952        self.assertIsInstance(dest_surface, test_utils.SurfaceSubclass)
953        self.assertIs(dest_surface, expected_dest_surface)
954        self.assertEqual(dest_surface.get_at((0, 0)), expected_color)
955        self.assertEqual(dest_surface.get_bitsize(), expected_depth)
956        self.assertEqual(dest_surface.get_size(), expected_size)
957        self.assertEqual(dest_surface.get_flags(), expected_flags)
958
959    def test_average_color(self):
960        """
961        """
962
963        a = [24, 32]
964        for i in a:
965            s = pygame.Surface((32, 32), 0, i)
966            s.fill((0, 100, 200))
967            s.fill((10, 50, 100), (0, 0, 16, 32))
968
969            self.assertEqual(pygame.transform.average_color(s), (5, 75, 150, 0))
970
971            # Also validate keyword arguments
972            avg_color = pygame.transform.average_color(surface=s, rect=(16, 0, 16, 32))
973            self.assertEqual(avg_color, (0, 100, 200, 0))
974
975    def test_rotate(self):
976        #setting colors and canvas
977        blue = (0, 0, 255, 255)
978        red = (255, 0, 0, 255)
979        black = (0,0,0)
980        canvas = pygame.Surface((3, 3))
981        rotation = 0
982
983        canvas.set_at((2, 0), blue)
984        canvas.set_at((0, 2), red)
985
986        self.assertEqual(canvas.get_at((0, 0)), black)
987        self.assertEqual(canvas.get_at((2, 0)), blue)
988        self.assertEqual(canvas.get_at((0, 2)), red)
989
990        for i in range(0,4):
991            if i % 2 == 0:
992                self.assertEqual(canvas.get_at((0, 0)), black)
993            elif i == 1:
994                self.assertEqual(canvas.get_at((0, 0)), blue)
995            elif i == 3:
996                self.assertEqual(canvas.get_at((0, 0)), red)
997
998            rotation+=90
999            # Also validate keyword arguments
1000            canvas = pygame.transform.rotate(surface=canvas, angle=90)
1001
1002        self.assertEqual(canvas.get_at((0, 0)), black)
1003
1004    def test_rotate_of_0_sized_surface(self):
1005        # This function just tests possible Segmentation Fault
1006        canvas1 = pygame.Surface((0, 1))
1007        canvas2 = pygame.Surface((1, 0))
1008        pygame.transform.rotate(canvas1, 42)
1009        pygame.transform.rotate(canvas2, 42)
1010
1011    def test_rotate__lossless_at_90_degrees(self):
1012        w, h = 32, 32
1013        s = pygame.Surface((w, h), pygame.SRCALPHA)
1014
1015        gradient = list(test_utils.gradient(w, h))
1016
1017        for pt, color in gradient:
1018            s.set_at(pt, color)
1019
1020        for rotation in (90, -90):
1021            s = pygame.transform.rotate(s, rotation)
1022
1023        for pt, color in gradient:
1024            self.assertTrue(s.get_at(pt) == color)
1025
1026    def test_scale2x(self):
1027
1028        # __doc__ (as of 2008-06-25) for pygame.transform.scale2x:
1029
1030        # pygame.transform.scale2x(Surface, DestSurface = None): Surface
1031        # specialized image doubler
1032
1033        w, h = 32, 32
1034        s = pygame.Surface((w, h), pygame.SRCALPHA, 32)
1035
1036        # s.set_at((0,0), (20, 20, 20, 255))
1037
1038        s1 = pygame.transform.scale2x(s)
1039        # Also validate keyword arguments
1040        s2 = pygame.transform.scale2x(surface=s)
1041        self.assertEqual(s1.get_rect().size, (64, 64))
1042        self.assertEqual(s2.get_rect().size, (64, 64))
1043
1044    def test_scale2xraw(self):
1045        w, h = 32, 32
1046        s = pygame.Surface((w, h), pygame.SRCALPHA, 32)
1047        s.fill((0, 0, 0))
1048        pygame.draw.circle(s, (255, 0, 0), (w // 2, h // 2), (w // 3))
1049
1050        s2 = pygame.transform.scale(s, (w * 2, h * 2))
1051        s2_2 = pygame.transform.scale(s2, (w * 4, h * 4))
1052        s4 = pygame.transform.scale(s, (w * 4, h * 4))
1053
1054        self.assertEqual(s2_2.get_rect().size, (128, 128))
1055
1056        for pt in test_utils.rect_area_pts(s2_2.get_rect()):
1057            self.assertEqual(s2_2.get_at(pt), s4.get_at(pt))
1058
1059    def test_get_smoothscale_backend(self):
1060        filter_type = pygame.transform.get_smoothscale_backend()
1061        self.assertTrue(filter_type in ["GENERIC", "MMX", "SSE"])
1062        # It would be nice to test if a non-generic type corresponds to an x86
1063        # processor. But there is no simple test for this. platform.machine()
1064        # returns process version specific information, like 'i686'.
1065
1066    def test_set_smoothscale_backend(self):
1067        # All machines should allow 'GENERIC'.
1068        original_type = pygame.transform.get_smoothscale_backend()
1069        pygame.transform.set_smoothscale_backend("GENERIC")
1070        filter_type = pygame.transform.get_smoothscale_backend()
1071        self.assertEqual(filter_type, "GENERIC")
1072        # All machines should allow returning to original value.
1073        # Also check that keyword argument works.
1074        pygame.transform.set_smoothscale_backend(backend=original_type)
1075        # Something invalid.
1076        def change():
1077            pygame.transform.set_smoothscale_backend("mmx")
1078
1079        self.assertRaises(ValueError, change)
1080        # Invalid argument keyword.
1081        def change():
1082            pygame.transform.set_smoothscale_backend(t="GENERIC")
1083
1084        self.assertRaises(TypeError, change)
1085        # Invalid argument type.
1086        def change():
1087            pygame.transform.set_smoothscale_backend(1)
1088
1089        self.assertRaises(TypeError, change)
1090        # Unsupported type, if possible.
1091        if original_type != "SSE":
1092
1093            def change():
1094                pygame.transform.set_smoothscale_backend("SSE")
1095
1096            self.assertRaises(ValueError, change)
1097        # Should be back where we started.
1098        filter_type = pygame.transform.get_smoothscale_backend()
1099        self.assertEqual(filter_type, original_type)
1100
1101    def test_chop(self):
1102        original_surface = pygame.Surface((20, 20))
1103        pygame.draw.rect(original_surface, (255, 0, 0), (0, 0, 10, 10))
1104        pygame.draw.rect(original_surface, (0, 255, 0), (0, 10, 10, 10))
1105        pygame.draw.rect(original_surface, (0, 0, 255), (10, 0, 10, 10))
1106        pygame.draw.rect(original_surface, (255, 255, 0), (10, 10, 10, 10))
1107        # Test chopping the corner of image
1108        rect = pygame.Rect(0, 0, 5, 15)
1109        test_surface = pygame.transform.chop(original_surface, rect)
1110        # Check the size of chopped image
1111        self.assertEqual(test_surface.get_size(), (15, 5))
1112        # Check if the colors of the chopped image are correct
1113        for x in range(15):
1114            for y in range(5):
1115                if x < 5:
1116                    self.assertEqual(test_surface.get_at((x, y)), (0, 255, 0))
1117                else:
1118                    self.assertEqual(test_surface.get_at((x, y)),
1119                                     (255, 255, 0))
1120        # Check if the original image stayed the same
1121        self.assertEqual(original_surface.get_size(), (20, 20))
1122        for x in range(20):
1123            for y in range(20):
1124                if x < 10 and y < 10:
1125                    self.assertEqual(original_surface.get_at((x, y)),
1126                                     (255, 0, 0))
1127                if x < 10 < y:
1128                    self.assertEqual(original_surface.get_at((x, y)),
1129                                     (0, 255, 0))
1130                if x > 10 > y:
1131                    self.assertEqual(original_surface.get_at((x, y)),
1132                                     (0, 0, 255))
1133                if x > 10 and y > 10:
1134                    self.assertEqual(original_surface.get_at((x, y)),
1135                                     (255, 255, 0))
1136        # Test chopping the center of the surface:
1137        rect = pygame.Rect(0, 0, 10, 10)
1138        rect.center = original_surface.get_rect().center
1139        # Also validate keyword arguments
1140        test_surface = pygame.transform.chop(surface=original_surface,
1141                                             rect=rect)
1142        self.assertEqual(test_surface.get_size(), (10, 10))
1143        for x in range(10):
1144            for y in range(10):
1145                if x < 5 and y < 5:
1146                    self.assertEqual(test_surface.get_at((x, y)), (255, 0, 0))
1147                if x < 5 < y:
1148                    self.assertEqual(test_surface.get_at((x, y)), (0, 255, 0))
1149                if x > 5 > y:
1150                    self.assertEqual(test_surface.get_at((x, y)), (0, 0, 255))
1151                if x > 5 and y > 5:
1152                    self.assertEqual(test_surface.get_at((x, y)), (255, 255, 0))
1153        # Test chopping with the empty rect
1154        rect = pygame.Rect(10, 10, 0, 0)
1155        test_surface = pygame.transform.chop(original_surface, rect)
1156        self.assertEqual(test_surface.get_size(), (20, 20))
1157        # Test chopping the entire surface
1158        rect = pygame.Rect(0, 0, 20, 20)
1159        test_surface = pygame.transform.chop(original_surface, rect)
1160        self.assertEqual(test_surface.get_size(), (0, 0))
1161        # Test chopping outside of surface
1162        rect = pygame.Rect(5, 15, 20, 20)
1163        test_surface = pygame.transform.chop(original_surface, rect)
1164        self.assertEqual(test_surface.get_size(), (5, 15))
1165        rect = pygame.Rect(400, 400, 10, 10)
1166        test_surface = pygame.transform.chop(original_surface, rect)
1167        self.assertEqual(test_surface.get_size(), (20, 20))
1168
1169    def test_rotozoom(self):
1170
1171        # __doc__ (as of 2008-08-02) for pygame.transform.rotozoom:
1172
1173        # pygame.transform.rotozoom(Surface, angle, scale): return Surface
1174        # filtered scale and rotation
1175        #
1176        # This is a combined scale and rotation transform. The resulting
1177        # Surface will be a filtered 32-bit Surface. The scale argument is a
1178        # floating point value that will be multiplied by the current
1179        # resolution. The angle argument is a floating point value that
1180        # represents the counterclockwise degrees to rotate. A negative
1181        # rotation angle will rotate clockwise.
1182
1183        s = pygame.Surface((10, 0))
1184        pygame.transform.scale(s, (10, 2))
1185        s1=pygame.transform.rotozoom(s, 30, 1)
1186        # Also validate keyword arguments
1187        s2=pygame.transform.rotozoom(surface=s, angle=30, scale=1)
1188
1189        self.assertEqual(s1.get_rect(), pygame.Rect(0,0,0,0))
1190        self.assertEqual(s2.get_rect(), pygame.Rect(0,0,0,0))
1191
1192    def test_smoothscale(self):
1193        """Tests the stated boundaries, sizing, and color blending of smoothscale function"""
1194        # __doc__ (as of 2008-08-02) for pygame.transform.smoothscale:
1195
1196        # pygame.transform.smoothscale(Surface, (width, height), DestSurface =
1197        # None): return Surface
1198        #
1199        # scale a surface to an arbitrary size smoothly
1200        #
1201        # Uses one of two different algorithms for scaling each dimension of
1202        # the input surface as required.  For shrinkage, the output pixels are
1203        # area averages of the colors they cover.  For expansion, a bilinear
1204        # filter is used. For the amd64 and i686 architectures, optimized MMX
1205        # routines are included and will run much faster than other machine
1206        # types. The size is a 2 number sequence for (width, height). This
1207        # function only works for 24-bit or 32-bit surfaces.  An exception
1208        # will be thrown if the input surface bit depth is less than 24.
1209        #
1210        # New in pygame 1.8
1211
1212        #check stated exceptions
1213        def smoothscale_low_bpp():
1214            starting_surface = pygame.Surface((20, 20), depth=12)
1215            smoothscaled_surface = pygame.transform.smoothscale(starting_surface, (10, 10))
1216        self.assertRaises(ValueError, smoothscale_low_bpp)
1217
1218        def smoothscale_high_bpp():
1219            starting_surface = pygame.Surface((20, 20), depth=48)
1220            smoothscaled_surface = pygame.transform.smoothscale(starting_surface, (10, 10))
1221        self.assertRaises(ValueError, smoothscale_high_bpp)
1222
1223        def smoothscale_invalid_scale():
1224            starting_surface = pygame.Surface((20, 20), depth=32)
1225            smoothscaled_surface = pygame.transform.smoothscale(starting_surface, (-1, -1))
1226        self.assertRaises(ValueError, smoothscale_invalid_scale)
1227
1228        # Test Color Blending Scaling-Up
1229        two_pixel_surface = pygame.Surface((2, 1), depth=32)
1230        two_pixel_surface.fill(pygame.Color(0, 0, 0), pygame.Rect(0, 0, 1, 1))
1231        two_pixel_surface.fill(pygame.Color(255, 255, 255), pygame.Rect(1, 0, 1, 1))
1232        for k in [2**x for x in range(5,8)]: # Enlarge to targets 32, 64...256
1233            bigger_surface = pygame.transform.smoothscale(two_pixel_surface, (k, 1))
1234            self.assertEqual(bigger_surface.get_at((k//2, 0)), pygame.Color(127, 127, 127))
1235            self.assertEqual(bigger_surface.get_size(), (k, 1))
1236        # Test Color Blending Scaling-Down
1237        two_five_six_surf = pygame.Surface((256, 1), depth=32)
1238        two_five_six_surf.fill(pygame.Color(0, 0, 0), pygame.Rect(0, 0, 128, 1))
1239        two_five_six_surf.fill(pygame.Color(255, 255, 255), pygame.Rect(128, 0, 128, 1))
1240        for k in range(3, 11, 2): #Shrink to targets 3, 5...11 pixels wide
1241            smaller_surface = pygame.transform.smoothscale(two_five_six_surf, (k, 1))
1242            self.assertEqual(smaller_surface.get_at(((k//2), 0)), pygame.Color(127, 127, 127))
1243            self.assertEqual(smaller_surface.get_size(), (k, 1))
1244
1245class TransformDisplayModuleTest(unittest.TestCase):
1246    def setUp(self):
1247        pygame.display.init()
1248        pygame.display.set_mode((320, 200))
1249
1250    def tearDown(self):
1251        pygame.display.quit()
1252
1253    def test_flip(self):
1254        """ honors the set_color key on the returned surface from flip.
1255        """
1256        image_loaded = pygame.image.load(example_path("data/chimp.png"))
1257
1258        image = pygame.Surface(image_loaded.get_size(), 0, 32)
1259        image.blit(image_loaded, (0, 0))
1260
1261        image_converted = image_loaded.convert()
1262
1263        self.assertFalse(image.get_flags() & pygame.SRCALPHA)
1264        self.assertFalse(image_converted.get_flags() & pygame.SRCALPHA)
1265
1266        surf = pygame.Surface(image.get_size(), 0, 32)
1267        surf2 = pygame.Surface(image.get_size(), 0, 32)
1268
1269        surf.fill((255, 255, 255))
1270        surf2.fill((255, 255, 255))
1271
1272        colorkey = image.get_at((0, 0))
1273        image.set_colorkey(colorkey, RLEACCEL)
1274        timage = pygame.transform.flip(image, 1, 0)
1275
1276        colorkey = image_converted.get_at((0, 0))
1277        image_converted.set_colorkey(colorkey, RLEACCEL)
1278        # Also validate keyword arguments
1279        timage_converted = pygame.transform.flip(surface=image_converted,
1280                                                 flip_x=1,
1281                                                 flip_y=0)
1282
1283        # blit the flipped surface, and non flipped surface.
1284        surf.blit(timage, (0, 0))
1285        surf2.blit(image, (0, 0))
1286
1287        # the results should be the same.
1288        self.assertEqual(surf.get_at((0, 0)), surf2.get_at((0, 0)))
1289        self.assertEqual(surf2.get_at((0, 0)), (255, 255, 255, 255))
1290
1291        # now we test the convert() ed image also works.
1292        surf.fill((255, 255, 255))
1293        surf2.fill((255, 255, 255))
1294        surf.blit(timage_converted, (0, 0))
1295        surf2.blit(image_converted, (0, 0))
1296        self.assertEqual(surf.get_at((0, 0)), surf2.get_at((0, 0)))
1297
1298    def test_flip_alpha(self):
1299        """ returns a surface with the same properties as the input.
1300        """
1301        image_loaded = pygame.image.load(example_path("data/chimp.png"))
1302
1303        image_alpha = pygame.Surface(image_loaded.get_size(), pygame.SRCALPHA, 32)
1304        image_alpha.blit(image_loaded, (0, 0))
1305
1306        surf = pygame.Surface(image_loaded.get_size(), 0, 32)
1307        surf2 = pygame.Surface(image_loaded.get_size(), 0, 32)
1308
1309        colorkey = image_alpha.get_at((0, 0))
1310        image_alpha.set_colorkey(colorkey, RLEACCEL)
1311        timage_alpha = pygame.transform.flip(image_alpha, 1, 0)
1312
1313        self.assertTrue(image_alpha.get_flags() & pygame.SRCALPHA)
1314        self.assertTrue(timage_alpha.get_flags() & pygame.SRCALPHA)
1315
1316        # now we test the alpha image works.
1317        surf.fill((255, 255, 255))
1318        surf2.fill((255, 255, 255))
1319        surf.blit(timage_alpha, (0, 0))
1320        surf2.blit(image_alpha, (0, 0))
1321        self.assertEqual(surf.get_at((0, 0)), surf2.get_at((0, 0)))
1322        self.assertEqual(surf2.get_at((0, 0)), (255, 0, 0, 255))
1323
1324
1325if __name__ == "__main__":
1326    unittest.main()
1327