blob: 1b1a5a384528f426fa105251017403d68eac4ac9 [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
27#include <cstdint>
28
Pablo Tello0c34fe22017-06-26 17:17:42 +010029#if ARM_COMPUTE_ENABLE_FP16
30#include <arm_fp16.h> // needed for float16_t
31#endif /* ARM_COMPUTE_ENABLE_FP16 */
32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033namespace arm_compute
34{
35/** Class describing the value of a pixel for any image format. */
36class PixelValue
37{
38public:
39 /** Default constructor: value initialized to 0 */
40 PixelValue()
41 : value{ { 0 } }
42 {
43 }
44 /** Initialize the union with a U8 pixel value
45 *
46 * @param[in] v U8 value.
47 */
48 PixelValue(uint8_t v)
49 : PixelValue()
50 {
51 value.u8 = v;
52 }
53 /** Initialize the union with a U16 pixel value
54 *
55 * @param[in] v U16 value.
56 */
57 PixelValue(uint16_t v)
58 : PixelValue()
59 {
60 value.u16 = v;
61 }
62 /** Initialize the union with a S16 pixel value
63 *
64 * @param[in] v S16 value.
65 */
66 PixelValue(int16_t v)
67 : PixelValue()
68 {
69 value.s16 = v;
70 }
71 /** Initialize the union with a U32 pixel value
72 *
73 * @param[in] v U32 value.
74 */
75 PixelValue(uint32_t v)
76 : PixelValue()
77 {
78 value.u32 = v;
79 }
80 /** Initialize the union with a S32 pixel value
81 *
82 * @param[in] v S32 value.
83 */
84 PixelValue(int32_t v)
85 : PixelValue()
86 {
87 value.s32 = v;
88 }
Pablo Tello0c34fe22017-06-26 17:17:42 +010089#if ARM_COMPUTE_ENABLE_FP16
90 /** Initialize the union with a F16 pixel value
91 *
92 * @param[in] v F16 value.
93 */
94 PixelValue(float16_t v)
95 : PixelValue()
96 {
97 value.f16 = v;
98 }
99#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 /** Initialize the union with a F32 pixel value
101 *
102 * @param[in] v F32 value.
103 */
104 PixelValue(float v)
105 : PixelValue()
106 {
107 value.f32 = v;
108 }
109 /** Union which describes the value of a pixel for any image format.
110 * Use the field corresponding to the image format
111 */
112 union
113 {
Pablo Tello0c34fe22017-06-26 17:17:42 +0100114 uint8_t rgb[3]; /**< 3 channels: RGB888 */
115 uint8_t yuv[3]; /**< 3 channels: Any YUV format */
116 uint8_t rgbx[4]; /**< 4 channels: RGBX8888 */
117 float f32; /**< Single channel float 32 */
118#if ARM_COMPUTE_ENABLE_FP16
119 float16_t f16; /**< Single channel F16 */
120#endif /* ARM_COMPUTE_ENABLE_FP16 */
121 uint8_t u8; /**< Single channel U8 */
122 int8_t s8; /**< Single channel S8 */
123 uint16_t u16; /**< Single channel U16 */
124 int16_t s16; /**< Single channel S16 */
125 uint32_t u32; /**< Single channel U32 */
126 int32_t s32; /**< Single channel S32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 } value;
128 /** Interpret the pixel value as a U8
129 *
130 * @param[out] v Returned value
131 */
132 void get(uint8_t &v) const
133 {
134 v = value.u8;
135 }
136 /** Interpret the pixel value as a S8
137 *
138 * @param[out] v Returned value
139 */
140 void get(int8_t &v) const
141 {
142 v = value.s8;
143 }
144 /** Interpret the pixel value as a U16
145 *
146 * @param[out] v Returned value
147 */
148 void get(uint16_t &v) const
149 {
150 v = value.u16;
151 }
152 /** Interpret the pixel value as a S16
153 *
154 * @param[out] v Returned value
155 */
156 void get(int16_t &v) const
157 {
158 v = value.s16;
159 }
160 /** Interpret the pixel value as a U32
161 *
162 * @param[out] v Returned value
163 */
164 void get(uint32_t &v) const
165 {
166 v = value.u32;
167 }
168 /** Interpret the pixel value as a S32
169 *
170 * @param[out] v Returned value
171 */
172 void get(int32_t &v) const
173 {
174 v = value.s32;
175 }
Pablo Tello0c34fe22017-06-26 17:17:42 +0100176#if ARM_COMPUTE_ENABLE_FP16
177 /** Interpret the pixel value as a F16
178 *
179 * @param[out] v Returned value
180 */
181 void get(float16_t &v) const
182 {
183 v = value.f16;
184 }
185#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186 /** Interpret the pixel value as a F32
187 *
188 * @param[out] v Returned value
189 */
190 void get(float &v) const
191 {
192 v = value.f32;
193 }
194};
195}
196#endif /* __ARM_COMPUTE_PIXELVALUE_H__ */