/src/libjxl/lib/jxl/base/common.h
Line | Count | Source (jump to first uncovered line) |
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 | 0 | constexpr inline size_t RoundUpBitsToByteMultiple(size_t bits) { |
27 | 0 | return (bits + 7) & ~static_cast<size_t>(7); |
28 | 0 | } |
29 | | |
30 | 0 | constexpr inline size_t RoundUpToBlockDim(size_t dim) { |
31 | 0 | return (dim + 7) & ~static_cast<size_t>(7); |
32 | 0 | } |
33 | | |
34 | | static inline bool JXL_MAYBE_UNUSED SafeAdd(const uint64_t a, const uint64_t b, |
35 | 397k | uint64_t& sum) { |
36 | 397k | sum = a + b; |
37 | 397k | return sum >= a; // no need to check b - either sum >= both or < both. |
38 | 397k | } Unexecuted instantiation: djxl_fuzzer.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: decode.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) fields.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Line | Count | Source | 35 | 397k | uint64_t& sum) { | 36 | 397k | sum = a + b; | 37 | 397k | return sum >= a; // no need to check b - either sum >= both or < both. | 38 | 397k | } |
Unexecuted instantiation: frame_header.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: headers.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: icc_codec.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: icc_codec_common.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: image_metadata.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: loop_filter.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: memory_manager_internal.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: encoding.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: modular_image.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: squeeze_params.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: transform.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: quant_weights.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: quantizer.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: decode_to_jpeg.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_jpeg_data.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_jpeg_data_writer.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: jpeg_data.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: color_encoding_internal.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_ans.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_bit_reader.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_context_map.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_frame.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_group.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_huffman.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_modular.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_noise.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_patch_dictionary.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_xyb.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: entropy_coder.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: epf.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: image.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_ma.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: palette.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: rct.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: squeeze.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: opsin_params.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: passes_state.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: render_pipeline.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: simple_render_pipeline.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_upsampling.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: splines.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: toc.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: ac_strategy.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: ans_common.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: blending.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: chroma_from_luma.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: coeff_order.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: compressed_dc.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_cache.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: low_memory_render_pipeline.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: render_pipeline_stage.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_blending.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_chroma_upsampling.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_cms.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_epf.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_from_linear.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_gaborish.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_noise.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_patches.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_splines.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_spot.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_to_linear.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_tone_mapping.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_write.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_xyb.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: stage_ycbcr.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: alpha.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: dec_group_border.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: image_bundle.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) Unexecuted instantiation: tracking_memory_manager.cc:jxl::SafeAdd(unsigned long, unsigned long, unsigned long&) |
39 | | |
40 | | template <typename T1, typename T2> |
41 | 41.6M | constexpr inline T1 DivCeil(T1 a, T2 b) { |
42 | 41.6M | return (a + b - 1) / b; |
43 | 41.6M | } unsigned long jxl::DivCeil<unsigned long, unsigned long>(unsigned long, unsigned long) Line | Count | Source | 41 | 30.4M | constexpr inline T1 DivCeil(T1 a, T2 b) { | 42 | 30.4M | return (a + b - 1) / b; | 43 | 30.4M | } |
unsigned long jxl::DivCeil<unsigned long, int>(unsigned long, int) Line | Count | Source | 41 | 10.2M | constexpr inline T1 DivCeil(T1 a, T2 b) { | 42 | 10.2M | return (a + b - 1) / b; | 43 | 10.2M | } |
int jxl::DivCeil<int, int>(int, int) Line | Count | Source | 41 | 489k | constexpr inline T1 DivCeil(T1 a, T2 b) { | 42 | 489k | return (a + b - 1) / b; | 43 | 489k | } |
unsigned long jxl::DivCeil<unsigned long, long>(unsigned long, long) Line | Count | Source | 41 | 468k | constexpr inline T1 DivCeil(T1 a, T2 b) { | 42 | 468k | return (a + b - 1) / b; | 43 | 468k | } |
|
44 | | |
45 | | // Works for any `align`; if a power of two, compiler emits ADD+AND. |
46 | 25.2M | constexpr inline size_t RoundUpTo(size_t what, size_t align) { |
47 | 25.2M | return DivCeil(what, align) * align; |
48 | 25.2M | } |
49 | | |
50 | | constexpr double kPi = 3.14159265358979323846264338327950288; |
51 | | |
52 | | // Multiplier for conversion of log2(x) result to ln(x). |
53 | | // print(1.0 / math.log2(math.e)) |
54 | | constexpr float kInvLog2e = 0.6931471805599453; |
55 | | |
56 | | // Reasonable default for sRGB, matches common monitors. We map white to this |
57 | | // many nits (cd/m^2) by default. Butteraugli was tuned for 250 nits, which is |
58 | | // very close. |
59 | | // NB: This constant is not very "base", but it is shared between modules. |
60 | | static constexpr float kDefaultIntensityTarget = 255; |
61 | | |
62 | | template <typename T> |
63 | | constexpr T Pi(T multiplier) { |
64 | | return static_cast<T>(multiplier * kPi); |
65 | | } |
66 | | |
67 | | // Prior to C++14 (i.e. C++11): provide our own make_unique |
68 | | #if __cplusplus < 201402L |
69 | | template <typename T, typename... Args> |
70 | | std::unique_ptr<T> make_unique(Args&&... args) { |
71 | | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
72 | | } |
73 | | #else |
74 | | using std::make_unique; |
75 | | #endif |
76 | | |
77 | | typedef std::array<float, 3> Color; |
78 | | |
79 | | // Backported std::experimental::to_array |
80 | | |
81 | | template <typename T> |
82 | | using remove_cv_t = typename std::remove_cv<T>::type; |
83 | | |
84 | | template <size_t... I> |
85 | | struct index_sequence {}; |
86 | | |
87 | | template <size_t N, size_t... I> |
88 | | struct make_index_sequence : make_index_sequence<N - 1, N - 1, I...> {}; |
89 | | |
90 | | template <size_t... I> |
91 | | struct make_index_sequence<0, I...> : index_sequence<I...> {}; |
92 | | |
93 | | namespace detail { |
94 | | |
95 | | template <typename T, size_t N, size_t... I> |
96 | | constexpr auto to_array(T (&&arr)[N], index_sequence<I...> _) |
97 | 0 | -> std::array<remove_cv_t<T>, N> { |
98 | 0 | return {{std::move(arr[I])...}}; |
99 | 0 | } |
100 | | |
101 | | } // namespace detail |
102 | | |
103 | | template <typename T, size_t N> |
104 | 0 | constexpr auto to_array(T (&&arr)[N]) -> std::array<remove_cv_t<T>, N> { |
105 | 0 | return detail::to_array(std::move(arr), make_index_sequence<N>()); |
106 | 0 | } |
107 | | |
108 | | template <typename T> |
109 | 90.0M | JXL_INLINE T Clamp1(T val, T low, T hi) { |
110 | 90.0M | return val < low ? low : val > hi ? hi : val; |
111 | 90.0M | } float jxl::Clamp1<float>(float, float, float) Line | Count | Source | 109 | 77.6M | JXL_INLINE T Clamp1(T val, T low, T hi) { | 110 | 77.6M | return val < low ? low : val > hi ? hi : val; | 111 | 77.6M | } |
double jxl::Clamp1<double>(double, double, double) Line | Count | Source | 109 | 326k | JXL_INLINE T Clamp1(T val, T low, T hi) { | 110 | 326k | return val < low ? low : val > hi ? hi : val; | 111 | 326k | } |
int jxl::Clamp1<int>(int, int, int) Line | Count | Source | 109 | 12.0M | JXL_INLINE T Clamp1(T val, T low, T hi) { | 110 | 12.0M | return val < low ? low : val > hi ? hi : val; | 111 | 12.0M | } |
|
112 | | |
113 | | // conversion from integer to string. |
114 | | template <typename T> |
115 | 16.3k | std::string ToString(T n) { |
116 | 16.3k | char data[32] = {}; |
117 | 16.3k | if (std::is_floating_point<T>::value) { |
118 | | // float |
119 | 16.3k | snprintf(data, sizeof(data), "%g", static_cast<double>(n)); |
120 | 16.3k | } else if (std::is_unsigned<T>::value) { |
121 | | // unsigned |
122 | 0 | snprintf(data, sizeof(data), "%llu", static_cast<unsigned long long>(n)); |
123 | 0 | } else { |
124 | | // signed |
125 | 0 | snprintf(data, sizeof(data), "%lld", static_cast<long long>(n)); |
126 | 0 | } |
127 | 16.3k | return data; |
128 | 16.3k | } |
129 | | |
130 | | #define JXL_JOIN(x, y) JXL_DO_JOIN(x, y) |
131 | | #define JXL_DO_JOIN(x, y) x##y |
132 | | |
133 | | } // namespace jxl |
134 | | |
135 | | #endif // LIB_JXL_BASE_COMMON_H_ |