xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/core/PixelValue.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2016-2021 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. */
34 class PixelValue
35 {
36 public:
37     /** Default constructor: value initialized to 0 */
PixelValue()38     PixelValue() noexcept
39         : value
40     {
41         int64_t(0)
42     }
43     {
44     }
45     /** Initialize the union with a pixel value of chosen datatype
46      *
47      * @param[in] v        value.
48      * @param[in] datatype DataType that @p v have to be stored
49      * @param[in] qinfo    (Optional) QuantizationInfo to apply in case of quantized data types to @p v
50      */
51     PixelValue(double v, DataType datatype, QuantizationInfo qinfo = QuantizationInfo())
PixelValue()52         : PixelValue()
53     {
54         switch(datatype)
55         {
56             case DataType::U8:
57                 value.u8 = static_cast<uint8_t>(v);
58                 break;
59             case DataType::S8:
60                 value.s8 = static_cast<int8_t>(v);
61                 break;
62             case DataType::QASYMM8:
63                 value.u8 = quantize_qasymm8(static_cast<float>(v), qinfo);
64                 break;
65             case DataType::QASYMM8_SIGNED:
66                 value.s8 = quantize_qasymm8_signed(static_cast<float>(v), qinfo);
67                 break;
68             case DataType::QSYMM8:
69                 value.s8 = quantize_qsymm8(static_cast<float>(v), qinfo);
70                 break;
71             case DataType::U16:
72                 value.u16 = static_cast<uint16_t>(v);
73                 break;
74             case DataType::S16:
75                 value.s16 = static_cast<int16_t>(v);
76                 break;
77             case DataType::QASYMM16:
78                 value.u16 = quantize_qasymm16(static_cast<float>(v), qinfo);
79                 break;
80             case DataType::QSYMM16:
81                 value.s16 = quantize_qsymm16(static_cast<float>(v), qinfo);
82                 break;
83             case DataType::U32:
84                 value.u32 = static_cast<uint32_t>(v);
85                 break;
86             case DataType::S32:
87                 value.s32 = static_cast<int32_t>(v);
88                 break;
89             case DataType::U64:
90                 value.u64 = static_cast<uint64_t>(v);
91                 break;
92             case DataType::S64:
93                 value.s64 = static_cast<int64_t>(v);
94                 break;
95             case DataType::BFLOAT16:
96                 value.bf16 = static_cast<bfloat16>(v);
97                 break;
98             case DataType::F16:
99                 value.f16 = static_cast<half>(v);
100                 break;
101             case DataType::F32:
102                 value.f32 = static_cast<float>(v);
103                 break;
104             case DataType::F64:
105             default:
106                 value.f64 = v;
107                 break;
108         }
109     }
110     /** Initialize the union with a S8 pixel value
111      *
112      * @param[in] v S8 value.
113      */
PixelValue(int8_t v)114     PixelValue(int8_t v)
115         : PixelValue()
116     {
117         value.s8 = v;
118     }
119     /** Initialize the union with a U8 pixel value
120      *
121      * @param[in] v U8 value.
122      */
PixelValue(uint8_t v)123     PixelValue(uint8_t v)
124         : PixelValue()
125     {
126         value.u8 = v;
127     }
128     /** Initialize the union with a U16 pixel value
129      *
130      * @param[in] v U16 value.
131      */
PixelValue(uint16_t v)132     PixelValue(uint16_t v)
133         : PixelValue()
134     {
135         value.u16 = v;
136     }
137     /** Initialize the union with a S16 pixel value
138      *
139      * @param[in] v S16 value.
140      */
PixelValue(int16_t v)141     PixelValue(int16_t v)
142         : PixelValue()
143     {
144         value.s16 = v;
145     }
146     /** Initialize the union with a U32 pixel value
147      *
148      * @param[in] v U32 value.
149      */
PixelValue(uint32_t v)150     PixelValue(uint32_t v)
151         : PixelValue()
152     {
153         value.u32 = v;
154     }
155     /** Initialize the union with a S32 pixel value
156      *
157      * @param[in] v S32 value.
158      */
PixelValue(int32_t v)159     PixelValue(int32_t v)
160         : PixelValue()
161     {
162         value.s32 = v;
163     }
164 
165     /** Initialize the union with a U64 pixel value
166      *
167      * @param[in] v U64 value.
168      */
PixelValue(uint64_t v)169     PixelValue(uint64_t v)
170         : PixelValue()
171     {
172         value.u64 = v;
173     }
174     /** Initialize the union with a S64 pixel value
175      *
176      * @param[in] v S64 value.
177      */
PixelValue(int64_t v)178     PixelValue(int64_t v)
179         : PixelValue()
180     {
181         value.s64 = v;
182     }
183     /** Initialize the union with a BFLOAT16 pixel value
184      *
185      * @param[in] v F16 value.
186      */
PixelValue(bfloat16 v)187     PixelValue(bfloat16 v)
188         : PixelValue()
189     {
190         value.bf16 = v;
191     }
192     /** Initialize the union with a F16 pixel value
193      *
194      * @param[in] v F16 value.
195      */
PixelValue(half v)196     PixelValue(half v)
197         : PixelValue()
198     {
199         value.f16 = v;
200     }
201     /** Initialize the union with a F32 pixel value
202      *
203      * @param[in] v F32 value.
204      */
PixelValue(float v)205     PixelValue(float v)
206         : PixelValue()
207     {
208         value.f32 = v;
209     }
210     /** Initialize the union with a F64 pixel value
211      *
212      * @param[in] v F64 value.
213      */
PixelValue(double v)214     PixelValue(double v)
215         : PixelValue()
216     {
217         value.f64 = v;
218     }
219     /** Union which describes the value of a pixel for any image format.
220      * Use the field corresponding to the image format
221      */
222     union
223         {
224             uint64_t u64;     /**< Single channel U64 */
225             int64_t  s64;     /**< Single channel S64 */
226             uint8_t  rgb[3];  /**< 3 channels: RGB888 */
227             uint8_t  yuv[3];  /**< 3 channels: Any YUV format */
228             uint8_t  rgbx[4]; /**< 4 channels: RGBX8888 */
229             double   f64;     /**< Single channel double */
230             float    f32;     /**< Single channel float 32 */
231             half     f16;     /**< Single channel F16 */
232             bfloat16 bf16;    /**< Single channel brain floating-point number */
233             uint8_t  u8;      /**< Single channel U8 */
234             int8_t   s8;      /**< Single channel S8 */
235             uint16_t u16;     /**< Single channel U16 */
236             int16_t  s16;     /**< Single channel S16 */
237             uint32_t u32;     /**< Single channel U32 */
238             int32_t  s32;     /**< Single channel S32 */
239         } value;
240     /** Interpret the pixel value as a U8
241      *
242      * @param[out] v Returned value
243      */
get(uint8_t & v)244     void get(uint8_t &v) const
245     {
246         v = value.u8;
247     }
248     /** Interpret the pixel value as a S8
249      *
250      * @param[out] v Returned value
251      */
get(int8_t & v)252     void get(int8_t &v) const
253     {
254         v = value.s8;
255     }
256     /** Interpret the pixel value as a U16
257      *
258      * @param[out] v Returned value
259      */
get(uint16_t & v)260     void get(uint16_t &v) const
261     {
262         v = value.u16;
263     }
264     /** Interpret the pixel value as a S16
265      *
266      * @param[out] v Returned value
267      */
get(int16_t & v)268     void get(int16_t &v) const
269     {
270         v = value.s16;
271     }
272     /** Interpret the pixel value as a U32
273      *
274      * @param[out] v Returned value
275      */
get(uint32_t & v)276     void get(uint32_t &v) const
277     {
278         v = value.u32;
279     }
280     /** Interpret the pixel value as a S32
281      *
282      * @param[out] v Returned value
283      */
get(int32_t & v)284     void get(int32_t &v) const
285     {
286         v = value.s32;
287     }
288     /** Interpret the pixel value as a U64
289      *
290      * @param[out] v Returned value
291      */
get(uint64_t & v)292     void get(uint64_t &v) const
293     {
294         v = value.u64;
295     }
296     /** Interpret the pixel value as a S64
297      *
298      * @param[out] v Returned value
299      */
get(int64_t & v)300     void get(int64_t &v) const
301     {
302         v = value.s64;
303     }
304     /** Interpret the pixel value as a BFLOAT16
305      *
306      * @param[out] v Returned value
307      */
get(bfloat16 & v)308     void get(bfloat16 &v) const
309     {
310         v = value.bf16;
311     }
312     /** Interpret the pixel value as a F16
313      *
314      * @param[out] v Returned value
315      */
get(half & v)316     void get(half &v) const
317     {
318         v = value.f16;
319     }
320     /** Interpret the pixel value as a F32
321      *
322      * @param[out] v Returned value
323      */
get(float & v)324     void get(float &v) const
325     {
326         v = value.f32;
327     }
328     /** Interpret the pixel value as a double
329      *
330      * @param[out] v Returned value
331      */
get(double & v)332     void get(double &v) const
333     {
334         v = value.f64;
335     }
336     /** Get the pixel value
337      *
338      * @return Pixel value
339      */
340     template <typename T>
get()341     T get() const
342     {
343         T val;
344         get(val);
345         return val;
346     }
347 };
348 } // namespace arm_compute
349 #endif /* ARM_COMPUTE_PIXELVALUE_H */
350