From 83d547dd8e41575e63d4a1b67f4bf26f03182f5f Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Wed, 10 Mar 2021 03:49:06 -0800 Subject: [PATCH] Rearrange bindings --- src/rgbcx/bindings/BlockEncoder.cpp | 64 ------------------- .../{BlockDecoder.cpp => Decoders.cpp} | 14 +++- src/rgbcx/bindings/{BC1.cpp => Encoders.cpp} | 43 +++++++++---- src/rgbcx/bindings/Module.cpp | 10 ++- 4 files changed, 46 insertions(+), 85 deletions(-) delete mode 100644 src/rgbcx/bindings/BlockEncoder.cpp rename src/rgbcx/bindings/{BlockDecoder.cpp => Decoders.cpp} (85%) rename src/rgbcx/bindings/{BC1.cpp => Encoders.cpp} (71%) diff --git a/src/rgbcx/bindings/BlockEncoder.cpp b/src/rgbcx/bindings/BlockEncoder.cpp deleted file mode 100644 index 2913fe1..0000000 --- a/src/rgbcx/bindings/BlockEncoder.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* Python-rgbcx Texture Compression Library - Copyright (C) 2021 Andrew Cassidy - Partially derived from rgbcx.h written by Richard Geldreich - and licenced under the public domain - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . - */ - -#include "../BlockEncoder.h" - -#include - -#include - -#include "../bitwiseEnums.h" - -#define STRINGIFY(x) #x -#define MACRO_STRINGIFY(x) STRINGIFY(x) - -namespace py = pybind11; -namespace rgbcx::bindings { - -py::bytes EncodeImage(const BlockEncoder &self, py::bytes decoded, unsigned image_width, unsigned image_height) { - if (image_width % self.BlockWidth() != 0) throw std::invalid_argument("Width is not an even multiple of block_width"); - if (image_height % self.BlockHeight() != 0) throw std::invalid_argument("Height is not an even multiple of block_height"); - if (image_width == 0 || image_height == 0) throw std::invalid_argument("Image has zero size"); - - size_t size = image_width * image_height; - size_t block_size = (size / (self.BlockHeight() * self.BlockWidth())) * self.BlockSize(); - size_t color_size = size * sizeof(Color); - - std::string encoded_str = std::string(block_size, 0); - std::string decoded_str = (std::string)decoded; // decoded data is copied here, unfortunately - - if (decoded_str.size() != color_size) throw std::invalid_argument("Incompatible data: image width and height do not match the size of the decoded image"); - - self.EncodeImage(reinterpret_cast(encoded_str.data()), reinterpret_cast(decoded_str.data()), image_width, image_height); - - auto bytes = py::bytes(encoded_str); // encoded data is copied here, unfortunately - - return bytes; -} - -void InitBlockEncoder(py::module_ &m) { - py::class_ block_encoder(m, "BlockEncoder"); - - block_encoder.def("encode_image", &EncodeImage); - block_encoder.def_property_readonly("block_size", &BlockEncoder::BlockSize); - block_encoder.def_property_readonly("block_width", &BlockEncoder::BlockWidth); - block_encoder.def_property_readonly("block_height", &BlockEncoder::BlockHeight); -} - -} // namespace rgbcx::bindings \ No newline at end of file diff --git a/src/rgbcx/bindings/BlockDecoder.cpp b/src/rgbcx/bindings/Decoders.cpp similarity index 85% rename from src/rgbcx/bindings/BlockDecoder.cpp rename to src/rgbcx/bindings/Decoders.cpp index 0d0e2cc..3931582 100644 --- a/src/rgbcx/bindings/BlockDecoder.cpp +++ b/src/rgbcx/bindings/Decoders.cpp @@ -17,12 +17,12 @@ along with this program. If not, see . */ -#include "../BlockDecoder.h" - #include #include +#include "../BC1/BC1Decoder.h" +#include "../BlockDecoder.h" #include "../bitwiseEnums.h" #define STRINGIFY(x) #x @@ -52,13 +52,21 @@ py::bytes DecodeImage(const BlockDecoder &self, py::bytes encoded, unsigned imag return bytes; } -void InitBlockDecoder(py::module_ &m) { +void InitDecoders(py::module_ &m) { + // BlockDecoder py::class_ block_decoder(m, "BlockDecoder"); block_decoder.def("decode_image", &DecodeImage); block_decoder.def_property_readonly("block_size", &BlockDecoder::BlockSize); block_decoder.def_property_readonly("block_width", &BlockDecoder::BlockWidth); block_decoder.def_property_readonly("block_height", &BlockDecoder::BlockHeight); + + // BC1Decoder + py::class_ bc1_decoder(m, "BC1Decoder", block_decoder); + + bc1_decoder.def(py::init(), py::arg("interpolator") = Interpolator::Type::Ideal, py::arg("write_alpha") = false); + bc1_decoder.def_property_readonly("interpolator_type", &BC1Decoder::GetInterpolatorType); + bc1_decoder.def_readwrite("write_alpha", &BC1Decoder::write_alpha); } } // namespace rgbcx::bindings \ No newline at end of file diff --git a/src/rgbcx/bindings/BC1.cpp b/src/rgbcx/bindings/Encoders.cpp similarity index 71% rename from src/rgbcx/bindings/BC1.cpp rename to src/rgbcx/bindings/Encoders.cpp index abbf6a6..94168b5 100644 --- a/src/rgbcx/bindings/BC1.cpp +++ b/src/rgbcx/bindings/Encoders.cpp @@ -19,9 +19,9 @@ #include -#include "../BC1/BC1Decoder.h" +#include + #include "../BC1/BC1Encoder.h" -#include "../BlockDecoder.h" #include "../BlockEncoder.h" #include "../bitwiseEnums.h" @@ -31,9 +31,35 @@ namespace py = pybind11; namespace rgbcx::bindings { -void InitBC1(py::module_ &m) { - auto block_encoder = py::type::of(); - auto block_decoder = py::type::of(); +py::bytes EncodeImage(const BlockEncoder &self, py::bytes decoded, unsigned image_width, unsigned image_height) { + if (image_width % self.BlockWidth() != 0) throw std::invalid_argument("Width is not an even multiple of block_width"); + if (image_height % self.BlockHeight() != 0) throw std::invalid_argument("Height is not an even multiple of block_height"); + if (image_width == 0 || image_height == 0) throw std::invalid_argument("Image has zero size"); + + size_t size = image_width * image_height; + size_t block_size = (size / (self.BlockHeight() * self.BlockWidth())) * self.BlockSize(); + size_t color_size = size * sizeof(Color); + + std::string encoded_str = std::string(block_size, 0); + std::string decoded_str = (std::string)decoded; // decoded data is copied here, unfortunately + + if (decoded_str.size() != color_size) throw std::invalid_argument("Incompatible data: image width and height do not match the size of the decoded image"); + + self.EncodeImage(reinterpret_cast(encoded_str.data()), reinterpret_cast(decoded_str.data()), image_width, image_height); + + auto bytes = py::bytes(encoded_str); // encoded data is copied here, unfortunately + + return bytes; +} + +void InitEncoders(py::module_ &m) { + // BlockEncoder + py::class_ block_encoder(m, "BlockEncoder"); + + block_encoder.def("encode_image", &EncodeImage); + block_encoder.def_property_readonly("block_size", &BlockEncoder::BlockSize); + block_encoder.def_property_readonly("block_width", &BlockEncoder::BlockWidth); + block_encoder.def_property_readonly("block_height", &BlockEncoder::BlockHeight); // BC1Encoder py::class_ bc1_encoder(m, "BC1Encoder", block_encoder); @@ -78,13 +104,6 @@ void InitBC1(py::module_ &m) { .value("Faster", BC1Encoder::ErrorMode::Faster) .value("Check2", BC1Encoder::ErrorMode::Check2) .value("Full", BC1Encoder::ErrorMode::Full); - - // BC1Decoder - py::class_ bc1_decoder(m, "BC1Decoder", block_decoder); - - bc1_decoder.def(py::init(), py::arg("interpolator") = Interpolator::Type::Ideal, py::arg("write_alpha") = false); - bc1_decoder.def_property_readonly("interpolator_type", &BC1Decoder::GetInterpolatorType); - bc1_decoder.def_readwrite("write_alpha", &BC1Decoder::write_alpha); } } // namespace rgbcx::bindings \ No newline at end of file diff --git a/src/rgbcx/bindings/Module.cpp b/src/rgbcx/bindings/Module.cpp index deedd8e..419c011 100644 --- a/src/rgbcx/bindings/Module.cpp +++ b/src/rgbcx/bindings/Module.cpp @@ -24,9 +24,8 @@ namespace py = pybind11; namespace rgbcx::bindings { -void InitBlockEncoder(py::module_ &m); -void InitBlockDecoder(py::module_ &m); -void InitBC1(py::module_ &m); +void InitEncoders(py::module_ &m); +void InitDecoders(py::module_ &m); PYBIND11_MODULE(_rgbcx, m) { m.doc() = "More Stuff"; @@ -38,9 +37,8 @@ PYBIND11_MODULE(_rgbcx, m) { .value("Nvidia", IType::Nvidia) .value("AMD", IType::AMD); - InitBlockEncoder(m); - InitBlockDecoder(m); - InitBC1(m); + InitEncoders(m); + InitDecoders(m); } } // namespace rgbcx::bindings \ No newline at end of file