blob: 4a07fa6c5d93f9d12fd834ab768a46ede685da4f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottinib412fab2018-12-10 17:40:23 +00002 * Copyright (c) 2016-2019 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 */
24#ifndef __ARM_COMPUTE_PIXELVALUE_H__
25#define __ARM_COMPUTE_PIXELVALUE_H__
26
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 */
38 PixelValue()
giuros0191a73f32018-09-21 12:23:44 +010039 : value{ uint64_t(0) }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 {
41 }
Manuel Bottinib412fab2018-12-10 17:40:23 +000042 /** Initialize the union with a pixel value of chosen datatype
43 *
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010044 * @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 QASYMM8 datatype to @p v
Manuel Bottinib412fab2018-12-10 17:40:23 +000047 */
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010048 PixelValue(uint64_t v, DataType datatype, QuantizationInfo qinfo = QuantizationInfo())
Manuel Bottinib412fab2018-12-10 17:40:23 +000049 : 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;
Manuel Bottini1d4f3852019-01-14 15:14:43 +000059 case DataType::QASYMM8:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010060 value.u8 = quantize_qasymm8(static_cast<uint8_t>(v), qinfo);
61 break;
62 case DataType::QSYMM8:
63 value.s8 = quantize_qsymm8(static_cast<int8_t>(v), qinfo);
Manuel Bottini1d4f3852019-01-14 15:14:43 +000064 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000065 case DataType::U16:
66 value.u16 = static_cast<uint16_t>(v);
67 break;
68 case DataType::S16:
69 value.s16 = static_cast<int16_t>(v);
70 break;
Manuel Bottini3689fcd2019-06-14 17:18:12 +010071 case DataType::QSYMM16:
72 value.s16 = quantize_qsymm16(static_cast<int16_t>(v), qinfo);
73 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000074 case DataType::U32:
75 value.u32 = static_cast<uint32_t>(v);
76 break;
77 case DataType::S32:
78 value.s32 = static_cast<int32_t>(v);
79 break;
80 case DataType::U64:
81 value.u64 = static_cast<uint64_t>(v);
82 break;
83 case DataType::S64:
84 value.s16 = static_cast<int64_t>(v);
85 break;
86 case DataType::F16:
87 value.f16 = static_cast<half>(v);
88 break;
89 case DataType::F32:
90 value.f32 = static_cast<float>(v);
91 break;
92 case DataType::F64:
93 value.f64 = static_cast<double>(v);
94 break;
95 default:
96 value.u64 = v;
97 break;
98 }
99 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 /** Initialize the union with a U8 pixel value
101 *
102 * @param[in] v U8 value.
103 */
104 PixelValue(uint8_t v)
105 : PixelValue()
106 {
107 value.u8 = v;
108 }
109 /** Initialize the union with a U16 pixel value
110 *
111 * @param[in] v U16 value.
112 */
113 PixelValue(uint16_t v)
114 : PixelValue()
115 {
116 value.u16 = v;
117 }
118 /** Initialize the union with a S16 pixel value
119 *
120 * @param[in] v S16 value.
121 */
122 PixelValue(int16_t v)
123 : PixelValue()
124 {
125 value.s16 = v;
126 }
127 /** Initialize the union with a U32 pixel value
128 *
129 * @param[in] v U32 value.
130 */
131 PixelValue(uint32_t v)
132 : PixelValue()
133 {
134 value.u32 = v;
135 }
136 /** Initialize the union with a S32 pixel value
137 *
138 * @param[in] v S32 value.
139 */
140 PixelValue(int32_t v)
141 : PixelValue()
142 {
143 value.s32 = v;
144 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100145
146 /** Initialize the union with a U64 pixel value
147 *
148 * @param[in] v U64 value.
149 */
150 PixelValue(uint64_t v)
151 : PixelValue()
152 {
153 value.u64 = v;
154 }
155 /** Initialize the union with a S64 pixel value
156 *
157 * @param[in] v S64 value.
158 */
159 PixelValue(int64_t v)
160 : PixelValue()
161 {
162 value.s64 = v;
163 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100164 /** Initialize the union with a F16 pixel value
165 *
166 * @param[in] v F16 value.
167 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100168 PixelValue(half v)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100169 : PixelValue()
170 {
171 value.f16 = v;
172 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 /** Initialize the union with a F32 pixel value
174 *
175 * @param[in] v F32 value.
176 */
177 PixelValue(float v)
178 : PixelValue()
179 {
180 value.f32 = v;
181 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100182 /** Initialize the union with a F64 pixel value
183 *
184 * @param[in] v F64 value.
185 */
186 PixelValue(double v)
187 : PixelValue()
188 {
189 value.f64 = v;
190 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191 /** Union which describes the value of a pixel for any image format.
192 * Use the field corresponding to the image format
193 */
194 union
195 {
giuros0191a73f32018-09-21 12:23:44 +0100196 uint64_t u64; /**< Single channel U64 */
197 int64_t s64; /**< Single channel S64 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100198 uint8_t rgb[3]; /**< 3 channels: RGB888 */
199 uint8_t yuv[3]; /**< 3 channels: Any YUV format */
200 uint8_t rgbx[4]; /**< 4 channels: RGBX8888 */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100201 double f64; /**< Single channel double */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100202 float f32; /**< Single channel float 32 */
203 half f16; /**< Single channel F16 */
204 uint8_t u8; /**< Single channel U8 */
205 int8_t s8; /**< Single channel S8 */
206 uint16_t u16; /**< Single channel U16 */
207 int16_t s16; /**< Single channel S16 */
208 uint32_t u32; /**< Single channel U32 */
209 int32_t s32; /**< Single channel S32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210 } value;
211 /** Interpret the pixel value as a U8
212 *
213 * @param[out] v Returned value
214 */
215 void get(uint8_t &v) const
216 {
217 v = value.u8;
218 }
219 /** Interpret the pixel value as a S8
220 *
221 * @param[out] v Returned value
222 */
223 void get(int8_t &v) const
224 {
225 v = value.s8;
226 }
227 /** Interpret the pixel value as a U16
228 *
229 * @param[out] v Returned value
230 */
231 void get(uint16_t &v) const
232 {
233 v = value.u16;
234 }
235 /** Interpret the pixel value as a S16
236 *
237 * @param[out] v Returned value
238 */
239 void get(int16_t &v) const
240 {
241 v = value.s16;
242 }
243 /** Interpret the pixel value as a U32
244 *
245 * @param[out] v Returned value
246 */
247 void get(uint32_t &v) const
248 {
249 v = value.u32;
250 }
251 /** Interpret the pixel value as a S32
252 *
253 * @param[out] v Returned value
254 */
255 void get(int32_t &v) const
256 {
257 v = value.s32;
258 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100259 /** Interpret the pixel value as a U64
260 *
261 * @param[out] v Returned value
262 */
263 void get(uint64_t &v) const
264 {
265 v = value.u64;
266 }
267 /** Interpret the pixel value as a S64
268 *
269 * @param[out] v Returned value
270 */
271 void get(int64_t &v) const
272 {
273 v = value.s64;
274 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100275 /** Interpret the pixel value as a F16
276 *
277 * @param[out] v Returned value
278 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100279 void get(half &v) const
Pablo Tello0c34fe22017-06-26 17:17:42 +0100280 {
281 v = value.f16;
282 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100283 /** Interpret the pixel value as a F32
284 *
285 * @param[out] v Returned value
286 */
287 void get(float &v) const
288 {
289 v = value.f32;
290 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100291 /** Interpret the pixel value as a double
292 *
293 * @param[out] v Returned value
294 */
295 void get(double &v) const
296 {
297 v = value.f64;
298 }
299 /** Get the pixel value
300 *
301 * @return Pixel value
302 */
303 template <typename T>
304 T get() const
305 {
306 T val;
307 get(val);
308 return val;
309 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100310};
311}
312#endif /* __ARM_COMPUTE_PIXELVALUE_H__ */