Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | 40efd53 | 2021-03-18 17:32:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +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 __UTILS_IMAGE_LOADER_H__ |
| 25 | #define __UTILS_IMAGE_LOADER_H__ |
| 26 | |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | |
| 32 | #include "utils/Utils.h" |
| 33 | |
| 34 | #pragma GCC diagnostic push |
| 35 | #pragma GCC diagnostic ignored "-Wswitch-default" |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 36 | #pragma GCC diagnostic ignored "-Wstrict-overflow" |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 37 | #include "stb/stb_image.h" |
| 38 | #pragma GCC diagnostic pop |
| 39 | |
| 40 | #include <cstdlib> |
| 41 | #include <memory> |
| 42 | #include <string> |
| 43 | |
| 44 | namespace arm_compute |
| 45 | { |
| 46 | namespace utils |
| 47 | { |
| 48 | /** Image feeder interface */ |
| 49 | class IImageDataFeeder |
| 50 | { |
| 51 | public: |
| 52 | /** Virtual base destructor */ |
| 53 | virtual ~IImageDataFeeder() = default; |
| 54 | /** Gets a character from an image feed */ |
| 55 | virtual uint8_t get() = 0; |
| 56 | /** Feed a whole row to a destination pointer |
| 57 | * |
| 58 | * @param[out] dst Destination pointer |
| 59 | * @param[in] row_size Row size in terms of bytes |
| 60 | */ |
| 61 | virtual void get_row(uint8_t *dst, size_t row_size) = 0; |
| 62 | }; |
| 63 | /** File Image feeder concrete implementation */ |
| 64 | class FileImageFeeder : public IImageDataFeeder |
| 65 | { |
| 66 | public: |
| 67 | /** Default constructor |
| 68 | * |
| 69 | * @param[in] fs Image file stream |
| 70 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | FileImageFeeder(std::ifstream &fs) : _fs(fs) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 72 | { |
| 73 | } |
| 74 | // Inherited overridden methods |
| 75 | uint8_t get() override |
| 76 | { |
| 77 | return _fs.get(); |
| 78 | } |
| 79 | void get_row(uint8_t *dst, size_t row_size) override |
| 80 | { |
| 81 | ARM_COMPUTE_ERROR_ON(dst == nullptr); |
| 82 | _fs.read(reinterpret_cast<std::fstream::char_type *>(dst), row_size); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | std::ifstream &_fs; |
| 87 | }; |
| 88 | /** Memory Image feeder concrete implementation */ |
| 89 | class MemoryImageFeeder : public IImageDataFeeder |
| 90 | { |
| 91 | public: |
| 92 | /** Default constructor |
| 93 | * |
| 94 | * @param[in] data Pointer to data |
| 95 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | MemoryImageFeeder(const uint8_t *data) : _data(data) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 97 | { |
| 98 | } |
| 99 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 100 | MemoryImageFeeder(const MemoryImageFeeder &) = delete; |
| 101 | /** Default move constructor */ |
| 102 | MemoryImageFeeder(MemoryImageFeeder &&) = default; |
| 103 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 104 | MemoryImageFeeder &operator=(const MemoryImageFeeder &) = delete; |
| 105 | /** Default move assignment operator */ |
| 106 | MemoryImageFeeder &operator=(MemoryImageFeeder &&) = default; |
| 107 | // Inherited overridden methods |
| 108 | uint8_t get() override |
| 109 | { |
| 110 | return *_data++; |
| 111 | } |
| 112 | void get_row(uint8_t *dst, size_t row_size) override |
| 113 | { |
| 114 | ARM_COMPUTE_ERROR_ON(dst == nullptr); |
| 115 | memcpy(dst, _data, row_size); |
| 116 | _data += row_size; |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | const uint8_t *_data; |
| 121 | }; |
| 122 | |
| 123 | /** Image loader interface */ |
| 124 | class IImageLoader |
| 125 | { |
| 126 | public: |
| 127 | /** Default Constructor */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 128 | IImageLoader() : _feeder(nullptr), _width(0), _height(0) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 129 | { |
| 130 | } |
| 131 | /** Virtual base destructor */ |
| 132 | virtual ~IImageLoader() = default; |
| 133 | /** Return the width of the currently open image file. */ |
| 134 | unsigned int width() const |
| 135 | { |
| 136 | return _width; |
| 137 | } |
| 138 | /** Return the height of the currently open image file. */ |
| 139 | unsigned int height() const |
| 140 | { |
| 141 | return _height; |
| 142 | } |
| 143 | /** Return true if the image file is currently open */ |
| 144 | virtual bool is_open() = 0; |
| 145 | /** Open an image file and reads its metadata (Width, height) |
| 146 | * |
| 147 | * @param[in] filename File to open |
| 148 | */ |
| 149 | virtual void open(const std::string &filename) = 0; |
| 150 | /** Closes an image file */ |
| 151 | virtual void close() = 0; |
| 152 | /** Initialise an image's metadata with the dimensions of the image file currently open |
| 153 | * |
| 154 | * @param[out] image Image to initialise |
| 155 | * @param[in] format Format to use for the image (Must be RGB888 or U8) |
| 156 | */ |
| 157 | template <typename T> |
| 158 | void init_image(T &image, Format format) |
| 159 | { |
| 160 | ARM_COMPUTE_ERROR_ON(!is_open()); |
| 161 | ARM_COMPUTE_ERROR_ON(format != Format::RGB888 && format != Format::U8); |
| 162 | |
| 163 | // Use the size of the input image |
| 164 | TensorInfo image_info(_width, _height, format); |
| 165 | image.allocator()->init(image_info); |
| 166 | } |
| 167 | /** Fill an image with the content of the currently open image file. |
| 168 | * |
| 169 | * @note If the image is a CLImage, the function maps and unmaps the image |
| 170 | * |
| 171 | * @param[in,out] image Image to fill (Must be allocated, and of matching dimensions with the opened image file). |
| 172 | */ |
| 173 | template <typename T> |
| 174 | void fill_image(T &image) |
| 175 | { |
| 176 | ARM_COMPUTE_ERROR_ON(!is_open()); |
| 177 | ARM_COMPUTE_ERROR_ON(image.info()->dimension(0) != _width || image.info()->dimension(1) != _height); |
| 178 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&image, Format::U8, Format::RGB888); |
| 179 | ARM_COMPUTE_ERROR_ON(_feeder.get() == nullptr); |
| 180 | try |
| 181 | { |
Michele Di Giorgio | 40efd53 | 2021-03-18 17:32:00 +0000 | [diff] [blame] | 182 | // Map buffer if creating a CLTensor |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 183 | map(image, true); |
| 184 | |
| 185 | // Validate feeding data |
| 186 | validate_info(image.info()); |
| 187 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 188 | switch (image.info()->format()) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 189 | { |
| 190 | case Format::U8: |
| 191 | { |
| 192 | // We need to convert the data from RGB to grayscale: |
| 193 | // Iterate through every pixel of the image |
| 194 | Window window; |
| 195 | window.set(Window::DimX, Window::Dimension(0, _width, 1)); |
| 196 | window.set(Window::DimY, Window::Dimension(0, _height, 1)); |
| 197 | |
| 198 | Iterator out(&image, window); |
| 199 | |
| 200 | unsigned char red = 0; |
| 201 | unsigned char green = 0; |
| 202 | unsigned char blue = 0; |
| 203 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 204 | execute_window_loop( |
| 205 | window, |
| 206 | [&](const Coordinates &) |
| 207 | { |
| 208 | red = _feeder->get(); |
| 209 | green = _feeder->get(); |
| 210 | blue = _feeder->get(); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 211 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 212 | *out.ptr() = 0.2126f * red + 0.7152f * green + 0.0722f * blue; |
| 213 | }, |
| 214 | out); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 215 | |
| 216 | break; |
| 217 | } |
| 218 | case Format::RGB888: |
| 219 | { |
| 220 | // There is no format conversion needed: we can simply copy the content of the input file to the image one row at the time. |
| 221 | // Create a vertical window to iterate through the image's rows: |
| 222 | Window window; |
| 223 | window.set(Window::DimY, Window::Dimension(0, _height, 1)); |
| 224 | |
| 225 | Iterator out(&image, window); |
| 226 | size_t row_size = _width * image.info()->element_size(); |
| 227 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 228 | execute_window_loop( |
| 229 | window, [&](const Coordinates &) { _feeder->get_row(out.ptr(), row_size); }, out); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 230 | |
| 231 | break; |
| 232 | } |
| 233 | default: |
| 234 | ARM_COMPUTE_ERROR("Unsupported format"); |
| 235 | } |
| 236 | |
Michele Di Giorgio | 40efd53 | 2021-03-18 17:32:00 +0000 | [diff] [blame] | 237 | // Unmap buffer if creating a CLTensor |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 238 | unmap(image); |
| 239 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 240 | catch (const std::ifstream::failure &e) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 241 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 242 | ARM_COMPUTE_ERROR_VAR("Loading image file: %s", e.what()); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | /** Fill a tensor with 3 planes (one for each channel) with the content of the currently open image file. |
| 246 | * |
| 247 | * @note If the image is a CLImage, the function maps and unmaps the image |
| 248 | * |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 249 | * @param[in,out] tensor Tensor with 3 planes to fill (Must be allocated, and of matching dimensions with the opened image). Data types supported: U8/F16/F32 |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 250 | * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false) |
| 251 | */ |
| 252 | template <typename T> |
| 253 | void fill_planar_tensor(T &tensor, bool bgr = false) |
| 254 | { |
| 255 | ARM_COMPUTE_ERROR_ON(!is_open()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 256 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::U8, DataType::QASYMM8, DataType::F32, |
| 257 | DataType::F16); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 258 | |
| 259 | const DataLayout data_layout = tensor.info()->data_layout(); |
| 260 | const TensorShape tensor_shape = tensor.info()->tensor_shape(); |
| 261 | |
| 262 | ARM_COMPUTE_UNUSED(tensor_shape); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 263 | ARM_COMPUTE_ERROR_ON(tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)] != |
| 264 | _width); |
| 265 | ARM_COMPUTE_ERROR_ON(tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)] != |
| 266 | _height); |
| 267 | ARM_COMPUTE_ERROR_ON(tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL)] != |
| 268 | 3); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 269 | |
| 270 | ARM_COMPUTE_ERROR_ON(_feeder.get() == nullptr); |
| 271 | |
| 272 | try |
| 273 | { |
| 274 | // Map buffer if creating a CLTensor |
| 275 | map(tensor, true); |
| 276 | |
| 277 | // Validate feeding data |
| 278 | validate_info(tensor.info()); |
| 279 | |
| 280 | // Stride across channels |
| 281 | size_t stride_z = 0; |
| 282 | |
| 283 | // Iterate through every pixel of the image |
| 284 | Window window; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 285 | if (data_layout == DataLayout::NCHW) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 286 | { |
| 287 | window.set(Window::DimX, Window::Dimension(0, _width, 1)); |
| 288 | window.set(Window::DimY, Window::Dimension(0, _height, 1)); |
| 289 | window.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
| 290 | stride_z = tensor.info()->strides_in_bytes()[2]; |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | window.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 295 | window.set(Window::DimY, Window::Dimension(0, _width, 1)); |
| 296 | window.set(Window::DimZ, Window::Dimension(0, _height, 1)); |
| 297 | stride_z = tensor.info()->strides_in_bytes()[0]; |
| 298 | } |
| 299 | |
| 300 | Iterator out(&tensor, window); |
| 301 | |
| 302 | unsigned char red = 0; |
| 303 | unsigned char green = 0; |
| 304 | unsigned char blue = 0; |
| 305 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 306 | execute_window_loop( |
| 307 | window, |
| 308 | [&](const Coordinates &) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 309 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 310 | red = _feeder->get(); |
| 311 | green = _feeder->get(); |
| 312 | blue = _feeder->get(); |
| 313 | |
| 314 | switch (tensor.info()->data_type()) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 315 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 316 | case DataType::U8: |
| 317 | case DataType::QASYMM8: |
| 318 | { |
| 319 | *(out.ptr() + 0 * stride_z) = bgr ? blue : red; |
| 320 | *(out.ptr() + 1 * stride_z) = green; |
| 321 | *(out.ptr() + 2 * stride_z) = bgr ? red : blue; |
| 322 | break; |
| 323 | } |
| 324 | case DataType::F32: |
| 325 | { |
| 326 | *reinterpret_cast<float *>(out.ptr() + 0 * stride_z) = static_cast<float>(bgr ? blue : red); |
| 327 | *reinterpret_cast<float *>(out.ptr() + 1 * stride_z) = static_cast<float>(green); |
| 328 | *reinterpret_cast<float *>(out.ptr() + 2 * stride_z) = static_cast<float>(bgr ? red : blue); |
| 329 | break; |
| 330 | } |
| 331 | case DataType::F16: |
| 332 | { |
| 333 | *reinterpret_cast<half *>(out.ptr() + 0 * stride_z) = static_cast<half>(bgr ? blue : red); |
| 334 | *reinterpret_cast<half *>(out.ptr() + 1 * stride_z) = static_cast<half>(green); |
| 335 | *reinterpret_cast<half *>(out.ptr() + 2 * stride_z) = static_cast<half>(bgr ? red : blue); |
| 336 | break; |
| 337 | } |
| 338 | default: |
| 339 | { |
| 340 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 341 | } |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 342 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 343 | }, |
| 344 | out); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 345 | |
| 346 | // Unmap buffer if creating a CLTensor |
| 347 | unmap(tensor); |
| 348 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 349 | catch (const std::ifstream::failure &e) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 350 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 351 | ARM_COMPUTE_ERROR_VAR("Loading image file: %s", e.what()); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | protected: |
| 356 | /** Validate metadata */ |
| 357 | virtual void validate_info(const ITensorInfo *tensor_info) |
| 358 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 359 | ARM_COMPUTE_UNUSED(tensor_info); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | protected: |
| 363 | std::unique_ptr<IImageDataFeeder> _feeder; |
| 364 | unsigned int _width; |
| 365 | unsigned int _height; |
| 366 | }; |
| 367 | |
| 368 | /** PPM Image loader concrete implementation */ |
| 369 | class PPMLoader : public IImageLoader |
| 370 | { |
| 371 | public: |
| 372 | /** Default Constructor */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 373 | PPMLoader() : IImageLoader(), _fs() |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 374 | { |
| 375 | } |
| 376 | |
| 377 | // Inherited methods overridden: |
| 378 | bool is_open() override |
| 379 | { |
| 380 | return _fs.is_open(); |
| 381 | } |
| 382 | void open(const std::string &filename) override |
| 383 | { |
| 384 | ARM_COMPUTE_ERROR_ON(is_open()); |
| 385 | try |
| 386 | { |
| 387 | _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 388 | _fs.open(filename, std::ios::in | std::ios::binary); |
| 389 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 390 | unsigned int max_val = 0; |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 391 | std::tie(_width, _height, max_val) = parse_ppm_header(_fs); |
| 392 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 393 | ARM_COMPUTE_ERROR_ON_MSG_VAR(max_val >= 256, "2 bytes per colour channel not supported in file %s", |
| 394 | filename.c_str()); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 395 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 396 | _feeder = std::make_unique<FileImageFeeder>(_fs); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 397 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 398 | catch (std::runtime_error &e) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 399 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 400 | ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", filename.c_str(), e.what()); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | void close() override |
| 404 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 405 | if (is_open()) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 406 | { |
| 407 | _fs.close(); |
| 408 | _feeder = nullptr; |
| 409 | } |
| 410 | ARM_COMPUTE_ERROR_ON(is_open()); |
| 411 | } |
| 412 | |
| 413 | protected: |
| 414 | // Inherited methods overridden: |
| 415 | void validate_info(const ITensorInfo *tensor_info) override |
| 416 | { |
| 417 | // Check if the file is large enough to fill the image |
| 418 | const size_t current_position = _fs.tellg(); |
| 419 | _fs.seekg(0, std::ios_base::end); |
| 420 | const size_t end_position = _fs.tellg(); |
| 421 | _fs.seekg(current_position, std::ios_base::beg); |
| 422 | |
| 423 | ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor_info->tensor_shape().total_size(), |
| 424 | "Not enough data in file"); |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 425 | ARM_COMPUTE_UNUSED(end_position, tensor_info); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | private: |
| 429 | std::ifstream _fs; |
| 430 | }; |
| 431 | |
| 432 | /** Class to load the content of a JPEG file into an Image */ |
| 433 | class JPEGLoader : public IImageLoader |
| 434 | { |
| 435 | private: |
| 436 | /** Custom malloc deleter struct */ |
| 437 | struct malloc_deleter |
| 438 | { |
| 439 | void operator()(uint8_t *p) const |
| 440 | { |
| 441 | free(p); |
| 442 | } |
| 443 | }; |
| 444 | |
| 445 | public: |
| 446 | /** Default Constructor */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 447 | JPEGLoader() : IImageLoader(), _is_loaded(false), _data(nullptr) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 448 | { |
| 449 | } |
| 450 | |
| 451 | // Inherited methods overridden: |
| 452 | bool is_open() override |
| 453 | { |
| 454 | return _is_loaded; |
| 455 | } |
| 456 | void open(const std::string &filename) override |
| 457 | { |
| 458 | int bpp, width, height; |
| 459 | uint8_t *rgb_image = stbi_load(filename.c_str(), &width, &height, &bpp, 3); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 460 | if (rgb_image == NULL) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 461 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 462 | ARM_COMPUTE_ERROR_VAR("Accessing %s failed", filename.c_str()); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 463 | } |
| 464 | else |
| 465 | { |
| 466 | _width = width; |
| 467 | _height = height; |
| 468 | _data = std::unique_ptr<uint8_t, malloc_deleter>(rgb_image); |
| 469 | _is_loaded = true; |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 470 | _feeder = std::make_unique<MemoryImageFeeder>(_data.get()); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | void close() override |
| 474 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 475 | if (is_open()) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 476 | { |
| 477 | _width = 0; |
| 478 | _height = 0; |
| 479 | release(); |
| 480 | } |
| 481 | ARM_COMPUTE_ERROR_ON(is_open()); |
| 482 | } |
| 483 | /** Explicitly Releases the memory of the loaded data */ |
| 484 | void release() |
| 485 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 486 | if (_is_loaded) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 487 | { |
| 488 | _data.reset(); |
| 489 | _is_loaded = false; |
| 490 | _feeder = nullptr; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | private: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 495 | bool _is_loaded; |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 496 | std::unique_ptr<uint8_t, malloc_deleter> _data; |
| 497 | }; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 498 | |
| 499 | /** Factory for generating appropriate image loader**/ |
| 500 | class ImageLoaderFactory final |
| 501 | { |
| 502 | public: |
| 503 | /** Create an image loader depending on the image type |
| 504 | * |
| 505 | * @param[in] filename File than needs to be loaded |
| 506 | * |
| 507 | * @return Image loader |
| 508 | */ |
| 509 | static std::unique_ptr<IImageLoader> create(const std::string &filename) |
| 510 | { |
| 511 | ImageType type = arm_compute::utils::get_image_type_from_file(filename); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 512 | switch (type) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 513 | { |
| 514 | case ImageType::PPM: |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 515 | return std::make_unique<PPMLoader>(); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 516 | case ImageType::JPEG: |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 517 | return std::make_unique<JPEGLoader>(); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 518 | case ImageType::UNKNOWN: |
| 519 | default: |
| 520 | return nullptr; |
| 521 | } |
| 522 | } |
| 523 | }; |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 524 | } // namespace utils |
| 525 | } // namespace arm_compute |
| 526 | #endif /* __UTILS_IMAGE_LOADER_H__*/ |