quicktex/tests/test_texture.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.9 KiB
Python
Raw Normal View History

2021-04-07 06:41:26 +00:00
import os.path
2022-05-23 01:40:13 +00:00
import pytest
2021-03-30 05:15:53 +00:00
from PIL import Image
2022-05-23 01:40:13 +00:00
from quicktex import RawTexture
from .images import image_path
2021-03-30 05:15:53 +00:00
2022-05-23 01:40:13 +00:00
class TestRawTexture:
2021-04-07 06:41:26 +00:00
boilerplate = Image.open(os.path.join(image_path, 'Boilerplate.png'))
boilerplate_bytes = boilerplate.tobytes('raw', 'RGBX')
width, height = boilerplate.size
2022-05-23 01:40:13 +00:00
nbytes = width * height * 4
2021-03-30 05:15:53 +00:00
def test_size(self):
2021-03-30 09:36:27 +00:00
"""Test byte size and image dimensions"""
2022-05-23 01:40:13 +00:00
tex = RawTexture(self.width, self.height)
assert tex.nbytes == self.nbytes
assert tex.width == self.width
assert tex.height == self.height
assert tex.size == (self.width, self.height)
2021-03-30 05:15:53 +00:00
def test_pixels(self):
2021-03-30 09:36:27 +00:00
"""Test getting and setting pixel values"""
2022-05-23 01:40:13 +00:00
tex = RawTexture(self.width, self.height)
2021-03-30 05:15:53 +00:00
color1 = (69, 13, 12, 0) # totally random color
color2 = (19, 142, 93, 44)
2022-05-23 01:40:13 +00:00
tex[0, 0] = color1
tex[-1, -1] = color2
data = tex.tobytes()
2021-03-30 05:15:53 +00:00
2022-05-23 01:40:13 +00:00
assert tex[0, 0] == color1
assert tex[-1, -1] == color2
assert tuple(data[0:4]) == color1
assert tuple(data[-4:]) == color2
2021-04-07 06:41:26 +00:00
2022-05-23 01:40:13 +00:00
with pytest.raises(IndexError):
_ = tex[self.width, self.height]
with pytest.raises(IndexError):
_ = tex[-1 - self.width, -1 - self.height]
2021-03-30 05:15:53 +00:00
def test_buffer(self):
"""Test the Buffer protocol implementation for RawTexture"""
2022-05-23 01:40:13 +00:00
tex = RawTexture(self.width, self.height)
mv = memoryview(tex)
2021-04-07 06:41:26 +00:00
mv[:] = self.boilerplate_bytes
2022-05-23 01:40:13 +00:00
assert not mv.readonly
assert mv.c_contiguous
assert mv.nbytes == self.nbytes
assert mv.format == 'B'
assert mv.tobytes() == self.boilerplate_bytes
assert mv.tobytes() == tex.tobytes()
2021-03-30 05:15:53 +00:00
2021-03-30 09:36:27 +00:00
def test_frombytes(self):
2021-03-30 05:15:53 +00:00
"""Test the frombytes factory function"""
2021-04-07 06:41:26 +00:00
bytetex = RawTexture.frombytes(self.boilerplate_bytes, *self.boilerplate.size)
2022-05-23 01:40:13 +00:00
assert self.boilerplate_bytes == bytetex.tobytes()