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)
|
737
src/rgbcx.cpp
737
src/rgbcx.cpp
File diff suppressed because it is too large
Load Diff
10
src/rgbcx.h
10
src/rgbcx.h
@ -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.
|
||||
|
||||
@ -195,7 +197,7 @@ void unpack_bc4(const void *pBlock_bits, uint8_t *pPixels, uint32_t stride = 4);
|
||||
bool unpack_bc3(const void *pBlock_bits, void *pPixels, bc1_approx_mode mode = bc1_approx_mode::cBC1Ideal);
|
||||
|
||||
void unpack_bc5(const void *pBlock_bits, void *pPixels, uint32_t chan0 = 0, uint32_t chan1 = 1, uint32_t stride = 4);
|
||||
} // namespace rgbcx
|
||||
} // namespace rgbcx
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
@ -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:
|
||||
|
15
src/util.h
15
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); }
|
||||
@ -55,7 +55,7 @@ static inline float clampf(float value, float low, float high) {
|
||||
static 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 inline int32_t clampi(int32_t value, int32_t low, int32_t high) {
|
||||
static inline int32_t clampi(int32_t value, int32_t low, int32_t high) {
|
||||
if (value < low)
|
||||
value = low;
|
||||
else if (value > high)
|
||||
@ -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