Compute Library
 23.08
PixelValue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021, 2023 Arm Limited.
3  *
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  */
24 #ifndef ARM_COMPUTE_PIXELVALUE_H
25 #define ARM_COMPUTE_PIXELVALUE_H
26 
27 #include "arm_compute/core/Types.h"
29 
30 #include <cstdint>
31 
32 namespace arm_compute
33 {
34 /** Class describing the value of a pixel for any image format. */
36 {
37 public:
38  /** Default constructor: value initialized to 0 */
39  PixelValue() noexcept
40  : value
41  {
42  int64_t(0)
43  }
44  {
45  }
46  /** Initialize the union with a pixel value of chosen datatype
47  *
48  * @param[in] v value.
49  * @param[in] datatype DataType that @p v have to be stored
50  * @param[in] qinfo (Optional) QuantizationInfo to apply in case of quantized data types to @p v
51  */
53  : 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;
63  case DataType::QASYMM8:
64  value.u8 = quantize_qasymm8(static_cast<float>(v), qinfo);
65  break;
67  value.s8 = quantize_qasymm8_signed(static_cast<float>(v), qinfo);
68  break;
69  case DataType::QSYMM8:
70  value.s8 = quantize_qsymm8(static_cast<float>(v), qinfo);
71  break;
72  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;
78  case DataType::QASYMM16:
79  value.u16 = quantize_qasymm16(static_cast<float>(v), qinfo);
80  break;
81  case DataType::QSYMM16:
82  value.s16 = quantize_qsymm16(static_cast<float>(v), qinfo);
83  break;
84  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:
94  value.s64 = static_cast<int64_t>(v);
95  break;
96  case DataType::BFLOAT16:
97  value.bf16 = static_cast<bfloat16>(v);
98  break;
99  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:
106  default:
107  value.f64 = v;
108  break;
109  }
110  }
111  /** 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  }
120  /** 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  }
165 
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  }
184  /** Initialize the union with a BFLOAT16 pixel value
185  *
186  * @param[in] v F16 value.
187  */
189  : PixelValue()
190  {
191  value.bf16 = v;
192  }
193  /** Initialize the union with a F16 pixel value
194  *
195  * @param[in] v F16 value.
196  */
198  : PixelValue()
199  {
200  value.f16 = v;
201  }
202  /** 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  }
211  /** 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  }
220  /** 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  {
225  uint64_t u64; /**< Single channel U64 */
226  int64_t s64; /**< Single channel S64 */
227  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 */
230  double f64; /**< Single channel double */
231  float f32; /**< Single channel float 32 */
232  half f16; /**< Single channel F16 */
233  bfloat16 bf16; /**< Single channel brain floating-point number */
234  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 */
240  } 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  }
289  /** 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  }
305  /** 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  }
313  /** Interpret the pixel value as a F16
314  *
315  * @param[out] v Returned value
316  */
317  void get(half &v) const
318  {
319  v = value.f16;
320  }
321  /** 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  }
329  /** 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  }
348 };
349 } // namespace arm_compute
350 #endif /* ARM_COMPUTE_PIXELVALUE_H */
arm_compute::DataType::QASYMM16
@ QASYMM16
quantized, asymmetric fixed-point 16-bit number
arm_compute::PixelValue::PixelValue
PixelValue(int8_t v)
Initialize the union with a S8 pixel value.
Definition: PixelValue.h:115
arm_compute::DataType::U64
@ U64
unsigned 64-bit number
arm_compute::PixelValue::PixelValue
PixelValue(int64_t v)
Initialize the union with a S64 pixel value.
Definition: PixelValue.h:179
arm_compute::PixelValue::PixelValue
PixelValue(uint8_t v)
Initialize the union with a U8 pixel value.
Definition: PixelValue.h:124
arm_compute::PixelValue::rgbx
uint8_t rgbx[4]
4 channels: RGBX8888
Definition: PixelValue.h:229
arm_compute::PixelValue::get
void get(half &v) const
Interpret the pixel value as a F16.
Definition: PixelValue.h:317
arm_compute::DataType::BFLOAT16
@ BFLOAT16
16-bit brain floating-point number
arm_compute::PixelValue::get
void get(uint32_t &v) const
Interpret the pixel value as a U32.
Definition: PixelValue.h:277
arm_compute::QuantizationInfo
Quantization information.
Definition: QuantizationInfo.h:68
arm_compute::PixelValue
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:35
arm_compute::DataType::F64
@ F64
64-bit floating-point number
arm_compute::quantize_qsymm16
int16_t quantize_qsymm16(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a 16-bit symmetric quantization scheme.
Definition: QuantizationInfo.h:444
arm_compute::PixelValue::s64
int64_t s64
Single channel S64.
Definition: PixelValue.h:226
arm_compute::DataType::QASYMM8
@ QASYMM8
quantized, asymmetric fixed-point 8-bit number unsigned
arm_compute::PixelValue::get
void get(uint8_t &v) const
Interpret the pixel value as a U8.
Definition: PixelValue.h:245
arm_compute::DataType::U16
@ U16
unsigned 16-bit number
arm_compute::PixelValue::get
void get(double &v) const
Interpret the pixel value as a double.
Definition: PixelValue.h:333
arm_compute::quantize_qsymm8
int8_t quantize_qsymm8(float value, const QuantizationInfo &qinfo)
Quantize a value given a 8-bit symmetric quantization scheme.
Definition: QuantizationInfo.h:326
arm_compute::PixelValue::rgb
uint8_t rgb[3]
3 channels: RGB888
Definition: PixelValue.h:227
arm_compute::DataType::QSYMM8
@ QSYMM8
quantized, symmetric fixed-point 8-bit number
Types.h
arm_compute::quantize_qasymm8
uint8_t quantize_qasymm8(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given an unsigned 8-bit asymmetric quantization scheme.
Definition: QuantizationInfo.h:300
arm_compute::PixelValue::u8
uint8_t u8
Single channel U8.
Definition: PixelValue.h:234
arm_compute::PixelValue::u16
uint16_t u16
Single channel U16.
Definition: PixelValue.h:236
arm_compute::PixelValue::PixelValue
PixelValue(double v)
Initialize the union with a F64 pixel value.
Definition: PixelValue.h:215
arm_compute::PixelValue::get
void get(int32_t &v) const
Interpret the pixel value as a S32.
Definition: PixelValue.h:285
arm_compute::PixelValue::get
void get(int8_t &v) const
Interpret the pixel value as a S8.
Definition: PixelValue.h:253
arm_compute::bfloat16
Brain floating point representation class.
Definition: Bfloat16.h:81
arm_compute::DataType::S8
@ S8
signed 8-bit number
arm_compute::DataType::QSYMM16
@ QSYMM16
quantized, symmetric fixed-point 16-bit number
arm_compute::PixelValue::PixelValue
PixelValue(half v)
Initialize the union with a F16 pixel value.
Definition: PixelValue.h:197
arm_compute::PixelValue::f64
double f64
Single channel double.
Definition: PixelValue.h:230
arm_compute::PixelValue::get
void get(int16_t &v) const
Interpret the pixel value as a S16.
Definition: PixelValue.h:269
arm_compute::PixelValue::f16
half f16
Single channel F16.
Definition: PixelValue.h:232
arm_compute::half
half_float::half half
16-bit floating point type
Definition: CoreTypes.h:35
arm_compute::PixelValue::get
void get(uint64_t &v) const
Interpret the pixel value as a U64.
Definition: PixelValue.h:293
arm_compute::PixelValue::PixelValue
PixelValue() noexcept
Default constructor: value initialized to 0.
Definition: PixelValue.h:39
arm_compute::DataType::U32
@ U32
unsigned 32-bit number
arm_compute::quantize_qasymm8_signed
int8_t quantize_qasymm8_signed(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a signed 8-bit asymmetric quantization scheme.
Definition: QuantizationInfo.h:314
arm_compute::PixelValue::get
void get(float &v) const
Interpret the pixel value as a F32.
Definition: PixelValue.h:325
arm_compute::DataType::U8
@ U8
unsigned 8-bit number
arm_compute::PixelValue::get
void get(int64_t &v) const
Interpret the pixel value as a S64.
Definition: PixelValue.h:301
arm_compute::DataType::S16
@ S16
signed 16-bit number
arm_compute::DataType::QASYMM8_SIGNED
@ QASYMM8_SIGNED
quantized, asymmetric fixed-point 8-bit number signed
arm_compute::PixelValue::get
void get(bfloat16 &v) const
Interpret the pixel value as a BFLOAT16.
Definition: PixelValue.h:309
arm_compute::PixelValue::bf16
bfloat16 bf16
Single channel brain floating-point number.
Definition: PixelValue.h:233
arm_compute::PixelValue::s8
int8_t s8
Single channel S8.
Definition: PixelValue.h:235
arm_compute::PixelValue::PixelValue
PixelValue(bfloat16 v)
Initialize the union with a BFLOAT16 pixel value.
Definition: PixelValue.h:188
arm_compute::PixelValue::get
T get() const
Get the pixel value.
Definition: PixelValue.h:342
arm_compute::PixelValue::PixelValue
PixelValue(uint64_t v)
Initialize the union with a U64 pixel value.
Definition: PixelValue.h:170
arm_compute::PixelValue::PixelValue
PixelValue(uint16_t v)
Initialize the union with a U16 pixel value.
Definition: PixelValue.h:133
arm_compute::PixelValue::u64
uint64_t u64
Single channel U64.
Definition: PixelValue.h:225
arm_compute::DataType::S64
@ S64
signed 64-bit number
arm_compute::PixelValue::s16
int16_t s16
Single channel S16.
Definition: PixelValue.h:237
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::PixelValue::f32
float f32
Single channel float 32.
Definition: PixelValue.h:231
arm_compute::PixelValue::get
void get(uint16_t &v) const
Interpret the pixel value as a U16.
Definition: PixelValue.h:261
arm_compute::DataType::F16
@ F16
16-bit floating-point number
arm_compute::PixelValue::PixelValue
PixelValue(uint32_t v)
Initialize the union with a U32 pixel value.
Definition: PixelValue.h:151
arm_compute::DataType::S32
@ S32
signed 32-bit number
arm_compute::PixelValue::value
union arm_compute::PixelValue::@0 value
Union which describes the value of a pixel for any image format.
arm_compute::quantize_qasymm16
uint16_t quantize_qasymm16(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a 16-bit asymmetric quantization scheme.
Definition: QuantizationInfo.h:495
arm_compute::PixelValue::u32
uint32_t u32
Single channel U32.
Definition: PixelValue.h:238
arm_compute::DataType::F32
@ F32
32-bit floating-point number
arm_compute::PixelValue::yuv
uint8_t yuv[3]
3 channels: Any YUV format
Definition: PixelValue.h:228
arm_compute::PixelValue::PixelValue
PixelValue(int32_t v)
Initialize the union with a S32 pixel value.
Definition: PixelValue.h:160
arm_compute::PixelValue::s32
int32_t s32
Single channel S32.
Definition: PixelValue.h:239
arm_compute::PixelValue::PixelValue
PixelValue(int16_t v)
Initialize the union with a S16 pixel value.
Definition: PixelValue.h:142
arm_compute::DataType
DataType
Available data types.
Definition: CoreTypes.h:82
QuantizationInfo.h
arm_compute::test::validation::qinfo
const QuantizationInfo qinfo
Definition: Im2Col.cpp:155
arm_compute::PixelValue::PixelValue
PixelValue(double v, DataType datatype, QuantizationInfo qinfo=QuantizationInfo())
Initialize the union with a pixel value of chosen datatype.
Definition: PixelValue.h:52
arm_compute::PixelValue::PixelValue
PixelValue(float v)
Initialize the union with a F32 pixel value.
Definition: PixelValue.h:206