From b77a5acfb64c420c976c94c28088541e2ecc2af7 Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Thu, 11 Mar 2021 02:01:16 -0800 Subject: [PATCH] Decoder APIs refinement --- src/rgbcx/BC3/BC3Decoder.h | 5 ++++- src/rgbcx/BC5/BC5Decoder.cpp | 4 ++-- src/rgbcx/BC5/BC5Decoder.h | 28 ++++++++++------------------ src/rgbcx/bindings/Decoders.cpp | 10 +++++----- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/rgbcx/BC3/BC3Decoder.h b/src/rgbcx/BC3/BC3Decoder.h index 187d3e9..0119b6e 100644 --- a/src/rgbcx/BC3/BC3Decoder.h +++ b/src/rgbcx/BC3/BC3Decoder.h @@ -36,11 +36,14 @@ class BC3Decoder : public BlockDecoderTemplate { using BC1DecoderPtr = std::shared_ptr; using BC4DecoderPtr = std::shared_ptr; - BC3Decoder(Interpolator::Type type = Interpolator::Type::Ideal) : BC3Decoder(std::make_shared(type)) {} + BC3Decoder(Interpolator::Type type = Interpolator::Type::Ideal) : BC3Decoder(std::make_shared(type), std::make_shared(3)) {} BC3Decoder(BC1DecoderPtr bc1_decoder, BC4DecoderPtr bc4_decoder = std::make_shared()) : _bc1_decoder(bc1_decoder), _bc4_decoder(bc4_decoder) {} void DecodeBlock(Color4x4 dest, BC3Block *const block) const noexcept(ndebug) override; + BC1DecoderPtr GetBC1Decoder() const { return _bc1_decoder; } + BC4DecoderPtr GetBC4Decoder() const { return _bc4_decoder; } + private: const BC1DecoderPtr _bc1_decoder; const BC4DecoderPtr _bc4_decoder; diff --git a/src/rgbcx/BC5/BC5Decoder.cpp b/src/rgbcx/BC5/BC5Decoder.cpp index ada279a..41b232f 100644 --- a/src/rgbcx/BC5/BC5Decoder.cpp +++ b/src/rgbcx/BC5/BC5Decoder.cpp @@ -26,7 +26,7 @@ namespace rgbcx { void BC5Decoder::DecodeBlock(Color4x4 dest, BC5Block *const block) const noexcept(ndebug) { - _bc4_decoder->DecodeBlock(dest, &block->chan0_block, _chan0); - _bc4_decoder->DecodeBlock(dest, &block->chan1_block, _chan1); + _chan0_decoder->DecodeBlock(dest, &block->chan0_block); + _chan1_decoder->DecodeBlock(dest, &block->chan1_block); } } // namespace rgbcx \ No newline at end of file diff --git a/src/rgbcx/BC5/BC5Decoder.h b/src/rgbcx/BC5/BC5Decoder.h index 4ea54e0..daa38f2 100644 --- a/src/rgbcx/BC5/BC5Decoder.h +++ b/src/rgbcx/BC5/BC5Decoder.h @@ -22,8 +22,8 @@ #include #include #include -#include #include +#include #include "../BC4/BC4Decoder.h" #include "../BlockDecoder.h" @@ -36,31 +36,23 @@ class BC5Decoder : public BlockDecoderTemplate { public: using ChannelPair = std::tuple; using BC4DecoderPtr = std::shared_ptr; + using BC4DecoderPair = std::tuple; - BC5Decoder(uint8_t chan0 = 0, uint8_t chan1 = 1) : BC5Decoder(std::make_shared(), chan0, chan1) {} - BC5Decoder(BC4DecoderPtr bc4_decoder, uint8_t chan0 = 0, uint8_t chan1 = 1) : _bc4_decoder(bc4_decoder), _chan0(chan0), _chan1(chan1) { - assert(chan0 < 4U); - assert(chan1 < 4U); - assert(chan0 != chan1); - } + BC5Decoder(uint8_t chan0 = 0, uint8_t chan1 = 1) : BC5Decoder(std::make_shared(chan0), std::make_shared(chan1)) {} + BC5Decoder(BC4DecoderPtr chan0_decoder, BC4DecoderPtr chan1_decoder) : _chan0_decoder(chan0_decoder), _chan1_decoder(chan1_decoder) {} void DecodeBlock(Color4x4 dest, BC5Block *const block) const noexcept(ndebug) override; - constexpr size_t GetChannel0() const { return _chan0; } - constexpr size_t GetChannel1() const { return _chan1; } - - ChannelPair GetChannels() const { return ChannelPair(_chan0, _chan1); } + ChannelPair GetChannels() const { return ChannelPair(_chan0_decoder->GetChannel(), _chan1_decoder->GetChannel()); } void SetChannels(ChannelPair channels) { - if (std::get<0>(channels) >= 4) throw std::invalid_argument("Channel 0 out of range"); - if (std::get<1>(channels) >= 4) throw std::invalid_argument("Channel 1 out of range"); - _chan0 = std::get<0>(channels); - _chan1 = std::get<1>(channels); + _chan0_decoder->SetChannel(std::get<0>(channels)); + _chan1_decoder->SetChannel(std::get<1>(channels)); } + BC4DecoderPair GetBC4Decoders() const { return BC4DecoderPair(_chan0_decoder, _chan1_decoder); } private: - const BC4DecoderPtr _bc4_decoder; - uint8_t _chan0; - uint8_t _chan1; + const BC4DecoderPtr _chan0_decoder; + const BC4DecoderPtr _chan1_decoder; }; } // namespace rgbcx diff --git a/src/rgbcx/bindings/Decoders.cpp b/src/rgbcx/bindings/Decoders.cpp index b9ce183..6aa095b 100644 --- a/src/rgbcx/bindings/Decoders.cpp +++ b/src/rgbcx/bindings/Decoders.cpp @@ -28,9 +28,6 @@ #include "../BlockDecoder.h" #include "../bitwiseEnums.h" -#define STRINGIFY(x) #x -#define MACRO_STRINGIFY(x) STRINGIFY(x) - namespace py = pybind11; namespace rgbcx::bindings { @@ -46,7 +43,7 @@ py::bytes DecodeImage(const BlockDecoder &self, py::bytes encoded, unsigned imag std::string encoded_str = (std::string)encoded; // encoded data is copied here, unfortunately std::string decoded_str = std::string(color_size, 0); - 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"); + if (encoded_str.size() != block_size) throw std::invalid_argument("Incompatible data: image width and height do not match the size of the encoded image"); self.DecodeImage(reinterpret_cast(encoded_str.data()), reinterpret_cast(decoded_str.data()), image_width, image_height); @@ -74,7 +71,9 @@ void InitDecoders(py::module_ &m) { // BC3Decoder py::class_ bc3_decoder(m, "BC3Decoder", block_decoder); - bc3_decoder.def(py::init(), py::arg("interpolator") = Interpolator::Type::Ideal); + bc3_decoder.def(py::init(), py::arg("type") = Interpolator::Type::Ideal); + bc3_decoder.def_property_readonly("bc1_decoder", &BC3Decoder::GetBC1Decoder); + bc3_decoder.def_property_readonly("bc4_decoder", &BC3Decoder::GetBC4Decoder); // BC4Decoder py::class_ bc4_decoder(m, "BC4Decoder", block_decoder); @@ -87,6 +86,7 @@ void InitDecoders(py::module_ &m) { bc5_decoder.def(py::init(), py::arg("chan0") = 0, py::arg("chan1") = 1); bc5_decoder.def_property("channels", &BC5Decoder::GetChannels, &BC5Decoder::SetChannels); + bc5_decoder.def_property_readonly("bc4_decoders", &BC5Decoder::GetBC4Decoders); } } // namespace rgbcx::bindings \ No newline at end of file