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