blob: 5a2825ebc3eb49ff1a5bbd890212c443b14c4e1a [file] [log] [blame]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +01001/*
giuros01351bd132019-08-23 14:27:30 +01002 * Copyright (c) 2018-2019 ARM Limited.
Georgios Pinitas7c3b9242018-06-21 19:01:25 +01003 *
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 Pinitas7c3b9242018-06-21 19:01:25 +010036#include "stb/stb_image.h"
37#pragma GCC diagnostic pop
38
39#include <cstdlib>
40#include <memory>
41#include <string>
42
43namespace arm_compute
44{
45namespace utils
46{
47/** Image feeder interface */
48class IImageDataFeeder
49{
50public:
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 */
63class FileImageFeeder : public IImageDataFeeder
64{
65public:
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
85private:
86 std::ifstream &_fs;
87};
88/** Memory Image feeder concrete implementation */
89class MemoryImageFeeder : public IImageDataFeeder
90{
91public:
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
120private:
121 const uint8_t *_data;
122};
123
124/** Image loader interface */
125class IImageLoader
126{
127public:
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
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100206 execute_window_loop(window, [&](const Coordinates &)
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100207 {
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
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100228 execute_window_loop(window, [&](const Coordinates &)
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100229 {
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 *
giuros01351bd132019-08-23 14:27:30 +0100252 * @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 Pinitas7c3b9242018-06-21 19:01:25 +0100253 * @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());
giuros01351bd132019-08-23 14:27:30 +0100259 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::U8, DataType::F32, DataType::F16);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100260
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
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100305 execute_window_loop(window, [&](const Coordinates &)
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100306 {
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 }
giuros01351bd132019-08-23 14:27:30 +0100327 case DataType::F16:
328 {
329 *reinterpret_cast<half *>(out.ptr() + 0 * stride_z) = static_cast<half>(bgr ? blue : red);
330 *reinterpret_cast<half *>(out.ptr() + 1 * stride_z) = static_cast<half>(green);
331 *reinterpret_cast<half *>(out.ptr() + 2 * stride_z) = static_cast<half>(bgr ? red : blue);
332 break;
333 }
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100334 default:
335 {
336 ARM_COMPUTE_ERROR("Unsupported data type");
337 }
338 }
339 },
340 out);
341
342 // Unmap buffer if creating a CLTensor
343 unmap(tensor);
344 }
345 catch(const std::ifstream::failure &e)
346 {
347 ARM_COMPUTE_ERROR("Loading image file: %s", e.what());
348 }
349 }
350
351protected:
352 /** Validate metadata */
353 virtual void validate_info(const ITensorInfo *tensor_info)
354 {
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100355 ARM_COMPUTE_UNUSED(tensor_info);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100356 }
357
358protected:
359 std::unique_ptr<IImageDataFeeder> _feeder;
360 unsigned int _width;
361 unsigned int _height;
362};
363
364/** PPM Image loader concrete implementation */
365class PPMLoader : public IImageLoader
366{
367public:
368 /** Default Constructor */
369 PPMLoader()
370 : IImageLoader(), _fs()
371 {
372 }
373
374 // Inherited methods overridden:
375 bool is_open() override
376 {
377 return _fs.is_open();
378 }
379 void open(const std::string &filename) override
380 {
381 ARM_COMPUTE_ERROR_ON(is_open());
382 try
383 {
384 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
385 _fs.open(filename, std::ios::in | std::ios::binary);
386
387 unsigned int max_val = 0;
388 std::tie(_width, _height, max_val) = parse_ppm_header(_fs);
389
390 ARM_COMPUTE_ERROR_ON_MSG(max_val >= 256, "2 bytes per colour channel not supported in file %s",
391 filename.c_str());
392
393 _feeder = support::cpp14::make_unique<FileImageFeeder>(_fs);
394 }
395 catch(std::runtime_error &e)
396 {
397 ARM_COMPUTE_ERROR("Accessing %s: %s", filename.c_str(), e.what());
398 }
399 }
400 void close() override
401 {
402 if(is_open())
403 {
404 _fs.close();
405 _feeder = nullptr;
406 }
407 ARM_COMPUTE_ERROR_ON(is_open());
408 }
409
410protected:
411 // Inherited methods overridden:
412 void validate_info(const ITensorInfo *tensor_info) override
413 {
414 // Check if the file is large enough to fill the image
415 const size_t current_position = _fs.tellg();
416 _fs.seekg(0, std::ios_base::end);
417 const size_t end_position = _fs.tellg();
418 _fs.seekg(current_position, std::ios_base::beg);
419
420 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor_info->tensor_shape().total_size(),
421 "Not enough data in file");
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100422 ARM_COMPUTE_UNUSED(end_position, tensor_info);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100423 }
424
425private:
426 std::ifstream _fs;
427};
428
429/** Class to load the content of a JPEG file into an Image */
430class JPEGLoader : public IImageLoader
431{
432private:
433 /** Custom malloc deleter struct */
434 struct malloc_deleter
435 {
436 void operator()(uint8_t *p) const
437 {
438 free(p);
439 }
440 };
441
442public:
443 /** Default Constructor */
444 JPEGLoader()
445 : IImageLoader(), _is_loaded(false), _data(nullptr)
446 {
447 }
448
449 // Inherited methods overridden:
450 bool is_open() override
451 {
452 return _is_loaded;
453 }
454 void open(const std::string &filename) override
455 {
456 int bpp, width, height;
457 uint8_t *rgb_image = stbi_load(filename.c_str(), &width, &height, &bpp, 3);
458 if(rgb_image == NULL)
459 {
460 ARM_COMPUTE_ERROR("Accessing %s failed", filename.c_str());
461 }
462 else
463 {
464 _width = width;
465 _height = height;
466 _data = std::unique_ptr<uint8_t, malloc_deleter>(rgb_image);
467 _is_loaded = true;
468 _feeder = support::cpp14::make_unique<MemoryImageFeeder>(_data.get());
469 }
470 }
471 void close() override
472 {
473 if(is_open())
474 {
475 _width = 0;
476 _height = 0;
477 release();
478 }
479 ARM_COMPUTE_ERROR_ON(is_open());
480 }
481 /** Explicitly Releases the memory of the loaded data */
482 void release()
483 {
484 if(_is_loaded)
485 {
486 _data.reset();
487 _is_loaded = false;
488 _feeder = nullptr;
489 }
490 }
491
492private:
493 bool _is_loaded;
494 std::unique_ptr<uint8_t, malloc_deleter> _data;
495};
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100496
497/** Factory for generating appropriate image loader**/
498class ImageLoaderFactory final
499{
500public:
501 /** Create an image loader depending on the image type
502 *
503 * @param[in] filename File than needs to be loaded
504 *
505 * @return Image loader
506 */
507 static std::unique_ptr<IImageLoader> create(const std::string &filename)
508 {
509 ImageType type = arm_compute::utils::get_image_type_from_file(filename);
510 switch(type)
511 {
512 case ImageType::PPM:
513 return support::cpp14::make_unique<PPMLoader>();
514 case ImageType::JPEG:
515 return support::cpp14::make_unique<JPEGLoader>();
516 case ImageType::UNKNOWN:
517 default:
518 return nullptr;
519 }
520 }
521};
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100522} // namespace utils
523} // namespace arm_compute
524#endif /* __UTILS_IMAGE_LOADER_H__*/