blob: ce3726b6fce35a55b4c0f7d05c021ba1561add64 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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
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()
39 : value{ { 0 } }
40 {
41 }
42 /** Initialize the union with a U8 pixel value
43 *
44 * @param[in] v U8 value.
45 */
46 PixelValue(uint8_t v)
47 : PixelValue()
48 {
49 value.u8 = v;
50 }
51 /** Initialize the union with a U16 pixel value
52 *
53 * @param[in] v U16 value.
54 */
55 PixelValue(uint16_t v)
56 : PixelValue()
57 {
58 value.u16 = v;
59 }
60 /** Initialize the union with a S16 pixel value
61 *
62 * @param[in] v S16 value.
63 */
64 PixelValue(int16_t v)
65 : PixelValue()
66 {
67 value.s16 = v;
68 }
69 /** Initialize the union with a U32 pixel value
70 *
71 * @param[in] v U32 value.
72 */
73 PixelValue(uint32_t v)
74 : PixelValue()
75 {
76 value.u32 = v;
77 }
78 /** Initialize the union with a S32 pixel value
79 *
80 * @param[in] v S32 value.
81 */
82 PixelValue(int32_t v)
83 : PixelValue()
84 {
85 value.s32 = v;
86 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +010087
88 /** Initialize the union with a U64 pixel value
89 *
90 * @param[in] v U64 value.
91 */
92 PixelValue(uint64_t v)
93 : PixelValue()
94 {
95 value.u64 = v;
96 }
97 /** Initialize the union with a S64 pixel value
98 *
99 * @param[in] v S64 value.
100 */
101 PixelValue(int64_t v)
102 : PixelValue()
103 {
104 value.s64 = v;
105 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100106 /** Initialize the union with a F16 pixel value
107 *
108 * @param[in] v F16 value.
109 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100110 PixelValue(half v)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100111 : PixelValue()
112 {
113 value.f16 = v;
114 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 /** Initialize the union with a F32 pixel value
116 *
117 * @param[in] v F32 value.
118 */
119 PixelValue(float v)
120 : PixelValue()
121 {
122 value.f32 = v;
123 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100124 /** Initialize the union with a F64 pixel value
125 *
126 * @param[in] v F64 value.
127 */
128 PixelValue(double v)
129 : PixelValue()
130 {
131 value.f64 = v;
132 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 /** Union which describes the value of a pixel for any image format.
134 * Use the field corresponding to the image format
135 */
136 union
137 {
Georgios Pinitas583137c2017-08-31 18:12:42 +0100138 uint8_t rgb[3]; /**< 3 channels: RGB888 */
139 uint8_t yuv[3]; /**< 3 channels: Any YUV format */
140 uint8_t rgbx[4]; /**< 4 channels: RGBX8888 */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100141 double f64; /**< Single channel double */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100142 float f32; /**< Single channel float 32 */
143 half f16; /**< Single channel F16 */
144 uint8_t u8; /**< Single channel U8 */
145 int8_t s8; /**< Single channel S8 */
146 uint16_t u16; /**< Single channel U16 */
147 int16_t s16; /**< Single channel S16 */
148 uint32_t u32; /**< Single channel U32 */
149 int32_t s32; /**< Single channel S32 */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100150 uint64_t u64; /**< Single channel U64 */
151 int64_t s64; /**< Single channel S64 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 } value;
153 /** Interpret the pixel value as a U8
154 *
155 * @param[out] v Returned value
156 */
157 void get(uint8_t &v) const
158 {
159 v = value.u8;
160 }
161 /** Interpret the pixel value as a S8
162 *
163 * @param[out] v Returned value
164 */
165 void get(int8_t &v) const
166 {
167 v = value.s8;
168 }
169 /** Interpret the pixel value as a U16
170 *
171 * @param[out] v Returned value
172 */
173 void get(uint16_t &v) const
174 {
175 v = value.u16;
176 }
177 /** Interpret the pixel value as a S16
178 *
179 * @param[out] v Returned value
180 */
181 void get(int16_t &v) const
182 {
183 v = value.s16;
184 }
185 /** Interpret the pixel value as a U32
186 *
187 * @param[out] v Returned value
188 */
189 void get(uint32_t &v) const
190 {
191 v = value.u32;
192 }
193 /** Interpret the pixel value as a S32
194 *
195 * @param[out] v Returned value
196 */
197 void get(int32_t &v) const
198 {
199 v = value.s32;
200 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100201 /** Interpret the pixel value as a U64
202 *
203 * @param[out] v Returned value
204 */
205 void get(uint64_t &v) const
206 {
207 v = value.u64;
208 }
209 /** Interpret the pixel value as a S64
210 *
211 * @param[out] v Returned value
212 */
213 void get(int64_t &v) const
214 {
215 v = value.s64;
216 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100217 /** Interpret the pixel value as a F16
218 *
219 * @param[out] v Returned value
220 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100221 void get(half &v) const
Pablo Tello0c34fe22017-06-26 17:17:42 +0100222 {
223 v = value.f16;
224 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225 /** Interpret the pixel value as a F32
226 *
227 * @param[out] v Returned value
228 */
229 void get(float &v) const
230 {
231 v = value.f32;
232 }
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100233 /** Interpret the pixel value as a double
234 *
235 * @param[out] v Returned value
236 */
237 void get(double &v) const
238 {
239 v = value.f64;
240 }
241 /** Get the pixel value
242 *
243 * @return Pixel value
244 */
245 template <typename T>
246 T get() const
247 {
248 T val;
249 get(val);
250 return val;
251 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252};
253}
254#endif /* __ARM_COMPUTE_PIXELVALUE_H__ */