CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/basis_universal/basisu.h
Views: 1401
// basisu.h1// Copyright (C) 2019-2021 Binomial LLC. All Rights Reserved.2// Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing3//4// Licensed under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.15#pragma once1617#ifdef _MSC_VER1819#pragma warning (disable : 4201)20#pragma warning (disable : 4127) // warning C4127: conditional expression is constant21#pragma warning (disable : 4530) // C++ exception handler used, but unwind semantics are not enabled.2223// Slamming this off always for v1.16 because we've gotten rid of most std containers.24#ifndef BASISU_NO_ITERATOR_DEBUG_LEVEL25#define BASISU_NO_ITERATOR_DEBUG_LEVEL (1)26#endif2728#ifndef BASISU_NO_ITERATOR_DEBUG_LEVEL29//#define _HAS_ITERATOR_DEBUGGING 03031#if defined(_DEBUG) || defined(DEBUG)32// This is madness, but we need to disable iterator debugging in debug builds or the encoder is unsable because MSVC's iterator debugging implementation is totally broken.33#ifndef _ITERATOR_DEBUG_LEVEL34#define _ITERATOR_DEBUG_LEVEL 135#endif36#ifndef _SECURE_SCL37#define _SECURE_SCL 138#endif39#else // defined(_DEBUG) || defined(DEBUG)40#ifndef _SECURE_SCL41#define _SECURE_SCL 042#endif43#ifndef _ITERATOR_DEBUG_LEVEL44#define _ITERATOR_DEBUG_LEVEL 045#endif46#endif // defined(_DEBUG) || defined(DEBUG)4748#endif // BASISU_NO_ITERATOR_DEBUG_LEVEL4950#endif // _MSC_VER5152#include <stdlib.h>53#include <stdio.h>54#include <math.h>55#include <stdarg.h>56#include <string.h>57#include <memory.h>58#include <limits.h>59#include <stdint.h>6061#include <algorithm>62#include <limits>63#include <functional>64#include <iterator>65#include <type_traits>66#include <assert.h>67#include <random>6869#include "basisu_containers.h"7071#ifdef max72#undef max73#endif7475#ifdef min76#undef min77#endif7879#ifdef _WIN3280#define strcasecmp _stricmp81#endif8283// Set to one to enable debug printf()'s when any errors occur, for development/debugging. Especially useful for WebGL development.84#ifndef BASISU_FORCE_DEVEL_MESSAGES85#define BASISU_FORCE_DEVEL_MESSAGES 086#endif8788#define BASISU_NOTE_UNUSED(x) (void)(x)89#define BASISU_ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))90#define BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(x) x(const x &) = delete; x& operator= (const x &) = delete;91#define BASISU_ASSUME(x) static_assert(x, #x);92#define BASISU_OFFSETOF(s, m) offsetof(s, m)93#define BASISU_STRINGIZE(x) #x94#define BASISU_STRINGIZE2(x) BASISU_STRINGIZE(x)9596#if BASISU_FORCE_DEVEL_MESSAGES97#define BASISU_DEVEL_ERROR(...) do { basisu::debug_printf(__VA_ARGS__); } while(0)98#else99#define BASISU_DEVEL_ERROR(...)100#endif101102namespace basisu103{104// Types/utilities105106#ifdef _WIN32107const char BASISU_PATH_SEPERATOR_CHAR = '\\';108#else109const char BASISU_PATH_SEPERATOR_CHAR = '/';110#endif111112typedef basisu::vector<uint8_t> uint8_vec;113typedef basisu::vector<int16_t> int16_vec;114typedef basisu::vector<uint16_t> uint16_vec;115typedef basisu::vector<uint32_t> uint_vec;116typedef basisu::vector<uint64_t> uint64_vec;117typedef basisu::vector<int> int_vec;118typedef basisu::vector<bool> bool_vec;119120void enable_debug_printf(bool enabled);121void debug_printf(const char *pFmt, ...);122123124template <typename T> inline void clear_obj(T& obj) { memset(&obj, 0, sizeof(obj)); }125126template <typename T0, typename T1> inline T0 lerp(T0 a, T0 b, T1 c) { return a + (b - a) * c; }127128template <typename S> inline S maximum(S a, S b) { return (a > b) ? a : b; }129template <typename S> inline S maximum(S a, S b, S c) { return maximum(maximum(a, b), c); }130template <typename S> inline S maximum(S a, S b, S c, S d) { return maximum(maximum(maximum(a, b), c), d); }131132template <typename S> inline S minimum(S a, S b) { return (a < b) ? a : b; }133template <typename S> inline S minimum(S a, S b, S c) { return minimum(minimum(a, b), c); }134template <typename S> inline S minimum(S a, S b, S c, S d) { return minimum(minimum(minimum(a, b), c), d); }135136inline float clampf(float value, float low, float high) { if (value < low) value = low; else if (value > high) value = high; return value; }137inline float saturate(float value) { return clampf(value, 0, 1.0f); }138inline uint8_t minimumub(uint8_t a, uint8_t b) { return (a < b) ? a : b; }139inline uint32_t minimumu(uint32_t a, uint32_t b) { return (a < b) ? a : b; }140inline int32_t minimumi(int32_t a, int32_t b) { return (a < b) ? a : b; }141inline float minimumf(float a, float b) { return (a < b) ? a : b; }142inline uint8_t maximumub(uint8_t a, uint8_t b) { return (a > b) ? a : b; }143inline uint32_t maximumu(uint32_t a, uint32_t b) { return (a > b) ? a : b; }144inline int32_t maximumi(int32_t a, int32_t b) { return (a > b) ? a : b; }145inline float maximumf(float a, float b) { return (a > b) ? a : b; }146inline int squarei(int i) { return i * i; }147inline float squaref(float i) { return i * i; }148template<typename T> inline T square(T a) { return a * a; }149150template <typename S> inline S clamp(S value, S low, S high) { return (value < low) ? low : ((value > high) ? high : value); }151152inline uint32_t iabs(int32_t i) { return (i < 0) ? static_cast<uint32_t>(-i) : static_cast<uint32_t>(i); }153inline uint64_t iabs64(int64_t i) { return (i < 0) ? static_cast<uint64_t>(-i) : static_cast<uint64_t>(i); }154155template<typename T> inline void clear_vector(T &vec) { vec.erase(vec.begin(), vec.end()); }156template<typename T> inline typename T::value_type *enlarge_vector(T &vec, size_t n) { size_t cs = vec.size(); vec.resize(cs + n); return &vec[cs]; }157158inline bool is_pow2(uint32_t x) { return x && ((x & (x - 1U)) == 0U); }159inline bool is_pow2(uint64_t x) { return x && ((x & (x - 1U)) == 0U); }160161template<typename T> inline T open_range_check(T v, T minv, T maxv) { assert(v >= minv && v < maxv); BASISU_NOTE_UNUSED(minv); BASISU_NOTE_UNUSED(maxv); return v; }162template<typename T> inline T open_range_check(T v, T maxv) { assert(v < maxv); BASISU_NOTE_UNUSED(maxv); return v; }163164inline uint32_t total_bits(uint32_t v) { uint32_t l = 0; for ( ; v > 0U; ++l) v >>= 1; return l; }165166template<typename T> inline T saturate(T val) { return clamp(val, 0.0f, 1.0f); }167168template<typename T, typename R> inline void append_vector(T &vec, const R *pObjs, size_t n)169{170if (n)171{172if (vec.size())173{174assert((pObjs + n) <= vec.begin() || (pObjs >= vec.end()));175}176const size_t cur_s = vec.size();177vec.resize(cur_s + n);178memcpy(&vec[cur_s], pObjs, sizeof(R) * n);179}180}181182template<typename T> inline void append_vector(T &vec, const T &other_vec)183{184assert(&vec != &other_vec);185if (other_vec.size())186append_vector(vec, &other_vec[0], other_vec.size());187}188189template<typename T> inline void vector_ensure_element_is_valid(T &vec, size_t idx)190{191if (idx >= vec.size())192vec.resize(idx + 1);193}194195template<typename T> inline void vector_sort(T &vec)196{197if (vec.size())198std::sort(vec.begin(), vec.end());199}200201template<typename T, typename U> inline bool unordered_set_contains(T& set, const U&obj)202{203return set.find(obj) != set.end();204}205206template<typename T> int vector_find(const T &vec, const typename T::value_type &obj)207{208assert(vec.size() <= INT_MAX);209for (size_t i = 0; i < vec.size(); i++)210if (vec[i] == obj)211return static_cast<int>(i);212return -1;213}214215template<typename T> void vector_set_all(T &vec, const typename T::value_type &obj)216{217for (size_t i = 0; i < vec.size(); i++)218vec[i] = obj;219}220221inline uint64_t read_be64(const void *p)222{223uint64_t val = 0;224for (uint32_t i = 0; i < 8; i++)225val |= (static_cast<uint64_t>(static_cast<const uint8_t *>(p)[7 - i]) << (i * 8));226return val;227}228229inline void write_be64(void *p, uint64_t x)230{231for (uint32_t i = 0; i < 8; i++)232static_cast<uint8_t *>(p)[7 - i] = static_cast<uint8_t>(x >> (i * 8));233}234235static inline uint16_t byteswap16(uint16_t x) { return static_cast<uint16_t>((x << 8) | (x >> 8)); }236static inline uint32_t byteswap32(uint32_t x) { return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24)); }237238inline uint32_t floor_log2i(uint32_t v)239{240uint32_t b = 0;241for (; v > 1U; ++b)242v >>= 1;243return b;244}245246inline uint32_t ceil_log2i(uint32_t v)247{248uint32_t b = floor_log2i(v);249if ((b != 32) && (v > (1U << b)))250++b;251return b;252}253254inline int posmod(int x, int y)255{256if (x >= 0)257return (x < y) ? x : (x % y);258int m = (-x) % y;259return (m != 0) ? (y - m) : m;260}261262inline bool do_excl_ranges_overlap(int la, int ha, int lb, int hb)263{264assert(la < ha && lb < hb);265if ((ha <= lb) || (la >= hb)) return false;266return true;267}268269static inline uint32_t read_le_dword(const uint8_t *pBytes)270{271return (pBytes[3] << 24U) | (pBytes[2] << 16U) | (pBytes[1] << 8U) | (pBytes[0]);272}273274static inline void write_le_dword(uint8_t* pBytes, uint32_t val)275{276pBytes[0] = (uint8_t)val;277pBytes[1] = (uint8_t)(val >> 8U);278pBytes[2] = (uint8_t)(val >> 16U);279pBytes[3] = (uint8_t)(val >> 24U);280}281282// Always little endian 1-8 byte unsigned int283template<uint32_t NumBytes>284struct packed_uint285{286uint8_t m_bytes[NumBytes];287288inline packed_uint() { static_assert(NumBytes <= sizeof(uint64_t), "Invalid NumBytes"); }289inline packed_uint(uint64_t v) { *this = v; }290inline packed_uint(const packed_uint& other) { *this = other; }291292inline packed_uint& operator= (uint64_t v)293{294for (uint32_t i = 0; i < NumBytes; i++)295m_bytes[i] = static_cast<uint8_t>(v >> (i * 8));296return *this;297}298299inline packed_uint& operator= (const packed_uint& rhs)300{301memcpy(m_bytes, rhs.m_bytes, sizeof(m_bytes));302return *this;303}304305inline operator uint32_t() const306{307switch (NumBytes)308{309case 1:310{311return m_bytes[0];312}313case 2:314{315return (m_bytes[1] << 8U) | m_bytes[0];316}317case 3:318{319return (m_bytes[2] << 16U) | (m_bytes[1] << 8U) | m_bytes[0];320}321case 4:322{323return read_le_dword(m_bytes);324}325case 5:326{327uint32_t l = read_le_dword(m_bytes);328uint32_t h = m_bytes[4];329return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);330}331case 6:332{333uint32_t l = read_le_dword(m_bytes);334uint32_t h = (m_bytes[5] << 8U) | m_bytes[4];335return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);336}337case 7:338{339uint32_t l = read_le_dword(m_bytes);340uint32_t h = (m_bytes[6] << 16U) | (m_bytes[5] << 8U) | m_bytes[4];341return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);342}343case 8:344{345uint32_t l = read_le_dword(m_bytes);346uint32_t h = read_le_dword(m_bytes + 4);347return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);348}349default:350{351assert(0);352return 0;353}354}355}356};357358enum eZero { cZero };359enum eNoClamp { cNoClamp };360361// Rice/Huffman entropy coding362363// This is basically Deflate-style canonical Huffman, except we allow for a lot more symbols.364enum365{366cHuffmanMaxSupportedCodeSize = 16, cHuffmanMaxSupportedInternalCodeSize = 31,367cHuffmanFastLookupBits = 10,368cHuffmanMaxSymsLog2 = 14, cHuffmanMaxSyms = 1 << cHuffmanMaxSymsLog2,369370// Small zero runs371cHuffmanSmallZeroRunSizeMin = 3, cHuffmanSmallZeroRunSizeMax = 10, cHuffmanSmallZeroRunExtraBits = 3,372373// Big zero run374cHuffmanBigZeroRunSizeMin = 11, cHuffmanBigZeroRunSizeMax = 138, cHuffmanBigZeroRunExtraBits = 7,375376// Small non-zero run377cHuffmanSmallRepeatSizeMin = 3, cHuffmanSmallRepeatSizeMax = 6, cHuffmanSmallRepeatExtraBits = 2,378379// Big non-zero run380cHuffmanBigRepeatSizeMin = 7, cHuffmanBigRepeatSizeMax = 134, cHuffmanBigRepeatExtraBits = 7,381382cHuffmanTotalCodelengthCodes = 21, cHuffmanSmallZeroRunCode = 17, cHuffmanBigZeroRunCode = 18, cHuffmanSmallRepeatCode = 19, cHuffmanBigRepeatCode = 20383};384385static const uint8_t g_huffman_sorted_codelength_codes[] = { cHuffmanSmallZeroRunCode, cHuffmanBigZeroRunCode, cHuffmanSmallRepeatCode, cHuffmanBigRepeatCode, 0, 8, 7, 9, 6, 0xA, 5, 0xB, 4, 0xC, 3, 0xD, 2, 0xE, 1, 0xF, 0x10 };386const uint32_t cHuffmanTotalSortedCodelengthCodes = sizeof(g_huffman_sorted_codelength_codes) / sizeof(g_huffman_sorted_codelength_codes[0]);387388// GPU texture formats389390enum class texture_format391{392cInvalidTextureFormat = -1,393394// Block-based formats395cETC1, // ETC1396cETC1S, // ETC1 (subset: diff colors only, no subblocks)397cETC2_RGB, // ETC2 color block (basisu doesn't support ETC2 planar/T/H modes - just basic ETC1)398cETC2_RGBA, // ETC2 EAC alpha block followed by ETC2 color block399cETC2_ALPHA, // ETC2 EAC alpha block400cBC1, // DXT1401cBC3, // DXT5 (BC4/DXT5A block followed by a BC1/DXT1 block)402cBC4, // DXT5A403cBC5, // 3DC/DXN (two BC4/DXT5A blocks)404cBC7,405cASTC4x4, // LDR only406cPVRTC1_4_RGB,407cPVRTC1_4_RGBA,408cATC_RGB,409cATC_RGBA_INTERPOLATED_ALPHA,410cFXT1_RGB,411cPVRTC2_4_RGBA,412cETC2_R11_EAC,413cETC2_RG11_EAC,414cUASTC4x4,415cBC1_NV,416cBC1_AMD,417418// Uncompressed/raw pixels419cRGBA32,420cRGB565,421cBGR565,422cRGBA4444,423cABGR4444424};425426inline uint32_t get_bytes_per_block(texture_format fmt)427{428switch (fmt)429{430case texture_format::cETC1:431case texture_format::cETC1S:432case texture_format::cETC2_RGB:433case texture_format::cETC2_ALPHA:434case texture_format::cBC1:435case texture_format::cBC1_NV:436case texture_format::cBC1_AMD:437case texture_format::cBC4:438case texture_format::cPVRTC1_4_RGB:439case texture_format::cPVRTC1_4_RGBA:440case texture_format::cATC_RGB:441case texture_format::cPVRTC2_4_RGBA:442case texture_format::cETC2_R11_EAC:443return 8;444case texture_format::cRGBA32:445return sizeof(uint32_t) * 16;446default:447break;448}449return 16;450}451452inline uint32_t get_qwords_per_block(texture_format fmt)453{454return get_bytes_per_block(fmt) >> 3;455}456457inline uint32_t get_block_width(texture_format fmt)458{459BASISU_NOTE_UNUSED(fmt);460switch (fmt)461{462case texture_format::cFXT1_RGB:463return 8;464default:465break;466}467return 4;468}469470inline uint32_t get_block_height(texture_format fmt)471{472BASISU_NOTE_UNUSED(fmt);473return 4;474}475476} // namespace basisu477478479480