In hindsight, SingleColorTable doesnt need to be a class at all

This commit is contained in:
Andrew Cassidy 2021-02-26 22:38:37 -08:00
parent b57a3d2561
commit ab752e51b6
3 changed files with 58 additions and 74 deletions

View File

@ -187,15 +187,14 @@ void BC1Encoder::EncodeBlockSingleColor(Color color, BC1Block *dest) const {
bool using_3color = false;
// why is there no subscript operator for shared_ptr<array>
auto match_r = _single_match5[color.r];
auto match_g = _single_match6[color.g];
auto match_b = _single_match5[color.b];
BC1MatchEntry match_r = _single_match5->at(color.r);
BC1MatchEntry match_g = _single_match6->at(color.g);
BC1MatchEntry match_b = _single_match5->at(color.b);
if ((_flags & (Flags::Use3ColorBlocks | Flags::Use3ColorBlocksForBlackPixels)) != Flags::None) {
auto match_r_half = _single_match5_half[color.r];
auto match_g_half = _single_match6_half[color.g];
auto match_b_half = _single_match5_half[color.b];
BC1MatchEntry match_r_half = _single_match5_half->at(color.r);
BC1MatchEntry match_g_half = _single_match6_half->at(color.g);
BC1MatchEntry match_b_half = _single_match5_half->at(color.b);
const unsigned err4 = match_r.error + match_g.error + match_b.error;
const unsigned err3 = match_r_half.error + match_g_half.error + match_b_half.error;

View File

@ -36,7 +36,7 @@
namespace rgbcx {
class BC1Encoder : public BlockEncoder<BC1Block, 4, 4> {
class BC1Encoder final : public BlockEncoder<BC1Block, 4, 4> {
public:
using InterpolatorPtr = std::shared_ptr<Interpolator>;
@ -99,11 +99,6 @@ class BC1Encoder : public BlockEncoder<BC1Block, 4, 4> {
EndpointSearchRoundsMask = 1023U << EndpointSearchRoundsShift,
};
BC1Encoder(InterpolatorPtr interpolator);
void EncodeBlock(Color4x4 pixels, BC1Block *dest) const override;
private:
// Unpacked BC1 block with metadata
struct EncodeResults {
Color low;
@ -114,6 +109,11 @@ class BC1Encoder : public BlockEncoder<BC1Block, 4, 4> {
unsigned error = UINT_MAX;
};
BC1Encoder(InterpolatorPtr interpolator);
void EncodeBlock(Color4x4 pixels, BC1Block *dest) const override;
private:
using Hash = uint16_t;
using BlockMetrics = Color4x4::BlockMetrics;
@ -123,10 +123,10 @@ class BC1Encoder : public BlockEncoder<BC1Block, 4, 4> {
// Each entry includes a high and low pair that best reproduces the 8-bit index as well as possible,
// with an included error value
// these depend on the interpolator
const SingleColorTable<5, 4> _single_match5 = SingleColorTable<5, 4>(_interpolator);
const SingleColorTable<6, 4> _single_match6 = SingleColorTable<6, 4>(_interpolator);
const SingleColorTable<5, 3> _single_match5_half = SingleColorTable<5, 3>(_interpolator);
const SingleColorTable<6, 3> _single_match6_half = SingleColorTable<6, 3>(_interpolator);
const MatchListPtr _single_match5 = SingleColorTable<5, 4>(_interpolator);
const MatchListPtr _single_match6 = SingleColorTable<6, 4>(_interpolator);
const MatchListPtr _single_match5_half = SingleColorTable<5, 3>(_interpolator);
const MatchListPtr _single_match6_half = SingleColorTable<6, 3>(_interpolator);
Flags _flags;
unsigned _search_rounds;

View File

@ -33,19 +33,20 @@ namespace rgbcx {
* @tparam B Number of bits (5 or 6)
* @tparam N Number of colors (3 or 4)
*/
template <size_t B, size_t N> class SingleColorTable {
public:
struct MatchEntry {
struct BC1MatchEntry {
uint8_t high;
uint8_t low;
uint8_t error;
};
using MatchList = std::array<MatchEntry, 256>;
using MatchList = std::array<BC1MatchEntry, 256>;
using MatchListPtr = std::shared_ptr<MatchList>;
using InterpolatorPtr = std::shared_ptr<Interpolator>;
SingleColorTable(InterpolatorPtr interpolator) {
template <size_t B, size_t N> MatchListPtr SingleColorTable(InterpolatorPtr interpolator) {
constexpr size_t Size = 1 << B;
MatchListPtr _matches = std::make_shared<MatchList>();
static_assert((B == 5 && Size == 32) || (B == 6 && Size == 64));
static_assert(N == 4 || N == 3);
@ -88,20 +89,4 @@ template <size_t B, size_t N> class SingleColorTable {
}
}
}
MatchEntry operator[](size_t index) const {
assert(index <= UINT8_MAX);
return (*_matches)[index];
}
MatchEntry &operator[](size_t index) {
assert(index <= UINT8_MAX);
return (*_matches)[index];
}
private:
static inline constexpr size_t Size = 1 << B;
MatchListPtr _matches = std::make_shared<MatchList>();
};
} // namespace rgbcx