blob: e23749892080ae5df47a3daf03a769b90a69921d [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
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010046 * @param[in] qinfo (Optional) QuantizationInfo to apply in case of quantized data types 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;
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +010071 case DataType::QASYMM16:
72 value.u16 = quantize_qasymm16(static_cast<uint16_t>(v), qinfo);
73 break;
Manuel Bottini3689fcd2019-06-14 17:18:12 +010074 case DataType::QSYMM16:
75 value.s16 = quantize_qsymm16(static_cast<int16_t>(v), qinfo);
76 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000077 case DataType::U32:
78 value.u32 = static_cast<uint32_t>(v);
79 break;
80 case DataType::S32:
81 value.s32 = static_cast<int32_t>(v);
82 break;
83 case DataType::U64:
84 value.u64 = static_cast<uint64_t>(v);
85 break;
86 case DataType::S64:
87 value.s16 = static_cast<int64_t>(v);
88 break;
89 case DataType::F16:
90 value.f16 = static_cast<half>(v);
91 break;
92 case DataType::F32:
93 value.f32 = static_cast<float>(v);
94 break;
95 case DataType::F64:
96 value.f64 = static_cast<double>(v);
97 break;
98 default:
99 value.u64 = v;
100 break;
101 }
102 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 /** Initialize the union with a U8 pixel value
104 *
105 * @param[in] v U8 value.
106 */
107 PixelValue(uint8_t v)
108 : PixelValue()
109 {
110 value.u8 = v;
111 }
112 /** Initialize the union with a U16 pixel value
113 *
114 * @param[in] v U16 value.
115 */
116 PixelValue(uint16_t v)
117 : PixelValue()
118 {
119 value.u16 = v;
120 }
121 /** Initialize the union with a S16 pixel value
122 *
123 * @param[in] v S16 value.
124 */
125 PixelValue(int16_t v)
126 : PixelValue()
127 {
128 value.s16 = v;
129 }
130 /** Initialize the union with a U32 pixel value
131 *
132 * @param[in] v U32 value.
133 */
134 PixelValue(uint32_t v)
135 : PixelValue()
136 {
137 value.u32 = v;
138 }
139 /** Initialize the union with a S32 pixel value
140 *
141 * @param[in] v S32 value.
142 */
143 PixelValue(int32_t v)
144 : PixelValue()
145 {
146 value.s32 = v;
147 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100148
149 /** Initialize the union with a U64 pixel value
150 *
151 * @param[in] v U64 value.
152 */
153 PixelValue(uint64_t v)
154 : PixelValue()
155 {
156 value.u64 = v;
157 }
158 /** Initialize the union with a S64 pixel value
159 *
160 * @param[in] v S64 value.
161 */
162 PixelValue(int64_t v)
163 : PixelValue()
164 {
165 value.s64 = v;
166 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100167 /** Initialize the union with a F16 pixel value
168 *
169 * @param[in] v F16 value.
170 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100171 PixelValue(half v)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100172 : PixelValue()
173 {
174 value.f16 = v;
175 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 /** Initialize the union with a F32 pixel value
177 *
178 * @param[in] v F32 value.
179 */
180 PixelValue(float v)
181 : PixelValue()
182 {
183 value.f32 = v;
184 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100185 /** Initialize the union with a F64 pixel value
186 *
187 * @param[in] v F64 value.
188 */
189 PixelValue(double v)
190 : PixelValue()
191 {
192 value.f64 = v;
193 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 /** Union which describes the value of a pixel for any image format.
195 * Use the field corresponding to the image format
196 */
197 union
198 {
giuros0191a73f32018-09-21 12:23:44 +0100199 uint64_t u64; /**< Single channel U64 */
200 int64_t s64; /**< Single channel S64 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100201 uint8_t rgb[3]; /**< 3 channels: RGB888 */
202 uint8_t yuv[3]; /**< 3 channels: Any YUV format */
203 uint8_t rgbx[4]; /**< 4 channels: RGBX8888 */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100204 double f64; /**< Single channel double */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100205 float f32; /**< Single channel float 32 */
206 half f16; /**< Single channel F16 */
207 uint8_t u8; /**< Single channel U8 */
208 int8_t s8; /**< Single channel S8 */
209 uint16_t u16; /**< Single channel U16 */
210 int16_t s16; /**< Single channel S16 */
211 uint32_t u32; /**< Single channel U32 */
212 int32_t s32; /**< Single channel S32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 } value;
214 /** Interpret the pixel value as a U8
215 *
216 * @param[out] v Returned value
217 */
218 void get(uint8_t &v) const
219 {
220 v = value.u8;
221 }
222 /** Interpret the pixel value as a S8
223 *
224 * @param[out] v Returned value
225 */
226 void get(int8_t &v) const
227 {
228 v = value.s8;
229 }
230 /** Interpret the pixel value as a U16
231 *
232 * @param[out] v Returned value
233 */
234 void get(uint16_t &v) const
235 {
236 v = value.u16;
237 }
238 /** Interpret the pixel value as a S16
239 *
240 * @param[out] v Returned value
241 */
242 void get(int16_t &v) const
243 {
244 v = value.s16;
245 }
246 /** Interpret the pixel value as a U32
247 *
248 * @param[out] v Returned value
249 */
250 void get(uint32_t &v) const
251 {
252 v = value.u32;
253 }
254 /** Interpret the pixel value as a S32
255 *
256 * @param[out] v Returned value
257 */
258 void get(int32_t &v) const
259 {
260 v = value.s32;
261 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100262 /** Interpret the pixel value as a U64
263 *
264 * @param[out] v Returned value
265 */
266 void get(uint64_t &v) const
267 {
268 v = value.u64;
269 }
270 /** Interpret the pixel value as a S64
271 *
272 * @param[out] v Returned value
273 */
274 void get(int64_t &v) const
275 {
276 v = value.s64;
277 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100278 /** Interpret the pixel value as a F16
279 *
280 * @param[out] v Returned value
281 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100282 void get(half &v) const
Pablo Tello0c34fe22017-06-26 17:17:42 +0100283 {
284 v = value.f16;
285 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100286 /** Interpret the pixel value as a F32
287 *
288 * @param[out] v Returned value
289 */
290 void get(float &v) const
291 {
292 v = value.f32;
293 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100294 /** Interpret the pixel value as a double
295 *
296 * @param[out] v Returned value
297 */
298 void get(double &v) const
299 {
300 v = value.f64;
301 }
302 /** Get the pixel value
303 *
304 * @return Pixel value
305 */
306 template <typename T>
307 T get() const
308 {
309 T val;
310 get(val);
311 return val;
312 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100313};
314}
315#endif /* __ARM_COMPUTE_PIXELVALUE_H__ */