24.02.1
|
Go to the documentation of this file.
24 #ifndef __UTILS_IMAGE_LOADER_H__
25 #define __UTILS_IMAGE_LOADER_H__
32 #include "utils/Utils.h"
34 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wswitch-default"
36 #pragma GCC diagnostic ignored "-Wstrict-overflow"
37 #include "stb/stb_image.h"
38 #pragma GCC diagnostic pop
55 virtual uint8_t
get() = 0;
61 virtual void get_row(uint8_t *
dst,
size_t row_size) = 0;
75 uint8_t
get()
override
82 _fs.read(
reinterpret_cast<std::fstream::char_type *
>(
dst), row_size);
115 memcpy(
dst, _data, row_size);
120 const uint8_t *_data;
149 virtual void open(
const std::string &filename) = 0;
151 virtual void close() = 0;
157 template <
typename T>
165 image.allocator()->init(image_info);
173 template <
typename T>
186 validate_info(
image.info());
188 switch (
image.info()->format())
200 unsigned char red = 0;
201 unsigned char green = 0;
202 unsigned char blue = 0;
208 red = _feeder->get();
209 green = _feeder->get();
210 blue = _feeder->get();
212 *out.
ptr() = 0.2126f * red + 0.7152f * green + 0.0722f * blue;
226 size_t row_size = _width *
image.info()->element_size();
229 window, [&](
const Coordinates &) { _feeder->get_row(out.
ptr(), row_size); }, out);
240 catch (
const std::ifstream::failure &e)
252 template <
typename T>
278 validate_info(
tensor.info());
290 stride_z =
tensor.info()->strides_in_bytes()[2];
297 stride_z =
tensor.info()->strides_in_bytes()[0];
302 unsigned char red = 0;
303 unsigned char green = 0;
304 unsigned char blue = 0;
310 red = _feeder->get();
311 green = _feeder->get();
312 blue = _feeder->get();
314 switch (
tensor.info()->data_type())
317 case DataType::QASYMM8:
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;
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);
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);
340 ARM_COMPUTE_ERROR(
"Unsupported data type");
349 catch (
const std::ifstream::failure &e)
357 virtual void validate_info(
const ITensorInfo *
tensor_info)
363 std::unique_ptr<IImageDataFeeder> _feeder;
365 unsigned int _height;
380 return _fs.is_open();
382 void open(
const std::string &filename)
override
387 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
388 _fs.open(filename, std::ios::in | std::ios::binary);
390 unsigned int max_val = 0;
396 _feeder = std::make_unique<FileImageFeeder>(_fs);
398 catch (std::runtime_error &e)
418 const size_t current_position = _fs.tellg();
420 const size_t end_position = _fs.tellg();
421 _fs.seekg(current_position, std::ios_base::beg);
424 "Not enough data in file");
437 struct malloc_deleter
439 void operator()(uint8_t *p)
const
456 void open(
const std::string &filename)
override
458 int bpp, width, height;
459 uint8_t *rgb_image = stbi_load(filename.c_str(), &width, &height, &bpp, 3);
460 if (rgb_image == NULL)
468 _data = std::unique_ptr<uint8_t, malloc_deleter>(rgb_image);
470 _feeder = std::make_unique<MemoryImageFeeder>(_data.get());
496 std::unique_ptr<uint8_t, malloc_deleter> _data;
509 static std::unique_ptr<IImageLoader>
create(
const std::string &filename)
515 return std::make_unique<PPMLoader>();
516 case ImageType::JPEG:
517 return std::make_unique<JPEGLoader>();
@ NCHW
Num samples, channels, height, width.
PPM Image loader concrete implementation.
@ RGB888
3 channels, 1 U8 per channel
decltype(strategy::transforms) typedef type
uint8_t get() override
Gets a character from an image feed.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
DataLayout
[DataLayout enum definition]
#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg,...)
static std::unique_ptr< IImageLoader > create(const std::string &filename)
Create an image loader depending on the image type.
virtual void open(const std::string &filename)=0
Open an image file and reads its metadata (Width, height)
File Image feeder concrete implementation.
@ QASYMM8
quantized, asymmetric fixed-point 8-bit number unsigned
virtual void close()=0
Closes an image file.
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
void fill_planar_tensor(T &tensor, bool bgr=false)
Fill a tensor with 3 planes (one for each channel) with the content of the currently open image file.
IImageLoader()
Default Constructor.
Factory for generating appropriate image loader.
virtual bool is_open()=0
Return true if the image file is currently open.
void get_row(uint8_t *dst, size_t row_size) override
Feed a whole row to a destination pointer.
void close() override
Closes an image file.
JPEGLoader()
Default Constructor.
virtual ~IImageLoader()=default
Virtual base destructor.
constexpr auto data_layout
ImageType
Supported image types.
FileImageFeeder(std::ifstream &fs)
Default constructor.
void open(const std::string &filename) override
Open an image file and reads its metadata (Width, height)
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
void get_row(uint8_t *dst, size_t row_size) override
Feed a whole row to a destination pointer.
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&...iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Iterator updated by execute_window_loop for each window element.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
@ U8
unsigned 8-bit number
MemoryImageFeeder & operator=(const MemoryImageFeeder &)=delete
Prevent instances of this class from being copied (As this class contains pointers)
uint8_t get() override
Gets a character from an image feed.
unsigned int width() const
Return the width of the currently open image file.
bool is_open() override
Return true if the image file is currently open.
Memory Image feeder concrete implementation.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
CLTensor * tensor
Pointer to the auxiliary tensor.
Describe one of the image's dimensions with a start, end and step.
void map(T &tensor, bool blocking)
Maps a tensor if needed.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
@ U8
1 channel, 1 U8 per channel
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Format
Image colour formats.
Class to load the content of a JPEG file into an Image.
PPMLoader()
Default Constructor.
void fill_image(T &image)
Fill an image with the content of the currently open image file.
size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension)
Get the index of the given dimension.
bool is_open() override
Return true if the image file is currently open.
Store the tensor's metadata.
std::tuple< unsigned int, unsigned int, int > parse_ppm_header(std::ifstream &fs)
Parse the ppm header from an input file stream.
Describe a multidimensional execution window.
void unmap(T &tensor)
Unmaps a tensor if needed.
#define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t,...)
MemoryImageFeeder(const uint8_t *data)
Default constructor.
Copyright (c) 2017-2024 Arm Limited.
@ F16
16-bit floating-point number
@ UNKNOWN
Unknown CL kernel type.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
virtual ~IImageDataFeeder()=default
Virtual base destructor.
void close() override
Closes an image file.
void end(TokenStream &in, bool &valid)
Store the tensor's metadata.
virtual void get_row(uint8_t *dst, size_t row_size)=0
Feed a whole row to a destination pointer.
@ F32
32-bit floating-point number
void open(const std::string &filename) override
Open an image file and reads its metadata (Width, height)
ImageType get_image_type_from_file(const std::string &filename)
Gets image type given a file.
void release()
Explicitly Releases the memory of the loaded data.
TensorInfo tensor_info
Associated tensor info.
unsigned int height() const
Return the height of the currently open image file.
virtual uint8_t get()=0
Gets a character from an image feed.
void init_image(T &image, Format format)
Initialise an image's metadata with the dimensions of the image file currently open.