~ubuntu-branches/debian/experimental/pygame/experimental

« back to all changes in this revision

Viewing changes to test/image_test.py

  • Committer: Package Import Robot
  • Author(s): Vincent Cheng
  • Date: 2013-02-21 00:23:03 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20130221002303-08xmo02oym2hxjee
Tags: 1.9.2~pre~r3189-1
* New upstream hg snapshot (rev 3189).
* Avoid potentially overriding a symlink in python3.2-dev. (Closes: #700997)
* Generate correct versioned dependency on python-numpy following ABI change
  using the dh_numpy and dh_numpy3 helpers. (Closes: #698169)
  - Add build-depends on python3-numpy.
* Fix a number of failing tests that rely on pygame being tested with OpenGL
  and a graphical display available, as well as audio/video devices.
* Remove deprecated DMUA flag in debian/control.
* Fix lintian tag vcs-field-not-canonical.
* Update Standards version from 3.9.3 to 3.9.4, no updates required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 
13
13
if is_pygame_pkg:
14
14
    from pygame.tests.test_utils \
15
 
         import test_not_implemented, example_path, unittest
 
15
         import test_not_implemented, example_path, unittest, png
16
16
else:
17
17
    from test.test_utils \
18
 
         import test_not_implemented, example_path, unittest
 
18
         import test_not_implemented, example_path, unittest, png
19
19
import pygame, pygame.image, pygame.pkgdata
20
20
from pygame.compat import xrange_, ord_
21
21
 
22
22
import os
23
23
import array
 
24
import tempfile
24
25
 
25
26
def test_magic(f, magic_hex):
26
27
    """ tests a given file to see if the magic hex matches.
51
52
        self.assertEqual(surf.get_width(),32)
52
53
 
53
54
    def testLoadPNG(self):
54
 
        """ see if we can load a png.
 
55
        """ see if we can load a png with color values in the proper channels.
55
56
        """
56
 
        f = example_path('data/alien1.png') # normalized
57
 
        # f = os.path.join("examples", "data", "alien1.png")
58
 
        surf = pygame.image.load(f)
59
 
 
60
 
        f = open(f, 'rb')
61
 
        # f = open(os.path.join("examples", "data", "alien1.png"), "rb")
62
 
        surf = pygame.image.load(f)
63
 
 
 
57
        # Create a PNG file with known colors
 
58
        reddish_pixel = (210, 0, 0, 255)
 
59
        greenish_pixel = (0, 220, 0, 255)
 
60
        bluish_pixel = (0, 0, 230, 255)
 
61
        greyish_pixel = (110, 120, 130, 140)
 
62
        pixel_array = [reddish_pixel + greenish_pixel,
 
63
                       bluish_pixel + greyish_pixel]
 
64
 
 
65
        f_descriptor, f_path = tempfile.mkstemp(suffix='.png')
 
66
        f = os.fdopen(f_descriptor, 'wb')
 
67
        w = png.Writer(2, 2, alpha=True)
 
68
        w.write(f, pixel_array)
 
69
        f.close()
 
70
 
 
71
        # Read the PNG file and verify that pygame interprets it correctly
 
72
        surf = pygame.image.load(f_path)
 
73
 
 
74
        pixel_x0_y0 = surf.get_at((0, 0))
 
75
        pixel_x1_y0 = surf.get_at((1, 0))
 
76
        pixel_x0_y1 = surf.get_at((0, 1))
 
77
        pixel_x1_y1 = surf.get_at((1, 1))
 
78
 
 
79
        self.assertEquals(pixel_x0_y0, reddish_pixel)
 
80
        self.assertEquals(pixel_x1_y0, greenish_pixel)
 
81
        self.assertEquals(pixel_x0_y1, bluish_pixel)
 
82
        self.assertEquals(pixel_x1_y1, greyish_pixel)
 
83
 
 
84
        # Read the PNG file obj. and verify that pygame interprets it correctly
 
85
        f = open(f_path, 'rb')
 
86
        surf = pygame.image.load(f)
 
87
        f.close()
 
88
 
 
89
        pixel_x0_y0 = surf.get_at((0, 0))
 
90
        pixel_x1_y0 = surf.get_at((1, 0))
 
91
        pixel_x0_y1 = surf.get_at((0, 1))
 
92
        pixel_x1_y1 = surf.get_at((1, 1))
 
93
 
 
94
        self.assertEquals(pixel_x0_y0, reddish_pixel)
 
95
        self.assertEquals(pixel_x1_y0, greenish_pixel)
 
96
        self.assertEquals(pixel_x0_y1, bluish_pixel)
 
97
        self.assertEquals(pixel_x1_y1, greyish_pixel)
 
98
 
 
99
        os.remove(f_path) 
64
100
 
65
101
    def testLoadJPG(self):
66
102
        """ see if we can load a jpg.
78
114
        
79
115
        # surf = pygame.image.load(open(os.path.join("examples", "data", "alien1.jpg"), "rb"))
80
116
 
 
117
    def testSavePNG(self):
 
118
        """ see if we can save a png with color values in the proper channels.
 
119
        """
 
120
        # Create a PNG file with known colors
 
121
        reddish_pixel = (215, 0, 0, 255)
 
122
        greenish_pixel = (0, 225, 0, 255)
 
123
        bluish_pixel = (0, 0, 235, 255)
 
124
        greyish_pixel = (115, 125, 135, 145)
 
125
 
 
126
        surf = pygame.Surface((1, 4), pygame.SRCALPHA, 32)
 
127
        surf.set_at((0, 0), reddish_pixel)
 
128
        surf.set_at((0, 1), greenish_pixel)
 
129
        surf.set_at((0, 2), bluish_pixel)
 
130
        surf.set_at((0, 3), greyish_pixel)
 
131
 
 
132
        f_path = tempfile.mktemp(suffix='.png')
 
133
        pygame.image.save(surf, f_path)
 
134
 
 
135
        # Read the PNG file and verify that pygame saved it correctly
 
136
        width, height, pixels, metadata = png.Reader(filename=f_path).asRGBA8()
 
137
        pixels_as_tuples = []
 
138
        for pixel in pixels:
 
139
            pixels_as_tuples.append(tuple(pixel))
 
140
 
 
141
        self.assertEquals(pixels_as_tuples[0], reddish_pixel)
 
142
        self.assertEquals(pixels_as_tuples[1], greenish_pixel)
 
143
        self.assertEquals(pixels_as_tuples[2], bluish_pixel)
 
144
        self.assertEquals(pixels_as_tuples[3], greyish_pixel)
 
145
 
 
146
        os.remove(f_path)
81
147
 
82
148
    def test_save(self):
83
149