| 1 |
from io import BytesIO |
| 2 |
import pytest |
| 3 |
|
| 4 |
from pyglet import gl, image |
| 5 |
|
| 6 |
from ...annotations import Platform, require_platform, require_gl_extension |
| 7 |
from ...base.event_loop import EventLoopFixture |
| 8 |
|
| 9 |
|
| 10 |
class ImageTestFixture(EventLoopFixture): |
| 11 |
def __init__(self, request, test_data): |
| 12 |
super().__init__(request) |
| 13 |
self.test_data = test_data |
| 14 |
|
| 15 |
self.show_checkerboard = True |
| 16 |
self.show_triangle_left = False |
| 17 |
self.show_text = True |
| 18 |
self.left_texture = None |
| 19 |
self.right_texture = None |
| 20 |
|
| 21 |
self.checkerboard = image.create(32, 32, image.CheckerImagePattern()) |
| 22 |
|
| 23 |
def on_draw(self): |
| 24 |
|
| 25 |
|
| 26 |
self.clear() |
| 27 |
self.draw_checkerboard() |
| 28 |
self.draw_left() |
| 29 |
self.draw_triangle_left() |
| 30 |
self.draw_right() |
| 31 |
if self.show_text: |
| 32 |
self.draw_text() |
| 33 |
|
| 34 |
def draw_checkerboard(self): |
| 35 |
if self.show_checkerboard: |
| 36 |
gl.glPushMatrix() |
| 37 |
gl.glScalef(self.window.width/float(self.checkerboard.width), |
| 38 |
self.window.height/float(self.checkerboard.height), |
| 39 |
1.) |
| 40 |
gl.glMatrixMode(gl.GL_TEXTURE) |
| 41 |
gl.glPushMatrix() |
| 42 |
gl.glScalef(self.window.width/float(self.checkerboard.width), |
| 43 |
self.window.height/float(self.checkerboard.height), |
| 44 |
1.) |
| 45 |
gl.glMatrixMode(gl.GL_MODELVIEW) |
| 46 |
self.checkerboard.blit(0, 0, 0) |
| 47 |
gl.glMatrixMode(gl.GL_TEXTURE) |
| 48 |
gl.glPopMatrix() |
| 49 |
gl.glMatrixMode(gl.GL_MODELVIEW) |
| 50 |
gl.glPopMatrix() |
| 51 |
|
| 52 |
def draw_left(self): |
| 53 |
if self.left_texture: |
| 54 |
self.left_texture.blit( |
| 55 |
self.window.width // 4 - self.left_texture.width // 2, |
| 56 |
(self.window.height - self.left_texture.height) // 2, |
| 57 |
0) |
| 58 |
|
| 59 |
def draw_right(self): |
| 60 |
if self.right_texture: |
| 61 |
x = self.window.width * 3 // 4 - self.right_texture.width // 2 |
| 62 |
x = max((x, self.window.width // 2)) |
| 63 |
self.right_texture.blit( |
| 64 |
x, |
| 65 |
(self.window.height - self.right_texture.height) // 2, |
| 66 |
0) |
| 67 |
|
| 68 |
def load_left(self, image_file, decoder=None): |
| 69 |
self.left_texture = image.load(image_file, decoder=decoder).get_texture() |
| 70 |
|
| 71 |
def copy_left_to_right(self, encoder=None): |
| 72 |
buf = BytesIO() |
| 73 |
self.left_texture.save("file.png", |
| 74 |
buf, |
| 75 |
encoder=encoder) |
| 76 |
buf.seek(0) |
| 77 |
self.right_texture = image.load("file.png", buf).get_texture() |
| 78 |
|
| 79 |
def enable_alpha(self): |
| 80 |
gl.glEnable(gl.GL_BLEND) |
| 81 |
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) |
| 82 |
|
| 83 |
def load_right_arb(self, image_file, pixel_format): |
| 84 |
img = image.load(image_file) |
| 85 |
img.format = pixel_format |
| 86 |
img.get_data() |
| 87 |
self.right_texture = img.get_texture() |
| 88 |
|
| 89 |
def draw_triangle_left(self): |
| 90 |
if self.show_triangle_left: |
| 91 |
w = 200 |
| 92 |
h = 200 |
| 93 |
x = self.window.width // 4 - w // 2 |
| 94 |
y = (self.window.height - h) // 2 |
| 95 |
|
| 96 |
gl.glEnable(gl.GL_DEPTH_TEST) |
| 97 |
gl.glBegin(gl.GL_TRIANGLES) |
| 98 |
gl.glColor4f(1, 0, 0, 1) |
| 99 |
gl.glVertex3f(x, y, -1) |
| 100 |
gl.glColor4f(0, 1, 0, 1) |
| 101 |
gl.glVertex3f(x+w, y, 0) |
| 102 |
gl.glColor4f(0, 0, 1, 1) |
| 103 |
gl.glVertex3f(x, y+h, 1) |
| 104 |
gl.glEnd() |
| 105 |
gl.glDisable(gl.GL_DEPTH_TEST) |
| 106 |
gl.glColor4f(1, 1, 1, 1) |
| 107 |
|
| 108 |
def copy_color_buffer(self): |
| 109 |
self.right_texture = \ |
| 110 |
image.get_buffer_manager().get_color_buffer().get_texture() |
| 111 |
|
| 112 |
def save_and_load_color_buffer(self): |
| 113 |
stream = BytesIO() |
| 114 |
image.get_buffer_manager().get_color_buffer().save('buffer.png', stream) |
| 115 |
stream.seek(0) |
| 116 |
self.right_texture = image.load('buffer.png', stream) |
| 117 |
|
| 118 |
def save_and_load_depth_buffer(self): |
| 119 |
stream = BytesIO() |
| 120 |
image.get_buffer_manager().get_depth_buffer().save('buffer.png', stream) |
| 121 |
stream.seek(0) |
| 122 |
self.right_texture = image.load('buffer.png', stream) |
| 123 |
|
| 124 |
|
| 125 |
def test_image_loading(self, decoder, image_name): |
| 126 |
"""Test loading images.""" |
| 127 |
self.create_window(width=800, height=600) |
| 128 |
self.load_left(self.test_data.get_file("images", image_name), decoder) |
| 129 |
self.enable_alpha() |
| 130 |
self.ask_question( |
| 131 |
"Do you see the {} image on a checkerboard background?".format(image_name) |
| 132 |
) |
| 133 |
|
| 134 |
def test_image_saving(self, encoder, image_name): |
| 135 |
"""Test saving images.""" |
| 136 |
self.create_window(width=800, height=600) |
| 137 |
self.load_left(self.test_data.get_file("images", image_name)) |
| 138 |
self.copy_left_to_right(encoder) |
| 139 |
self.enable_alpha() |
| 140 |
self.ask_question( |
| 141 |
"Do you see the {} image twice on a checkerboard background?".format(image_name) |
| 142 |
) |
| 143 |
|
| 144 |
@pytest.fixture |
| 145 |
def image_test(request, test_data): |
| 146 |
return ImageTestFixture(request, test_data) |
| 147 |
|
| 148 |
|
| 149 |
bmp_images = ['rgb_16bpp.bmp', 'rgb_1bpp.bmp', 'rgb_24bpp.bmp', 'rgb_32bpp.bmp', 'rgb_4bpp.bmp', |
| 150 |
'rgb_8bpp.bmp', 'rgba_32bpp.bmp'] |
| 151 |
dds_images = ['rgba_dxt1.dds', 'rgba_dxt3.dds', 'rgba_dxt5.dds', 'rgb_dxt1.dds'] |
| 152 |
png_images = ['la.png', 'l.png', 'rgba.png', 'rgb.png'] |
| 153 |
pypng_images = png_images + ['rgb_8bpp.png', 'rgb_8bpp_trans.png'] |
| 154 |
gif_images = ['8bpp.gif'] |
| 155 |
|
| 156 |
|
| 157 |
def test_checkerboard(image_test): |
| 158 |
"""Test that the checkerboard pattern looks correct.""" |
| 159 |
image_test.create_window() |
| 160 |
image_test.ask_question( |
| 161 |
"Do you see a checkboard pattern in two levels of grey?" |
| 162 |
) |
| 163 |
|
| 164 |
@pytest.mark.parametrize('image_name', bmp_images) |
| 165 |
def test_bmp_loading(image_test, image_name): |
| 166 |
"""Test loading BMP images.""" |
| 167 |
from pyglet.image.codecs.bmp import BMPImageDecoder |
| 168 |
image_test.test_image_loading(BMPImageDecoder(), image_name) |
| 169 |
|
| 170 |
|
| 171 |
@pytest.mark.parametrize('image_name', dds_images) |
| 172 |
def test_dds_loading(image_test, image_name): |
| 173 |
"""Test loading DDS images.""" |
| 174 |
from pyglet.image.codecs.dds import DDSImageDecoder |
| 175 |
image_test.test_image_loading(DDSImageDecoder(), image_name) |
| 176 |
|
| 177 |
|
| 178 |
@pytest.mark.parametrize('image_name', png_images) |
| 179 |
def test_pil_loading(image_test, image_name): |
| 180 |
"""Test loading PNG images using PIL""" |
| 181 |
try: |
| 182 |
from PIL import Image |
| 183 |
except ImportError: |
| 184 |
pytest.skip('PIL not available') |
| 185 |
from pyglet.image.codecs.pil import PILImageDecoder |
| 186 |
image_test.test_image_loading(PILImageDecoder(), image_name) |
| 187 |
|
| 188 |
|
| 189 |
@pytest.mark.parametrize('image_name', png_images + gif_images) |
| 190 |
@require_platform(Platform.LINUX) |
| 191 |
def test_gdkpixbuf2_loading(image_test, image_name): |
| 192 |
"""Test loading PNG images using Linux specific GdkPixbuf2.""" |
| 193 |
from pyglet.image.codecs.gdkpixbuf2 import GdkPixbuf2ImageDecoder |
| 194 |
image_test.test_image_loading(GdkPixbuf2ImageDecoder(), image_name) |
| 195 |
|
| 196 |
|
| 197 |
@pytest.mark.parametrize('image_name', png_images) |
| 198 |
@require_platform(Platform.WINDOWS) |
| 199 |
def test_gdiplus_loading(image_test, image_name): |
| 200 |
"""Test loading PNG images using Windows specific GDI+.""" |
| 201 |
from pyglet.image.codecs.gdiplus import GDIPlusDecoder |
| 202 |
image_test.test_image_loading(GDIPlusDecoder(), image_name) |
| 203 |
|
| 204 |
|
| 205 |
@pytest.mark.parametrize('image_name', png_images) |
| 206 |
@require_platform(Platform.OSX) |
| 207 |
def test_quartz_loading(image_test, image_name): |
| 208 |
"""Test loading PNG images using OSX specific Quartz.""" |
| 209 |
from pyglet.image.codecs.quartz import QuartzImageDecoder |
| 210 |
image_test.test_image_loading(QuartzImageDecoder(), image_name) |
| 211 |
|
| 212 |
|
| 213 |
@pytest.mark.parametrize('image_name', pypng_images) |
| 214 |
def test_pypng_loading(image_test, image_name): |
| 215 |
"""Test loading PNG images using PyPNG.""" |
| 216 |
from pyglet.image.codecs.png import PNGImageDecoder |
| 217 |
image_test.test_image_loading(PNGImageDecoder(), image_name) |
| 218 |
|
| 219 |
|
| 220 |
@pytest.mark.parametrize('image_name', png_images) |
| 221 |
def test_pil_saving(image_test, image_name): |
| 222 |
"""Test saving images using PIL.""" |
| 223 |
try: |
| 224 |
from PIL import Image |
| 225 |
except ImportError: |
| 226 |
pytest.skip('PIL not available') |
| 227 |
from pyglet.image.codecs.pil import PILImageEncoder |
| 228 |
image_test.test_image_saving(PILImageEncoder(), image_name) |
| 229 |
|
| 230 |
|
| 231 |
@pytest.mark.parametrize('image_name', png_images) |
| 232 |
def test_pypng_saving(image_test, image_name): |
| 233 |
"""Test saving images using PyPNG.""" |
| 234 |
from pyglet.image.codecs.png import PNGImageEncoder |
| 235 |
image_test.test_image_saving(PNGImageEncoder(), image_name) |
| 236 |
|
| 237 |
|
| 238 |
@pytest.mark.parametrize('image_name', ['rgb.png', 'rgba.png']) |
| 239 |
@require_gl_extension('GL_ARB_imaging') |
| 240 |
def test_arb(image_test, image_name): |
| 241 |
"""Test swapping color channels using the ARB imaging extension.""" |
| 242 |
image_test.create_window() |
| 243 |
image_test.load_left(image_test.test_data.get_file('images', image_name)) |
| 244 |
image_test.load_right_arb(image_test.test_data.get_file('images', image_name), 'GRB') |
| 245 |
image_test.ask_question( |
| 246 |
"In the right image red and green should be swapped." |
| 247 |
) |
| 248 |
|
| 249 |
|
| 250 |
def test_buffer_copy(image_test): |
| 251 |
"""Test colour buffer copy to texture. |
| 252 |
|
| 253 |
A scene consisting of a single coloured triangle will be rendered. The |
| 254 |
colour buffer will then be saved to a stream and loaded as a texture. |
| 255 |
|
| 256 |
You might see the original scene first shortly before the |
| 257 |
buffer image appears (because retrieving and saving the image is a slow |
| 258 |
operation). |
| 259 |
""" |
| 260 |
image_test.create_window(width=800, height=600) |
| 261 |
image_test.show_triangle_left = True |
| 262 |
image_test.show_text = False |
| 263 |
image_test.show_checkerboard = False |
| 264 |
|
| 265 |
def step(dt): |
| 266 |
image_test.copy_color_buffer() |
| 267 |
image_test.show_text = True |
| 268 |
return True |
| 269 |
image_test.schedule_once(step) |
| 270 |
|
| 271 |
image_test.ask_question( |
| 272 |
'You should see the same coloured triangle left and right.' |
| 273 |
) |
| 274 |
|
| 275 |
def test_buffer_saving(image_test): |
| 276 |
"""Test colour buffer save. |
| 277 |
|
| 278 |
A scene consisting of a single coloured triangle will be rendered. The |
| 279 |
colour buffer will then be saved to a stream and loaded as a texture. |
| 280 |
|
| 281 |
You might see the original scene first shortly before the |
| 282 |
buffer image appears (because retrieving and saving the image is a slow |
| 283 |
operation). |
| 284 |
""" |
| 285 |
image_test.create_window(width=800, height=600) |
| 286 |
image_test.show_triangle_left = True |
| 287 |
image_test.show_text = False |
| 288 |
image_test.show_checkerboard = False |
| 289 |
|
| 290 |
def step(dt): |
| 291 |
image_test.save_and_load_color_buffer() |
| 292 |
image_test.show_text = True |
| 293 |
return True |
| 294 |
image_test.schedule_once(step) |
| 295 |
|
| 296 |
image_test.ask_question( |
| 297 |
'You should see the same coloured triangle left and right.' |
| 298 |
) |
| 299 |
|
| 300 |
def test_depth_buffer_saving(image_test): |
| 301 |
"""Test depth buffer save. |
| 302 |
|
| 303 |
A scene consisting of a single coloured triangle will be rendered. The |
| 304 |
depth buffer will then be saved to a stream and loaded as a texture. |
| 305 |
|
| 306 |
You might see the original scene first for up to several seconds before the |
| 307 |
depth buffer image appears (because retrieving and saving the image is |
| 308 |
a slow operation). |
| 309 |
""" |
| 310 |
image_test.create_window(width=800, height=600) |
| 311 |
image_test.show_triangle_left = True |
| 312 |
image_test.show_text = False |
| 313 |
image_test.show_checkerboard = False |
| 314 |
|
| 315 |
def step(dt): |
| 316 |
image_test.save_and_load_depth_buffer() |
| 317 |
image_test.show_text = True |
| 318 |
return True |
| 319 |
image_test.schedule_once(step) |
| 320 |
|
| 321 |
image_test.ask_question( |
| 322 |
'You should see a coloured triangle left and its depth buffer right. ' |
| 323 |
'The bottom-left corner is lightest, the bottom-right is darker and ' |
| 324 |
'the top corner is darkest (corresponding the depth of the triangle.' |
| 325 |
) |
| 326 |
|