Compute Library
 20.08
PixelValue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 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 */
39  : value{ int64_t(0) }
40  {
41  }
42  /** Initialize the union with a pixel value of chosen datatype
43  *
44  * @param[in] v value.
45  * @param[in] datatype DataType that @p v have to be stored
46  * @param[in] qinfo (Optional) QuantizationInfo to apply in case of quantized data types to @p v
47  */
49  : PixelValue()
50  {
51  switch(datatype)
52  {
53  case DataType::U8:
54  value.u8 = static_cast<uint8_t>(v);
55  break;
56  case DataType::S8:
57  value.s8 = static_cast<int8_t>(v);
58  break;
59  case DataType::QASYMM8:
60  value.u8 = quantize_qasymm8(static_cast<float>(v), qinfo);
61  break;
63  value.s8 = quantize_qasymm8_signed(static_cast<float>(v), qinfo);
64  break;
65  case DataType::QSYMM8:
66  value.s8 = quantize_qsymm8(static_cast<float>(v), qinfo);
67  break;
68  case DataType::U16:
69  value.u16 = static_cast<uint16_t>(v);
70  break;
71  case DataType::S16:
72  value.s16 = static_cast<int16_t>(v);
73  break;
74  case DataType::QASYMM16:
75  value.u16 = quantize_qasymm16(static_cast<float>(v), qinfo);
76  break;
77  case DataType::QSYMM16:
78  value.s16 = quantize_qsymm16(static_cast<float>(v), qinfo);
79  break;
80  case DataType::U32:
81  value.u32 = static_cast<uint32_t>(v);
82  break;
83  case DataType::S32:
84  value.s32 = static_cast<int32_t>(v);
85  break;
86  case DataType::U64:
87  value.u64 = static_cast<uint64_t>(v);
88  break;
89  case DataType::S64:
90  value.s64 = static_cast<int64_t>(v);
91  break;
92  case DataType::BFLOAT16:
93  value.bf16 = static_cast<bfloat16>(v);
94  break;
95  case DataType::F16:
96  value.f16 = static_cast<half>(v);
97  break;
98  case DataType::F32:
99  value.f32 = static_cast<float>(v);
100  break;
101  case DataType::F64:
102  default:
103  value.f64 = v;
104  break;
105  }
106  }
107  /** Initialize the union with a S8 pixel value
108  *
109  * @param[in] v S8 value.
110  */
111  PixelValue(int8_t v)
112  : PixelValue()
113  {
114  value.s8 = v;
115  }
116  /** Initialize the union with a U8 pixel value
117  *
118  * @param[in] v U8 value.
119  */
120  PixelValue(uint8_t v)
121  : PixelValue()
122  {
123  value.u8 = v;
124  }
125  /** Initialize the union with a U16 pixel value
126  *
127  * @param[in] v U16 value.
128  */
129  PixelValue(uint16_t v)
130  : PixelValue()
131  {
132  value.u16 = v;
133  }
134  /** Initialize the union with a S16 pixel value
135  *
136  * @param[in] v S16 value.
137  */
138  PixelValue(int16_t v)
139  : PixelValue()
140  {
141  value.s16 = v;
142  }
143  /** Initialize the union with a U32 pixel value
144  *
145  * @param[in] v U32 value.
146  */
147  PixelValue(uint32_t v)
148  : PixelValue()
149  {
150  value.u32 = v;
151  }
152  /** Initialize the union with a S32 pixel value
153  *
154  * @param[in] v S32 value.
155  */
156  PixelValue(int32_t v)
157  : PixelValue()
158  {
159  value.s32 = v;
160  }
161 
162  /** Initialize the union with a U64 pixel value
163  *
164  * @param[in] v U64 value.
165  */
166  PixelValue(uint64_t v)
167  : PixelValue()
168  {
169  value.u64 = v;
170  }
171  /** Initialize the union with a S64 pixel value
172  *
173  * @param[in] v S64 value.
174  */
175  PixelValue(int64_t v)
176  : PixelValue()
177  {
178  value.s64 = v;
179  }
180  /** Initialize the union with a BFLOAT16 pixel value
181  *
182  * @param[in] v F16 value.
183  */
185  : PixelValue()
186  {
187  value.bf16 = v;
188  }
189  /** Initialize the union with a F16 pixel value
190  *
191  * @param[in] v F16 value.
192  */
194  : PixelValue()
195  {
196  value.f16 = v;
197  }
198  /** Initialize the union with a F32 pixel value
199  *
200  * @param[in] v F32 value.
201  */
202  PixelValue(float v)
203  : PixelValue()
204  {
205  value.f32 = v;
206  }
207  /** Initialize the union with a F64 pixel value
208  *
209  * @param[in] v F64 value.
210  */
211  PixelValue(double v)
212  : PixelValue()
213  {
214  value.f64 = v;
215  }
216  /** Union which describes the value of a pixel for any image format.
217  * Use the field corresponding to the image format
218  */
219  union
220  {
221  uint64_t u64; /**< Single channel U64 */
222  int64_t s64; /**< Single channel S64 */
223  uint8_t rgb[3]; /**< 3 channels: RGB888 */
224  uint8_t yuv[3]; /**< 3 channels: Any YUV format */
225  uint8_t rgbx[4]; /**< 4 channels: RGBX8888 */
226  double f64; /**< Single channel double */
227  float f32; /**< Single channel float 32 */
228  half f16; /**< Single channel F16 */
229  bfloat16 bf16; /**< Single channel brain floating-point number */
230  uint8_t u8; /**< Single channel U8 */
231  int8_t s8; /**< Single channel S8 */
232  uint16_t u16; /**< Single channel U16 */
233  int16_t s16; /**< Single channel S16 */
234  uint32_t u32; /**< Single channel U32 */
235  int32_t s32; /**< Single channel S32 */
236  } value;
237  /** Interpret the pixel value as a U8
238  *
239  * @param[out] v Returned value
240  */
241  void get(uint8_t &v) const
242  {
243  v = value.u8;
244  }
245  /** Interpret the pixel value as a S8
246  *
247  * @param[out] v Returned value
248  */
249  void get(int8_t &v) const
250  {
251  v = value.s8;
252  }
253  /** Interpret the pixel value as a U16
254  *
255  * @param[out] v Returned value
256  */
257  void get(uint16_t &v) const
258  {
259  v = value.u16;
260  }
261  /** Interpret the pixel value as a S16
262  *
263  * @param[out] v Returned value
264  */
265  void get(int16_t &v) const
266  {
267  v = value.s16;
268  }
269  /** Interpret the pixel value as a U32
270  *
271  * @param[out] v Returned value
272  */
273  void get(uint32_t &v) const
274  {
275  v = value.u32;
276  }
277  /** Interpret the pixel value as a S32
278  *
279  * @param[out] v Returned value
280  */
281  void get(int32_t &v) const
282  {
283  v = value.s32;
284  }
285  /** Interpret the pixel value as a U64
286  *
287  * @param[out] v Returned value
288  */
289  void get(uint64_t &v) const
290  {
291  v = value.u64;
292  }
293  /** Interpret the pixel value as a S64
294  *
295  * @param[out] v Returned value
296  */
297  void get(int64_t &v) const
298  {
299  v = value.s64;
300  }
301  /** Interpret the pixel value as a BFLOAT16
302  *
303  * @param[out] v Returned value
304  */
305  void get(bfloat16 &v) const
306  {
307  v = value.bf16;
308  }
309  /** Interpret the pixel value as a F16
310  *
311  * @param[out] v Returned value
312  */
313  void get(half &v) const
314  {
315  v = value.f16;
316  }
317  /** Interpret the pixel value as a F32
318  *
319  * @param[out] v Returned value
320  */
321  void get(float &v) const
322  {
323  v = value.f32;
324  }
325  /** Interpret the pixel value as a double
326  *
327  * @param[out] v Returned value
328  */
329  void get(double &v) const
330  {
331  v = value.f64;
332  }
333  /** Get the pixel value
334  *
335  * @return Pixel value
336  */
337  template <typename T>
338  T get() const
339  {
340  T val;
341  get(val);
342  return val;
343  }
344 };
345 } // namespace arm_compute
346 #endif /* ARM_COMPUTE_PIXELVALUE_H */
void get(uint32_t &v) const
Interpret the pixel value as a U32.
Definition: PixelValue.h:273
uint8_t rgbx[4]
4 channels: RGBX8888
Definition: PixelValue.h:225
PixelValue()
Default constructor: value initialized to 0.
Definition: PixelValue.h:38
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:202
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:120
uint8_t rgb[3]
3 channels: RGB888
Definition: PixelValue.h:223
uint8_t u8
Single channel U8.
Definition: PixelValue.h:230
uint16_t u16
Single channel U16.
Definition: PixelValue.h:232
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:80
PixelValue(double v)
Initialize the union with a F64 pixel value.
Definition: PixelValue.h:211
void get(int32_t &v) const
Interpret the pixel value as a S32.
Definition: PixelValue.h:281
int64_t s64
Single channel S64.
Definition: PixelValue.h:222
1 channel, 1 U8 per channel
void get(uint8_t &v) const
Interpret the pixel value as a U8.
Definition: PixelValue.h:241
void get(int8_t &v) const
Interpret the pixel value as a S8.
Definition: PixelValue.h:249
void get(double &v) const
Interpret the pixel value as a double.
Definition: PixelValue.h:329
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:265
half f16
Single channel F16.
Definition: PixelValue.h:228
Copyright (c) 2017-2020 Arm Limited.
1 channel, 1 F16 per channel
void get(uint64_t &v) const
Interpret the pixel value as a U64.
Definition: PixelValue.h:289
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:193
signed 64-bit number
double f64
Single channel double.
Definition: PixelValue.h:226
Quantization information.
void get(int64_t &v) const
Interpret the pixel value as a S64.
Definition: PixelValue.h:297
1 channel, 1 U32 per channel
void get(bfloat16 &v) const
Interpret the pixel value as a BFLOAT16.
Definition: PixelValue.h:305
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:321
bfloat16 bf16
Single channel brain floating-point number.
Definition: PixelValue.h:229
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:184
quantized, symmetric fixed-point 8-bit number
PixelValue(uint64_t v)
Initialize the union with a U64 pixel value.
Definition: PixelValue.h:166
int8_t s8
Single channel S8.
Definition: PixelValue.h:231
float f32
Single channel float 32.
Definition: PixelValue.h:227
void get(uint16_t &v) const
Interpret the pixel value as a U16.
Definition: PixelValue.h:257
T get() const
Get the pixel value.
Definition: PixelValue.h:338
PixelValue(uint16_t v)
Initialize the union with a U16 pixel value.
Definition: PixelValue.h:129
const QuantizationInfo qinfo
Definition: Im2Col.cpp:150
uint64_t u64
Single channel U64.
Definition: PixelValue.h:221
int16_t s16
Single channel S16.
Definition: PixelValue.h:233
PixelValue(uint32_t v)
Initialize the union with a U32 pixel value.
Definition: PixelValue.h:147
quantized, asymmetric fixed-point 8-bit number signed
PixelValue(int32_t v)
Initialize the union with a S32 pixel value.
Definition: PixelValue.h:156
int32_t s32
Single channel S32.
Definition: PixelValue.h:235
64-bit floating-point number
uint32_t u32
Single channel U32.
Definition: PixelValue.h:234
unsigned 64-bit number
DataType
Available data types.
Definition: Types.h:77
uint8_t yuv[3]
3 channels: Any YUV format
Definition: PixelValue.h:224
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:48
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:111
PixelValue(int16_t v)
Initialize the union with a S16 pixel value.
Definition: PixelValue.h:138
PixelValue(int64_t v)
Initialize the union with a S64 pixel value.
Definition: PixelValue.h:175
void get(half &v) const
Interpret the pixel value as a F16.
Definition: PixelValue.h:313