mirror of
https://github.com/drewcassidy/quicktex.git
synced 2024-09-13 06:37:34 +00:00
API changes
This commit is contained in:
parent
7149864173
commit
09e05b7cde
@ -41,13 +41,13 @@ class Texture {
|
||||
|
||||
virtual int Width() const { return _width; }
|
||||
virtual int Height() const { return _height; }
|
||||
virtual std::tuple<int, int> Dimensions() const { return std::tuple<int, int>(_width, _height); }
|
||||
virtual std::tuple<int, int> Size() const { return std::tuple<int, int>(_width, _height); }
|
||||
|
||||
/**
|
||||
* The texture's total size
|
||||
* @return The size of the texture in bytes.
|
||||
*/
|
||||
virtual size_t Size() const noexcept = 0;
|
||||
virtual size_t NBytes() const noexcept = 0;
|
||||
|
||||
virtual const uint8_t *Data() const noexcept = 0;
|
||||
virtual uint8_t *Data() noexcept = 0;
|
||||
@ -85,7 +85,7 @@ class RawTexture : public Texture {
|
||||
_pixels.at(x + (y * _width)) = val;
|
||||
}
|
||||
|
||||
size_t Size() const noexcept override { return static_cast<unsigned long>(Width() * Height()) * sizeof(Color); }
|
||||
size_t NBytes() const noexcept override { return static_cast<unsigned long>(Width() * Height()) * sizeof(Color); }
|
||||
|
||||
template <int N, int M> ColorBlock<N, M> GetBlock(int block_x, int block_y) const {
|
||||
if (block_x < 0 || (block_x + 1) * N > _width) throw std::out_of_range("x value out of range.");
|
||||
@ -178,7 +178,7 @@ template <typename B> class BlockTexture final : public Texture {
|
||||
_blocks.at(x + (y * _width_b)) = val;
|
||||
}
|
||||
|
||||
size_t Size() const noexcept override { return _blocks.size() * sizeof(B); }
|
||||
size_t NBytes() const noexcept override { return _blocks.size() * sizeof(B); }
|
||||
|
||||
const uint8_t *Data() const noexcept override { return reinterpret_cast<const uint8_t *>(_blocks.data()); }
|
||||
uint8_t *Data() noexcept override{ return reinterpret_cast<uint8_t *>(_blocks.data()); }
|
||||
|
@ -42,13 +42,13 @@ PYBIND11_MODULE(_quicktex, m) {
|
||||
|
||||
py::class_<Texture> texture(m, "Texture", py::buffer_protocol());
|
||||
|
||||
texture.def_property_readonly("nbytes", &Texture::NBytes);
|
||||
texture.def_property_readonly("size", &Texture::Size);
|
||||
texture.def_property_readonly("dimensions", &Texture::Dimensions);
|
||||
texture.def_property_readonly("width", &Texture::Width);
|
||||
texture.def_property_readonly("height", &Texture::Height);
|
||||
|
||||
texture.def_buffer([](Texture &t) { return py::buffer_info(t.Data(), t.Size()); });
|
||||
texture.def("tobytes", [](const Texture &t) { return py::bytes(reinterpret_cast<const char *>(t.Data()), t.Size()); });
|
||||
texture.def_buffer([](Texture &t) { return py::buffer_info(t.Data(), t.NBytes()); });
|
||||
texture.def("tobytes", [](const Texture &t) { return py::bytes(reinterpret_cast<const char *>(t.Data()), t.NBytes()); });
|
||||
|
||||
// RawTexture
|
||||
|
||||
@ -57,7 +57,7 @@ PYBIND11_MODULE(_quicktex, m) {
|
||||
raw_texture.def(py::init<int, int>(), "width"_a, "height"_a);
|
||||
raw_texture.def_static("frombytes", &BufferToTexture<RawTexture>, "data"_a, "width"_a, "height"_a);
|
||||
|
||||
DefSubscript2D(raw_texture, &RawTexture::GetPixel, &RawTexture::SetPixel, &RawTexture::Dimensions);
|
||||
DefSubscript2D(raw_texture, &RawTexture::GetPixel, &RawTexture::SetPixel, &RawTexture::Size);
|
||||
|
||||
InitS3TC(m);
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ template <typename T> T BufferToTexture(py::buffer buf, int width, int height) {
|
||||
|
||||
auto info = buf.request(false);
|
||||
auto output = T(width, height);
|
||||
auto dst_size = output.Size();
|
||||
auto dst_size = output.NBytes();
|
||||
|
||||
if (info.format != py::format_descriptor<uint8_t>::format()) throw std::runtime_error("Incompatible format in python buffer: expected a byte array.");
|
||||
if (info.size < (ssize_t)dst_size) std::runtime_error("Incompatible format in python buffer: Input data is smaller than texture size.");
|
||||
@ -184,9 +184,9 @@ template <typename B> py::class_<B> BindBlock(py::module_& m, const char* name)
|
||||
block.def_readonly_static("width", &B::Width, "The width of the block in pixels.");
|
||||
block.def_readonly_static("height", &B::Height, "The height of the block in pixels.");
|
||||
block.def_property_readonly_static(
|
||||
"dimensions", [](py::object) { return std::make_tuple(B::Width, B::Height); }, "The dimensions of the block in pixels.");
|
||||
"size", [](py::object) { return std::make_tuple(B::Width, B::Height); }, "The dimensions of the block in pixels.");
|
||||
block.def_property_readonly_static(
|
||||
"size", [](py::object) { return sizeof(B); }, "The size of the block in bytes.");
|
||||
"nbytes", [](py::object) { return sizeof(B); }, "The size of the block in bytes.");
|
||||
|
||||
block.def(py::self == py::self);
|
||||
|
||||
@ -227,7 +227,7 @@ template <typename B> py::class_<BlockTexture<B>> BindBlockTexture(py::module_&
|
||||
|
||||
block_texture.def_property_readonly("width_blocks", &BTex::BlocksX, "The width of the texture in blocks.");
|
||||
block_texture.def_property_readonly("height_blocks", &BTex::BlocksY, "The height of the texture in blocks.");
|
||||
block_texture.def_property_readonly("dimensions_blocks", &BTex::BlocksXY, "The dimensions of the texture in blocks.");
|
||||
block_texture.def_property_readonly("size_blocks", &BTex::BlocksXY, "The dimensions of the texture in blocks.");
|
||||
|
||||
DefSubscript2D(block_texture, &BTex::GetBlock, &BTex::SetBlock, &BTex::BlocksXY);
|
||||
|
||||
|
@ -16,10 +16,10 @@ class TestBC1Block(unittest.TestCase):
|
||||
|
||||
def test_size(self):
|
||||
"""Test the size and dimensions of BC1Block"""
|
||||
self.assertEqual(BC1Block.size, 8, 'incorrect block size')
|
||||
self.assertEqual(BC1Block.nbytes, 8, 'incorrect block size')
|
||||
self.assertEqual(BC1Block.width, 4, 'incorrect block width')
|
||||
self.assertEqual(BC1Block.height, 4, 'incorrect block width')
|
||||
self.assertEqual(BC1Block.dimensions, (4, 4), 'incorrect block dimensions')
|
||||
self.assertEqual(BC1Block.size, (4, 4), 'incorrect block dimensions')
|
||||
|
||||
def test_buffer(self):
|
||||
"""Test the buffer protocol of BC1Block"""
|
||||
@ -29,7 +29,7 @@ class TestBC1Block(unittest.TestCase):
|
||||
self.assertFalse(mv.readonly, 'buffer is readonly')
|
||||
self.assertTrue(mv.c_contiguous, 'buffer is not contiguous')
|
||||
self.assertEqual(mv.ndim, 1, 'buffer is multidimensional')
|
||||
self.assertEqual(mv.nbytes, BC1Block.size, 'buffer is the wrong size')
|
||||
self.assertEqual(mv.nbytes, BC1Block.nbytes, 'buffer is the wrong size')
|
||||
self.assertEqual(mv.format, 'B', 'buffer has the wrong format')
|
||||
|
||||
mv[:] = block_bytes
|
||||
@ -68,24 +68,24 @@ class TestBC1Block(unittest.TestCase):
|
||||
class TestBC1Texture(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tex = BC1Texture(self.w, self.h)
|
||||
self.size = self.wb * self.hb * BC1Block.size
|
||||
self.nbytes = self.wb * self.hb * BC1Block.nbytes
|
||||
|
||||
def test_size(self):
|
||||
"""Test size of BC1Texture in bytes"""
|
||||
self.assertEqual(self.tex.size, self.size, 'incorrect texture size')
|
||||
self.assertEqual(len(self.tex.tobytes()), self.size, 'incorrect texture size from tobytes')
|
||||
self.assertEqual(self.tex.nbytes, self.nbytes, 'incorrect texture size')
|
||||
self.assertEqual(len(self.tex.tobytes()), self.nbytes, 'incorrect texture size from tobytes')
|
||||
|
||||
def test_dimensions(self):
|
||||
"""Test dimensions of BC1Texture in pixels"""
|
||||
self.assertEqual(self.tex.width, self.w, 'incorrect texture width')
|
||||
self.assertEqual(self.tex.height, self.h, 'incorrect texture height')
|
||||
self.assertEqual(self.tex.dimensions, (self.w, self.h), 'incorrect texture dimensions')
|
||||
self.assertEqual(self.tex.size, (self.w, self.h), 'incorrect texture dimensions')
|
||||
|
||||
def test_dimensions_blocks(self):
|
||||
"""Test dimensions of BC1Texture in blocks"""
|
||||
self.assertEqual(self.tex.width_blocks, self.wb, 'incorrect texture width_blocks')
|
||||
self.assertEqual(self.tex.height_blocks, self.hb, 'incorrect texture width_blocks')
|
||||
self.assertEqual(self.tex.dimensions_blocks, (self.wb, self.hb), 'incorrect texture dimensions_blocks')
|
||||
self.assertEqual(self.tex.size_blocks, (self.wb, self.hb), 'incorrect texture dimensions_blocks')
|
||||
|
||||
def test_blocks(self):
|
||||
"""Test getting and setting blocks to BC1Texture"""
|
||||
@ -97,9 +97,9 @@ class TestBC1Texture(unittest.TestCase):
|
||||
b = self.tex.tobytes()
|
||||
for x in range(self.wb):
|
||||
for y in range(self.hb):
|
||||
index = (x + (y * self.wb)) * BC1Block.size
|
||||
index = (x + (y * self.wb)) * BC1Block.nbytes
|
||||
tb = self.tex[x, y]
|
||||
fb = BC1Block.frombytes(b[index:index + BC1Block.size])
|
||||
fb = BC1Block.frombytes(b[index:index + BC1Block.nbytes])
|
||||
self.assertEqual(tb, blocks[y][x], 'incorrect block read from texture')
|
||||
self.assertEqual(fb, blocks[y][x], 'incorrect block read from texture bytes')
|
||||
|
||||
@ -116,7 +116,7 @@ class TestBC1Texture(unittest.TestCase):
|
||||
|
||||
self.assertFalse(mv.readonly, 'buffer is readonly')
|
||||
self.assertTrue(mv.c_contiguous, 'buffer is not contiguous')
|
||||
self.assertEqual(mv.nbytes, self.size, 'buffer is the wrong size')
|
||||
self.assertEqual(mv.nbytes, self.nbytes, 'buffer is the wrong size')
|
||||
self.assertEqual(mv.format, 'B', 'buffer has the wrong format')
|
||||
|
||||
data = block_bytes * self.wb * self.hb
|
||||
@ -141,7 +141,7 @@ class TestBC1Encoder(unittest.TestCase):
|
||||
"""Test encoder output with 4 color greyscale test block"""
|
||||
out_tex = self.bc1_encoder.encode(BC1Blocks.greyscale.texture)
|
||||
|
||||
self.assertEqual(out_tex.dimensions_blocks, (1, 1), 'encoded texture has multiple blocks')
|
||||
self.assertEqual(out_tex.size_blocks, (1, 1), 'encoded texture has multiple blocks')
|
||||
|
||||
out_block = out_tex[0, 0]
|
||||
|
||||
@ -152,7 +152,7 @@ class TestBC1Encoder(unittest.TestCase):
|
||||
"""Test encoder output with 3 color test block"""
|
||||
out_tex = self.bc1_encoder.encode(BC1Blocks.three_color.texture)
|
||||
|
||||
self.assertEqual(out_tex.dimensions_blocks, (1, 1), 'encoded texture has multiple blocks')
|
||||
self.assertEqual(out_tex.size_blocks, (1, 1), 'encoded texture has multiple blocks')
|
||||
|
||||
out_block = out_tex[0, 0]
|
||||
|
||||
@ -166,7 +166,7 @@ class TestBC1Encoder(unittest.TestCase):
|
||||
"""Test encoder output with 3 color test block with black pixels"""
|
||||
out_tex = self.bc1_encoder.encode(BC1Blocks.three_color_black.texture)
|
||||
|
||||
self.assertEqual(out_tex.dimensions_blocks, (1, 1), 'encoded texture has multiple blocks')
|
||||
self.assertEqual(out_tex.size_blocks, (1, 1), 'encoded texture has multiple blocks')
|
||||
|
||||
out_block = out_tex[0, 0]
|
||||
has_black = 3 in [j for row in out_block.selectors for j in row]
|
||||
@ -199,7 +199,7 @@ class TestBC1Decoder(unittest.TestCase):
|
||||
in_tex[0, 0] = block
|
||||
out_tex = self.bc1_decoder.decode(in_tex)
|
||||
|
||||
self.assertEqual(out_tex.dimensions, (4, 4), 'decoded texture has incorrect dimensions')
|
||||
self.assertEqual(out_tex.size, (4, 4), 'decoded texture has incorrect dimensions')
|
||||
|
||||
out_img = Image.frombytes('RGBA', (4, 4), out_tex.tobytes())
|
||||
img_diff = ImageChops.difference(out_img, image).convert('L')
|
||||
|
@ -17,10 +17,10 @@ class TestBC4Block(unittest.TestCase):
|
||||
|
||||
def test_size(self):
|
||||
"""Test the size and dimensions of BC4Block"""
|
||||
self.assertEqual(BC4Block.size, 8, 'incorrect block size')
|
||||
self.assertEqual(BC4Block.nbytes, 8, 'incorrect block size')
|
||||
self.assertEqual(BC4Block.width, 4, 'incorrect block width')
|
||||
self.assertEqual(BC4Block.height, 4, 'incorrect block width')
|
||||
self.assertEqual(BC4Block.dimensions, (4, 4), 'incorrect block dimensions')
|
||||
self.assertEqual(BC4Block.size, (4, 4), 'incorrect block dimensions')
|
||||
|
||||
def test_buffer(self):
|
||||
"""Test the buffer protocol of BC4Block"""
|
||||
@ -30,7 +30,7 @@ class TestBC4Block(unittest.TestCase):
|
||||
self.assertFalse(mv.readonly, 'buffer is readonly')
|
||||
self.assertTrue(mv.c_contiguous, 'buffer is not contiguous')
|
||||
self.assertEqual(mv.ndim, 1, 'buffer is multidimensional')
|
||||
self.assertEqual(mv.nbytes, BC4Block.size, 'buffer is the wrong size')
|
||||
self.assertEqual(mv.nbytes, BC4Block.nbytes, 'buffer is the wrong size')
|
||||
self.assertEqual(mv.format, 'B', 'buffer has the wrong format')
|
||||
|
||||
mv[:] = self.block_bytes
|
||||
@ -81,24 +81,24 @@ class TestBC4Block(unittest.TestCase):
|
||||
class TestBC4Texture(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tex = BC4Texture(self.w, self.h)
|
||||
self.size = self.wb * self.hb * BC4Block.size
|
||||
self.nbytes = self.wb * self.hb * BC4Block.nbytes
|
||||
|
||||
def test_size(self):
|
||||
"""Test size of BC4Texture in bytes"""
|
||||
self.assertEqual(self.tex.size, self.size, 'incorrect texture size')
|
||||
self.assertEqual(len(self.tex.tobytes()), self.size, 'incorrect texture size from tobytes')
|
||||
self.assertEqual(self.tex.nbytes, self.nbytes, 'incorrect texture size')
|
||||
self.assertEqual(len(self.tex.tobytes()), self.nbytes, 'incorrect texture size from tobytes')
|
||||
|
||||
def test_dimensions(self):
|
||||
"""Test dimensions of BC4Texture in pixels"""
|
||||
self.assertEqual(self.tex.width, self.w, 'incorrect texture width')
|
||||
self.assertEqual(self.tex.height, self.h, 'incorrect texture height')
|
||||
self.assertEqual(self.tex.dimensions, (self.w, self.h), 'incorrect texture dimensions')
|
||||
self.assertEqual(self.tex.size, (self.w, self.h), 'incorrect texture dimensions')
|
||||
|
||||
def test_dimensions_blocks(self):
|
||||
"""Test dimensions of BC4Texture in blocks"""
|
||||
self.assertEqual(self.tex.width_blocks, self.wb, 'incorrect texture width_blocks')
|
||||
self.assertEqual(self.tex.height_blocks, self.hb, 'incorrect texture width_blocks')
|
||||
self.assertEqual(self.tex.dimensions_blocks, (self.wb, self.hb), 'incorrect texture dimensions_blocks')
|
||||
self.assertEqual(self.tex.size_blocks, (self.wb, self.hb), 'incorrect texture dimensions_blocks')
|
||||
|
||||
def test_blocks(self):
|
||||
"""Test getting and setting blocks to BC4Texture"""
|
||||
@ -110,9 +110,9 @@ class TestBC4Texture(unittest.TestCase):
|
||||
b = self.tex.tobytes()
|
||||
for x in range(self.wb):
|
||||
for y in range(self.hb):
|
||||
index = (x + (y * self.wb)) * BC4Block.size
|
||||
index = (x + (y * self.wb)) * BC4Block.nbytes
|
||||
tb = self.tex[x, y]
|
||||
fb = BC4Block.frombytes(b[index:index + BC4Block.size])
|
||||
fb = BC4Block.frombytes(b[index:index + BC4Block.nbytes])
|
||||
self.assertEqual(tb, blocks[y][x], 'incorrect block read from texture')
|
||||
self.assertEqual(fb, blocks[y][x], 'incorrect block read from texture bytes')
|
||||
|
||||
@ -129,7 +129,7 @@ class TestBC4Texture(unittest.TestCase):
|
||||
|
||||
self.assertFalse(mv.readonly, 'buffer is readonly')
|
||||
self.assertTrue(mv.c_contiguous, 'buffer is not contiguous')
|
||||
self.assertEqual(mv.nbytes, self.size, 'buffer is the wrong size')
|
||||
self.assertEqual(mv.nbytes, self.nbytes, 'buffer is the wrong size')
|
||||
self.assertEqual(mv.format, 'B', 'buffer has the wrong format')
|
||||
|
||||
data = b'\xF0\x10\x88\x86\x68\xAC\xCF\xFA' * self.wb * self.hb
|
||||
@ -149,7 +149,7 @@ class TestBC4Encoder(unittest.TestCase):
|
||||
"""Test encoder output with 8 value test block"""
|
||||
out_tex = self.bc4_encoder.encode(BC4Blocks.eight_value.texture)
|
||||
|
||||
self.assertEqual(out_tex.dimensions_blocks, (1, 1), 'encoded texture has multiple blocks')
|
||||
self.assertEqual(out_tex.size_blocks, (1, 1), 'encoded texture has multiple blocks')
|
||||
|
||||
out_block = out_tex[0, 0]
|
||||
|
||||
@ -174,7 +174,7 @@ class TestBC4Decoder(unittest.TestCase):
|
||||
in_tex[0, 0] = block
|
||||
out_tex = self.bc4_decoder.decode(in_tex)
|
||||
|
||||
self.assertEqual(out_tex.dimensions, (4, 4), 'decoded texture has incorrect dimensions')
|
||||
self.assertEqual(out_tex.size, (4, 4), 'decoded texture has incorrect dimensions')
|
||||
|
||||
out_img = Image.frombytes('RGBA', (4, 4), out_tex.tobytes())
|
||||
img_diff = ImageChops.difference(out_img, image).convert('L')
|
||||
|
@ -17,10 +17,10 @@ class TestRawTexture(unittest.TestCase):
|
||||
|
||||
def test_size(self):
|
||||
"""Test byte size and image dimensions"""
|
||||
self.assertEqual(self.tex.size, self.size, "incorrect texture byte size")
|
||||
self.assertEqual(self.tex.nbytes, self.size, "incorrect texture byte size")
|
||||
self.assertEqual(self.tex.width, self.width, "incorrect texture width")
|
||||
self.assertEqual(self.tex.height, self.height, "incorrect texture height")
|
||||
self.assertEqual(self.tex.dimensions, (self.width, self.height), "incorrect texture dimensions")
|
||||
self.assertEqual(self.tex.size, (self.width, self.height), "incorrect texture dimensions")
|
||||
|
||||
def test_pixels(self):
|
||||
"""Test getting and setting pixel values"""
|
||||
|
Loading…
Reference in New Issue
Block a user