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