blob: 790f58a7935ab3f210a1038695506c177e5dbd6f [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
Georgios Pinitas583137c2017-08-31 18:12:42 +010027#include "arm_compute/core/Types.h"
Matthew Benthamf1aeab92023-05-30 13:35:34 +000028#include "arm_compute/core/QuantizationInfo.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 */
Pablo Marquez Tello383de022021-03-18 11:31:13 +000039 PixelValue() noexcept
40 : value
41 {
42 int64_t(0)
43 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 {
45 }
Manuel Bottinib412fab2018-12-10 17:40:23 +000046 /** Initialize the union with a pixel value of chosen datatype
47 *
Giorgio Arena1856ff72020-02-07 13:46:45 +000048 * @param[in] v value.
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010049 * @param[in] datatype DataType that @p v have to be stored
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010050 * @param[in] qinfo (Optional) QuantizationInfo to apply in case of quantized data types to @p v
Manuel Bottinib412fab2018-12-10 17:40:23 +000051 */
Giorgio Arena1856ff72020-02-07 13:46:45 +000052 PixelValue(double v, DataType datatype, QuantizationInfo qinfo = QuantizationInfo())
Manuel Bottinib412fab2018-12-10 17:40:23 +000053 : PixelValue()
54 {
55 switch(datatype)
56 {
57 case DataType::U8:
58 value.u8 = static_cast<uint8_t>(v);
59 break;
60 case DataType::S8:
61 value.s8 = static_cast<int8_t>(v);
62 break;
Manuel Bottini1d4f3852019-01-14 15:14:43 +000063 case DataType::QASYMM8:
Giorgio Arena1856ff72020-02-07 13:46:45 +000064 value.u8 = quantize_qasymm8(static_cast<float>(v), qinfo);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010065 break;
Georgios Pinitas33843562019-12-10 13:33:18 +000066 case DataType::QASYMM8_SIGNED:
Giorgio Arena1856ff72020-02-07 13:46:45 +000067 value.s8 = quantize_qasymm8_signed(static_cast<float>(v), qinfo);
Georgios Pinitas33843562019-12-10 13:33:18 +000068 break;
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010069 case DataType::QSYMM8:
Giorgio Arena1856ff72020-02-07 13:46:45 +000070 value.s8 = quantize_qsymm8(static_cast<float>(v), qinfo);
Manuel Bottini1d4f3852019-01-14 15:14:43 +000071 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000072 case DataType::U16:
73 value.u16 = static_cast<uint16_t>(v);
74 break;
75 case DataType::S16:
76 value.s16 = static_cast<int16_t>(v);
77 break;
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010078 case DataType::QASYMM16:
Giorgio Arena1856ff72020-02-07 13:46:45 +000079 value.u16 = quantize_qasymm16(static_cast<float>(v), qinfo);
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010080 break;
Manuel Bottini3689fcd2019-06-14 17:18:12 +010081 case DataType::QSYMM16:
Giorgio Arena1856ff72020-02-07 13:46:45 +000082 value.s16 = quantize_qsymm16(static_cast<float>(v), qinfo);
Manuel Bottini3689fcd2019-06-14 17:18:12 +010083 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000084 case DataType::U32:
85 value.u32 = static_cast<uint32_t>(v);
86 break;
87 case DataType::S32:
88 value.s32 = static_cast<int32_t>(v);
89 break;
90 case DataType::U64:
91 value.u64 = static_cast<uint64_t>(v);
92 break;
93 case DataType::S64:
Michalis Spyrouf44e57b2020-01-22 11:07:54 +000094 value.s64 = static_cast<int64_t>(v);
Manuel Bottinib412fab2018-12-10 17:40:23 +000095 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +000096 case DataType::BFLOAT16:
97 value.bf16 = static_cast<bfloat16>(v);
98 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000099 case DataType::F16:
100 value.f16 = static_cast<half>(v);
101 break;
102 case DataType::F32:
103 value.f32 = static_cast<float>(v);
104 break;
105 case DataType::F64:
Manuel Bottinib412fab2018-12-10 17:40:23 +0000106 default:
Giorgio Arena1856ff72020-02-07 13:46:45 +0000107 value.f64 = v;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000108 break;
109 }
110 }
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000111 /** Initialize the union with a S8 pixel value
112 *
113 * @param[in] v S8 value.
114 */
115 PixelValue(int8_t v)
116 : PixelValue()
117 {
118 value.s8 = v;
119 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120 /** Initialize the union with a U8 pixel value
121 *
122 * @param[in] v U8 value.
123 */
124 PixelValue(uint8_t v)
125 : PixelValue()
126 {
127 value.u8 = v;
128 }
129 /** Initialize the union with a U16 pixel value
130 *
131 * @param[in] v U16 value.
132 */
133 PixelValue(uint16_t v)
134 : PixelValue()
135 {
136 value.u16 = v;
137 }
138 /** Initialize the union with a S16 pixel value
139 *
140 * @param[in] v S16 value.
141 */
142 PixelValue(int16_t v)
143 : PixelValue()
144 {
145 value.s16 = v;
146 }
147 /** Initialize the union with a U32 pixel value
148 *
149 * @param[in] v U32 value.
150 */
151 PixelValue(uint32_t v)
152 : PixelValue()
153 {
154 value.u32 = v;
155 }
156 /** Initialize the union with a S32 pixel value
157 *
158 * @param[in] v S32 value.
159 */
160 PixelValue(int32_t v)
161 : PixelValue()
162 {
163 value.s32 = v;
164 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100165
166 /** Initialize the union with a U64 pixel value
167 *
168 * @param[in] v U64 value.
169 */
170 PixelValue(uint64_t v)
171 : PixelValue()
172 {
173 value.u64 = v;
174 }
175 /** Initialize the union with a S64 pixel value
176 *
177 * @param[in] v S64 value.
178 */
179 PixelValue(int64_t v)
180 : PixelValue()
181 {
182 value.s64 = v;
183 }
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000184 /** Initialize the union with a BFLOAT16 pixel value
185 *
186 * @param[in] v F16 value.
187 */
188 PixelValue(bfloat16 v)
189 : PixelValue()
190 {
191 value.bf16 = v;
192 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100193 /** Initialize the union with a F16 pixel value
194 *
195 * @param[in] v F16 value.
196 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100197 PixelValue(half v)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100198 : PixelValue()
199 {
200 value.f16 = v;
201 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202 /** Initialize the union with a F32 pixel value
203 *
204 * @param[in] v F32 value.
205 */
206 PixelValue(float v)
207 : PixelValue()
208 {
209 value.f32 = v;
210 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100211 /** Initialize the union with a F64 pixel value
212 *
213 * @param[in] v F64 value.
214 */
215 PixelValue(double v)
216 : PixelValue()
217 {
218 value.f64 = v;
219 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220 /** Union which describes the value of a pixel for any image format.
221 * Use the field corresponding to the image format
222 */
223 union
224 {
giuros0191a73f32018-09-21 12:23:44 +0100225 uint64_t u64; /**< Single channel U64 */
226 int64_t s64; /**< Single channel S64 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100227 uint8_t rgb[3]; /**< 3 channels: RGB888 */
228 uint8_t yuv[3]; /**< 3 channels: Any YUV format */
229 uint8_t rgbx[4]; /**< 4 channels: RGBX8888 */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100230 double f64; /**< Single channel double */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100231 float f32; /**< Single channel float 32 */
232 half f16; /**< Single channel F16 */
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000233 bfloat16 bf16; /**< Single channel brain floating-point number */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100234 uint8_t u8; /**< Single channel U8 */
235 int8_t s8; /**< Single channel S8 */
236 uint16_t u16; /**< Single channel U16 */
237 int16_t s16; /**< Single channel S16 */
238 uint32_t u32; /**< Single channel U32 */
239 int32_t s32; /**< Single channel S32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240 } value;
241 /** Interpret the pixel value as a U8
242 *
243 * @param[out] v Returned value
244 */
245 void get(uint8_t &v) const
246 {
247 v = value.u8;
248 }
249 /** Interpret the pixel value as a S8
250 *
251 * @param[out] v Returned value
252 */
253 void get(int8_t &v) const
254 {
255 v = value.s8;
256 }
257 /** Interpret the pixel value as a U16
258 *
259 * @param[out] v Returned value
260 */
261 void get(uint16_t &v) const
262 {
263 v = value.u16;
264 }
265 /** Interpret the pixel value as a S16
266 *
267 * @param[out] v Returned value
268 */
269 void get(int16_t &v) const
270 {
271 v = value.s16;
272 }
273 /** Interpret the pixel value as a U32
274 *
275 * @param[out] v Returned value
276 */
277 void get(uint32_t &v) const
278 {
279 v = value.u32;
280 }
281 /** Interpret the pixel value as a S32
282 *
283 * @param[out] v Returned value
284 */
285 void get(int32_t &v) const
286 {
287 v = value.s32;
288 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100289 /** Interpret the pixel value as a U64
290 *
291 * @param[out] v Returned value
292 */
293 void get(uint64_t &v) const
294 {
295 v = value.u64;
296 }
297 /** Interpret the pixel value as a S64
298 *
299 * @param[out] v Returned value
300 */
301 void get(int64_t &v) const
302 {
303 v = value.s64;
304 }
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000305 /** Interpret the pixel value as a BFLOAT16
306 *
307 * @param[out] v Returned value
308 */
309 void get(bfloat16 &v) const
310 {
311 v = value.bf16;
312 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100313 /** Interpret the pixel value as a F16
314 *
315 * @param[out] v Returned value
316 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100317 void get(half &v) const
Pablo Tello0c34fe22017-06-26 17:17:42 +0100318 {
319 v = value.f16;
320 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321 /** Interpret the pixel value as a F32
322 *
323 * @param[out] v Returned value
324 */
325 void get(float &v) const
326 {
327 v = value.f32;
328 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100329 /** Interpret the pixel value as a double
330 *
331 * @param[out] v Returned value
332 */
333 void get(double &v) const
334 {
335 v = value.f64;
336 }
337 /** Get the pixel value
338 *
339 * @return Pixel value
340 */
341 template <typename T>
342 T get() const
343 {
344 T val;
345 get(val);
346 return val;
347 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100348};
Georgios Pinitas33843562019-12-10 13:33:18 +0000349} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000350#endif /* ARM_COMPUTE_PIXELVALUE_H */