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