blob: 0e3d26c515b0f05a4e9ee7c778aca30b3a3a6fd9 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Pablo Marquez Tello383de022021-03-18 11:31:13 +00002 * Copyright (c) 2016-2021 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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028
Georgios Pinitas583137c2017-08-31 18:12:42 +010029#include <cstdint>
Pablo Tello0c34fe22017-06-26 17:17:42 +010030
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031namespace arm_compute
32{
33/** Class describing the value of a pixel for any image format. */
34class PixelValue
35{
36public:
37 /** Default constructor: value initialized to 0 */
Pablo Marquez Tello383de022021-03-18 11:31:13 +000038 PixelValue() noexcept
39 : value
40 {
41 int64_t(0)
42 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 {
44 }
Manuel Bottinib412fab2018-12-10 17:40:23 +000045 /** Initialize the union with a pixel value of chosen datatype
46 *
Giorgio Arena1856ff72020-02-07 13:46:45 +000047 * @param[in] v value.
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010048 * @param[in] datatype DataType that @p v have to be stored
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010049 * @param[in] qinfo (Optional) QuantizationInfo to apply in case of quantized data types to @p v
Manuel Bottinib412fab2018-12-10 17:40:23 +000050 */
Giorgio Arena1856ff72020-02-07 13:46:45 +000051 PixelValue(double v, DataType datatype, QuantizationInfo qinfo = QuantizationInfo())
Manuel Bottinib412fab2018-12-10 17:40:23 +000052 : PixelValue()
53 {
54 switch(datatype)
55 {
56 case DataType::U8:
57 value.u8 = static_cast<uint8_t>(v);
58 break;
59 case DataType::S8:
60 value.s8 = static_cast<int8_t>(v);
61 break;
Manuel Bottini1d4f3852019-01-14 15:14:43 +000062 case DataType::QASYMM8:
Giorgio Arena1856ff72020-02-07 13:46:45 +000063 value.u8 = quantize_qasymm8(static_cast<float>(v), qinfo);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010064 break;
Georgios Pinitas33843562019-12-10 13:33:18 +000065 case DataType::QASYMM8_SIGNED:
Giorgio Arena1856ff72020-02-07 13:46:45 +000066 value.s8 = quantize_qasymm8_signed(static_cast<float>(v), qinfo);
Georgios Pinitas33843562019-12-10 13:33:18 +000067 break;
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010068 case DataType::QSYMM8:
Giorgio Arena1856ff72020-02-07 13:46:45 +000069 value.s8 = quantize_qsymm8(static_cast<float>(v), qinfo);
Manuel Bottini1d4f3852019-01-14 15:14:43 +000070 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000071 case DataType::U16:
72 value.u16 = static_cast<uint16_t>(v);
73 break;
74 case DataType::S16:
75 value.s16 = static_cast<int16_t>(v);
76 break;
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010077 case DataType::QASYMM16:
Giorgio Arena1856ff72020-02-07 13:46:45 +000078 value.u16 = quantize_qasymm16(static_cast<float>(v), qinfo);
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010079 break;
Manuel Bottini3689fcd2019-06-14 17:18:12 +010080 case DataType::QSYMM16:
Giorgio Arena1856ff72020-02-07 13:46:45 +000081 value.s16 = quantize_qsymm16(static_cast<float>(v), qinfo);
Manuel Bottini3689fcd2019-06-14 17:18:12 +010082 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000083 case DataType::U32:
84 value.u32 = static_cast<uint32_t>(v);
85 break;
86 case DataType::S32:
87 value.s32 = static_cast<int32_t>(v);
88 break;
89 case DataType::U64:
90 value.u64 = static_cast<uint64_t>(v);
91 break;
92 case DataType::S64:
Michalis Spyrouf44e57b2020-01-22 11:07:54 +000093 value.s64 = static_cast<int64_t>(v);
Manuel Bottinib412fab2018-12-10 17:40:23 +000094 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +000095 case DataType::BFLOAT16:
96 value.bf16 = static_cast<bfloat16>(v);
97 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000098 case DataType::F16:
99 value.f16 = static_cast<half>(v);
100 break;
101 case DataType::F32:
102 value.f32 = static_cast<float>(v);
103 break;
104 case DataType::F64:
Manuel Bottinib412fab2018-12-10 17:40:23 +0000105 default:
Giorgio Arena1856ff72020-02-07 13:46:45 +0000106 value.f64 = v;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000107 break;
108 }
109 }
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000110 /** Initialize the union with a S8 pixel value
111 *
112 * @param[in] v S8 value.
113 */
114 PixelValue(int8_t v)
115 : PixelValue()
116 {
117 value.s8 = v;
118 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 /** Initialize the union with a U8 pixel value
120 *
121 * @param[in] v U8 value.
122 */
123 PixelValue(uint8_t v)
124 : PixelValue()
125 {
126 value.u8 = v;
127 }
128 /** Initialize the union with a U16 pixel value
129 *
130 * @param[in] v U16 value.
131 */
132 PixelValue(uint16_t v)
133 : PixelValue()
134 {
135 value.u16 = v;
136 }
137 /** Initialize the union with a S16 pixel value
138 *
139 * @param[in] v S16 value.
140 */
141 PixelValue(int16_t v)
142 : PixelValue()
143 {
144 value.s16 = v;
145 }
146 /** Initialize the union with a U32 pixel value
147 *
148 * @param[in] v U32 value.
149 */
150 PixelValue(uint32_t v)
151 : PixelValue()
152 {
153 value.u32 = v;
154 }
155 /** Initialize the union with a S32 pixel value
156 *
157 * @param[in] v S32 value.
158 */
159 PixelValue(int32_t v)
160 : PixelValue()
161 {
162 value.s32 = v;
163 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100164
165 /** Initialize the union with a U64 pixel value
166 *
167 * @param[in] v U64 value.
168 */
169 PixelValue(uint64_t v)
170 : PixelValue()
171 {
172 value.u64 = v;
173 }
174 /** Initialize the union with a S64 pixel value
175 *
176 * @param[in] v S64 value.
177 */
178 PixelValue(int64_t v)
179 : PixelValue()
180 {
181 value.s64 = v;
182 }
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000183 /** Initialize the union with a BFLOAT16 pixel value
184 *
185 * @param[in] v F16 value.
186 */
187 PixelValue(bfloat16 v)
188 : PixelValue()
189 {
190 value.bf16 = v;
191 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100192 /** Initialize the union with a F16 pixel value
193 *
194 * @param[in] v F16 value.
195 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100196 PixelValue(half v)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100197 : PixelValue()
198 {
199 value.f16 = v;
200 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 /** Initialize the union with a F32 pixel value
202 *
203 * @param[in] v F32 value.
204 */
205 PixelValue(float v)
206 : PixelValue()
207 {
208 value.f32 = v;
209 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100210 /** Initialize the union with a F64 pixel value
211 *
212 * @param[in] v F64 value.
213 */
214 PixelValue(double v)
215 : PixelValue()
216 {
217 value.f64 = v;
218 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219 /** Union which describes the value of a pixel for any image format.
220 * Use the field corresponding to the image format
221 */
222 union
223 {
giuros0191a73f32018-09-21 12:23:44 +0100224 uint64_t u64; /**< Single channel U64 */
225 int64_t s64; /**< Single channel S64 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100226 uint8_t rgb[3]; /**< 3 channels: RGB888 */
227 uint8_t yuv[3]; /**< 3 channels: Any YUV format */
228 uint8_t rgbx[4]; /**< 4 channels: RGBX8888 */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100229 double f64; /**< Single channel double */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100230 float f32; /**< Single channel float 32 */
231 half f16; /**< Single channel F16 */
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000232 bfloat16 bf16; /**< Single channel brain floating-point number */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100233 uint8_t u8; /**< Single channel U8 */
234 int8_t s8; /**< Single channel S8 */
235 uint16_t u16; /**< Single channel U16 */
236 int16_t s16; /**< Single channel S16 */
237 uint32_t u32; /**< Single channel U32 */
238 int32_t s32; /**< Single channel S32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239 } value;
240 /** Interpret the pixel value as a U8
241 *
242 * @param[out] v Returned value
243 */
244 void get(uint8_t &v) const
245 {
246 v = value.u8;
247 }
248 /** Interpret the pixel value as a S8
249 *
250 * @param[out] v Returned value
251 */
252 void get(int8_t &v) const
253 {
254 v = value.s8;
255 }
256 /** Interpret the pixel value as a U16
257 *
258 * @param[out] v Returned value
259 */
260 void get(uint16_t &v) const
261 {
262 v = value.u16;
263 }
264 /** Interpret the pixel value as a S16
265 *
266 * @param[out] v Returned value
267 */
268 void get(int16_t &v) const
269 {
270 v = value.s16;
271 }
272 /** Interpret the pixel value as a U32
273 *
274 * @param[out] v Returned value
275 */
276 void get(uint32_t &v) const
277 {
278 v = value.u32;
279 }
280 /** Interpret the pixel value as a S32
281 *
282 * @param[out] v Returned value
283 */
284 void get(int32_t &v) const
285 {
286 v = value.s32;
287 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100288 /** Interpret the pixel value as a U64
289 *
290 * @param[out] v Returned value
291 */
292 void get(uint64_t &v) const
293 {
294 v = value.u64;
295 }
296 /** Interpret the pixel value as a S64
297 *
298 * @param[out] v Returned value
299 */
300 void get(int64_t &v) const
301 {
302 v = value.s64;
303 }
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000304 /** Interpret the pixel value as a BFLOAT16
305 *
306 * @param[out] v Returned value
307 */
308 void get(bfloat16 &v) const
309 {
310 v = value.bf16;
311 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100312 /** Interpret the pixel value as a F16
313 *
314 * @param[out] v Returned value
315 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100316 void get(half &v) const
Pablo Tello0c34fe22017-06-26 17:17:42 +0100317 {
318 v = value.f16;
319 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100320 /** Interpret the pixel value as a F32
321 *
322 * @param[out] v Returned value
323 */
324 void get(float &v) const
325 {
326 v = value.f32;
327 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100328 /** Interpret the pixel value as a double
329 *
330 * @param[out] v Returned value
331 */
332 void get(double &v) const
333 {
334 v = value.f64;
335 }
336 /** Get the pixel value
337 *
338 * @return Pixel value
339 */
340 template <typename T>
341 T get() const
342 {
343 T val;
344 get(val);
345 return val;
346 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100347};
Georgios Pinitas33843562019-12-10 13:33:18 +0000348} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000349#endif /* ARM_COMPUTE_PIXELVALUE_H */