blob: 0b4df4f2e2e68c8fb49ed4580f4ea1b4212c0dee [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2016-2021, 2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_PIXELVALUE_H
25#define ARM_COMPUTE_PIXELVALUE_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Matthew Benthamf1aeab92023-05-30 13:35:34 +000027#include "arm_compute/core/QuantizationInfo.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "arm_compute/core/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
Georgios Pinitas583137c2017-08-31 18:12:42 +010030#include <cstdint>
Pablo Tello0c34fe22017-06-26 17:17:42 +010031
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032namespace arm_compute
33{
34/** Class describing the value of a pixel for any image format. */
35class PixelValue
36{
37public:
38 /** Default constructor: value initialized to 0 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 PixelValue() noexcept : value{int64_t(0)}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 {
41 }
Manuel Bottinib412fab2018-12-10 17:40:23 +000042 /** Initialize the union with a pixel value of chosen datatype
43 *
Giorgio Arena1856ff72020-02-07 13:46:45 +000044 * @param[in] v value.
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010045 * @param[in] datatype DataType that @p v have to be stored
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010046 * @param[in] qinfo (Optional) QuantizationInfo to apply in case of quantized data types to @p v
Manuel Bottinib412fab2018-12-10 17:40:23 +000047 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048 PixelValue(double v, DataType datatype, QuantizationInfo qinfo = QuantizationInfo()) : PixelValue()
Manuel Bottinib412fab2018-12-10 17:40:23 +000049 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050 switch (datatype)
Manuel Bottinib412fab2018-12-10 17:40:23 +000051 {
52 case DataType::U8:
53 value.u8 = static_cast<uint8_t>(v);
54 break;
55 case DataType::S8:
56 value.s8 = static_cast<int8_t>(v);
57 break;
Manuel Bottini1d4f3852019-01-14 15:14:43 +000058 case DataType::QASYMM8:
Giorgio Arena1856ff72020-02-07 13:46:45 +000059 value.u8 = quantize_qasymm8(static_cast<float>(v), qinfo);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010060 break;
Georgios Pinitas33843562019-12-10 13:33:18 +000061 case DataType::QASYMM8_SIGNED:
Giorgio Arena1856ff72020-02-07 13:46:45 +000062 value.s8 = quantize_qasymm8_signed(static_cast<float>(v), qinfo);
Georgios Pinitas33843562019-12-10 13:33:18 +000063 break;
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010064 case DataType::QSYMM8:
Giorgio Arena1856ff72020-02-07 13:46:45 +000065 value.s8 = quantize_qsymm8(static_cast<float>(v), qinfo);
Manuel Bottini1d4f3852019-01-14 15:14:43 +000066 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000067 case DataType::U16:
68 value.u16 = static_cast<uint16_t>(v);
69 break;
70 case DataType::S16:
71 value.s16 = static_cast<int16_t>(v);
72 break;
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010073 case DataType::QASYMM16:
Giorgio Arena1856ff72020-02-07 13:46:45 +000074 value.u16 = quantize_qasymm16(static_cast<float>(v), qinfo);
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010075 break;
Manuel Bottini3689fcd2019-06-14 17:18:12 +010076 case DataType::QSYMM16:
Giorgio Arena1856ff72020-02-07 13:46:45 +000077 value.s16 = quantize_qsymm16(static_cast<float>(v), qinfo);
Manuel Bottini3689fcd2019-06-14 17:18:12 +010078 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000079 case DataType::U32:
80 value.u32 = static_cast<uint32_t>(v);
81 break;
82 case DataType::S32:
83 value.s32 = static_cast<int32_t>(v);
84 break;
85 case DataType::U64:
86 value.u64 = static_cast<uint64_t>(v);
87 break;
88 case DataType::S64:
Michalis Spyrouf44e57b2020-01-22 11:07:54 +000089 value.s64 = static_cast<int64_t>(v);
Manuel Bottinib412fab2018-12-10 17:40:23 +000090 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +000091 case DataType::BFLOAT16:
92 value.bf16 = static_cast<bfloat16>(v);
93 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000094 case DataType::F16:
95 value.f16 = static_cast<half>(v);
96 break;
97 case DataType::F32:
98 value.f32 = static_cast<float>(v);
99 break;
100 case DataType::F64:
Manuel Bottinib412fab2018-12-10 17:40:23 +0000101 default:
Giorgio Arena1856ff72020-02-07 13:46:45 +0000102 value.f64 = v;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000103 break;
104 }
105 }
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000106 /** Initialize the union with a S8 pixel value
107 *
108 * @param[in] v S8 value.
109 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 PixelValue(int8_t v) : PixelValue()
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000111 {
112 value.s8 = v;
113 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 /** Initialize the union with a U8 pixel value
115 *
116 * @param[in] v U8 value.
117 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 PixelValue(uint8_t v) : PixelValue()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 {
120 value.u8 = v;
121 }
122 /** Initialize the union with a U16 pixel value
123 *
124 * @param[in] v U16 value.
125 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 PixelValue(uint16_t v) : PixelValue()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 {
128 value.u16 = v;
129 }
130 /** Initialize the union with a S16 pixel value
131 *
132 * @param[in] v S16 value.
133 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 PixelValue(int16_t v) : PixelValue()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 {
136 value.s16 = v;
137 }
138 /** Initialize the union with a U32 pixel value
139 *
140 * @param[in] v U32 value.
141 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 PixelValue(uint32_t v) : PixelValue()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 {
144 value.u32 = v;
145 }
146 /** Initialize the union with a S32 pixel value
147 *
148 * @param[in] v S32 value.
149 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150 PixelValue(int32_t v) : PixelValue()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 {
152 value.s32 = v;
153 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100154
155 /** Initialize the union with a U64 pixel value
156 *
157 * @param[in] v U64 value.
158 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100159 PixelValue(uint64_t v) : PixelValue()
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100160 {
161 value.u64 = v;
162 }
163 /** Initialize the union with a S64 pixel value
164 *
165 * @param[in] v S64 value.
166 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 PixelValue(int64_t v) : PixelValue()
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100168 {
169 value.s64 = v;
170 }
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000171 /** Initialize the union with a BFLOAT16 pixel value
172 *
173 * @param[in] v F16 value.
174 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 PixelValue(bfloat16 v) : PixelValue()
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000176 {
177 value.bf16 = v;
178 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100179 /** Initialize the union with a F16 pixel value
180 *
181 * @param[in] v F16 value.
182 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100183 PixelValue(half v) : PixelValue()
Pablo Tello0c34fe22017-06-26 17:17:42 +0100184 {
185 value.f16 = v;
186 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 /** Initialize the union with a F32 pixel value
188 *
189 * @param[in] v F32 value.
190 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 PixelValue(float v) : PixelValue()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192 {
193 value.f32 = v;
194 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100195 /** Initialize the union with a F64 pixel value
196 *
197 * @param[in] v F64 value.
198 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100199 PixelValue(double v) : PixelValue()
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100200 {
201 value.f64 = v;
202 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203 /** Union which describes the value of a pixel for any image format.
204 * Use the field corresponding to the image format
205 */
206 union
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100207 {
208 uint64_t u64; /**< Single channel U64 */
209 int64_t s64; /**< Single channel S64 */
210 uint8_t rgb[3]; /**< 3 channels: RGB888 */
211 uint8_t yuv[3]; /**< 3 channels: Any YUV format */
212 uint8_t rgbx[4]; /**< 4 channels: RGBX8888 */
213 double f64; /**< Single channel double */
214 float f32; /**< Single channel float 32 */
215 half f16; /**< Single channel F16 */
216 bfloat16 bf16; /**< Single channel brain floating-point number */
217 uint8_t u8; /**< Single channel U8 */
218 int8_t s8; /**< Single channel S8 */
219 uint16_t u16; /**< Single channel U16 */
220 int16_t s16; /**< Single channel S16 */
221 uint32_t u32; /**< Single channel U32 */
222 int32_t s32; /**< Single channel S32 */
223 } value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224 /** Interpret the pixel value as a U8
225 *
226 * @param[out] v Returned value
227 */
228 void get(uint8_t &v) const
229 {
230 v = value.u8;
231 }
232 /** Interpret the pixel value as a S8
233 *
234 * @param[out] v Returned value
235 */
236 void get(int8_t &v) const
237 {
238 v = value.s8;
239 }
240 /** Interpret the pixel value as a U16
241 *
242 * @param[out] v Returned value
243 */
244 void get(uint16_t &v) const
245 {
246 v = value.u16;
247 }
248 /** Interpret the pixel value as a S16
249 *
250 * @param[out] v Returned value
251 */
252 void get(int16_t &v) const
253 {
254 v = value.s16;
255 }
256 /** Interpret the pixel value as a U32
257 *
258 * @param[out] v Returned value
259 */
260 void get(uint32_t &v) const
261 {
262 v = value.u32;
263 }
264 /** Interpret the pixel value as a S32
265 *
266 * @param[out] v Returned value
267 */
268 void get(int32_t &v) const
269 {
270 v = value.s32;
271 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100272 /** Interpret the pixel value as a U64
273 *
274 * @param[out] v Returned value
275 */
276 void get(uint64_t &v) const
277 {
278 v = value.u64;
279 }
280 /** Interpret the pixel value as a S64
281 *
282 * @param[out] v Returned value
283 */
284 void get(int64_t &v) const
285 {
286 v = value.s64;
287 }
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000288 /** Interpret the pixel value as a BFLOAT16
289 *
290 * @param[out] v Returned value
291 */
292 void get(bfloat16 &v) const
293 {
294 v = value.bf16;
295 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100296 /** Interpret the pixel value as a F16
297 *
298 * @param[out] v Returned value
299 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100300 void get(half &v) const
Pablo Tello0c34fe22017-06-26 17:17:42 +0100301 {
302 v = value.f16;
303 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100304 /** Interpret the pixel value as a F32
305 *
306 * @param[out] v Returned value
307 */
308 void get(float &v) const
309 {
310 v = value.f32;
311 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100312 /** Interpret the pixel value as a double
313 *
314 * @param[out] v Returned value
315 */
316 void get(double &v) const
317 {
318 v = value.f64;
319 }
320 /** Get the pixel value
321 *
322 * @return Pixel value
323 */
324 template <typename T>
325 T get() const
326 {
327 T val;
328 get(val);
329 return val;
330 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331};
Georgios Pinitas33843562019-12-10 13:33:18 +0000332} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000333#endif /* ARM_COMPUTE_PIXELVALUE_H */