/src/libheif/libheif/common_utils.h
Line | Count | Source |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2023 Dirk Farin <dirk.farin@gmail.com> |
4 | | * |
5 | | * This file is part of libheif. |
6 | | * |
7 | | * libheif is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libheif is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #ifndef LIBHEIF_COMMON_UTILS_H |
22 | | #define LIBHEIF_COMMON_UTILS_H |
23 | | |
24 | | #include <cinttypes> |
25 | | #include <string> |
26 | | #include <vector> |
27 | | |
28 | | #include "libheif/heif.h" |
29 | | #include "error.h" |
30 | | |
31 | | #ifdef _MSC_VER |
32 | | #define MAYBE_UNUSED |
33 | | #else |
34 | | #define MAYBE_UNUSED __attribute__((unused)) |
35 | | #endif |
36 | | |
37 | | |
38 | | constexpr uint32_t four_bytes_to_uint32(uint8_t msb, uint8_t b, uint8_t c, uint8_t lsb) |
39 | 5.39M | { |
40 | 5.39M | return (static_cast<uint32_t>(msb << 24) | |
41 | 5.39M | static_cast<uint32_t>(b << 16) | |
42 | 5.39M | static_cast<uint32_t>(c << 8) | |
43 | 5.39M | static_cast<uint32_t>(lsb)); |
44 | 5.39M | } |
45 | | |
46 | | constexpr uint16_t two_bytes_to_uint16(uint8_t msb, uint8_t lsb) |
47 | 1.72k | { |
48 | 1.72k | return (static_cast<uint16_t>(msb << 8) | |
49 | 1.72k | static_cast<uint16_t>(lsb)); |
50 | 1.72k | } |
51 | | |
52 | | constexpr uint32_t fourcc(const char* id) |
53 | 3.57M | { |
54 | 3.57M | return four_bytes_to_uint32(static_cast<uint8_t>(id[0]), |
55 | 3.57M | static_cast<uint8_t>(id[1]), |
56 | 3.57M | static_cast<uint8_t>(id[2]), |
57 | 3.57M | static_cast<uint8_t>(id[3])); |
58 | 3.57M | } |
59 | | |
60 | | std::string fourcc_to_string(uint32_t code); |
61 | | |
62 | | |
63 | | // Functions for common use in libheif and the plugins. |
64 | | |
65 | | uint8_t chroma_h_subsampling(heif_chroma c); |
66 | | |
67 | | uint8_t chroma_v_subsampling(heif_chroma c); |
68 | | |
69 | | enum class scaling_mode : uint8_t { |
70 | | round_down, |
71 | | round_up, |
72 | | is_divisible |
73 | | }; |
74 | | |
75 | | uint32_t get_subsampled_size_h(uint32_t width, |
76 | | heif_channel channel, |
77 | | heif_chroma chroma, |
78 | | scaling_mode mode); |
79 | | |
80 | | uint32_t get_subsampled_size_v(uint32_t height, |
81 | | heif_channel channel, |
82 | | heif_chroma chroma, |
83 | | scaling_mode mode); |
84 | | |
85 | | void get_subsampled_size(uint32_t width, uint32_t height, |
86 | | heif_channel channel, |
87 | | heif_chroma chroma, |
88 | | uint32_t* subsampled_width, uint32_t* subsampled_height); |
89 | | |
90 | | uint8_t compute_avif_profile(int bits_per_pixel, heif_chroma chroma); |
91 | | |
92 | | |
93 | | inline uint8_t clip_int_u8(int x) |
94 | 56.3k | { |
95 | 56.3k | if (x < 0) return 0; |
96 | 56.3k | if (x > 255) return 255; |
97 | 56.3k | return static_cast<uint8_t>(x); |
98 | 56.3k | } |
99 | | |
100 | | inline uint16_t clip_int_u16(int32_t x, uint16_t maxi) |
101 | 15.4M | { |
102 | 15.4M | if (x < 0) return 0; |
103 | 15.0M | if (x > maxi) return maxi; |
104 | 14.9M | return static_cast<uint16_t>(x); |
105 | 15.0M | } |
106 | | |
107 | | |
108 | | inline uint16_t clip_f_u16(float fx, int32_t maxi) |
109 | 12.7G | { |
110 | 12.7G | int32_t x = (int32_t) (fx + 0.5f); |
111 | 12.7G | if (x < 0) return 0; |
112 | 8.15G | if (x > maxi) return (uint16_t) maxi; |
113 | 7.93G | return static_cast<uint16_t>(x); |
114 | 8.15G | } |
115 | | |
116 | | inline uint8_t clip_f_u8(float fx) |
117 | 136M | { |
118 | 136M | int32_t x = (int32_t) (fx + 0.5f); |
119 | 136M | if (x < 0) return 0; |
120 | 136M | if (x > 255) return 255; |
121 | 136M | return static_cast<uint8_t>(x); |
122 | 136M | } |
123 | | |
124 | | Result<std::string> vector_to_string(const std::vector<uint8_t>& vec); |
125 | | |
126 | | #endif //LIBHEIF_COMMON_UTILS_H |