| 1 | import unittest |
| 2 | |
| 3 | from ...base.data import PygletTestCase |
| 4 | |
| 5 | try: |
| 6 | from pyglet.image.codecs import gdkpixbuf2 |
| 7 | except ImportError: |
| 8 | gdkpixbuf2 = None |
| 9 | |
| 10 | |
| 11 | @unittest.skipIf(gdkpixbuf2 is None, 'GdkPixBuf not available') |
| 12 | class GdkPixBufTest(PygletTestCase): |
| 13 | def test_load_image(self): |
| 14 | filename = self.get_test_data_file('images', '8bpp.gif') |
| 15 | with open(filename, 'rb') as f: |
| 16 | loader = gdkpixbuf2.GdkPixBufLoader(filename, f) |
| 17 | pixbuf = loader.get_pixbuf() |
| 18 | assert pixbuf is not None |
| 19 | assert pixbuf.height == 257 |
| 20 | assert pixbuf.width == 235 |
| 21 | assert pixbuf.channels == 4 |
| 22 | assert pixbuf.has_alpha |
| 23 | image = pixbuf.to_image() |
| 24 | assert image is not None |
| 25 | assert image.height == 257 |
| 26 | assert image.width == 235 |
| 27 | |
| 28 | def test_load_image_requires_loader_close(self): |
| 29 | """Some image files require the loader to be closed to force it |
| 30 | to read and parse all data.""" |
| 31 | filename = self.get_test_data_file('images', 'gdk_close.png') |
| 32 | with open(filename, 'rb') as f: |
| 33 | loader = gdkpixbuf2.GdkPixBufLoader(filename, f) |
| 34 | pixbuf = loader.get_pixbuf() |
| 35 | assert pixbuf is not None |
| 36 | assert pixbuf.height == 200 |
| 37 | assert pixbuf.width == 200 |
| 38 | assert pixbuf.channels == 4 |
| 39 | assert pixbuf.has_alpha |
| 40 | image = pixbuf.to_image() |
| 41 | assert image is not None |
| 42 | assert image.height == 200 |
| 43 | assert image.width == 200 |
| 44 | |
| 45 | def test_load_animation(self): |
| 46 | filename = self.get_test_data_file('images', 'dinosaur.gif') |
| 47 | with open(filename, 'rb') as f: |
| 48 | loader = gdkpixbuf2.GdkPixBufLoader(filename, f) |
| 49 | gdk_anim = loader.get_animation() |
| 50 | assert gdk_anim is not None |
| 51 | anim = gdk_anim.to_animation() |
| 52 | assert anim is not None |
| 53 | assert len(anim.frames) == 12 |
| 54 | for frame in anim.frames: |
| 55 | assert frame.image is not None |
| 56 | assert frame.duration is not None |
| 57 | assert frame.duration == 0.1 |
| 58 | assert frame.image.width == 129 |
| 59 | assert frame.image.height == 79 |
| 60 | |
| 61 | def test_incomplete_load(self): |
| 62 | """Start loading a file and stop. Should clean up correctly.""" |
| 63 | filename = self.get_test_data_file('images', 'gdk_close.png') |
| 64 | with open(filename, 'rb') as f: |
| 65 | loader = gdkpixbuf2.GdkPixBufLoader(filename, f) |
| 66 | del loader |