Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Types.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 29 | #include <cstdint> |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 30 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 */ |
| 38 | PixelValue() |
giuros01 | 91a73f3 | 2018-09-21 12:23:44 +0100 | [diff] [blame] | 39 | : value{ uint64_t(0) } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | { |
| 41 | } |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 42 | /** Initialize the union with a pixel value of chosen datatype |
| 43 | * |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame^] | 44 | * @param[in] v int value. |
| 45 | * @param[in] datatype DataType that @p v have to be stored |
| 46 | * @param[in] quant_info QuantizationInfo to apply in case of QASYMM8 datatype to @p v |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 47 | */ |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame^] | 48 | PixelValue(uint64_t v, DataType datatype, QuantizationInfo quant_info = QuantizationInfo()) |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 49 | : 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame^] | 59 | case DataType::QASYMM8: |
| 60 | value.u8 = sqcvt_qasymm8_f32(v, quant_info.scale, quant_info.offset); |
| 61 | break; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 62 | case DataType::U16: |
| 63 | value.u16 = static_cast<uint16_t>(v); |
| 64 | break; |
| 65 | case DataType::S16: |
| 66 | value.s16 = static_cast<int16_t>(v); |
| 67 | break; |
| 68 | case DataType::U32: |
| 69 | value.u32 = static_cast<uint32_t>(v); |
| 70 | break; |
| 71 | case DataType::S32: |
| 72 | value.s32 = static_cast<int32_t>(v); |
| 73 | break; |
| 74 | case DataType::U64: |
| 75 | value.u64 = static_cast<uint64_t>(v); |
| 76 | break; |
| 77 | case DataType::S64: |
| 78 | value.s16 = static_cast<int64_t>(v); |
| 79 | break; |
| 80 | case DataType::F16: |
| 81 | value.f16 = static_cast<half>(v); |
| 82 | break; |
| 83 | case DataType::F32: |
| 84 | value.f32 = static_cast<float>(v); |
| 85 | break; |
| 86 | case DataType::F64: |
| 87 | value.f64 = static_cast<double>(v); |
| 88 | break; |
| 89 | default: |
| 90 | value.u64 = v; |
| 91 | break; |
| 92 | } |
| 93 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | /** Initialize the union with a U8 pixel value |
| 95 | * |
| 96 | * @param[in] v U8 value. |
| 97 | */ |
| 98 | PixelValue(uint8_t v) |
| 99 | : PixelValue() |
| 100 | { |
| 101 | value.u8 = v; |
| 102 | } |
| 103 | /** Initialize the union with a U16 pixel value |
| 104 | * |
| 105 | * @param[in] v U16 value. |
| 106 | */ |
| 107 | PixelValue(uint16_t v) |
| 108 | : PixelValue() |
| 109 | { |
| 110 | value.u16 = v; |
| 111 | } |
| 112 | /** Initialize the union with a S16 pixel value |
| 113 | * |
| 114 | * @param[in] v S16 value. |
| 115 | */ |
| 116 | PixelValue(int16_t v) |
| 117 | : PixelValue() |
| 118 | { |
| 119 | value.s16 = v; |
| 120 | } |
| 121 | /** Initialize the union with a U32 pixel value |
| 122 | * |
| 123 | * @param[in] v U32 value. |
| 124 | */ |
| 125 | PixelValue(uint32_t v) |
| 126 | : PixelValue() |
| 127 | { |
| 128 | value.u32 = v; |
| 129 | } |
| 130 | /** Initialize the union with a S32 pixel value |
| 131 | * |
| 132 | * @param[in] v S32 value. |
| 133 | */ |
| 134 | PixelValue(int32_t v) |
| 135 | : PixelValue() |
| 136 | { |
| 137 | value.s32 = v; |
| 138 | } |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 139 | |
| 140 | /** Initialize the union with a U64 pixel value |
| 141 | * |
| 142 | * @param[in] v U64 value. |
| 143 | */ |
| 144 | PixelValue(uint64_t v) |
| 145 | : PixelValue() |
| 146 | { |
| 147 | value.u64 = v; |
| 148 | } |
| 149 | /** Initialize the union with a S64 pixel value |
| 150 | * |
| 151 | * @param[in] v S64 value. |
| 152 | */ |
| 153 | PixelValue(int64_t v) |
| 154 | : PixelValue() |
| 155 | { |
| 156 | value.s64 = v; |
| 157 | } |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 158 | /** Initialize the union with a F16 pixel value |
| 159 | * |
| 160 | * @param[in] v F16 value. |
| 161 | */ |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 162 | PixelValue(half v) |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 163 | : PixelValue() |
| 164 | { |
| 165 | value.f16 = v; |
| 166 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | /** Initialize the union with a F32 pixel value |
| 168 | * |
| 169 | * @param[in] v F32 value. |
| 170 | */ |
| 171 | PixelValue(float v) |
| 172 | : PixelValue() |
| 173 | { |
| 174 | value.f32 = v; |
| 175 | } |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 176 | /** Initialize the union with a F64 pixel value |
| 177 | * |
| 178 | * @param[in] v F64 value. |
| 179 | */ |
| 180 | PixelValue(double v) |
| 181 | : PixelValue() |
| 182 | { |
| 183 | value.f64 = v; |
| 184 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | /** Union which describes the value of a pixel for any image format. |
| 186 | * Use the field corresponding to the image format |
| 187 | */ |
| 188 | union |
| 189 | { |
giuros01 | 91a73f3 | 2018-09-21 12:23:44 +0100 | [diff] [blame] | 190 | uint64_t u64; /**< Single channel U64 */ |
| 191 | int64_t s64; /**< Single channel S64 */ |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 192 | uint8_t rgb[3]; /**< 3 channels: RGB888 */ |
| 193 | uint8_t yuv[3]; /**< 3 channels: Any YUV format */ |
| 194 | uint8_t rgbx[4]; /**< 4 channels: RGBX8888 */ |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 195 | double f64; /**< Single channel double */ |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 196 | float f32; /**< Single channel float 32 */ |
| 197 | half f16; /**< Single channel F16 */ |
| 198 | uint8_t u8; /**< Single channel U8 */ |
| 199 | int8_t s8; /**< Single channel S8 */ |
| 200 | uint16_t u16; /**< Single channel U16 */ |
| 201 | int16_t s16; /**< Single channel S16 */ |
| 202 | uint32_t u32; /**< Single channel U32 */ |
| 203 | int32_t s32; /**< Single channel S32 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 204 | } value; |
| 205 | /** Interpret the pixel value as a U8 |
| 206 | * |
| 207 | * @param[out] v Returned value |
| 208 | */ |
| 209 | void get(uint8_t &v) const |
| 210 | { |
| 211 | v = value.u8; |
| 212 | } |
| 213 | /** Interpret the pixel value as a S8 |
| 214 | * |
| 215 | * @param[out] v Returned value |
| 216 | */ |
| 217 | void get(int8_t &v) const |
| 218 | { |
| 219 | v = value.s8; |
| 220 | } |
| 221 | /** Interpret the pixel value as a U16 |
| 222 | * |
| 223 | * @param[out] v Returned value |
| 224 | */ |
| 225 | void get(uint16_t &v) const |
| 226 | { |
| 227 | v = value.u16; |
| 228 | } |
| 229 | /** Interpret the pixel value as a S16 |
| 230 | * |
| 231 | * @param[out] v Returned value |
| 232 | */ |
| 233 | void get(int16_t &v) const |
| 234 | { |
| 235 | v = value.s16; |
| 236 | } |
| 237 | /** Interpret the pixel value as a U32 |
| 238 | * |
| 239 | * @param[out] v Returned value |
| 240 | */ |
| 241 | void get(uint32_t &v) const |
| 242 | { |
| 243 | v = value.u32; |
| 244 | } |
| 245 | /** Interpret the pixel value as a S32 |
| 246 | * |
| 247 | * @param[out] v Returned value |
| 248 | */ |
| 249 | void get(int32_t &v) const |
| 250 | { |
| 251 | v = value.s32; |
| 252 | } |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 253 | /** Interpret the pixel value as a U64 |
| 254 | * |
| 255 | * @param[out] v Returned value |
| 256 | */ |
| 257 | void get(uint64_t &v) const |
| 258 | { |
| 259 | v = value.u64; |
| 260 | } |
| 261 | /** Interpret the pixel value as a S64 |
| 262 | * |
| 263 | * @param[out] v Returned value |
| 264 | */ |
| 265 | void get(int64_t &v) const |
| 266 | { |
| 267 | v = value.s64; |
| 268 | } |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 269 | /** Interpret the pixel value as a F16 |
| 270 | * |
| 271 | * @param[out] v Returned value |
| 272 | */ |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 273 | void get(half &v) const |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 274 | { |
| 275 | v = value.f16; |
| 276 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 277 | /** Interpret the pixel value as a F32 |
| 278 | * |
| 279 | * @param[out] v Returned value |
| 280 | */ |
| 281 | void get(float &v) const |
| 282 | { |
| 283 | v = value.f32; |
| 284 | } |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 285 | /** Interpret the pixel value as a double |
| 286 | * |
| 287 | * @param[out] v Returned value |
| 288 | */ |
| 289 | void get(double &v) const |
| 290 | { |
| 291 | v = value.f64; |
| 292 | } |
| 293 | /** Get the pixel value |
| 294 | * |
| 295 | * @return Pixel value |
| 296 | */ |
| 297 | template <typename T> |
| 298 | T get() const |
| 299 | { |
| 300 | T val; |
| 301 | get(val); |
| 302 | return val; |
| 303 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 304 | }; |
| 305 | } |
| 306 | #endif /* __ARM_COMPUTE_PIXELVALUE_H__ */ |