2021-02-01 08:25:19 +00:00
|
|
|
/* Python-rgbcx Texture Compression Library
|
|
|
|
Copyright (C) 2021 Andrew Cassidy <drewcassidy@me.com>
|
2021-02-15 03:01:41 +00:00
|
|
|
Partially derived from rgbcx.h written by Richard Geldreich <richgel99@gmail.com>
|
2021-02-01 08:25:19 +00:00
|
|
|
and licenced under the public domain
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
2021-02-07 23:20:03 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
2021-02-01 08:25:19 +00:00
|
|
|
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
|
2021-02-07 23:20:03 +00:00
|
|
|
GNU Lesser General Public License for more details.
|
2021-02-01 08:25:19 +00:00
|
|
|
|
2021-02-07 23:20:03 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2021-02-01 08:25:19 +00:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-02-06 03:44:19 +00:00
|
|
|
#include <array>
|
2021-02-01 08:25:19 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
2021-02-02 07:31:27 +00:00
|
|
|
#include <cstdlib>
|
2021-02-01 08:25:19 +00:00
|
|
|
|
2021-03-13 12:14:04 +00:00
|
|
|
#include "../../util.h"
|
2021-02-01 08:25:19 +00:00
|
|
|
|
2021-03-28 09:36:47 +00:00
|
|
|
namespace quicktex::s3tc {
|
2021-02-01 08:25:19 +00:00
|
|
|
|
2021-04-01 23:37:15 +00:00
|
|
|
class alignas(8) BC4Block {
|
2021-02-06 03:44:19 +00:00
|
|
|
public:
|
2021-04-01 23:37:15 +00:00
|
|
|
static constexpr int Width = 4;
|
|
|
|
static constexpr int Height = 4;
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-04-01 23:37:15 +00:00
|
|
|
using SelectorArray = std::array<std::array<uint8_t, Width>, Height>;
|
|
|
|
using AlphaPair = std::pair<uint8_t, uint8_t>;
|
|
|
|
|
|
|
|
constexpr BC4Block() {
|
|
|
|
static_assert(sizeof(BC4Block) == 8);
|
|
|
|
static_assert(sizeof(std::array<BC4Block, 10>) == 8 * 10);
|
2021-04-02 05:37:25 +00:00
|
|
|
static_assert(alignof(BC4Block) >= 8);
|
2021-04-01 23:37:15 +00:00
|
|
|
alpha0 = alpha1 = 0;
|
2021-04-02 05:37:25 +00:00
|
|
|
_selectors = {0, 0, 0, 0, 0, 0};
|
2021-04-01 23:37:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 05:37:25 +00:00
|
|
|
BC4Block(uint8_t valpha0, uint8_t valpha1, const SelectorArray& selectors) {
|
2021-04-01 23:37:15 +00:00
|
|
|
alpha0 = valpha0;
|
|
|
|
alpha1 = valpha1;
|
|
|
|
SetSelectors(selectors);
|
|
|
|
}
|
|
|
|
|
2021-04-02 05:37:25 +00:00
|
|
|
constexpr AlphaPair GetAlphas() const { return AlphaPair(alpha0, alpha1); }
|
2021-04-01 23:37:15 +00:00
|
|
|
|
|
|
|
void SetAlphas(AlphaPair as) {
|
|
|
|
alpha0 = as.first;
|
|
|
|
alpha1 = as.second;
|
|
|
|
}
|
2021-02-01 08:25:19 +00:00
|
|
|
|
2021-04-02 05:37:25 +00:00
|
|
|
constexpr uint64_t GetSelectorBits() const {
|
|
|
|
auto packed = Pack<uint8_t, uint64_t, 8, SelectorSize>(_selectors);
|
|
|
|
assert(packed <= SelectorsPackedMax);
|
2021-02-13 08:16:04 +00:00
|
|
|
return packed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSelectorBits(uint64_t packed) {
|
2021-04-02 05:37:25 +00:00
|
|
|
assert(packed <= SelectorsPackedMax);
|
|
|
|
_selectors = Unpack<uint64_t, uint8_t, 8, SelectorSize>(packed);
|
2021-02-13 08:16:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 05:37:25 +00:00
|
|
|
constexpr SelectorArray GetSelectors() const {
|
|
|
|
auto rows = Unpack<uint64_t, uint16_t, SelectorBits * Width, Height>(GetSelectorBits());
|
|
|
|
auto unpacked = MapArray(rows, Unpack<uint16_t, uint8_t, SelectorBits, Width>);
|
2021-02-13 08:16:04 +00:00
|
|
|
return unpacked;
|
|
|
|
}
|
|
|
|
|
2021-04-01 23:37:15 +00:00
|
|
|
void SetSelectors(const SelectorArray& unpacked) {
|
2021-04-02 05:37:25 +00:00
|
|
|
auto rows = MapArray(unpacked, Pack<uint8_t, uint16_t, SelectorBits, Width>);
|
|
|
|
auto packed = Pack<uint16_t, uint64_t, SelectorBits * Width, Height>(rows);
|
2021-02-16 18:18:17 +00:00
|
|
|
SetSelectorBits(packed);
|
|
|
|
}
|
|
|
|
|
2021-04-02 05:37:25 +00:00
|
|
|
constexpr bool Is6Value() const { return alpha0 <= alpha1; }
|
|
|
|
|
|
|
|
static constexpr std::array<uint8_t, 8> GetValues6(unsigned l, unsigned h) {
|
2021-02-06 03:44:19 +00:00
|
|
|
return {static_cast<uint8_t>(l),
|
|
|
|
static_cast<uint8_t>(h),
|
|
|
|
static_cast<uint8_t>((l * 4 + h) / 5),
|
|
|
|
static_cast<uint8_t>((l * 3 + h * 2) / 5),
|
|
|
|
static_cast<uint8_t>((l * 2 + h * 3) / 5),
|
|
|
|
static_cast<uint8_t>((l + h * 4) / 5),
|
|
|
|
0,
|
|
|
|
255};
|
2021-02-01 08:25:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 05:37:25 +00:00
|
|
|
static constexpr std::array<uint8_t, 8> GetValues8(unsigned l, unsigned h) {
|
2021-02-06 03:44:19 +00:00
|
|
|
return {static_cast<uint8_t>(l),
|
|
|
|
static_cast<uint8_t>(h),
|
|
|
|
static_cast<uint8_t>((l * 6 + h) / 7),
|
|
|
|
static_cast<uint8_t>((l * 5 + h * 2) / 7),
|
|
|
|
static_cast<uint8_t>((l * 4 + h * 3) / 7),
|
|
|
|
static_cast<uint8_t>((l * 3 + h * 4) / 7),
|
|
|
|
static_cast<uint8_t>((l * 2 + h * 5) / 7),
|
|
|
|
static_cast<uint8_t>((l + h * 6) / 7)};
|
2021-02-01 08:25:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 05:37:25 +00:00
|
|
|
static constexpr std::array<uint8_t, 8> GetValues(unsigned l, unsigned h) { return l > h ? GetValues8(l, h) : GetValues6(l, h); }
|
2021-02-01 08:25:19 +00:00
|
|
|
|
2021-04-02 05:37:25 +00:00
|
|
|
static constexpr size_t SelectorSize = 6;
|
|
|
|
static constexpr uint8_t SelectorBits = 3;
|
|
|
|
static constexpr uint64_t SelectorsPackedMax = (1ULL << (8U * SelectorSize)) - 1U;
|
2021-02-06 03:44:19 +00:00
|
|
|
|
2021-04-01 23:37:15 +00:00
|
|
|
uint8_t alpha0;
|
|
|
|
uint8_t alpha1;
|
|
|
|
|
|
|
|
private:
|
2021-04-02 05:37:25 +00:00
|
|
|
std::array<uint8_t, SelectorSize> _selectors;
|
2021-02-01 08:25:19 +00:00
|
|
|
};
|
2021-03-14 08:59:16 +00:00
|
|
|
} // namespace quicktex::s3tc
|