mirror of
https://github.com/drewcassidy/quicktex.git
synced 2024-09-13 06:37:34 +00:00
Compare commits
8 Commits
e5521796aa
...
0eaf76c675
Author | SHA1 | Date | |
---|---|---|---|
0eaf76c675 | |||
155231a440 | |||
59db9fdb27 | |||
7d830daee3 | |||
2d9abcc15f | |||
647853dd46 | |||
a4e8d7302d | |||
6018ab0cfb |
@ -1,6 +1,10 @@
|
||||
---
|
||||
BasedOnStyle: LLVM
|
||||
BasedOnStyle: google
|
||||
IndentWidth: 4
|
||||
ColumnLimit: 160
|
||||
AllowShortBlocksOnASingleLine: Always
|
||||
AllowShortFunctionsOnASingleLine: All
|
||||
AlwaysBreakTemplateDeclarations: MultiLine
|
||||
#AlignConsecutiveDeclarations: true
|
||||
---
|
||||
|
||||
|
35
.clang-tidy
35
.clang-tidy
@ -1,21 +1,18 @@
|
||||
---
|
||||
FormatStyle: LLVM
|
||||
Checks: 'clang-diagnostic-*,clang-analyzer-*,readability-*'
|
||||
FormatStyle: google
|
||||
|
||||
Checks: '-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,-misc-non-private-member-variables-in-classes,readability-identifier-naming,cppcoreguidelines-narrowing-conversions'
|
||||
CheckOptions:
|
||||
- key: readability-identifier-naming.AbstractClassCase
|
||||
value: 'CamelCase'
|
||||
- key: readability-identifier-naming.ClassCase
|
||||
value: 'CamelCase'
|
||||
- key: readability-identifier-naming.EnumCase
|
||||
value: 'CamelCase'
|
||||
- key: readability-identifier-naming.StructCase
|
||||
value: 'CamelCase'
|
||||
- key: readability-identifier-naming.TypedefCase
|
||||
value: 'CamelCase'
|
||||
- key: readability-identifier-naming.MemberCase
|
||||
value: 'CamelCase'
|
||||
- key: readability-identifier-naming.FunctionCase
|
||||
value: 'camelBack'
|
||||
- key: readability-identifier-naming.ClassMethodCase
|
||||
value: 'camelBack'
|
||||
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
|
||||
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
|
||||
- { key: readability-identifier-naming.StructCase, value: CamelCase }
|
||||
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
|
||||
- { key: readability-identifier-naming.FunctionCase, value: aNy_CasE }
|
||||
- { key: readability-identifier-naming.VariableCase, value: lower_case }
|
||||
- { key: readability-identifier-naming.MemberCase, value: lower_case }
|
||||
- { key: readability-identifier-naming.PrivateMemberPrefix, value: _ }
|
||||
- { key: readability-identifier-naming.ProtectedMemberPrefix, value: _ }
|
||||
- { key: readability-identifier-naming.EnumConstantCase, value: CamelCase }
|
||||
- { key: readability-identifier-naming.ConstexprVariableCase, value: CamelCase }
|
||||
- { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase }
|
||||
- { key: readability-identifier-naming.MemberConstantCase, value: CamelCase }
|
||||
- { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
|
28
.github/workflows/cmake.yml
vendored
28
.github/workflows/cmake.yml
vendored
@ -12,7 +12,26 @@ jobs:
|
||||
# well on Windows or Mac. You can convert this to a matrix build if you need
|
||||
# cross-platform coverage.
|
||||
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- os: macos-latest
|
||||
name: "macOS Clang"
|
||||
cc: "clang"
|
||||
cxx: "clang++"
|
||||
- os: ubuntu-latest
|
||||
name: "Ubuntu Clang"
|
||||
cc: "clang-9"
|
||||
cxx: "clang++-9"
|
||||
- os: ubuntu-latest
|
||||
name: "Ubuntu GCC"
|
||||
cc: "gcc-10"
|
||||
cxx: "g++-10"
|
||||
- os: windows-latest
|
||||
name: "Windows MSVC"
|
||||
cc: "cl"
|
||||
cxx: "cl"
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
@ -21,6 +40,10 @@ jobs:
|
||||
# recursively checkout submodules.
|
||||
submodules: 'true'
|
||||
|
||||
- name: Set Windows ENV
|
||||
if: runner.os == 'Windows'
|
||||
uses: ilammy/msvc-dev-cmd@v1
|
||||
|
||||
- name: Create Build Environment
|
||||
# Some projects don't allow in-source building, so create a separate build directory
|
||||
# We'll use this as our working directory for all subsequent commands
|
||||
@ -34,11 +57,12 @@ jobs:
|
||||
# Note the current convention is to use the -S and -B options here to specify source
|
||||
# and build directories, but this is only available with CMake 3.13 and higher.
|
||||
# The CMake binaries on the Github Actions machines are (as of this writing) 3.12
|
||||
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE
|
||||
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_C_COMPILER=${{ matrix.cc }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx }}
|
||||
|
||||
- name: Build
|
||||
working-directory: ${{github.workspace}}/build
|
||||
shell: bash
|
||||
|
||||
# Execute the build. You can specify a specific target with "--target <NAME>"
|
||||
run: cmake --build . --config $BUILD_TYPE
|
||||
|
||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -17,4 +17,4 @@ install_manifest.txt
|
||||
compile_commands.json
|
||||
CTestTestfile.cmake
|
||||
_deps
|
||||
cmake-build-debug
|
||||
cmake-build-*
|
||||
|
@ -18,8 +18,7 @@ source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE_FILES} ${HEADER_FIL
|
||||
pybind11_add_module(python_rgbcx
|
||||
${SOURCE_FILES}
|
||||
${HEADER_FILES}
|
||||
${PYTHON_FILES}
|
||||
)
|
||||
${PYTHON_FILES})
|
||||
|
||||
add_executable(test_rgbcx
|
||||
${SOURCE_FILES}
|
||||
@ -27,5 +26,11 @@ add_executable(test_rgbcx
|
||||
${TEST_FILES})
|
||||
|
||||
# Set module features, like C/C++ standards
|
||||
target_compile_features(python_rgbcx PUBLIC cxx_std_17 c_std_11)
|
||||
target_compile_features(test_rgbcx PUBLIC cxx_std_17 c_std_11)
|
||||
target_compile_features(python_rgbcx PUBLIC cxx_std_20 c_std_11)
|
||||
target_compile_features(test_rgbcx PUBLIC cxx_std_20 c_std_11)
|
||||
|
||||
set_property(TARGET python_rgbcx test_rgbcx PROPERTY INTERPROCEDURAL_OPTIMIZATION True) #enable FLTO if available
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
|
||||
set_property(TARGET python_rgbcx test_rgbcx PROPERTY OSX_ARCHITECTURES_RELEASE x86_64 arm64) #Mach-O fat binary for arm and x86
|
||||
endif ()
|
||||
|
@ -18,38 +18,8 @@
|
||||
*/
|
||||
|
||||
#include "blocks.h"
|
||||
#include "color.h"
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
// region Color32 implementation
|
||||
Color32::Color32(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { set(vr, vg, vb, va); }
|
||||
|
||||
uint8_t Color32::operator[](uint32_t idx) const {
|
||||
assert(idx < 4);
|
||||
return C[idx];
|
||||
}
|
||||
|
||||
uint8_t &Color32::operator[](uint32_t idx) {
|
||||
assert(idx < 4);
|
||||
return C[idx];
|
||||
}
|
||||
|
||||
void Color32::set(uint8_t vr, uint8_t vg, uint8_t vb, uint8_t va) {
|
||||
C[0] = vr;
|
||||
C[1] = vg;
|
||||
C[2] = vb;
|
||||
C[3] = va;
|
||||
}
|
||||
|
||||
void Color32::set(const Color32 &other) {
|
||||
C[0] = other.C[0];
|
||||
C[1] = other.C[1];
|
||||
C[2] = other.C[2];
|
||||
}
|
||||
Color32 Color32::min(const Color32 &a, const Color32 &b) {
|
||||
return Color32(std::min(a[0], b[0]), std::min(a[1], b[1]), std::min(a[2], b[2]), std::min(a[3], b[3]));
|
||||
}
|
||||
Color32 Color32::max(const Color32 &a, const Color32 &b) {
|
||||
return Color32(std::max(a[0], b[0]), std::max(a[1], b[1]), std::max(a[2], b[2]), std::max(a[3], b[3]));
|
||||
}
|
||||
// endregion
|
||||
|
226
src/blocks.h
226
src/blocks.h
@ -19,172 +19,120 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "util.h"
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
constexpr inline uint8_t DXT1SelectorBits = 2U;
|
||||
#include "color.h"
|
||||
#include "util.h"
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct Color32 {
|
||||
union {
|
||||
struct {
|
||||
uint8_t R;
|
||||
uint8_t G;
|
||||
uint8_t B;
|
||||
uint8_t A;
|
||||
};
|
||||
class BC1Block {
|
||||
public:
|
||||
uint16_t GetLowColor() const { return _low_color[0] | _low_color[1] << 8U; }
|
||||
uint16_t GetHighColor() const { return _high_color[0] | _high_color[1] << 8U; }
|
||||
Color32 GetLowColor32() const { return Color32::unpack565(GetLowColor()); }
|
||||
Color32 GetHighColor32() const { return Color32::unpack565(GetHighColor()); }
|
||||
|
||||
uint8_t C[4];
|
||||
};
|
||||
|
||||
Color32() {}
|
||||
|
||||
Color32(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va);
|
||||
|
||||
void set(uint8_t vr, uint8_t vg, uint8_t vb, uint8_t va);
|
||||
|
||||
void set(const Color32 &other);
|
||||
|
||||
uint8_t operator[](uint32_t idx) const;
|
||||
uint8_t &operator[](uint32_t idx);
|
||||
|
||||
bool operator==(const Color32 &rhs) const {
|
||||
return R == rhs.R && G == rhs.G && B == rhs.B && A == rhs.A;
|
||||
bool Is3Color() const { return GetLowColor() <= GetHighColor(); }
|
||||
void SetLowColor(uint16_t c) {
|
||||
_low_color[0] = c & 0xFF;
|
||||
_low_color[1] = (c >> 8) & 0xFF;
|
||||
}
|
||||
void SetHighColor(uint16_t c) {
|
||||
_high_color[0] = c & 0xFF;
|
||||
_high_color[1] = (c >> 8) & 0xFF;
|
||||
}
|
||||
uint32_t GetSelector(uint32_t x, uint32_t y) const {
|
||||
assert((x < 4U) && (y < 4U));
|
||||
return (selectors[y] >> (x * SelectorBits)) & SelectorMask;
|
||||
}
|
||||
void SetSelector(uint32_t x, uint32_t y, uint32_t val) {
|
||||
assert((x < 4U) && (y < 4U) && (val < 4U));
|
||||
selectors[y] &= (~(SelectorMask << (x * SelectorBits)));
|
||||
selectors[y] |= (val << (x * SelectorBits));
|
||||
}
|
||||
|
||||
static Color32 min(const Color32 &a, const Color32 &b);
|
||||
static Color32 max(const Color32 &a, const Color32 &b);
|
||||
};
|
||||
|
||||
struct BC1Block {
|
||||
constexpr static inline size_t EndpointSize = 2;
|
||||
constexpr static inline size_t SelectorSize = 4;
|
||||
constexpr static inline uint8_t SelectorBits = 2;
|
||||
constexpr static inline uint8_t SelectorValues = 1 << SelectorBits;
|
||||
constexpr static inline uint8_t SelectorMask = SelectorValues - 1;
|
||||
|
||||
uint8_t LowColor[EndpointSize];
|
||||
uint8_t HighColor[EndpointSize];
|
||||
uint8_t Selectors[SelectorSize];
|
||||
private:
|
||||
std::array<uint8_t, EndpointSize> _low_color;
|
||||
std::array<uint8_t, EndpointSize> _high_color;
|
||||
|
||||
inline uint32_t get_low_color() const { return LowColor[0] | (LowColor[1] << 8U); }
|
||||
inline uint32_t get_high_color() const { return HighColor[0] | (HighColor[1] << 8U); }
|
||||
inline bool is_3color() const { return get_low_color() <= get_high_color(); }
|
||||
inline void set_low_color(uint16_t c) {
|
||||
LowColor[0] = static_cast<uint8_t>(c & 0xFF);
|
||||
LowColor[1] = static_cast<uint8_t>((c >> 8) & 0xFF);
|
||||
}
|
||||
inline void set_high_color(uint16_t c) {
|
||||
HighColor[0] = static_cast<uint8_t>(c & 0xFF);
|
||||
HighColor[1] = static_cast<uint8_t>((c >> 8) & 0xFF);
|
||||
}
|
||||
inline uint32_t get_selector(uint32_t x, uint32_t y) const {
|
||||
assert((x < 4U) && (y < 4U));
|
||||
return (Selectors[y] >> (x * SelectorBits)) & SelectorMask;
|
||||
}
|
||||
inline void set_selector(uint32_t x, uint32_t y, uint32_t val) {
|
||||
assert((x < 4U) && (y < 4U) && (val < 4U));
|
||||
Selectors[y] &= (~(SelectorMask << (x * SelectorBits)));
|
||||
Selectors[y] |= (val << (x * DXT1SelectorBits));
|
||||
}
|
||||
|
||||
static inline uint16_t pack_color(const Color32 &color, bool scaled, uint32_t bias = 127U) {
|
||||
uint32_t r = color.R, g = color.G, b = color.B;
|
||||
if (scaled) {
|
||||
r = (r * 31U + bias) / 255U;
|
||||
g = (g * 63U + bias) / 255U;
|
||||
b = (b * 31U + bias) / 255U;
|
||||
}
|
||||
return static_cast<uint16_t>(minimum(b, 31U) | (minimum(g, 63U) << 5U) | (minimum(r, 31U) << 11U));
|
||||
}
|
||||
|
||||
static inline uint16_t pack_unscaled_color(uint32_t r, uint32_t g, uint32_t b) { return static_cast<uint16_t>(b | (g << 5U) | (r << 11U)); }
|
||||
|
||||
static inline void unpack_color(uint32_t c, uint32_t &r, uint32_t &g, uint32_t &b) {
|
||||
r = (c >> 11) & 31;
|
||||
g = (c >> 5) & 63;
|
||||
b = c & 31;
|
||||
|
||||
r = (r << 3) | (r >> 2);
|
||||
g = (g << 2) | (g >> 4);
|
||||
b = (b << 3) | (b >> 2);
|
||||
}
|
||||
|
||||
static inline void unpack_color_unscaled(uint32_t c, uint32_t &r, uint32_t &g, uint32_t &b) {
|
||||
r = (c >> 11) & 31;
|
||||
g = (c >> 5) & 63;
|
||||
b = c & 31;
|
||||
}
|
||||
public:
|
||||
std::array<uint8_t, 4> selectors;
|
||||
};
|
||||
|
||||
struct BC4Block {
|
||||
class BC4Block {
|
||||
public:
|
||||
inline uint32_t GetLowAlpha() const { return low_alpha; }
|
||||
inline uint32_t GetHighAlpha() const { return high_alpha; }
|
||||
inline bool Is6Alpha() const { return GetLowAlpha() <= GetHighAlpha(); }
|
||||
|
||||
inline uint64_t GetSelectorBits() const {
|
||||
return ((uint64_t)((uint32_t)selectors[0] | ((uint32_t)selectors[1] << 8U) | ((uint32_t)selectors[2] << 16U) | ((uint32_t)selectors[3] << 24U))) |
|
||||
(((uint64_t)selectors[4]) << 32U) | (((uint64_t)selectors[5]) << 40U);
|
||||
}
|
||||
|
||||
inline uint32_t GetSelector(uint32_t x, uint32_t y, uint64_t selector_bits) const {
|
||||
assert((x < 4U) && (y < 4U));
|
||||
return (selector_bits >> (((y * 4) + x) * SelectorBits)) & (SelectorMask);
|
||||
}
|
||||
|
||||
static inline std::array<uint8_t, 8> GetBlockValues6(uint32_t l, uint32_t h) {
|
||||
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};
|
||||
}
|
||||
|
||||
static inline std::array<uint8_t, 8> GetBlockValues8(uint32_t l, uint32_t h) {
|
||||
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)};
|
||||
}
|
||||
|
||||
static inline std::array<uint8_t, 8> GetBlockValues(uint32_t l, uint32_t h) {
|
||||
if (l > h)
|
||||
return GetBlockValues8(l, h);
|
||||
else
|
||||
return GetBlockValues6(l, h);
|
||||
}
|
||||
|
||||
constexpr static inline size_t EndpointSize = 1;
|
||||
constexpr static inline size_t SelectorSize = 6;
|
||||
constexpr static inline uint8_t SelectorBits = 3;
|
||||
constexpr static inline uint8_t SelectorValues = 1 << SelectorBits;
|
||||
constexpr static inline uint8_t SelectorMask = SelectorValues - 1;
|
||||
|
||||
uint8_t LowAlpha;
|
||||
uint8_t HighAlpha;
|
||||
uint8_t Selectors[SelectorSize];
|
||||
|
||||
inline uint32_t get_low_alpha() const { return LowAlpha; }
|
||||
inline uint32_t get_high_alpha() const { return HighAlpha; }
|
||||
inline bool is_alpha6_block() const { return get_low_alpha() <= get_high_alpha(); }
|
||||
|
||||
inline uint64_t get_selector_bits() const {
|
||||
return ((uint64_t)((uint32_t)Selectors[0] | ((uint32_t)Selectors[1] << 8U) | ((uint32_t)Selectors[2] << 16U) |
|
||||
((uint32_t)Selectors[3] << 24U))) |
|
||||
(((uint64_t)Selectors[4]) << 32U) | (((uint64_t)Selectors[5]) << 40U);
|
||||
}
|
||||
|
||||
inline uint32_t get_selector(uint32_t x, uint32_t y, uint64_t selector_bits) const {
|
||||
assert((x < 4U) && (y < 4U));
|
||||
return (selector_bits >> (((y * 4) + x) * SelectorBits)) & (SelectorMask);
|
||||
}
|
||||
|
||||
static inline uint32_t get_block_values6(uint8_t *pDst, uint32_t l, uint32_t h) {
|
||||
pDst[0] = static_cast<uint8_t>(l);
|
||||
pDst[1] = static_cast<uint8_t>(h);
|
||||
pDst[2] = static_cast<uint8_t>((l * 4 + h) / 5);
|
||||
pDst[3] = static_cast<uint8_t>((l * 3 + h * 2) / 5);
|
||||
pDst[4] = static_cast<uint8_t>((l * 2 + h * 3) / 5);
|
||||
pDst[5] = static_cast<uint8_t>((l + h * 4) / 5);
|
||||
pDst[6] = 0;
|
||||
pDst[7] = 255;
|
||||
return 6;
|
||||
}
|
||||
|
||||
static inline uint32_t get_block_values8(uint8_t *pDst, uint32_t l, uint32_t h) {
|
||||
pDst[0] = static_cast<uint8_t>(l);
|
||||
pDst[1] = static_cast<uint8_t>(h);
|
||||
pDst[2] = static_cast<uint8_t>((l * 6 + h) / 7);
|
||||
pDst[3] = static_cast<uint8_t>((l * 5 + h * 2) / 7);
|
||||
pDst[4] = static_cast<uint8_t>((l * 4 + h * 3) / 7);
|
||||
pDst[5] = static_cast<uint8_t>((l * 3 + h * 4) / 7);
|
||||
pDst[6] = static_cast<uint8_t>((l * 2 + h * 5) / 7);
|
||||
pDst[7] = static_cast<uint8_t>((l + h * 6) / 7);
|
||||
return 8;
|
||||
}
|
||||
|
||||
static inline uint32_t get_block_values(uint8_t *pDst, uint32_t l, uint32_t h) {
|
||||
if (l > h)
|
||||
return get_block_values8(pDst, l, h);
|
||||
else
|
||||
return get_block_values6(pDst, l, h);
|
||||
}
|
||||
uint8_t low_alpha;
|
||||
uint8_t high_alpha;
|
||||
uint8_t selectors[SelectorSize];
|
||||
};
|
||||
|
||||
struct BC3Block {
|
||||
BC4Block AlphaBlock;
|
||||
BC1Block ColorBlock;
|
||||
class BC3Block {
|
||||
public:
|
||||
BC4Block alpha_block;
|
||||
BC1Block color_block;
|
||||
};
|
||||
|
||||
struct BC5Block {
|
||||
BC4Block RBlock;
|
||||
BC4Block GBlock;
|
||||
class BC5Block {
|
||||
public:
|
||||
BC4Block r_block;
|
||||
BC4Block g_block;
|
||||
};
|
||||
|
||||
#pragma pack(pop)
|
76
src/color.cpp
Normal file
76
src/color.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
/* Python-rgbcx Texture Compression Library
|
||||
Copyright (C) 2021 Andrew Cassidy <drewcassidy@me.com>
|
||||
Partially derived from rgbcx.h written by Richard Geldreich <richgel99@gmail.com>
|
||||
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 Affero 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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "color.h"
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
// region Color32 implementation
|
||||
Color32::Color32() { set(0, 0, 0, 0xFF); }
|
||||
|
||||
Color32::Color32(uint8_t R, uint8_t G, uint8_t B, uint8_t A) { set(R, G, B, A); }
|
||||
|
||||
uint16_t Color32::pack565Unscaled(uint16_t R, uint16_t G, uint16_t B) { return B | (G << 5) | (R << 11); }
|
||||
|
||||
uint16_t Color32::pack565(uint16_t R, uint16_t G, uint16_t B) { return pack565Unscaled(scale8To5(R), scale8To6(G), scale8To5(B)); }
|
||||
|
||||
Color32 Color32::unpack565(uint16_t Packed) {
|
||||
uint8_t R = scale5To8((Packed >> 11) & 0x1F);
|
||||
uint8_t G = scale6To8((Packed >> 5) & 0x3F);
|
||||
uint8_t B = scale5To8(Packed & 0x1F);
|
||||
|
||||
return Color32(R, G, B);
|
||||
}
|
||||
|
||||
uint8_t Color32::operator[](uint32_t Index) const {
|
||||
assert(Index < 4);
|
||||
return C[Index];
|
||||
}
|
||||
|
||||
uint8_t &Color32::operator[](uint32_t Index) {
|
||||
assert(Index < 4);
|
||||
return C[Index];
|
||||
}
|
||||
|
||||
void Color32::set(uint8_t R, uint8_t G, uint8_t B, uint8_t A) {
|
||||
this->R = R;
|
||||
this->G = G;
|
||||
this->B = B;
|
||||
this->A = A;
|
||||
}
|
||||
|
||||
void Color32::set(const Color32 &Other) {
|
||||
this->R = Other.R;
|
||||
this->G = Other.G;
|
||||
this->B = Other.B;
|
||||
this->A = Other.A;
|
||||
}
|
||||
|
||||
Color32 Color32::min(const Color32 &a, const Color32 &b) {
|
||||
return Color32(std::min(a[0], b[0]), std::min(a[1], b[1]), std::min(a[2], b[2]), std::min(a[3], b[3]));
|
||||
}
|
||||
|
||||
Color32 Color32::max(const Color32 &a, const Color32 &b) {
|
||||
return Color32(std::max(a[0], b[0]), std::max(a[1], b[1]), std::max(a[2], b[2]), std::max(a[3], b[3]));
|
||||
}
|
||||
|
||||
uint16_t Color32::pack565() { return pack565(R, G, B); }
|
||||
|
||||
uint16_t Color32::pack565Unscaled() { return pack565Unscaled(R, G, B); }
|
||||
// endregion
|
64
src/color.h
Normal file
64
src/color.h
Normal file
@ -0,0 +1,64 @@
|
||||
/* Python-rgbcx Texture Compression Library
|
||||
Copyright (C) 2021 Andrew Cassidy <drewcassidy@me.com>
|
||||
Partially derived from rgbcx.h written by Richard Geldreich <richgel99@gmail.com>
|
||||
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 Affero 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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "util.h"
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
#pragma pack(push, 1)
|
||||
class Color32 {
|
||||
public:
|
||||
union {
|
||||
struct {
|
||||
uint8_t R;
|
||||
uint8_t G;
|
||||
uint8_t B;
|
||||
uint8_t A;
|
||||
};
|
||||
|
||||
uint8_t C[4];
|
||||
};
|
||||
|
||||
Color32();
|
||||
|
||||
Color32(uint8_t R, uint8_t G, uint8_t B, uint8_t A = 0xFF);
|
||||
|
||||
static uint16_t pack565Unscaled(uint16_t R, uint16_t G, uint16_t B);
|
||||
static uint16_t pack565(uint16_t R, uint16_t G, uint16_t B);
|
||||
|
||||
static Color32 unpack565(uint16_t Packed);
|
||||
|
||||
bool operator==(const Color32 &Rhs) const { return R == Rhs.R && G == Rhs.G && B == Rhs.B && A == Rhs.A; }
|
||||
|
||||
uint8_t operator[](uint32_t Index) const;
|
||||
uint8_t &operator[](uint32_t Index);
|
||||
|
||||
uint16_t pack565();
|
||||
uint16_t pack565Unscaled();
|
||||
|
||||
static Color32 min(const Color32 &A, const Color32 &B);
|
||||
static Color32 max(const Color32 &A, const Color32 &B);
|
||||
|
||||
void set(uint8_t R, uint8_t G, uint8_t B, uint8_t A);
|
||||
|
||||
void set(const Color32 &Other);
|
||||
};
|
||||
#pragma pack(pop)
|
333
src/rgbcx.cpp
333
src/rgbcx.cpp
@ -1,16 +1,19 @@
|
||||
// rgbcx.h v1.12
|
||||
// High-performance scalar BC1-5 encoders. Public Domain or MIT license (you choose - see below), written by Richard Geldreich 2020 <richgel99@gmail.com>.
|
||||
|
||||
#include "rgbcx.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include "util.h"
|
||||
#include "tables.h"
|
||||
|
||||
#include "blocks.h"
|
||||
#include "rgbcx.h"
|
||||
#include "color.h"
|
||||
#include "tables.h"
|
||||
#include "util.h"
|
||||
|
||||
namespace rgbcx {
|
||||
|
||||
@ -40,14 +43,10 @@ struct hist4 {
|
||||
}
|
||||
|
||||
inline bool operator==(const hist4 &h) const {
|
||||
if (m_hist[0] != h.m_hist[0])
|
||||
return false;
|
||||
if (m_hist[1] != h.m_hist[1])
|
||||
return false;
|
||||
if (m_hist[2] != h.m_hist[2])
|
||||
return false;
|
||||
if (m_hist[3] != h.m_hist[3])
|
||||
return false;
|
||||
if (m_hist[0] != h.m_hist[0]) return false;
|
||||
if (m_hist[1] != h.m_hist[1]) return false;
|
||||
if (m_hist[2] != h.m_hist[2]) return false;
|
||||
if (m_hist[3] != h.m_hist[3]) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -80,12 +79,9 @@ struct hist3 {
|
||||
}
|
||||
|
||||
inline bool operator==(const hist3 &h) const {
|
||||
if (m_hist[0] != h.m_hist[0])
|
||||
return false;
|
||||
if (m_hist[1] != h.m_hist[1])
|
||||
return false;
|
||||
if (m_hist[2] != h.m_hist[2])
|
||||
return false;
|
||||
if (m_hist[0] != h.m_hist[0]) return false;
|
||||
if (m_hist[1] != h.m_hist[1]) return false;
|
||||
if (m_hist[2] != h.m_hist[2]) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -159,7 +155,7 @@ static inline int interp_half_5_6_amd(int c0, int c1) {
|
||||
}
|
||||
|
||||
static inline int interp_5(int v0, int v1, int c0, int c1, bc1_approx_mode mode) {
|
||||
// assert(scale_5_to_8(v0) == c0 && scale_5_to_8(v1) == c1);
|
||||
// assert(scale_5_to_8(v0) == c0 && scale5To8(v1) == c1);
|
||||
switch (mode) {
|
||||
case bc1_approx_mode::cBC1NVidia:
|
||||
return interp_5_nv(v0, v1);
|
||||
@ -176,7 +172,7 @@ static inline int interp_5(int v0, int v1, int c0, int c1, bc1_approx_mode mode)
|
||||
static inline int interp_6(int v0, int v1, int c0, int c1, bc1_approx_mode mode) {
|
||||
(void)v0;
|
||||
(void)v1;
|
||||
// assert(scale_6_to_8(v0) == c0 && scale_6_to_8(v1) == c1);
|
||||
// assert(scale_6_to_8(v0) == c0 && scale6To8(v1) == c1);
|
||||
switch (mode) {
|
||||
case bc1_approx_mode::cBC1NVidia:
|
||||
return interp_6_nv(c0, c1);
|
||||
@ -191,7 +187,7 @@ static inline int interp_6(int v0, int v1, int c0, int c1, bc1_approx_mode mode)
|
||||
}
|
||||
|
||||
static inline int interp_half_5(int v0, int v1, int c0, int c1, bc1_approx_mode mode) {
|
||||
assert(scale_5_to_8(v0) == c0 && scale_5_to_8(v1) == c1);
|
||||
assert(scale5To8(v0) == c0 && scale5To8(v1) == c1);
|
||||
switch (mode) {
|
||||
case bc1_approx_mode::cBC1NVidia:
|
||||
return interp_half_5_nv(v0, v1);
|
||||
@ -207,7 +203,7 @@ static inline int interp_half_5(int v0, int v1, int c0, int c1, bc1_approx_mode
|
||||
static inline int interp_half_6(int v0, int v1, int c0, int c1, bc1_approx_mode mode) {
|
||||
(void)v0;
|
||||
(void)v1;
|
||||
assert(scale_6_to_8(v0) == c0 && scale_6_to_8(v1) == c1);
|
||||
assert(scale6To8(v0) == c0 && scale6To8(v1) == c1);
|
||||
switch (mode) {
|
||||
case bc1_approx_mode::cBC1NVidia:
|
||||
return interp_half_6_nv(c0, c1);
|
||||
@ -234,8 +230,7 @@ static void prepare_bc1_single_color_table_half(bc1_match_entry *pTable, const u
|
||||
int e = iabs(v - i);
|
||||
|
||||
// We only need to factor in 3% error in BC1 ideal mode.
|
||||
if ((mode == bc1_approx_mode::cBC1Ideal) || (mode == bc1_approx_mode::cBC1IdealRound4))
|
||||
e += (iabs(hi_e - lo_e) * 3) / 100;
|
||||
if ((mode == bc1_approx_mode::cBC1Ideal) || (mode == bc1_approx_mode::cBC1IdealRound4)) e += (iabs(hi_e - lo_e) * 3) / 100;
|
||||
|
||||
// Favor equal endpoints, for lower error on actual GPU's which approximate the interpolation.
|
||||
if ((e < lowest_e) || ((e == lowest_e) && (lo == hi))) {
|
||||
@ -266,8 +261,7 @@ static void prepare_bc1_single_color_table(bc1_match_entry *pTable, const uint8_
|
||||
|
||||
int e = iabs(v - i);
|
||||
|
||||
if ((mode == bc1_approx_mode::cBC1Ideal) || (mode == bc1_approx_mode::cBC1IdealRound4))
|
||||
e += (iabs(hi_e - lo_e) * 3) / 100;
|
||||
if ((mode == bc1_approx_mode::cBC1Ideal) || (mode == bc1_approx_mode::cBC1IdealRound4)) e += (iabs(hi_e - lo_e) * 3) / 100;
|
||||
|
||||
// Favor equal endpoints, for lower error on actual GPU's which approximate the interpolation.
|
||||
if ((e < lowest_e) || ((e == lowest_e) && (lo == hi))) {
|
||||
@ -294,8 +288,7 @@ static const uint32_t g_weight_vals3[3] = {0x000004, 0x040000, 0x010101};
|
||||
|
||||
static inline void compute_selector_factors4(const hist4 &h, float &iz00, float &iz10, float &iz11) {
|
||||
uint32_t weight_accum = 0;
|
||||
for (uint32_t sel = 0; sel < 4; sel++)
|
||||
weight_accum += g_weight_vals4[sel] * h.m_hist[sel];
|
||||
for (uint32_t sel = 0; sel < 4; sel++) weight_accum += g_weight_vals4[sel] * h.m_hist[sel];
|
||||
|
||||
float z00 = (float)((weight_accum >> 16) & 0xFF);
|
||||
float z10 = (float)((weight_accum >> 8) & 0xFF);
|
||||
@ -315,8 +308,7 @@ static inline void compute_selector_factors4(const hist4 &h, float &iz00, float
|
||||
|
||||
static inline void compute_selector_factors3(const hist3 &h, float &iz00, float &iz10, float &iz11) {
|
||||
uint32_t weight_accum = 0;
|
||||
for (uint32_t sel = 0; sel < 3; sel++)
|
||||
weight_accum += g_weight_vals3[sel] * h.m_hist[sel];
|
||||
for (uint32_t sel = 0; sel < 3; sel++) weight_accum += g_weight_vals3[sel] * h.m_hist[sel];
|
||||
|
||||
float z00 = (float)((weight_accum >> 16) & 0xFF);
|
||||
float z10 = (float)((weight_accum >> 8) & 0xFF);
|
||||
@ -340,14 +332,12 @@ void init(bc1_approx_mode mode) {
|
||||
g_bc1_approx_mode = mode;
|
||||
|
||||
uint8_t bc1_expand5[32];
|
||||
for (int i = 0; i < 32; i++)
|
||||
bc1_expand5[i] = static_cast<uint8_t>((i << 3) | (i >> 2));
|
||||
for (int i = 0; i < 32; i++) bc1_expand5[i] = static_cast<uint8_t>((i << 3) | (i >> 2));
|
||||
prepare_bc1_single_color_table(g_bc1_match5_equals_1, bc1_expand5, 32, mode);
|
||||
prepare_bc1_single_color_table_half(g_bc1_match5_half, bc1_expand5, 32, mode);
|
||||
|
||||
uint8_t bc1_expand6[64];
|
||||
for (int i = 0; i < 64; i++)
|
||||
bc1_expand6[i] = static_cast<uint8_t>((i << 2) | (i >> 4));
|
||||
for (int i = 0; i < 64; i++) bc1_expand6[i] = static_cast<uint8_t>((i << 2) | (i >> 4));
|
||||
prepare_bc1_single_color_table(g_bc1_match6_equals_1, bc1_expand6, 64, mode);
|
||||
prepare_bc1_single_color_table_half(g_bc1_match6_half, bc1_expand6, 64, mode);
|
||||
|
||||
@ -399,8 +389,7 @@ void encode_bc1_solid_block(void *pDst, uint32_t fr, uint32_t fg, uint32_t fb, b
|
||||
max16 = (g_bc1_match5_half[fr].m_hi << 11) | (g_bc1_match6_half[fg].m_hi << 5) | g_bc1_match5_half[fb].m_hi;
|
||||
min16 = (g_bc1_match5_half[fr].m_lo << 11) | (g_bc1_match6_half[fg].m_lo << 5) | g_bc1_match5_half[fb].m_lo;
|
||||
|
||||
if (max16 > min16)
|
||||
std::swap(max16, min16);
|
||||
if (max16 > min16) std::swap(max16, min16);
|
||||
}
|
||||
}
|
||||
|
||||
@ -434,12 +423,12 @@ void encode_bc1_solid_block(void *pDst, uint32_t fr, uint32_t fg, uint32_t fb, b
|
||||
}
|
||||
}
|
||||
|
||||
pDst_block->set_low_color(static_cast<uint16_t>(max16));
|
||||
pDst_block->set_high_color(static_cast<uint16_t>(min16));
|
||||
pDst_block->Selectors[0] = static_cast<uint8_t>(mask);
|
||||
pDst_block->Selectors[1] = static_cast<uint8_t>(mask);
|
||||
pDst_block->Selectors[2] = static_cast<uint8_t>(mask);
|
||||
pDst_block->Selectors[3] = static_cast<uint8_t>(mask);
|
||||
pDst_block->SetLowColor(static_cast<uint16_t>(max16));
|
||||
pDst_block->SetHighColor(static_cast<uint16_t>(min16));
|
||||
pDst_block->selectors[0] = static_cast<uint8_t>(mask);
|
||||
pDst_block->selectors[1] = static_cast<uint8_t>(mask);
|
||||
pDst_block->selectors[2] = static_cast<uint8_t>(mask);
|
||||
pDst_block->selectors[3] = static_cast<uint8_t>(mask);
|
||||
}
|
||||
|
||||
static const float g_midpoint5[32] = {.015686f, .047059f, .078431f, .111765f, .145098f, .176471f, .207843f, .241176f, .274510f, .305882f, .337255f,
|
||||
@ -505,8 +494,7 @@ static inline bool compute_least_squares_endpoints4_rgb(const Color32 *pColors,
|
||||
float z01 = z10;
|
||||
|
||||
float det = z00 * z11 - z01 * z10;
|
||||
if (fabs(det) < 1e-8f)
|
||||
return false;
|
||||
if (fabs(det) < 1e-8f) return false;
|
||||
|
||||
det = (3.0f / 255.0f) / det;
|
||||
|
||||
@ -560,14 +548,12 @@ static inline bool compute_least_squares_endpoints3_rgb(bool use_black, const Co
|
||||
for (uint32_t i = 0; i < 16; i++) {
|
||||
const uint8_t r = pColors[i].C[0], g = pColors[i].C[1], b = pColors[i].C[2];
|
||||
if (use_black) {
|
||||
if ((r | g | b) < 4)
|
||||
continue;
|
||||
if ((r | g | b) < 4) continue;
|
||||
}
|
||||
|
||||
const uint8_t sel = pSelectors[i];
|
||||
assert(sel <= 3);
|
||||
if (sel == 3)
|
||||
continue;
|
||||
if (sel == 3) continue;
|
||||
|
||||
weight_accum += g_weight_vals3[sel];
|
||||
|
||||
@ -592,8 +578,7 @@ static inline bool compute_least_squares_endpoints3_rgb(bool use_black, const Co
|
||||
float z01 = z10;
|
||||
|
||||
float det = z00 * z11 - z01 * z10;
|
||||
if (fabs(det) < 1e-8f)
|
||||
return false;
|
||||
if (fabs(det) < 1e-8f) return false;
|
||||
|
||||
det = (2.0f / 255.0f) / det;
|
||||
|
||||
@ -687,8 +672,7 @@ static inline void bc1_find_sels4_noerr(const Color32 *pSrc_pixels, uint32_t lr,
|
||||
int ar = block_r[3] - block_r[0], ag = block_g[3] - block_g[0], ab = block_b[3] - block_b[0];
|
||||
|
||||
int dots[4];
|
||||
for (uint32_t i = 0; i < 4; i++)
|
||||
dots[i] = (int)block_r[i] * ar + (int)block_g[i] * ag + (int)block_b[i] * ab;
|
||||
for (uint32_t i = 0; i < 4; i++) dots[i] = (int)block_r[i] * ar + (int)block_g[i] * ag + (int)block_b[i] * ab;
|
||||
|
||||
int t0 = dots[0] + dots[1], t1 = dots[1] + dots[2], t2 = dots[2] + dots[3];
|
||||
|
||||
@ -719,8 +703,7 @@ static inline uint32_t bc1_find_sels4_fasterr(const Color32 *pSrc_pixels, uint32
|
||||
int ar = block_r[3] - block_r[0], ag = block_g[3] - block_g[0], ab = block_b[3] - block_b[0];
|
||||
|
||||
int dots[4];
|
||||
for (uint32_t i = 0; i < 4; i++)
|
||||
dots[i] = (int)block_r[i] * ar + (int)block_g[i] * ag + (int)block_b[i] * ab;
|
||||
for (uint32_t i = 0; i < 4; i++) dots[i] = (int)block_r[i] * ar + (int)block_g[i] * ag + (int)block_b[i] * ab;
|
||||
|
||||
int t0 = dots[0] + dots[1], t1 = dots[1] + dots[2], t2 = dots[2] + dots[3];
|
||||
|
||||
@ -757,8 +740,7 @@ static inline uint32_t bc1_find_sels4_fasterr(const Color32 *pSrc_pixels, uint32
|
||||
total_err +=
|
||||
squarei(pSrc_pixels[i + 3].R - block_r[sel3]) + squarei(pSrc_pixels[i + 3].G - block_g[sel3]) + squarei(pSrc_pixels[i + 3].B - block_b[sel3]);
|
||||
|
||||
if (total_err >= cur_err)
|
||||
break;
|
||||
if (total_err >= cur_err) break;
|
||||
}
|
||||
|
||||
return total_err;
|
||||
@ -790,8 +772,7 @@ static inline uint32_t bc1_find_sels4_check2_err(const Color32 *pSrc_pixels, uin
|
||||
uint32_t best_err = err1;
|
||||
if (err0 == err1) {
|
||||
// Prefer non-interpolation
|
||||
if ((best_sel - 1) == 0)
|
||||
best_sel = 0;
|
||||
if ((best_sel - 1) == 0) best_sel = 0;
|
||||
} else if (err0 < best_err) {
|
||||
best_sel = sel - 1;
|
||||
best_err = err0;
|
||||
@ -799,8 +780,7 @@ static inline uint32_t bc1_find_sels4_check2_err(const Color32 *pSrc_pixels, uin
|
||||
|
||||
total_err += best_err;
|
||||
|
||||
if (total_err >= cur_err)
|
||||
break;
|
||||
if (total_err >= cur_err) break;
|
||||
|
||||
sels[i] = (uint8_t)best_sel;
|
||||
}
|
||||
@ -832,8 +812,7 @@ static inline uint32_t bc1_find_sels4_fullerr(const Color32 *pSrc_pixels, uint32
|
||||
|
||||
total_err += best_err;
|
||||
|
||||
if (total_err >= cur_err)
|
||||
break;
|
||||
if (total_err >= cur_err) break;
|
||||
|
||||
sels[i] = (uint8_t)best_sel;
|
||||
}
|
||||
@ -890,8 +869,7 @@ static inline uint32_t bc1_find_sels3_fullerr(bool use_black, const Color32 *pSr
|
||||
}
|
||||
|
||||
total_err += best_err;
|
||||
if (total_err >= cur_err)
|
||||
return total_err;
|
||||
if (total_err >= cur_err) return total_err;
|
||||
|
||||
sels[i] = (uint8_t)best_sel;
|
||||
}
|
||||
@ -944,8 +922,8 @@ static inline void precise_round_565_noscale(vec3F xl, vec3F xh, int &trial_lr,
|
||||
}
|
||||
|
||||
static inline void bc1_encode4(BC1Block *pDst_block, int lr, int lg, int lb, int hr, int hg, int hb, const uint8_t sels[16]) {
|
||||
uint32_t lc16 = BC1Block::pack_unscaled_color(lr, lg, lb);
|
||||
uint32_t hc16 = BC1Block::pack_unscaled_color(hr, hg, hb);
|
||||
uint16_t lc16 = Color32::pack565Unscaled(lr, lg, lb);
|
||||
uint16_t hc16 = Color32::pack565Unscaled(hr, hg, hb);
|
||||
|
||||
// Always forbid 3 color blocks
|
||||
if (lc16 == hc16) {
|
||||
@ -964,13 +942,13 @@ static inline void bc1_encode4(BC1Block *pDst_block, int lr, int lg, int lb, int
|
||||
}
|
||||
|
||||
assert(lc16 > hc16);
|
||||
pDst_block->set_low_color(static_cast<uint16_t>(lc16));
|
||||
pDst_block->set_high_color(static_cast<uint16_t>(hc16));
|
||||
pDst_block->SetLowColor(static_cast<uint16_t>(lc16));
|
||||
pDst_block->SetHighColor(static_cast<uint16_t>(hc16));
|
||||
|
||||
pDst_block->Selectors[0] = mask;
|
||||
pDst_block->Selectors[1] = mask;
|
||||
pDst_block->Selectors[2] = mask;
|
||||
pDst_block->Selectors[3] = mask;
|
||||
pDst_block->selectors[0] = mask;
|
||||
pDst_block->selectors[1] = mask;
|
||||
pDst_block->selectors[2] = mask;
|
||||
pDst_block->selectors[3] = mask;
|
||||
} else {
|
||||
uint8_t invert_mask = 0;
|
||||
if (lc16 < hc16) {
|
||||
@ -979,24 +957,24 @@ static inline void bc1_encode4(BC1Block *pDst_block, int lr, int lg, int lb, int
|
||||
}
|
||||
|
||||
assert(lc16 > hc16);
|
||||
pDst_block->set_low_color((uint16_t)lc16);
|
||||
pDst_block->set_high_color((uint16_t)hc16);
|
||||
pDst_block->SetLowColor((uint16_t)lc16);
|
||||
pDst_block->SetHighColor((uint16_t)hc16);
|
||||
|
||||
uint32_t packed_sels = 0;
|
||||
static const uint8_t s_sel_trans[4] = {0, 2, 3, 1};
|
||||
for (uint32_t i = 0; i < 16; i++)
|
||||
packed_sels |= ((uint32_t)s_sel_trans[sels[i]] << (i * 2));
|
||||
for (uint32_t i = 0; i < 16; i++) packed_sels |= ((uint32_t)s_sel_trans[sels[i]] << (i * 2));
|
||||
|
||||
pDst_block->Selectors[0] = (uint8_t)packed_sels ^ invert_mask;
|
||||
pDst_block->Selectors[1] = (uint8_t)(packed_sels >> 8) ^ invert_mask;
|
||||
pDst_block->Selectors[2] = (uint8_t)(packed_sels >> 16) ^ invert_mask;
|
||||
pDst_block->Selectors[3] = (uint8_t)(packed_sels >> 24) ^ invert_mask;
|
||||
// todo: make this less silly to prevent packing and unpacking
|
||||
pDst_block->selectors[0] = (uint8_t)packed_sels ^ invert_mask;
|
||||
pDst_block->selectors[1] = (uint8_t)(packed_sels >> 8) ^ invert_mask;
|
||||
pDst_block->selectors[2] = (uint8_t)(packed_sels >> 16) ^ invert_mask;
|
||||
pDst_block->selectors[3] = (uint8_t)(packed_sels >> 24) ^ invert_mask;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void bc1_encode3(BC1Block *pDst_block, int lr, int lg, int lb, int hr, int hg, int hb, const uint8_t sels[16]) {
|
||||
uint32_t lc16 = BC1Block::pack_unscaled_color(lr, lg, lb);
|
||||
uint32_t hc16 = BC1Block::pack_unscaled_color(hr, hg, hb);
|
||||
uint16_t lc16 = Color32::pack565Unscaled(lr, lg, lb);
|
||||
uint16_t hc16 = Color32::pack565Unscaled(hr, hg, hb);
|
||||
|
||||
bool invert_flag = false;
|
||||
if (lc16 > hc16) {
|
||||
@ -1006,25 +984,24 @@ static inline void bc1_encode3(BC1Block *pDst_block, int lr, int lg, int lb, int
|
||||
|
||||
assert(lc16 <= hc16);
|
||||
|
||||
pDst_block->set_low_color((uint16_t)lc16);
|
||||
pDst_block->set_high_color((uint16_t)hc16);
|
||||
pDst_block->SetLowColor((uint16_t)lc16);
|
||||
pDst_block->SetHighColor((uint16_t)hc16);
|
||||
|
||||
uint32_t packed_sels = 0;
|
||||
|
||||
if (invert_flag) {
|
||||
static const uint8_t s_sel_trans_inv[4] = {1, 0, 2, 3};
|
||||
|
||||
for (uint32_t i = 0; i < 16; i++)
|
||||
packed_sels |= ((uint32_t)s_sel_trans_inv[sels[i]] << (i * 2));
|
||||
for (uint32_t i = 0; i < 16; i++) packed_sels |= ((uint32_t)s_sel_trans_inv[sels[i]] << (i * 2));
|
||||
} else {
|
||||
for (uint32_t i = 0; i < 16; i++)
|
||||
packed_sels |= ((uint32_t)sels[i] << (i * 2));
|
||||
for (uint32_t i = 0; i < 16; i++) packed_sels |= ((uint32_t)sels[i] << (i * 2));
|
||||
}
|
||||
|
||||
pDst_block->Selectors[0] = (uint8_t)packed_sels;
|
||||
pDst_block->Selectors[1] = (uint8_t)(packed_sels >> 8);
|
||||
pDst_block->Selectors[2] = (uint8_t)(packed_sels >> 16);
|
||||
pDst_block->Selectors[3] = (uint8_t)(packed_sels >> 24);
|
||||
// todo: make this less silly to prevent packing and unpacking
|
||||
pDst_block->selectors[0] = (uint8_t)packed_sels;
|
||||
pDst_block->selectors[1] = (uint8_t)(packed_sels >> 8);
|
||||
pDst_block->selectors[2] = (uint8_t)(packed_sels >> 16);
|
||||
pDst_block->selectors[3] = (uint8_t)(packed_sels >> 24);
|
||||
}
|
||||
|
||||
struct bc1_encode_results {
|
||||
@ -1041,8 +1018,7 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
|
||||
int total_pixels = 0;
|
||||
for (uint32_t i = 0; i < 16; i++) {
|
||||
const int r = pSrc_pixels[i].R, g = pSrc_pixels[i].G, b = pSrc_pixels[i].B;
|
||||
if ((r | g | b) < 4)
|
||||
continue;
|
||||
if ((r | g | b) < 4) continue;
|
||||
|
||||
max_r = std::max(max_r, r);
|
||||
max_g = std::max(max_g, g);
|
||||
@ -1057,8 +1033,7 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
|
||||
total_pixels++;
|
||||
}
|
||||
|
||||
if (!total_pixels)
|
||||
return false;
|
||||
if (!total_pixels) return false;
|
||||
|
||||
int half_total_pixels = total_pixels >> 1;
|
||||
int avg_r = (total_r + half_total_pixels) / total_pixels;
|
||||
@ -1073,8 +1048,7 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
|
||||
int g = (int)pSrc_pixels[i].G;
|
||||
int b = (int)pSrc_pixels[i].B;
|
||||
|
||||
if ((r | g | b) < 4)
|
||||
continue;
|
||||
if ((r | g | b) < 4) continue;
|
||||
|
||||
r -= avg_r;
|
||||
g -= avg_g;
|
||||
@ -1089,18 +1063,15 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
|
||||
}
|
||||
|
||||
float cov[6];
|
||||
for (uint32_t i = 0; i < 6; i++)
|
||||
cov[i] = (float)(icov[i]) * (1.0f / 255.0f);
|
||||
for (uint32_t i = 0; i < 6; i++) cov[i] = (float)(icov[i]) * (1.0f / 255.0f);
|
||||
|
||||
float xr = (float)(max_r - min_r);
|
||||
float xg = (float)(max_g - min_g);
|
||||
float xb = (float)(max_b - min_b);
|
||||
|
||||
if (icov[2] < 0)
|
||||
xr = -xr;
|
||||
if (icov[2] < 0) xr = -xr;
|
||||
|
||||
if (icov[4] < 0)
|
||||
xg = -xg;
|
||||
if (icov[4] < 0) xg = -xg;
|
||||
|
||||
for (uint32_t power_iter = 0; power_iter < 4; power_iter++) {
|
||||
float r = xr * cov[0] + xg * cov[1] + xb * cov[2];
|
||||
@ -1124,8 +1095,7 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
|
||||
for (uint32_t i = 0; i < 16; i++) {
|
||||
int r = (int)pSrc_pixels[i].R, g = (int)pSrc_pixels[i].G, b = (int)pSrc_pixels[i].B;
|
||||
|
||||
if ((r | g | b) < 4)
|
||||
continue;
|
||||
if ((r | g | b) < 4) continue;
|
||||
|
||||
int dot = r * saxis_r + g * saxis_g + b * saxis_b;
|
||||
if (dot < low_dot) {
|
||||
@ -1138,13 +1108,13 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
|
||||
}
|
||||
}
|
||||
|
||||
int lr = scale_8_to_5(pSrc_pixels[low_c].R);
|
||||
int lg = scale_8_to_6(pSrc_pixels[low_c].G);
|
||||
int lb = scale_8_to_5(pSrc_pixels[low_c].B);
|
||||
int lr = scale8To5(pSrc_pixels[low_c].R);
|
||||
int lg = scale8To6(pSrc_pixels[low_c].G);
|
||||
int lb = scale8To5(pSrc_pixels[low_c].B);
|
||||
|
||||
int hr = scale_8_to_5(pSrc_pixels[high_c].R);
|
||||
int hg = scale_8_to_6(pSrc_pixels[high_c].G);
|
||||
int hb = scale_8_to_5(pSrc_pixels[high_c].B);
|
||||
int hr = scale8To5(pSrc_pixels[high_c].R);
|
||||
int hg = scale8To6(pSrc_pixels[high_c].G);
|
||||
int hb = scale8To5(pSrc_pixels[high_c].B);
|
||||
|
||||
uint8_t trial_sels[16];
|
||||
uint32_t trial_err = bc1_find_sels3_fullerr(true, pSrc_pixels, lr, lg, lb, hr, hg, hb, trial_sels, UINT32_MAX);
|
||||
@ -1166,8 +1136,7 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
|
||||
precise_round_565(xl, xh, hr2, hg2, hb2, lr2, lg2, lb2);
|
||||
}
|
||||
|
||||
if ((lr == lr2) && (lg == lg2) && (lb == lb2) && (hr == hr2) && (hg == hg2) && (hb == hb2))
|
||||
break;
|
||||
if ((lr == lr2) && (lg == lg2) && (lb == lb2) && (hr == hr2) && (hg == hg2) && (hb == hb2)) break;
|
||||
|
||||
uint8_t trial_sels2[16];
|
||||
uint32_t trial_err2 = bc1_find_sels3_fullerr(true, pSrc_pixels, lr2, lg2, lb2, hr2, hg2, hb2, trial_sels2, trial_err);
|
||||
@ -1226,8 +1195,7 @@ static bool try_3color_block(const Color32 *pSrc_pixels, uint32_t flags, uint32_
|
||||
precise_round_565(xl, xh, hr2, hg2, hb2, lr2, lg2, lb2);
|
||||
}
|
||||
|
||||
if ((lr == lr2) && (lg == lg2) && (lb == lb2) && (hr == hr2) && (hg == hg2) && (hb == hb2))
|
||||
break;
|
||||
if ((lr == lr2) && (lg == lg2) && (lb == lb2) && (hr == hr2) && (hg == hg2) && (hb == hb2)) break;
|
||||
|
||||
uint8_t trial_sels2[16];
|
||||
uint32_t trial_err2 = bc1_find_sels3_fullerr(false, pSrc_pixels, lr2, lg2, lb2, hr2, hg2, hb2, trial_sels2, trial_err);
|
||||
@ -1491,23 +1459,21 @@ static inline void encode_bc1_pick_initial(const Color32 *pSrc_pixels, uint32_t
|
||||
|
||||
// Grayscale blocks are a common enough case to specialize.
|
||||
if ((max_r - min_r) < 2) {
|
||||
lr = lb = hr = hb = scale_8_to_5(fr);
|
||||
lg = hg = scale_8_to_6(fr);
|
||||
lr = lb = hr = hb = scale8To5(fr);
|
||||
lg = hg = scale8To6(fr);
|
||||
} else {
|
||||
lr = lb = scale_8_to_5(min_r);
|
||||
lg = scale_8_to_6(min_r);
|
||||
lr = lb = scale8To5(min_r);
|
||||
lg = scale8To6(min_r);
|
||||
|
||||
hr = hb = scale_8_to_5(max_r);
|
||||
hg = scale_8_to_6(max_r);
|
||||
hr = hb = scale8To5(max_r);
|
||||
hg = scale8To6(max_r);
|
||||
}
|
||||
} else if (flags & cEncodeBC1Use2DLS) {
|
||||
// 2D Least Squares approach from Humus's example, with added inset and optimal rounding.
|
||||
int big_chan = 0, min_chan_val = min_r, max_chan_val = max_r;
|
||||
if ((max_g - min_g) > (max_chan_val - min_chan_val))
|
||||
big_chan = 1, min_chan_val = min_g, max_chan_val = max_g;
|
||||
if ((max_g - min_g) > (max_chan_val - min_chan_val)) big_chan = 1, min_chan_val = min_g, max_chan_val = max_g;
|
||||
|
||||
if ((max_b - min_b) > (max_chan_val - min_chan_val))
|
||||
big_chan = 2, min_chan_val = min_b, max_chan_val = max_b;
|
||||
if ((max_b - min_b) > (max_chan_val - min_chan_val)) big_chan = 2, min_chan_val = min_b, max_chan_val = max_b;
|
||||
|
||||
int sum_xy_r = 0, sum_xy_g = 0, sum_xy_b = 0;
|
||||
vec3F l, h;
|
||||
@ -1670,11 +1636,9 @@ static inline void encode_bc1_pick_initial(const Color32 *pSrc_pixels, uint32_t
|
||||
icov_yz += g * b;
|
||||
}
|
||||
|
||||
if (icov_xz < 0)
|
||||
std::swap(l.c[0], h.c[0]);
|
||||
if (icov_xz < 0) std::swap(l.c[0], h.c[0]);
|
||||
|
||||
if (icov_yz < 0)
|
||||
std::swap(l.c[1], h.c[1]);
|
||||
if (icov_yz < 0) std::swap(l.c[1], h.c[1]);
|
||||
|
||||
precise_round_565(l, h, lr, lg, lb, hr, hg, hb);
|
||||
} else if (flags & cEncodeBC1BoundingBoxInt) {
|
||||
@ -1715,19 +1679,17 @@ static inline void encode_bc1_pick_initial(const Color32 *pSrc_pixels, uint32_t
|
||||
int x1 = max_r;
|
||||
int y1 = max_g;
|
||||
|
||||
if (icov_xz < 0)
|
||||
std::swap(x0, x1);
|
||||
if (icov_xz < 0) std::swap(x0, x1);
|
||||
|
||||
if (icov_yz < 0)
|
||||
std::swap(y0, y1);
|
||||
if (icov_yz < 0) std::swap(y0, y1);
|
||||
|
||||
lr = scale_8_to_5(x0);
|
||||
lg = scale_8_to_6(y0);
|
||||
lb = scale_8_to_5(min_b);
|
||||
lr = scale8To5(x0);
|
||||
lg = scale8To6(y0);
|
||||
lb = scale8To5(min_b);
|
||||
|
||||
hr = scale_8_to_5(x1);
|
||||
hg = scale_8_to_6(y1);
|
||||
hb = scale_8_to_5(max_b);
|
||||
hr = scale8To5(x1);
|
||||
hg = scale8To6(y1);
|
||||
hb = scale8To5(max_b);
|
||||
} else {
|
||||
// Select 2 colors along the principle axis. (There must be a faster/simpler way.)
|
||||
uint32_t low_c = 0, high_c = 0;
|
||||
@ -1751,15 +1713,12 @@ static inline void encode_bc1_pick_initial(const Color32 *pSrc_pixels, uint32_t
|
||||
float xg = (float)(max_g - min_g);
|
||||
float xb = (float)(max_b - min_b);
|
||||
|
||||
if (icov[2] < 0)
|
||||
xr = -xr;
|
||||
if (icov[2] < 0) xr = -xr;
|
||||
|
||||
if (icov[4] < 0)
|
||||
xg = -xg;
|
||||
if (icov[4] < 0) xg = -xg;
|
||||
|
||||
float cov[6];
|
||||
for (uint32_t i = 0; i < 6; i++)
|
||||
cov[i] = (float)(icov[i]) * (1.0f / 255.0f);
|
||||
for (uint32_t i = 0; i < 6; i++) cov[i] = (float)(icov[i]) * (1.0f / 255.0f);
|
||||
|
||||
const uint32_t total_power_iters = (flags & cEncodeBC1Use6PowerIters) ? 6 : 4;
|
||||
for (uint32_t power_iter = 0; power_iter < total_power_iters; power_iter++) {
|
||||
@ -1806,13 +1765,13 @@ static inline void encode_bc1_pick_initial(const Color32 *pSrc_pixels, uint32_t
|
||||
low_c = low_dot & 15;
|
||||
high_c = high_dot & 15;
|
||||
|
||||
lr = scale_8_to_5(pSrc_pixels[low_c].R);
|
||||
lg = scale_8_to_6(pSrc_pixels[low_c].G);
|
||||
lb = scale_8_to_5(pSrc_pixels[low_c].B);
|
||||
lr = scale8To5(pSrc_pixels[low_c].R);
|
||||
lg = scale8To6(pSrc_pixels[low_c].G);
|
||||
lb = scale8To5(pSrc_pixels[low_c].B);
|
||||
|
||||
hr = scale_8_to_5(pSrc_pixels[high_c].R);
|
||||
hg = scale_8_to_6(pSrc_pixels[high_c].G);
|
||||
hb = scale_8_to_5(pSrc_pixels[high_c].B);
|
||||
hr = scale8To5(pSrc_pixels[high_c].R);
|
||||
hg = scale8To6(pSrc_pixels[high_c].G);
|
||||
hb = scale8To5(pSrc_pixels[high_c].B);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1847,8 +1806,7 @@ static inline void encode_bc1_endpoint_search(const Color32 *pSrc_pixels, bool a
|
||||
for (int i = 0; i < endpoint_search_rounds; i++) {
|
||||
assert(s_adjacent_voxels[s_adjacent_voxels[i & 15][3]][3] == (i & 15));
|
||||
|
||||
if (forbidden_direction == (i & 31))
|
||||
continue;
|
||||
if (forbidden_direction == (i & 31)) continue;
|
||||
|
||||
const int8_t delta[3] = {s_adjacent_voxels[i & 15][0], s_adjacent_voxels[i & 15][1], s_adjacent_voxels[i & 15][2]};
|
||||
|
||||
@ -1886,8 +1844,7 @@ static inline void encode_bc1_endpoint_search(const Color32 *pSrc_pixels, bool a
|
||||
prev_improvement_index = i;
|
||||
}
|
||||
|
||||
if (i - prev_improvement_index > 32)
|
||||
break;
|
||||
if (i - prev_improvement_index > 32) break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1903,8 +1860,7 @@ void encode_bc1(void *pDst, const uint8_t *pPixels, uint32_t flags, uint32_t tot
|
||||
|
||||
uint32_t j;
|
||||
for (j = 15; j >= 1; --j)
|
||||
if ((pSrc_pixels[j].R != fr) || (pSrc_pixels[j].G != fg) || (pSrc_pixels[j].B != fb))
|
||||
break;
|
||||
if ((pSrc_pixels[j].R != fr) || (pSrc_pixels[j].G != fg) || (pSrc_pixels[j].B != fb)) break;
|
||||
|
||||
if (j == 0) {
|
||||
encode_bc1_solid_block(pDst, fr, fg, fb, (flags & (cEncodeBC1Use3ColorBlocks | cEncodeBC1Use3ColorBlocksForBlackPixels)) != 0);
|
||||
@ -1983,8 +1939,7 @@ void encode_bc1(void *pDst, const uint8_t *pPixels, uint32_t flags, uint32_t tot
|
||||
precise_round_565(xl, xh, trial_hr, trial_hg, trial_hb, trial_lr, trial_lg, trial_lb);
|
||||
}
|
||||
|
||||
if ((lr == trial_lr) && (lg == trial_lg) && (lb == trial_lb) && (hr == trial_hr) && (hg == trial_hg) && (hb == trial_hb))
|
||||
break;
|
||||
if ((lr == trial_lr) && (lg == trial_lg) && (lb == trial_lb) && (hr == trial_hr) && (hg == trial_hg) && (hb == trial_hb)) break;
|
||||
|
||||
bc1_find_sels4_noerr(pSrc_pixels, trial_lr, trial_lg, trial_lb, trial_hr, trial_hg, trial_hb, sels);
|
||||
|
||||
@ -2181,8 +2136,7 @@ void encode_bc1(void *pDst, const uint8_t *pPixels, uint32_t flags, uint32_t tot
|
||||
|
||||
} // s
|
||||
|
||||
if ((!cur_err) || (cur_err == orig_err))
|
||||
break;
|
||||
if ((!cur_err) || (cur_err == orig_err)) break;
|
||||
|
||||
} // iter_index
|
||||
}
|
||||
@ -2349,6 +2303,7 @@ void encode_bc4(void *pDst, const uint8_t *pPixels, uint32_t stride) {
|
||||
|
||||
const uint64_t f = a0 | a1 | a2 | a3;
|
||||
|
||||
// TODO: make this less silly by using the BC4Block class
|
||||
pDst_bytes[2] = (uint8_t)f;
|
||||
pDst_bytes[3] = (uint8_t)(f >> 8U);
|
||||
pDst_bytes[4] = (uint8_t)(f >> 16U);
|
||||
@ -2357,28 +2312,28 @@ void encode_bc4(void *pDst, const uint8_t *pPixels, uint32_t stride) {
|
||||
pDst_bytes[7] = (uint8_t)(f >> 40U);
|
||||
}
|
||||
|
||||
void encode_bc3(void *pDst, const uint8_t *pPixels, uint32_t flags, uint32_t total_orderings_to_try) {
|
||||
void encode_bc3(BC3Block *pDst, const uint8_t *pPixels, uint32_t flags, uint32_t total_orderings_to_try) {
|
||||
assert(g_initialized);
|
||||
|
||||
// 3-color blocks are not allowed with BC3 (on most GPU's).
|
||||
flags &= ~(cEncodeBC1Use3ColorBlocksForBlackPixels | cEncodeBC1Use3ColorBlocks);
|
||||
|
||||
encode_bc4(pDst, pPixels + 3, 4);
|
||||
encode_bc1(static_cast<uint8_t *>(pDst) + 8, pPixels, flags, total_orderings_to_try);
|
||||
encode_bc4(&pDst->alpha_block, pPixels + 3, 4);
|
||||
encode_bc1(&pDst->color_block, pPixels, flags, total_orderings_to_try);
|
||||
}
|
||||
|
||||
void encode_bc3(uint32_t level, void *pDst, const uint8_t *pPixels) {
|
||||
void encode_bc3(uint32_t level, BC3Block *pDst, const uint8_t *pPixels) {
|
||||
assert(g_initialized);
|
||||
|
||||
encode_bc4(pDst, pPixels + 3, 4);
|
||||
encode_bc1(level, static_cast<uint8_t *>(pDst) + 8, pPixels, false, false);
|
||||
encode_bc4(&pDst->alpha_block, pPixels + 3, 4);
|
||||
encode_bc1(level, &pDst->color_block, pPixels, false, false);
|
||||
}
|
||||
|
||||
void encode_bc5(void *pDst, const uint8_t *pPixels, uint32_t chan0, uint32_t chan1, uint32_t stride) {
|
||||
void encode_bc5(BC5Block *pDst, const uint8_t *pPixels, uint32_t chan0, uint32_t chan1, uint32_t stride) {
|
||||
assert(g_initialized);
|
||||
|
||||
encode_bc4(pDst, pPixels + chan0, stride);
|
||||
encode_bc4(static_cast<uint8_t *>(pDst) + 8, pPixels + chan1, stride);
|
||||
encode_bc4(&pDst->r_block, pPixels + chan0, stride);
|
||||
encode_bc4(&pDst->g_block, pPixels + chan1, stride);
|
||||
}
|
||||
|
||||
// Returns true if the block uses 3 color punchthrough alpha mode.
|
||||
@ -2390,8 +2345,8 @@ bool unpack_bc1(const void *pBlock_bits, void *pPixels, bool set_alpha, bc1_appr
|
||||
|
||||
const BC1Block *pBlock = static_cast<const BC1Block *>(pBlock_bits);
|
||||
|
||||
const uint32_t l = pBlock->get_low_color();
|
||||
const uint32_t h = pBlock->get_high_color();
|
||||
const uint32_t l = pBlock->GetLowColor();
|
||||
const uint32_t h = pBlock->GetHighColor();
|
||||
|
||||
Color32 c[4];
|
||||
|
||||
@ -2454,17 +2409,17 @@ bool unpack_bc1(const void *pBlock_bits, void *pPixels, bool set_alpha, bc1_appr
|
||||
|
||||
if (set_alpha) {
|
||||
for (uint32_t y = 0; y < 4; y++, pDst_pixels += 4) {
|
||||
pDst_pixels[0] = c[pBlock->get_selector(0, y)];
|
||||
pDst_pixels[1] = c[pBlock->get_selector(1, y)];
|
||||
pDst_pixels[2] = c[pBlock->get_selector(2, y)];
|
||||
pDst_pixels[3] = c[pBlock->get_selector(3, y)];
|
||||
pDst_pixels[0] = c[pBlock->GetSelector(0, y)];
|
||||
pDst_pixels[1] = c[pBlock->GetSelector(1, y)];
|
||||
pDst_pixels[2] = c[pBlock->GetSelector(2, y)];
|
||||
pDst_pixels[3] = c[pBlock->GetSelector(3, y)];
|
||||
}
|
||||
} else {
|
||||
for (uint32_t y = 0; y < 4; y++, pDst_pixels += 4) {
|
||||
pDst_pixels[0].set(c[pBlock->get_selector(0, y)]);
|
||||
pDst_pixels[1].set(c[pBlock->get_selector(1, y)]);
|
||||
pDst_pixels[2].set(c[pBlock->get_selector(2, y)]);
|
||||
pDst_pixels[3].set(c[pBlock->get_selector(3, y)]);
|
||||
pDst_pixels[0].set(c[pBlock->GetSelector(0, y)]);
|
||||
pDst_pixels[1].set(c[pBlock->GetSelector(1, y)]);
|
||||
pDst_pixels[2].set(c[pBlock->GetSelector(2, y)]);
|
||||
pDst_pixels[3].set(c[pBlock->GetSelector(3, y)]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2476,16 +2431,15 @@ void unpack_bc4(const void *pBlock_bits, uint8_t *pPixels, uint32_t stride) {
|
||||
|
||||
const BC4Block *pBlock = static_cast<const BC4Block *>(pBlock_bits);
|
||||
|
||||
uint8_t sel_values[8];
|
||||
BC4Block::get_block_values(sel_values, pBlock->get_low_alpha(), pBlock->get_high_alpha());
|
||||
auto sel_values = BC4Block::GetBlockValues(pBlock->GetLowAlpha(), pBlock->GetHighAlpha());
|
||||
|
||||
const uint64_t selector_bits = pBlock->get_selector_bits();
|
||||
const uint64_t selector_bits = pBlock->GetSelectorBits();
|
||||
|
||||
for (uint32_t y = 0; y < 4; y++, pPixels += (stride * 4U)) {
|
||||
pPixels[0] = sel_values[pBlock->get_selector(0, y, selector_bits)];
|
||||
pPixels[stride * 1] = sel_values[pBlock->get_selector(1, y, selector_bits)];
|
||||
pPixels[stride * 2] = sel_values[pBlock->get_selector(2, y, selector_bits)];
|
||||
pPixels[stride * 3] = sel_values[pBlock->get_selector(3, y, selector_bits)];
|
||||
pPixels[0] = sel_values[pBlock->GetSelector(0, y, selector_bits)];
|
||||
pPixels[stride * 1] = sel_values[pBlock->GetSelector(1, y, selector_bits)];
|
||||
pPixels[stride * 2] = sel_values[pBlock->GetSelector(2, y, selector_bits)];
|
||||
pPixels[stride * 3] = sel_values[pBlock->GetSelector(3, y, selector_bits)];
|
||||
}
|
||||
}
|
||||
|
||||
@ -2495,8 +2449,7 @@ bool unpack_bc3(const void *pBlock_bits, void *pPixels, bc1_approx_mode mode) {
|
||||
|
||||
bool success = true;
|
||||
|
||||
if (unpack_bc1((const uint8_t *)pBlock_bits + sizeof(BC4Block), pDst_pixels, true, mode))
|
||||
success = false;
|
||||
if (unpack_bc1((const uint8_t *)pBlock_bits + sizeof(BC4Block), pDst_pixels, true, mode)) success = false;
|
||||
|
||||
unpack_bc4(pBlock_bits, &pDst_pixels[0].A, sizeof(Color32));
|
||||
|
||||
|
@ -55,6 +55,8 @@
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "blocks.h"
|
||||
|
||||
// By default, the table used to accelerate cluster fit on 4 color blocks uses a 969x128 entry table.
|
||||
// To reduce the executable size, set RGBCX_USE_SMALLER_TABLES to 1, which selects the smaller 969x32 entry table.
|
||||
#ifndef RGBCX_USE_SMALLER_TABLES
|
||||
@ -173,8 +175,8 @@ void encode_bc1(void *pDst, const uint8_t *pPixels, uint32_t flags = 0, uint32_t
|
||||
// There are two encode_bc3() functions.
|
||||
// The first is the recommended function, which accepts a level parameter.
|
||||
// The second is a low-level version that allows fine control over BC1 encoding.
|
||||
void encode_bc3(uint32_t level, void *pDst, const uint8_t *pPixels);
|
||||
void encode_bc3(void *pDst, const uint8_t *pPixels, uint32_t flags = 0, uint32_t total_orderings_to_try = DEFAULT_TOTAL_ORDERINGS_TO_TRY);
|
||||
void encode_bc3(uint32_t level, BC3Block *pDst, const uint8_t *pPixels);
|
||||
void encode_bc3(BC3Block *pDst, const uint8_t *pPixels, uint32_t flags = 0, uint32_t total_orderings_to_try = DEFAULT_TOTAL_ORDERINGS_TO_TRY);
|
||||
|
||||
// Encodes a single channel to BC4.
|
||||
// stride is the source pixel stride in bytes.
|
||||
@ -182,7 +184,7 @@ void encode_bc4(void *pDst, const uint8_t *pPixels, uint32_t stride = 4);
|
||||
|
||||
// Encodes two channels to BC5.
|
||||
// chan0/chan1 control which channels, stride is the source pixel stride in bytes.
|
||||
void encode_bc5(void *pDst, const uint8_t *pPixels, uint32_t chan0 = 0, uint32_t chan1 = 1, uint32_t stride = 4);
|
||||
void encode_bc5(BC5Block *pDst, const uint8_t *pPixels, uint32_t chan0 = 0, uint32_t chan1 = 1, uint32_t stride = 4);
|
||||
|
||||
// Decompression functions.
|
||||
|
||||
|
@ -20,10 +20,6 @@
|
||||
|
||||
const int MAX_UBER_LEVEL = 5;
|
||||
|
||||
inline int iabs(int i) { if (i < 0) i = -i; return i; }
|
||||
inline uint8_t clamp255(int32_t i) { return (uint8_t)((i & 0xFFFFFF00U) ? (~(i >> 31)) : i); }
|
||||
template <typename S> inline S clamp(S value, S low, S high) { return (value < low) ? low : ((value > high) ? high : value); }
|
||||
|
||||
static int print_usage()
|
||||
{
|
||||
fprintf(stderr, "bc7enc\n");
|
||||
@ -781,7 +777,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
case DXGI_FORMAT_BC3_UNORM:
|
||||
{
|
||||
block16* pBlock = &packed_image16[bx + by * blocks_x];
|
||||
BC3Block* pBlock = reinterpret_cast<BC3Block *>(&packed_image16[bx + by * blocks_x]);
|
||||
|
||||
rgbcx::encode_bc3(bc1_quality_level, pBlock, &pixels[0].m_c[0]);
|
||||
break;
|
||||
@ -797,7 +793,7 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
block16* pBlock = &packed_image16[bx + by * blocks_x];
|
||||
|
||||
rgbcx::encode_bc5(pBlock, &pixels[0].m_c[0], bc45_channel0, bc45_channel1, 4);
|
||||
rgbcx::encode_bc5(reinterpret_cast<BC5Block *>(pBlock), &pixels[0].m_c[0], bc45_channel0, bc45_channel1, 4);
|
||||
break;
|
||||
}
|
||||
case DXGI_FORMAT_BC7_UNORM:
|
||||
|
13
src/util.h
13
src/util.h
@ -23,17 +23,17 @@
|
||||
static inline uint32_t iabs(int32_t i) { return (i < 0) ? static_cast<uint32_t>(-i) : static_cast<uint32_t>(i); }
|
||||
static inline uint64_t iabs(int64_t i) { return (i < 0) ? static_cast<uint64_t>(-i) : static_cast<uint64_t>(i); }
|
||||
|
||||
static inline uint8_t scale_8_to_5(uint32_t v) {
|
||||
static inline uint8_t scale8To5(uint32_t v) {
|
||||
v = v * 31 + 128;
|
||||
return (uint8_t)((v + (v >> 8)) >> 8);
|
||||
}
|
||||
static inline uint8_t scale_8_to_6(uint32_t v) {
|
||||
static inline uint8_t scale8To6(uint32_t v) {
|
||||
v = v * 63 + 128;
|
||||
return (uint8_t)((v + (v >> 8)) >> 8);
|
||||
}
|
||||
|
||||
static inline int scale_5_to_8(int v) { return (v << 3) | (v >> 2); }
|
||||
static inline int scale_6_to_8(int v) { return (v << 2) | (v >> 4); }
|
||||
static inline int scale5To8(int v) { return (v << 3) | (v >> 2); }
|
||||
static inline int scale6To8(int v) { return (v << 2) | (v >> 4); }
|
||||
|
||||
template <typename S> inline S maximum(S a, S b) { return (a > b) ? a : b; }
|
||||
template <typename S> inline S maximum(S a, S b, S c) { return maximum(maximum(a, b), c); }
|
||||
@ -67,8 +67,3 @@ static inline int squarei(int a) { return a * a; }
|
||||
static inline int absi(int a) { return (a < 0) ? -a : a; }
|
||||
|
||||
template <typename F> inline F lerp(F a, F b, F s) { return a + (b - a) * s; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user