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