14
14
from pygame.tests.test_utils \
15
import test_not_implemented, example_path, unittest
15
import test_not_implemented, example_path, unittest, png
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_
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)
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.
56
f = example_path('data/alien1.png') # normalized
57
# f = os.path.join("examples", "data", "alien1.png")
58
surf = pygame.image.load(f)
61
# f = open(os.path.join("examples", "data", "alien1.png"), "rb")
62
surf = pygame.image.load(f)
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]
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)
71
# Read the PNG file and verify that pygame interprets it correctly
72
surf = pygame.image.load(f_path)
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))
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)
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)
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))
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)
65
101
def testLoadJPG(self):
66
102
""" see if we can load a jpg.
79
115
# surf = pygame.image.load(open(os.path.join("examples", "data", "alien1.jpg"), "rb"))
117
def testSavePNG(self):
118
""" see if we can save a png with color values in the proper channels.
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)
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)
132
f_path = tempfile.mktemp(suffix='.png')
133
pygame.image.save(surf, f_path)
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 = []
139
pixels_as_tuples.append(tuple(pixel))
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)
82
148
def test_save(self):