Source

pygame / test / sdl_video_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
import sys
import unittest

try:
    import StringIO as stringio
except ImportError:
    import io as stringio

import copy
import pygame2
from pygame2.colorpalettes import CGAPALETTE
from pygame2 import Rect, Color
import pygame2.sdl.video as video
import pygame2.sdl.image as image
import pygame2.sdlimage as sdlimage
import pygame2.sdl.constants as constants

class SDLVideoSurfaceTest (unittest.TestCase):

    def _cmppixels (self, sf1, sf2):
        # Simple pixel comparision
        w1, h1 = sf1.size
        w2, h2 = sf2.size
        w, h = min (w1, w2), min (h1, h2)
        getat1 = sf1.get_at
        getat2 = sf2.get_at
        failmsg = "%s != %s at (%d, %d)"
        for x in range (w):
            for y in range (h):
                self.failUnlessEqual (getat1 (x, y), getat2 (x, y),
                    failmsg % (getat1 (x, y), getat2 (x, y), x, y))

    def _cmpcolor (self, sf, color, area=None):
        # Simple color comparision with clip area support
        getat = sf.get_at
        sx, sy = 0, 0
        w, h = sf.size
        if area:
            sx, sy = area.x, area.y 
            w, h = area.w, area.h
        c = sf.format.get_rgba (color)
        failmsg = "%s != %s at (%d, %d)"
        for x in range (sx, sx + w):
            for y in range (sy, sy + h):
                self.failUnlessEqual (getat (x, y), c,
                    failmsg % (getat (x, y), c, x, y))


    def setUp (self):
        video.init ()

    def tearDown (self):
        video.quit ()
    
    def test_pygame2_sdl_video_Surface_blit(self):
        # This is done in sdl_video_surface_blit_test.py
        pass

    def test_pygame2_sdl_video_Surface_clip_rect(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.clip_rect:

        # Gets or sets the current clipping rectangle for
        # operations on the Surface.
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            sf.fill (Color (255, 255, 255))
            sf.clip_rect = Rect (3, 3, 3, 3)
            self.assertEqual (sf.clip_rect, Rect (3, 3, 3, 3))
            
            sf.fill (Color (255, 0, 0))
            self._cmpcolor (sf, Color (255, 255, 255), Rect (0, 0,  3, 10))
            self._cmpcolor (sf, Color (255, 255, 255), Rect (0, 0, 10,  2))
            self._cmpcolor (sf, Color (255, 255, 255), Rect (6, 0,  4, 10))
            self._cmpcolor (sf, Color (255, 255, 255), Rect (0, 6, 10,  4))
            self._cmpcolor (sf, Color (255, 0, 0), Rect (3, 3, 3, 3))
            
            sf.clip_rect = None
            self.assertEqual (sf.clip_rect, Rect (sf.size))
            sf.fill (Color (255, 255, 255))
            self._cmpcolor (sf, Color (255, 255, 255))
            
            sf.clip_rect = Rect (3, 3, 3, 3)
            sf.fill (Color (255, 0, 0))
            sf.fill (Color (0, 255, 0), Rect (4, 5, 10, 10))
            self._cmpcolor (sf, Color (255, 255, 255), Rect (0, 0,  3, 10))
            self._cmpcolor (sf, Color (255, 255, 255), Rect (0, 0, 10,  2))
            self._cmpcolor (sf, Color (255, 0, 0), Rect (3, 3, 3, 2))
            self._cmpcolor (sf, Color (255, 0, 0), Rect (3, 3, 1, 3))
            self._cmpcolor (sf, Color (0, 255, 0), Rect (4, 5, 2, 1))
            self._cmpcolor (sf, Color (255, 255, 255), Rect (6, 0,  4, 10))
            self._cmpcolor (sf, Color (255, 255, 255), Rect (0, 6, 10,  4))

    def todo_test_pygame2_sdl_video_Surface_convert(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.convert:

        # convert ([pixelformat, flags]) -> Surface
        # 
        # Converts the Surface to the desired pixel format.
        # 
        # Converts the Surface to the desired pixel format. If no format
        # is given, the active display format is used, which will be the
        # fastest for blit operations to the screen. The flags are the
        # same as for surface creation.
        # 
        # This creates a new, converted surface and leaves the original one
        # untouched.
        self.fail() 

    def test_pygame2_sdl_video_Surface_copy(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.copy:

        # copy () -> Surface
        # 
        # Creates an exact copy of the Surface and its image data.
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            if bpp == 8:
                sf.set_palette (CGAPALETTE)

            sfcopy = sf.copy ()
        
            self.assertEqual (sf.size, sfcopy.size)
            self.assertEqual (sf.format.bits_per_pixel,
                              sfcopy.format.bits_per_pixel)
            self.assertEqual (sf.format.masks, sfcopy.format.masks)
            self._cmppixels (sf, sfcopy)
        
            sf.fill (Color (200, 100, 0))
            sfcopy = sf.copy ()
            self._cmppixels (sf, sfcopy)

            sfcopy2 = copy.copy (sf)
            self.assertEqual (sf.size, sfcopy2.size)
            self.assertEqual (sf.format.bits_per_pixel,
                              sfcopy2.format.bits_per_pixel)
            self.assertEqual (sf.format.masks, sfcopy2.format.masks)
            self._cmppixels (sf, sfcopy2)

            sfcopy3 = copy.deepcopy (sf)
            self.assertEqual (sf.size, sfcopy3.size)
            self.assertEqual (sf.format.bits_per_pixel,
                              sfcopy3.format.bits_per_pixel)
            self.assertEqual (sf.format.masks, sfcopy3.format.masks)
            self._cmppixels (sf, sfcopy3)

    def test_pygame2_sdl_video_Surface_fill(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.fill:

        # fill (color[, dstrect, blendargs]) -> None
        # 
        # Fills the Surface with a color.
        # 
        # Fills the Surface with the desired color. The color does not need to
        # match the Surface's format, it will be converted implicitly to the
        # nearest appropriate color for its format.
        # 
        # The optional destination rectangle limits the color fill to
        # the specified area. The blendargs are the same as for the blit
        # operation, but compare the color with the specific Surface
        # pixel value.
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (20, 20, bpp)
            if bpp == 8:
                sf.set_palette (CGAPALETTE)

            self._cmpcolor (sf, Color ("black"))
            sf.fill (Color ("cyan"))
            self._cmpcolor (sf, Color ("cyan"))

            sf.fill (Color ("cyan"))
            self._cmpcolor (sf, Color ("cyan"))

            sf.fill (Color ("yellow"), Rect (5, 5, 5, 5))
            self._cmpcolor (sf, Color ("yellow"), Rect (5, 5, 5, 5))
            self._cmpcolor (sf, Color ("cyan"), Rect (0, 0, 5, 20))
            self._cmpcolor (sf, Color ("cyan"), Rect (10, 10, 10, 10))

    def test_pygame2_sdl_video_Surface_flags(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.flags:

        # The currently set flags for the Surface.
        # TODO: Add 24bpp and 8bpp alpha masks in the checks below.
        modes = [32, 24, 16, 8]
        c = constants
        for bpp in modes:
        
            sf = video.Surface (10, 10, bpp, flags=c.SWSURFACE)
            self.assertEqual ((sf.flags & c.SWSURFACE), c.SWSURFACE)

            # HWSURFACE solely depends on the underlying hardware, additional
            # flags and whatever else, so we won't test for it here.
        
            # SDL does not set SRCCOLORKEY instantly on creation. Instead it is
            # set on applying a color key.
            sf = video.Surface (10, 10, bpp, flags=c.SRCCOLORKEY)
            self.assertEqual ((sf.flags & c.SRCCOLORKEY), 0)
        
            if bpp in (16, 32):
                sf = video.Surface (10, 10, bpp, flags=c.SRCALPHA)
                self.assertEqual ((sf.flags & c.SRCALPHA), c.SRCALPHA)

                sf = video.Surface (10, 10, bpp, flags=c.SWSURFACE|c.SRCALPHA)
                self.assertEqual ((sf.flags & c.SWSURFACE), c.SWSURFACE)
                self.assertEqual ((sf.flags & c.SRCALPHA), c.SRCALPHA)

                sf = video.Surface (10, 10, bpp, flags=c.SRCCOLORKEY|c.SRCALPHA)
                self.assertEqual ((sf.flags & c.SRCCOLORKEY), 0)
                self.assertEqual ((sf.flags & c.SRCALPHA), c.SRCALPHA)

                sf = video.Surface (10, 10, bpp,
                                    flags=c.SWSURFACE|c.SRCCOLORKEY|c.SRCALPHA)
                self.assertEqual ((sf.flags & c.SRCCOLORKEY), 0)
                self.assertEqual ((sf.flags & c.SWSURFACE), c.SWSURFACE)
                self.assertEqual ((sf.flags & c.SRCALPHA), c.SRCALPHA)

    def test_pygame2_sdl_video_Surface_flip(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.flip:

        # flip () -> None
        # 
        # Swaps the screen buffers for the Surface.
        # 
        # Swaps screen buffers for the Surface, causing a full update
        # and redraw of its whole area.
        modes =  [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            self.assert_ (sf.flip () == None)

    def test_pygame2_sdl_video_Surface_format(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.format:

        # Gets the (read-only) pygame2.sdl.video.PixelFormat for this
        # Surface.
        modes =  [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            fmt = sf.format
            self.assertEqual (type (fmt), video.PixelFormat)
            self.assertEqual (fmt.bits_per_pixel, bpp)
            self.assertEqual (fmt.bytes_per_pixel, bpp // 8)
            self.assertEqual (fmt.readonly, True)

    def test_pygame2_sdl_video_Surface_get_alpha(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.get_alpha:

        # get_alpha () -> int
        # 
        # Gets the current overall alpha value of the Surface.
        # 
        # Gets the current overall alpha value of the Surface. In case the
        # surface does not support alpha transparency (SRCALPHA flag not set),
        # None will be returned.
        modes = [32, 24, 16, 8]
        # TODO: Add 24bpp and 8bpp alpha masks in the checks below.
        for bpp in modes:
            if bpp in (16, 32):
                sf = video.Surface (10, 10, bpp, flags=constants.SRCALPHA)
                self.assertEqual ((sf.flags & constants.SRCALPHA),
                                  constants.SRCALPHA)
                self.assertEqual (sf.get_alpha (), 255)
        
                for alpha in (0, 32, 127, 255):
                    sf.set_alpha (alpha)
                    for t in range (4):
                        sf.set_alpha (sf.get_alpha ())
                    self.assert_ (sf.get_alpha () == alpha)
                
                # Colorkey based surfaces.
                sf = video.Surface (10, 10, bpp, flags=constants.SRCCOLORKEY)
                sf.set_colorkey (Color (0, 0, 0))
                self.assertEqual ((sf.flags & constants.SRCCOLORKEY),
                                  constants.SRCCOLORKEY)
                self.assertEqual (sf.get_alpha (), 255)
        
                for alpha in (0, 32, 127, 255):
                    sf.set_alpha (alpha)
                    for t in range (4):
                        sf.set_alpha (sf.get_alpha ())
                    self.assertEqual (sf.get_alpha (), alpha)

    def test_pygame2_sdl_video_Surface_get_at(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.get_at:

        # get_at (x, y) -> Color
        # get_at (point) -> Color
        # 
        # Gets the Surface pixel value at the specified point.
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            if bpp == 8:
                sf.set_palette (CGAPALETTE)

            color = Color (100, 50, 20)
            sf.fill (color)
            color = sf.format.get_rgba (color)
            rect = Rect (0, 0, 3, 7)
            for x in range (10):
                for y in range (10):
                    rect.topleft = x, y
                    self.assertEqual (sf.get_at (x, y), color)
                    self.assertEqual (sf.get_at ((x, y)), color)
                    self.assertEqual (sf.get_at (rect), color)

    def test_pygame2_sdl_video_Surface_get_colorkey(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.get_colorkey:

        # get_colorkey () -> Color
        # 
        # Gets the colorkey for the Surface.
        # 
        # Gets the colorkey for the Surface or None in case it has no colorkey
        # (SRCCOLORKEY flag not set).
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            if bpp == 8:
                sf.set_palette (CGAPALETTE)
            self.assertEqual (sf.get_colorkey (), None)
            sf = video.Surface (10, 10, bpp, flags=constants.SRCCOLORKEY)
            if bpp == 8:
                sf.set_palette (CGAPALETTE)
            self.assertEqual (sf.get_colorkey (), None)
        
            sf = video.Surface (10, 10, bpp)
            if bpp == 8:
                sf.set_palette (CGAPALETTE)
            color = Color (255, 0, 0)
            key = sf.format.get_rgba (color)
            self.assertTrue (sf.set_colorkey (color))
            self.assertEqual (sf.get_colorkey (), key)

    def test_pygame2_sdl_video_Surface_get_palette(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.get_palette:

        # get_palette () -> (Color, Color, ...)
        #
        # Gets the palette colors used by the Surface.
        #
        # Gets the palette colors used by the Surface or None, if the Surface
        # does not use any palette.
        palette = [Color (i, i, i) for i in range (256)]
        
        surface = video.Surface(2, 2, 8)
        surface.set_palette (palette)
        palette2 = surface.get_palette ()
        r, g, b, a  = palette2[0]

        self.failUnlessEqual (len (palette2), len (palette))
        for c2, c in zip (palette2, palette):
            self.failUnlessEqual (c2, c)
        for c in palette2:
            self.failUnless (isinstance (c, Color))

    def test_pygame2_sdl_video_Surface_h(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.h:

        # Gets the height of the Surface.
        self.assertEqual (video.Surface (10, 10).h, 10)
        self.assertEqual (video.Surface (10, 1).h, 1)
        self.assertEqual (video.Surface (10, 100).h, 100)
        self.assertEqual (video.Surface (0, 0).h, 0)
        self.assertEqual (video.Surface (0, 10).h, 10)
        self.assertEqual (video.Surface (10, 0).h, 0)
        self.assertEqual (video.Surface (2, 65535).h, 65535)
        self.assertRaises (ValueError, video.Surface, -10, 10)
        self.assertRaises (pygame2.Error, video.Surface, 68000, 10)

    def test_pygame2_sdl_video_Surface_height(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.height:

        # Gets the height of the Surface.
        self.assertEqual (video.Surface (10, 10).height, 10)
        self.assertEqual (video.Surface (10, 1).height, 1)
        self.assertEqual (video.Surface (10, 100).height, 100)
        self.assertEqual (video.Surface (0, 0).height, 0)
        self.assertEqual (video.Surface (0, 10).height, 10)
        self.assertEqual (video.Surface (10, 0).height, 0)
        self.assertEqual (video.Surface (2, 65535).height, 65535)
        self.assertRaises (ValueError, video.Surface, -10, 10)
        self.assertRaises (pygame2.Error, video.Surface, 68000, 10)

    def test_pygame2_sdl_video_Surface_lock(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.lock:

        # lock () -> None
        # 
        # Locks the Surface for a direct access to its internal pixel data.
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            self.assert_ (sf.lock () == None)
            self.assert_ (sf.unlock () == None)

    def test_pygame2_sdl_video_Surface_locked(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.locked:

        # Gets, whether the Surface is currently locked.
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            sf.lock ()
            self.assertTrue (sf.locked)
            sf.unlock ()
            self.assertFalse (sf.locked)

            for i in range (4):
                sf.lock () 
                self.assertTrue (sf.locked)
            self.assertTrue (sf.locked)
        
            for i in range (3):
                sf.unlock ()
                self.assertTrue (sf.locked)
            sf.unlock ()
            self.assertFalse (sf.locked)
            sf.unlock ()
            self.assertFalse (sf.locked)

    def test_pygame2_sdl_video_Surface_pitch(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.pitch:

        # Get the length of a surface scanline in bytes.
        sf = video.Surface (10, 10, 32)
        self.assert_ (sf.pitch >= 40) # 10 * 4 bpp
        sf = video.Surface (10, 10, 24)
        self.assert_ (sf.pitch >= 30) # 10 * 3 bpp
        sf = video.Surface (10, 10, 16)
        self.assert_ (sf.pitch >= 20) # 10 * 2 bpp
        sf = video.Surface (10, 10, 8)
        self.assert_ (sf.pitch >= 10) # 10 * 1 bpp

    def test_pygame2_sdl_video_Surface_pixels(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.pixels:

        # Gets the pixel buffer of the Surface.
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            self.assertFalse (sf.locked)
            self.assert_ (type (sf.pixels) == pygame2.BufferProxy)
            self.assertFalse (sf.locked)
            buf = sf.pixels
            self.assertTrue (sf.locked)
            self.assert_ (buf.length >= 10 * sf.format.bytes_per_pixel)
            del buf
            self.assertFalse (sf.locked)

    def test_pygame2_sdl_video_Surface_save(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.save:

        # save (file[, type]) -> None
        # 
        # Saves the Surface to a file.
        # 
        # Saves the Surface to a file. The file argument can be either a
        # file name or a file-like object to save the Surface to. The
        # optional type argument is required, if the file type cannot be
        # determined by the suffix.
        # 
        # Currently supported file types (suitable to pass as string for
        # the type argument) are:
        # 
        # * BMP
        # * TGA
        # * PNG
        # * JPEG (JPG)
        # 
        # If no type information is supplied and the file type cannot be
        # determined either, it will use TGA.
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf1 = video.Surface (16, 16, bpp)
            if bpp == 8:
                sf1.set_palette (CGAPALETTE)
            sf1.fill (pygame2.Color ("red"))
            bufcreat = None
            if sys.version_info[0] >= 3:
                bufcreat = stringio.BytesIO
            else:
                bufcreat = stringio.StringIO
            buf = bufcreat ()
        
            sf1.save (buf, "bmp")
            buf.seek (0)
            sf2 = image.load_bmp (buf).convert (sf1.format)
            self.assertEqual (sf1.size, sf2.size)
            self._cmppixels (sf1, sf2)
        
            buf.seek (0)
            sf2 = sdlimage.load (buf, "bmp").convert (sf1.format)
            self.assertEqual (sf1.size, sf2.size)
            self._cmppixels (sf1, sf2)

            buf = bufcreat ()
            sf1.save (buf, "jpg")
            buf.seek (0)
            sf2 = sdlimage.load (buf, "jpg").convert (sf1.format)
            self.assertEqual (sf1.size, sf2.size)

            buf = bufcreat ()
            sf1.save (buf, "png")
            buf.seek (0)
            sf2 = sdlimage.load (buf, "png").convert (sf1.format)
            self.assertEqual (sf1.size, sf2.size)
            self._cmppixels (sf1, sf2)

            buf = bufcreat ()
            sf1.save (buf, "tga")
            buf.seek (0)
            sf2 = sdlimage.load (buf, "tga").convert (sf1.format)
            self.assertEqual (sf1.size, sf2.size)
            self._cmppixels (sf1, sf2)

    def test_pygame2_sdl_video_Surface_set_alpha(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.set_alpha:

        # set_alpha (alpha[, flags]) -> None
        # 
        # Adjusts the alpha properties of the Surface.
        # 
        # TODO
        
        # Already handled in get_alpha() test.
        pass

    def test_pygame2_sdl_video_Surface_set_at(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.set_at:

        # set_at (x, y, color) -> None
        # set_at (point, color) -> None
        # 
        # Sets the Surface pixel value at the specified point.
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            if bpp == 8:
                sf.set_palette (CGAPALETTE)

            color = Color (255, 255, 255)
            sf.fill (color)
            color = Color (100, 37, 44)
            cc = sf.format.get_rgba (color)
            rect = Rect (4, 4, 3, 6)
            for x in range (rect.x, rect.x + rect.w):
                for y in range (rect.y, rect.y + rect.h):
                    sf.set_at (x, y, color)
                    self.assertEqual (sf.get_at (x, y), cc)
                    sf.set_at ((x, y), color)
                    self.assertEqual (sf.get_at (x, y), cc)
            self._cmpcolor (sf, color, rect)

    def test_pygame2_sdl_video_Surface_set_colorkey(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.set_colorkey:

        # set_colorkey (colorkey[, flags]) -> None
        # 
        # Adjusts the colorkey of the Surface.
        # 
        # TODO
        
        # Handled in test_pygame2_sdl_video_Surface_get_colorkey().
        pass
    
    def todo_test_pygame2_sdl_video_Surface_set_colors(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.set_colors:

        # set_colors ((color1, color2, ...)[, first]) -> bool
        # 
        # Sets a portion of the colormap palette for the 8-bit Surface.
        # 
        # Sets a portion of the colormap palette for the 8-bit Surface,
        # starting at the desired first position. If the first position
        # plus length of the passed colormap exceeds the Surface palette
        # size, the palette will be unchanged and False returned.
        # 
        # If any other error occurs, False will be returned and the
        # Surface palette should be inspected for any changes.
        self.fail() 

    def test_pygame2_sdl_video_Surface_set_palette(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.set_palette:

        # set_palette ((color1, color2, ...), flags[, first]) -> bool
        # 
        # Sets a portion of the palette for the given 8-bit surface.
        # 
        # Sets a portion of the color palette for the 8-bit Surface,
        # starting at the desired first position. If the first position
        # plus length of the passed colormap exceeds the Surface palette
        # size, the palette will be unchanged and False returned.
        # 
        # If any other error occurs, False will be returned and the
        # Surface palette should be inspected for any changes.

        palette = [Color (i, i, i) for i in range (256)]
        
        surface = video.Surface (2, 2, 8)
        surface.set_palette (palette)
        pxfmt = surface.format
        for i in range(256):
            self.failUnlessEqual (pxfmt.map_rgba (palette[i]), i,
                                  "palette color %i" % (i,))
            c = palette[i]
            surface.fill (c)
            self.failUnlessEqual (surface.get_at (0, 0), c,
                                  "palette color %i" % (i,))
        
            for i in range (10):
                palette[i] = Color (255 - i, 0, 0)
            surface.set_palette (palette[0:10])
            for i in range (256):
                self.failUnlessEqual (pxfmt.map_rgba (palette[i]), i,
                                      "palette color %i" % (i,))
                c = palette[i]
                surface.fill (c)
                self.failUnlessEqual (surface.get_at(0, 0), c,
                                      "palette color %i" % (i,))
            self.failUnlessRaises (ValueError, surface.set_palette,
                                   (1, 2, 3, 254))

    def test_pygame2_sdl_video_Surface_size(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.size:

        # Gets the size of the Surface.
        self.assertEqual (video.Surface (10, 10).size, (10, 10))
        self.assertEqual (video.Surface (1, 10).size, (1, 10))
        self.assertEqual (video.Surface (100, 10).size, (100, 10))
        self.assertEqual (video.Surface (0, 0).size, (0, 0))
        self.assertEqual (video.Surface (0, 10).size, (0, 10))
        self.assertEqual (video.Surface (16383, 2).size, (16383, 2))
        self.assertEqual (video.Surface (2, 65535).size, (2, 65535))
        self.assertRaises (ValueError, video.Surface, -10, 10)
        self.assertRaises (pygame2.Error, video.Surface, 68000, 10)

    def test_pygame2_sdl_video_Surface_unlock(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.unlock:

        # unlock () -> None
        # 
        # Unlocks the Surface, releasing the direct access to the pixel data.
        modes = [32, 24, 16, 8]
        for bpp in modes:
            sf = video.Surface (10, 10, bpp)
            self.assert_ (sf.lock () == None)
            self.assert_ (sf.unlock () == None)

    def todo_test_pygame2_sdl_video_Surface_update(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.update:

        # update ([rect]) -> None
        # update ([(rect1, rect2, ...)]) -> None
        # 
        # Updates the given area on the Surface.
        # 
        # Upates the given area (or areas, if a list of rects is passed)
        # on the Surface.
        self.fail() 

    def test_pygame2_sdl_video_Surface_w(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.w:

        # Gets the width of the Surface.
        self.assertEqual (video.Surface (10, 10).w, 10)
        self.assertEqual (video.Surface (1, 10).w, 1)
        self.assertEqual (video.Surface (100, 10).w, 100)
        self.assertEqual (video.Surface (0, 0).w, 0)
        self.assertEqual (video.Surface (0, 10).w, 0)
        self.assertEqual (video.Surface (10, 0).w, 10)
        self.assertEqual (video.Surface (16383, 2).w, 16383)
        self.assertRaises (ValueError, video.Surface, -10, 10)
        self.assertRaises (pygame2.Error, video.Surface, 68000, 10)

    def test_pygame2_sdl_video_Surface_width(self):

        # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.width:

        # Gets the width of the Surface.
        self.assertEqual (video.Surface (10, 10).width, 10)
        self.assertEqual (video.Surface (1, 10).width, 1)
        self.assertEqual (video.Surface (100, 10).width, 100)
        self.assertEqual (video.Surface (0, 0).width, 0)
        self.assertEqual (video.Surface (0, 10).width, 0)
        self.assertEqual (video.Surface (10, 0).width, 10)
        self.assertEqual (video.Surface (16383, 2).width, 16383)
        self.assertRaises (ValueError, video.Surface, -10, 10)
        self.assertRaises (pygame2.Error, video.Surface, 68000, 10)

    def test_pygame2_sdl_video_Surface_scroll (self):

        # __doc__ (as of 2009-06-24) for pygame2.sdl.video.Surface.width:

        # Scrolls the Surface in place.

        # Move the Surface contents by *dx* pixels right and *dy* pixels
        # down. dx and dy may be negative for left and up scrolls
        # respectively. Areas of the surface that are not overwritten
        # retain their original pixel values. Scrolling is contained by
        # the Surface clip area. It is safe to have *dx* and *dy* values
        # that exceed the surface size.

        scrolls = [(8, 2, 3),
                   (16, 2, 3),
                   (24, 2, 3),
                   (32, 2, 3),
                   (32, -1, -3),
                   (32, 0, 0),
                   (32, 11, 0),
                   (32, 0, 11),
                   (32, -11, 0),
                   (32, 0, -11),
                   (32, -11, 2),
                   (32, 2, -11)]
        for bitsize, dx, dy in scrolls:
            if bitsize == 8:
                masks = (0xFF >> 6 << 5, 0xFF >> 5 << 2, 0xFF >> 6, 0)
                surf = video.Surface((10, 10), bitsize, masks=masks)
            else:
                surf = video.Surface((10, 10), bitsize)
            surf.fill(Color(255, 0, 0))
            surf.fill(Color(0, 255, 0), (2, 2, 2, 2,))
            comp = surf.copy()
            comp.blit(surf, (dx, dy))
            surf.scroll(dx, dy)
            w, h = surf.size
            for x in range(w):
                for y in range(h):
                    self.failUnlessEqual(surf.get_at((x, y)),
                                         comp.get_at((x, y)),
                                         "%s != %s, bpp:, %i, x: %i, y: %i" %
                                         (surf.get_at((x, y)),
                                          comp.get_at((x, y)),
                                          bitsize, dx, dy))
        # Confirm clip rect containment
        surf = video.Surface((20, 13), 32)
        surf.fill(Color(255, 0, 0))
        surf.fill(Color(0, 255, 0), (7, 1, 6, 6))
        comp = surf.copy()
        clip = Rect(3, 1, 8, 14)
        surf.clip_rect = clip
        comp.clip_rect = clip
        comp.blit(surf, (clip.x + 2, clip.y + 3), surf.clip_rect)
        surf.scroll(2, 3)
        w, h = surf.size
        for x in range(w):
            for y in range(h):
                self.failUnlessEqual(surf.get_at((x, y)),
                                     comp.get_at((x, y)))
        # Confirm keyword arguments and per-pixel alpha
        spot_color = Color(0, 255, 0, 128)
        surf = video.Surface((4, 4), 32, constants.SRCALPHA)
        surf.fill(Color(255, 0, 0, 255))
        surf.set_at((1, 1), spot_color)
        surf.scroll(dx=1)
        self.failUnlessEqual(surf.get_at((2, 1)), spot_color)
        surf.scroll(dy=1)
        self.failUnlessEqual(surf.get_at((2, 2)), spot_color)
        surf.scroll(dy=1, dx=1)
        self.failUnlessEqual(surf.get_at((3, 3)), spot_color)
        surf.scroll(dx=-3, dy=-3)
        self.failUnlessEqual(surf.get_at((0, 0)), spot_color)
        
        sf = video.Surface (20, 20)
        sf.fill (Color (255, 0, 0), Rect (10, 10, 20, 20))
        self._cmpcolor (sf, Color (255, 0, 0), Rect (10, 10, 10, 10))
        sf.scroll (0, -5)
        self._cmpcolor (sf, Color (255, 0, 0), Rect (10, 5, 10, 15))
        sf.scroll (-10, 0)
        self._cmpcolor (sf, Color (255, 0, 0), Rect (0, 5, 10, 15))
        self._cmpcolor (sf, Color (255, 0, 0), Rect (10, 10, 10, 10))

        sf.fill (Color (0))
        sf.fill (Color (255, 0, 0), Rect (0, 0, 5, 5))
        sf.scroll (-10, -10)
        self._cmpcolor (sf, Color (0, 0, 0))
        
    def test_pygame2_sdl_video_Surface___repr__(self):
        sf = video.Surface (10, 10, 8)
        text = "<Surface 10x10@8bpp>"
        self.assertEqual (repr (sf), text)
        sf = video.Surface (0, 0, 16)
        text = "<Surface 0x0@16bpp>"
        self.assertEqual (repr (sf), text)
        sf = video.Surface (34, 728, 24)
        text = "<Surface 34x728@24bpp>"
        self.assertEqual (repr (sf), text)
        sf = video.Surface (1, 1, 32)
        text = "<Surface 1x1@32bpp>"
        self.assertEqual (repr (sf), text)

if __name__ == "__main__":
    unittest.main ()