blob: e86eeba1217c4f2640835d7c1cf051c1c2360dd6 [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 *
44 * @param[in] v int value.
45 * @param[in] datatype DataType that @p v have to be stored
46 */
47 PixelValue(uint64_t v, DataType datatype)
48 : PixelValue()
49 {
50 switch(datatype)
51 {
52 case DataType::U8:
53 value.u8 = static_cast<uint8_t>(v);
54 break;
55 case DataType::S8:
56 value.s8 = static_cast<int8_t>(v);
57 break;
58 case DataType::U16:
59 value.u16 = static_cast<uint16_t>(v);
60 break;
61 case DataType::S16:
62 value.s16 = static_cast<int16_t>(v);
63 break;
64 case DataType::U32:
65 value.u32 = static_cast<uint32_t>(v);
66 break;
67 case DataType::S32:
68 value.s32 = static_cast<int32_t>(v);
69 break;
70 case DataType::U64:
71 value.u64 = static_cast<uint64_t>(v);
72 break;
73 case DataType::S64:
74 value.s16 = static_cast<int64_t>(v);
75 break;
76 case DataType::F16:
77 value.f16 = static_cast<half>(v);
78 break;
79 case DataType::F32:
80 value.f32 = static_cast<float>(v);
81 break;
82 case DataType::F64:
83 value.f64 = static_cast<double>(v);
84 break;
85 default:
86 value.u64 = v;
87 break;
88 }
89 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 /** Initialize the union with a U8 pixel value
91 *
92 * @param[in] v U8 value.
93 */
94 PixelValue(uint8_t v)
95 : PixelValue()
96 {
97 value.u8 = v;
98 }
99 /** Initialize the union with a U16 pixel value
100 *
101 * @param[in] v U16 value.
102 */
103 PixelValue(uint16_t v)
104 : PixelValue()
105 {
106 value.u16 = v;
107 }
108 /** Initialize the union with a S16 pixel value
109 *
110 * @param[in] v S16 value.
111 */
112 PixelValue(int16_t v)
113 : PixelValue()
114 {
115 value.s16 = v;
116 }
117 /** Initialize the union with a U32 pixel value
118 *
119 * @param[in] v U32 value.
120 */
121 PixelValue(uint32_t v)
122 : PixelValue()
123 {
124 value.u32 = v;
125 }
126 /** Initialize the union with a S32 pixel value
127 *
128 * @param[in] v S32 value.
129 */
130 PixelValue(int32_t v)
131 : PixelValue()
132 {
133 value.s32 = v;
134 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100135
136 /** Initialize the union with a U64 pixel value
137 *
138 * @param[in] v U64 value.
139 */
140 PixelValue(uint64_t v)
141 : PixelValue()
142 {
143 value.u64 = v;
144 }
145 /** Initialize the union with a S64 pixel value
146 *
147 * @param[in] v S64 value.
148 */
149 PixelValue(int64_t v)
150 : PixelValue()
151 {
152 value.s64 = v;
153 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100154 /** Initialize the union with a F16 pixel value
155 *
156 * @param[in] v F16 value.
157 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100158 PixelValue(half v)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100159 : PixelValue()
160 {
161 value.f16 = v;
162 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 /** Initialize the union with a F32 pixel value
164 *
165 * @param[in] v F32 value.
166 */
167 PixelValue(float v)
168 : PixelValue()
169 {
170 value.f32 = v;
171 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100172 /** Initialize the union with a F64 pixel value
173 *
174 * @param[in] v F64 value.
175 */
176 PixelValue(double v)
177 : PixelValue()
178 {
179 value.f64 = v;
180 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 /** Union which describes the value of a pixel for any image format.
182 * Use the field corresponding to the image format
183 */
184 union
185 {
giuros0191a73f32018-09-21 12:23:44 +0100186 uint64_t u64; /**< Single channel U64 */
187 int64_t s64; /**< Single channel S64 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100188 uint8_t rgb[3]; /**< 3 channels: RGB888 */
189 uint8_t yuv[3]; /**< 3 channels: Any YUV format */
190 uint8_t rgbx[4]; /**< 4 channels: RGBX8888 */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100191 double f64; /**< Single channel double */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100192 float f32; /**< Single channel float 32 */
193 half f16; /**< Single channel F16 */
194 uint8_t u8; /**< Single channel U8 */
195 int8_t s8; /**< Single channel S8 */
196 uint16_t u16; /**< Single channel U16 */
197 int16_t s16; /**< Single channel S16 */
198 uint32_t u32; /**< Single channel U32 */
199 int32_t s32; /**< Single channel S32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200 } value;
201 /** Interpret the pixel value as a U8
202 *
203 * @param[out] v Returned value
204 */
205 void get(uint8_t &v) const
206 {
207 v = value.u8;
208 }
209 /** Interpret the pixel value as a S8
210 *
211 * @param[out] v Returned value
212 */
213 void get(int8_t &v) const
214 {
215 v = value.s8;
216 }
217 /** Interpret the pixel value as a U16
218 *
219 * @param[out] v Returned value
220 */
221 void get(uint16_t &v) const
222 {
223 v = value.u16;
224 }
225 /** Interpret the pixel value as a S16
226 *
227 * @param[out] v Returned value
228 */
229 void get(int16_t &v) const
230 {
231 v = value.s16;
232 }
233 /** Interpret the pixel value as a U32
234 *
235 * @param[out] v Returned value
236 */
237 void get(uint32_t &v) const
238 {
239 v = value.u32;
240 }
241 /** Interpret the pixel value as a S32
242 *
243 * @param[out] v Returned value
244 */
245 void get(int32_t &v) const
246 {
247 v = value.s32;
248 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100249 /** Interpret the pixel value as a U64
250 *
251 * @param[out] v Returned value
252 */
253 void get(uint64_t &v) const
254 {
255 v = value.u64;
256 }
257 /** Interpret the pixel value as a S64
258 *
259 * @param[out] v Returned value
260 */
261 void get(int64_t &v) const
262 {
263 v = value.s64;
264 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100265 /** Interpret the pixel value as a F16
266 *
267 * @param[out] v Returned value
268 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100269 void get(half &v) const
Pablo Tello0c34fe22017-06-26 17:17:42 +0100270 {
271 v = value.f16;
272 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100273 /** Interpret the pixel value as a F32
274 *
275 * @param[out] v Returned value
276 */
277 void get(float &v) const
278 {
279 v = value.f32;
280 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100281 /** Interpret the pixel value as a double
282 *
283 * @param[out] v Returned value
284 */
285 void get(double &v) const
286 {
287 v = value.f64;
288 }
289 /** Get the pixel value
290 *
291 * @return Pixel value
292 */
293 template <typename T>
294 T get() const
295 {
296 T val;
297 get(val);
298 return val;
299 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100300};
301}
302#endif /* __ARM_COMPUTE_PIXELVALUE_H__ */