| 1 |
import unittest |
| 2 |
|
| 3 |
from pyglet.image import * |
| 4 |
from pyglet.window import * |
| 5 |
|
| 6 |
|
| 7 |
def colorbyte(color): |
| 8 |
return bytes((color,)) |
| 9 |
|
| 10 |
|
| 11 |
class TestTexture3D(unittest.TestCase): |
| 12 |
"""Test the Texture3D for image grids.""" |
| 13 |
|
| 14 |
def create_image(self, width, height, color): |
| 15 |
data = colorbyte(color) * (width * height) |
| 16 |
return ImageData(width, height, 'R', data) |
| 17 |
|
| 18 |
def check_image(self, image, width, height, color): |
| 19 |
self.assertTrue(image.width == width) |
| 20 |
self.assertTrue(image.height == height) |
| 21 |
image = image.get_image_data() |
| 22 |
data = image.get_data('R', image.width) |
| 23 |
self.assertTrue(data == colorbyte(color) * len(data)) |
| 24 |
|
| 25 |
def set_grid_image(self, itemwidth, itemheight, rows, cols, rowpad, colpad): |
| 26 |
data = b'' |
| 27 |
color = 1 |
| 28 |
width = itemwidth * cols + colpad * (cols - 1) |
| 29 |
height = itemheight * rows + rowpad * (rows - 1) |
| 30 |
for row in range(rows): |
| 31 |
rowdata = b'' |
| 32 |
for col in range(cols): |
| 33 |
rowdata += colorbyte(color) * itemwidth |
| 34 |
if col < cols - 1: |
| 35 |
rowdata += b'\0' * colpad |
| 36 |
color += 1 |
| 37 |
|
| 38 |
data += rowdata * itemheight |
| 39 |
if row < rows - 1: |
| 40 |
data += (width * b'\0') * rowpad |
| 41 |
assert len(data) == width * height |
| 42 |
self.image = ImageData(width, height, 'R', data) |
| 43 |
grid = ImageGrid(self.image, rows, cols, |
| 44 |
itemwidth, itemheight, rowpad, colpad) |
| 45 |
self.grid = Texture3D.create_for_image_grid(grid) |
| 46 |
|
| 47 |
def check_cell(self, cellimage, cellindex): |
| 48 |
self.assertTrue(cellimage.width == self.grid.item_width) |
| 49 |
self.assertTrue(cellimage.height == self.grid.item_height) |
| 50 |
|
| 51 |
cellimage = cellimage.get_image_data() |
| 52 |
data = cellimage.get_data('R', cellimage.width) |
| 53 |
self.assertTrue(data == colorbyte(cellindex + 1) * len(data)) |
| 54 |
|
| 55 |
def setUp(self): |
| 56 |
self.w = Window(visible=False) |
| 57 |
|
| 58 |
def tearDown(self) -> None: |
| 59 |
self.w.close() |
| 60 |
|
| 61 |
def test2(self): |
| 62 |
|
| 63 |
images = [self.create_image(32, 32, i + 1) for i in range(2)] |
| 64 |
texture = Texture3D.create_for_images(images) |
| 65 |
self.assertTrue(len(texture) == 2) |
| 66 |
for i in range(2): |
| 67 |
self.check_image(texture[i], 32, 32, i + 1) |
| 68 |
|
| 69 |
def test5(self): |
| 70 |
|
| 71 |
images = [self.create_image(31, 94, i + 1) for i in range(5)] |
| 72 |
texture = Texture3D.create_for_images(images) |
| 73 |
self.assertTrue(len(texture) == 5) |
| 74 |
for i in range(5): |
| 75 |
self.check_image(texture[i], 31, 94, i + 1) |
| 76 |
|
| 77 |
def testSet(self): |
| 78 |
|
| 79 |
images = [self.create_image(32, 32, i + 1) for i in range(3)] |
| 80 |
texture = Texture3D.create_for_images(images) |
| 81 |
self.assertTrue(len(texture) == 3) |
| 82 |
for i in range(3): |
| 83 |
self.check_image(texture[i], 32, 32, i + 1) |
| 84 |
texture[1] = self.create_image(32, 32, 87) |
| 85 |
self.check_image(texture[0], 32, 32, 1) |
| 86 |
self.check_image(texture[1], 32, 32, 87) |
| 87 |
self.check_image(texture[2], 32, 32, 3) |
| 88 |
|
| 89 |
def testSquare(self): |
| 90 |
|
| 91 |
rows = cols = 3 |
| 92 |
self.set_grid_image(4, 4, rows, cols, 0, 0) |
| 93 |
for i in range(rows * cols): |
| 94 |
self.check_cell(self.grid[i], i) |
| 95 |
|
| 96 |
def testRect(self): |
| 97 |
|
| 98 |
rows, cols = 2, 5 |
| 99 |
self.set_grid_image(3, 8, rows, cols, 0, 0) |
| 100 |
for i in range(rows * cols): |
| 101 |
self.check_cell(self.grid[i], i) |
| 102 |
|
| 103 |
def testPad(self): |
| 104 |
|
| 105 |
rows, cols = 5, 3 |
| 106 |
self.set_grid_image(10, 9, rows, cols, 3, 7) |
| 107 |
for i in range(rows * cols): |
| 108 |
self.check_cell(self.grid[i], i) |