Compute Library
 21.05
PixelValue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021 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"
28 
29 #include <cstdint>
30 
31 namespace arm_compute
32 {
33 /** Class describing the value of a pixel for any image format. */
35 {
36 public:
37  /** Default constructor: value initialized to 0 */
38  PixelValue() noexcept
39  : value
40  {
41  int64_t(0)
42  }
43  {
44  }
45  /** Initialize the union with a pixel value of chosen datatype
46  *
47  * @param[in] v value.
48  * @param[in] datatype DataType that @p v have to be stored
49  * @param[in] qinfo (Optional) QuantizationInfo to apply in case of quantized data types to @p v
50  */
52  : 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;
62  case DataType::QASYMM8:
63  value.u8 = quantize_qasymm8(static_cast<float>(v), qinfo);
64  break;
66  value.s8 = quantize_qasymm8_signed(static_cast<float>(v), qinfo);
67  break;
68  case DataType::QSYMM8:
69  value.s8 = quantize_qsymm8(static_cast<float>(v), qinfo);
70  break;
71  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;
77  case DataType::QASYMM16:
78  value.u16 = quantize_qasymm16(static_cast<float>(v), qinfo);
79  break;
80  case DataType::QSYMM16:
81  value.s16 = quantize_qsymm16(static_cast<float>(v), qinfo);
82  break;
83  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:
93  value.s64 = static_cast<int64_t>(v);
94  break;
95  case DataType::BFLOAT16:
96  value.bf16 = static_cast<bfloat16>(v);
97  break;
98  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:
105  default:
106  value.f64 = v;
107  break;
108  }
109  }
110  /** 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  }
119  /** 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  }
164 
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  }
183  /** Initialize the union with a BFLOAT16 pixel value
184  *
185  * @param[in] v F16 value.
186  */
188  : PixelValue()
189  {
190  value.bf16 = v;
191  }
192  /** Initialize the union with a F16 pixel value
193  *
194  * @param[in] v F16 value.
195  */
197  : PixelValue()
198  {
199  value.f16 = v;
200  }
201  /** 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  }
210  /** 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  }
219  /** 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  {
224  uint64_t u64; /**< Single channel U64 */
225  int64_t s64; /**< Single channel S64 */
226  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 */
229  double f64; /**< Single channel double */
230  float f32; /**< Single channel float 32 */
231  half f16; /**< Single channel F16 */
232  bfloat16 bf16; /**< Single channel brain floating-point number */
233  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 */
239  } 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  }
288  /** 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  }
304  /** 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  }
312  /** Interpret the pixel value as a F16
313  *
314  * @param[out] v Returned value
315  */
316  void get(half &v) const
317  {
318  v = value.f16;
319  }
320  /** 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  }
328  /** 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  }
347 };
348 } // namespace arm_compute
349 #endif /* ARM_COMPUTE_PIXELVALUE_H */
void get(uint32_t &v) const
Interpret the pixel value as a U32.
Definition: PixelValue.h:276
uint8_t rgbx[4]
4 channels: RGBX8888
Definition: PixelValue.h:228
int8_t quantize_qsymm8(float value, const QuantizationInfo &qinfo)
Quantize a value given a 8-bit symmetric quantization scheme.
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
PixelValue(float v)
Initialize the union with a F32 pixel value.
Definition: PixelValue.h:205
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.
quantized, symmetric fixed-point 16-bit number
PixelValue(uint8_t v)
Initialize the union with a U8 pixel value.
Definition: PixelValue.h:123
uint8_t rgb[3]
3 channels: RGB888
Definition: PixelValue.h:226
uint8_t u8
Single channel U8.
Definition: PixelValue.h:233
uint16_t u16
Single channel U16.
Definition: PixelValue.h:235
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.
Brain floating point representation class.
Definition: Bfloat16.h:81
PixelValue(double v)
Initialize the union with a F64 pixel value.
Definition: PixelValue.h:214
void get(int32_t &v) const
Interpret the pixel value as a S32.
Definition: PixelValue.h:284
int64_t s64
Single channel S64.
Definition: PixelValue.h:225
1 channel, 1 U8 per channel
void get(uint8_t &v) const
Interpret the pixel value as a U8.
Definition: PixelValue.h:244
void get(int8_t &v) const
Interpret the pixel value as a S8.
Definition: PixelValue.h:252
void get(double &v) const
Interpret the pixel value as a double.
Definition: PixelValue.h:332
half_float::half half
16-bit floating point type
Definition: Types.h:46
1 channel, 1 F32 per channel
quantized, asymmetric fixed-point 16-bit number
1 channel, 1 U16 per channel
void get(int16_t &v) const
Interpret the pixel value as a S16.
Definition: PixelValue.h:268
half f16
Single channel F16.
Definition: PixelValue.h:231
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
void get(uint64_t &v) const
Interpret the pixel value as a U64.
Definition: PixelValue.h:292
1 channel, 1 S32 per channel
16-bit brain floating-point number
PixelValue(half v)
Initialize the union with a F16 pixel value.
Definition: PixelValue.h:196
signed 64-bit number
double f64
Single channel double.
Definition: PixelValue.h:229
Quantization information.
void get(int64_t &v) const
Interpret the pixel value as a S64.
Definition: PixelValue.h:300
PixelValue() noexcept
Default constructor: value initialized to 0.
Definition: PixelValue.h:38
1 channel, 1 U32 per channel
void get(bfloat16 &v) const
Interpret the pixel value as a BFLOAT16.
Definition: PixelValue.h:308
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.
quantized, asymmetric fixed-point 8-bit number unsigned
void get(float &v) const
Interpret the pixel value as a F32.
Definition: PixelValue.h:324
bfloat16 bf16
Single channel brain floating-point number.
Definition: PixelValue.h:232
union arm_compute::PixelValue::@0 value
Union which describes the value of a pixel for any image format.
1 channel, 1 S16 per channel
PixelValue(bfloat16 v)
Initialize the union with a BFLOAT16 pixel value.
Definition: PixelValue.h:187
quantized, symmetric fixed-point 8-bit number
PixelValue(uint64_t v)
Initialize the union with a U64 pixel value.
Definition: PixelValue.h:169
int8_t s8
Single channel S8.
Definition: PixelValue.h:234
float f32
Single channel float 32.
Definition: PixelValue.h:230
void get(uint16_t &v) const
Interpret the pixel value as a U16.
Definition: PixelValue.h:260
T get() const
Get the pixel value.
Definition: PixelValue.h:341
PixelValue(uint16_t v)
Initialize the union with a U16 pixel value.
Definition: PixelValue.h:132
const QuantizationInfo qinfo
Definition: Im2Col.cpp:155
uint64_t u64
Single channel U64.
Definition: PixelValue.h:224
int16_t s16
Single channel S16.
Definition: PixelValue.h:236
PixelValue(uint32_t v)
Initialize the union with a U32 pixel value.
Definition: PixelValue.h:150
quantized, asymmetric fixed-point 8-bit number signed
PixelValue(int32_t v)
Initialize the union with a S32 pixel value.
Definition: PixelValue.h:159
int32_t s32
Single channel S32.
Definition: PixelValue.h:238
64-bit floating-point number
uint32_t u32
Single channel U32.
Definition: PixelValue.h:237
unsigned 64-bit number
DataType
Available data types.
Definition: Types.h:77
uint8_t yuv[3]
3 channels: Any YUV format
Definition: PixelValue.h:227
signed 8-bit number
PixelValue(double v, DataType datatype, QuantizationInfo qinfo=QuantizationInfo())
Initialize the union with a pixel value of chosen datatype.
Definition: PixelValue.h:51
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.
PixelValue(int8_t v)
Initialize the union with a S8 pixel value.
Definition: PixelValue.h:114
PixelValue(int16_t v)
Initialize the union with a S16 pixel value.
Definition: PixelValue.h:141
PixelValue(int64_t v)
Initialize the union with a S64 pixel value.
Definition: PixelValue.h:178
void get(half &v) const
Interpret the pixel value as a F16.
Definition: PixelValue.h:316