libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

common.h (3575B)


      1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style
      4 // license that can be found in the LICENSE file.
      5 
      6 #ifndef LIB_JXL_BASE_COMMON_H_
      7 #define LIB_JXL_BASE_COMMON_H_
      8 
      9 // Shared constants and helper functions.
     10 
     11 #include <array>
     12 #include <cstddef>
     13 #include <cstdint>
     14 #include <cstdio>
     15 #include <memory>
     16 #include <string>
     17 #include <type_traits>
     18 
     19 #include "lib/jxl/base/compiler_specific.h"
     20 
     21 namespace jxl {
     22 // Some enums and typedefs used by more than one header file.
     23 
     24 constexpr size_t kBitsPerByte = 8;  // more clear than CHAR_BIT
     25 
     26 constexpr inline size_t RoundUpBitsToByteMultiple(size_t bits) {
     27   return (bits + 7) & ~static_cast<size_t>(7);
     28 }
     29 
     30 constexpr inline size_t RoundUpToBlockDim(size_t dim) {
     31   return (dim + 7) & ~static_cast<size_t>(7);
     32 }
     33 
     34 static inline bool JXL_MAYBE_UNUSED SafeAdd(const uint64_t a, const uint64_t b,
     35                                             uint64_t& sum) {
     36   sum = a + b;
     37   return sum >= a;  // no need to check b - either sum >= both or < both.
     38 }
     39 
     40 template <typename T1, typename T2>
     41 constexpr inline T1 DivCeil(T1 a, T2 b) {
     42   return (a + b - 1) / b;
     43 }
     44 
     45 // Works for any `align`; if a power of two, compiler emits ADD+AND.
     46 constexpr inline size_t RoundUpTo(size_t what, size_t align) {
     47   return DivCeil(what, align) * align;
     48 }
     49 
     50 constexpr double kPi = 3.14159265358979323846264338327950288;
     51 
     52 // Reasonable default for sRGB, matches common monitors. We map white to this
     53 // many nits (cd/m^2) by default. Butteraugli was tuned for 250 nits, which is
     54 // very close.
     55 // NB: This constant is not very "base", but it is shared between modules.
     56 static constexpr float kDefaultIntensityTarget = 255;
     57 
     58 template <typename T>
     59 constexpr T Pi(T multiplier) {
     60   return static_cast<T>(multiplier * kPi);
     61 }
     62 
     63 // Prior to C++14 (i.e. C++11): provide our own make_unique
     64 #if __cplusplus < 201402L
     65 template <typename T, typename... Args>
     66 std::unique_ptr<T> make_unique(Args&&... args) {
     67   return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
     68 }
     69 #else
     70 using std::make_unique;
     71 #endif
     72 
     73 // Backported std::experimental::to_array
     74 
     75 template <typename T>
     76 using remove_cv_t = typename std::remove_cv<T>::type;
     77 
     78 template <size_t... I>
     79 struct index_sequence {};
     80 
     81 template <size_t N, size_t... I>
     82 struct make_index_sequence : make_index_sequence<N - 1, N - 1, I...> {};
     83 
     84 template <size_t... I>
     85 struct make_index_sequence<0, I...> : index_sequence<I...> {};
     86 
     87 namespace detail {
     88 
     89 template <typename T, size_t N, size_t... I>
     90 constexpr auto to_array(T (&&arr)[N], index_sequence<I...> _)
     91     -> std::array<remove_cv_t<T>, N> {
     92   return {{std::move(arr[I])...}};
     93 }
     94 
     95 }  // namespace detail
     96 
     97 template <typename T, size_t N>
     98 constexpr auto to_array(T (&&arr)[N]) -> std::array<remove_cv_t<T>, N> {
     99   return detail::to_array(std::move(arr), make_index_sequence<N>());
    100 }
    101 
    102 template <typename T>
    103 JXL_INLINE T Clamp1(T val, T low, T hi) {
    104   return val < low ? low : val > hi ? hi : val;
    105 }
    106 
    107 // conversion from integer to string.
    108 template <typename T>
    109 std::string ToString(T n) {
    110   char data[32] = {};
    111   if (std::is_floating_point<T>::value) {
    112     // float
    113     snprintf(data, sizeof(data), "%g", static_cast<double>(n));
    114   } else if (std::is_unsigned<T>::value) {
    115     // unsigned
    116     snprintf(data, sizeof(data), "%llu", static_cast<unsigned long long>(n));
    117   } else {
    118     // signed
    119     snprintf(data, sizeof(data), "%lld", static_cast<long long>(n));
    120   }
    121   return data;
    122 }
    123 
    124 #define JXL_JOIN(x, y) JXL_DO_JOIN(x, y)
    125 #define JXL_DO_JOIN(x, y) x##y
    126 
    127 }  // namespace jxl
    128 
    129 #endif  // LIB_JXL_BASE_COMMON_H_