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;
76 uint8_t
get()
override 83 _fs.read(reinterpret_cast<std::fstream::char_type *>(
dst), row_size);
117 memcpy(
dst, _data, row_size);
122 const uint8_t *_data;
131 : _feeder(nullptr), _width(0), _height(0)
152 virtual void open(
const std::string &filename) = 0;
154 virtual void close() = 0;
160 template <
typename T>
167 TensorInfo image_info(_width, _height, format);
168 image.allocator()->init(image_info);
176 template <
typename T>
189 validate_info(
image.info());
191 switch(
image.info()->format())
203 unsigned char red = 0;
204 unsigned char green = 0;
205 unsigned char blue = 0;
209 red = _feeder->get();
210 green = _feeder->get();
211 blue = _feeder->get();
213 *out.
ptr() = 0.2126f * red + 0.7152f * green + 0.0722f * blue;
227 size_t row_size = _width *
image.info()->element_size();
231 _feeder->get_row(out.
ptr(), row_size);
244 catch(
const std::ifstream::failure &e)
256 template <
typename T>
263 const TensorShape tensor_shape = tensor.info()->tensor_shape();
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;
308 red = _feeder->get();
309 green = _feeder->get();
310 blue = _feeder->get();
312 switch(tensor.info()->data_type())
317 *(out.
ptr() + 0 * stride_z) = bgr ? blue : red;
318 *(out.
ptr() + 1 * stride_z) = green;
319 *(out.
ptr() + 2 * stride_z) = bgr ? red : blue;
324 *reinterpret_cast<float *>(out.
ptr() + 0 * stride_z) = static_cast<float>(bgr ? blue : red);
325 *reinterpret_cast<float *>(out.
ptr() + 1 * stride_z) = static_cast<float>(green);
326 *reinterpret_cast<float *>(out.
ptr() + 2 * stride_z) = static_cast<float>(bgr ? red : blue);
331 *reinterpret_cast<half *>(out.
ptr() + 0 * stride_z) = static_cast<half>(bgr ? blue : red);
332 *reinterpret_cast<half *>(out.
ptr() + 1 * stride_z) = static_cast<half>(green);
333 *reinterpret_cast<half *>(out.
ptr() + 2 * stride_z) = static_cast<half>(bgr ? red : blue);
347 catch(
const std::ifstream::failure &e)
355 virtual void validate_info(
const ITensorInfo *tensor_info)
361 std::unique_ptr<IImageDataFeeder> _feeder;
363 unsigned int _height;
379 return _fs.is_open();
381 void open(
const std::string &filename)
override 386 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
387 _fs.open(filename, std::ios::in | std::ios::binary);
389 unsigned int max_val = 0;
395 _feeder = std::make_unique<FileImageFeeder>(_fs);
397 catch(std::runtime_error &e)
414 void validate_info(
const ITensorInfo *tensor_info)
override 417 const size_t current_position = _fs.tellg();
419 const size_t end_position = _fs.tellg();
420 _fs.seekg(current_position, std::ios_base::beg);
423 "Not enough data in file");
436 struct malloc_deleter
438 void operator()(uint8_t *p)
const 456 void open(
const std::string &filename)
override 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>();
517 return std::make_unique<JPEGLoader>();
virtual void get_row(uint8_t *dst, size_t row_size)=0
Feed a whole row to a destination pointer.
void close() override
Closes an image file.
Class to load the content of a JPEG file into an Image.
virtual uint8_t get()=0
Gets a character from an image feed.
MemoryImageFeeder(const uint8_t *data)
Default constructor.
virtual void close()=0
Closes an image file.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
void open(const std::string &filename) override
Open an image file and reads its metadata (Width, height)
1 channel, 1 U8 per channel
virtual void open(const std::string &filename)=0
Open an image file and reads its metadata (Width, height)
1 channel, 1 F32 per channel
MemoryImageFeeder & operator=(const MemoryImageFeeder &)=delete
Prevent instances of this class from being copied (As this class contains pointers)
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
const DataLayout data_layout
Store the tensor's metadata.
Describe one of the image's dimensions with a start, end and step.
#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 ~IImageLoader()=default
Virtual base destructor.
#define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t,...)
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.
decltype(strategy::transforms) typedef type
IImageLoader()
Default Constructor.
JPEGLoader()
Default Constructor.
Copyright (c) 2017-2021 Arm Limited.
virtual bool is_open()=0
Return true if the image file is currently open.
1 channel, 1 F16 per channel
void unmap(T &tensor)
Unmaps a tensor if needed.
bool is_open() override
Return true if the image file is currently open.
3 channels, 1 U8 per channel
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Format
Image colour formats.
uint8_t get() override
Gets a character from an image feed.
File Image feeder concrete implementation.
quantized, asymmetric fixed-point 8-bit number unsigned
Factory for generating appropriate image loader.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
unsigned int width() const
Return the width of the currently open image file.
size_t total_size() const
Collapses all dimensions to a single linear total size.
void get_row(uint8_t *dst, size_t row_size) override
Feed a whole row to a destination pointer.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
void end(TokenStream &in, bool &valid)
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Memory Image feeder concrete implementation.
Num samples, channels, height, width.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
void fill_image(T &image)
Fill an image with the content of the currently open image file.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
void get_row(uint8_t *dst, size_t row_size) override
Feed a whole row to a destination pointer.
bool is_open() override
Return true if the image file is currently open.
virtual ~IImageDataFeeder()=default
Virtual base destructor.
void map(T &tensor, bool blocking)
Maps a tensor if needed.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
void close() override
Closes an image file.
FileImageFeeder(std::ifstream &fs)
Default constructor.
ImageType
Supported image types.
std::tuple< unsigned int, unsigned int, int > parse_ppm_header(std::ifstream &fs)
Parse the ppm header from an input file stream.
Store the tensor's metadata.
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...
ImageType get_image_type_from_file(const std::string &filename)
Gets image type given a file.
PPMLoader()
Default Constructor.
uint8_t get() override
Gets a character from an image feed.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Iterator updated by execute_window_loop for each window element.
unsigned int height() const
Return the height of the currently open image file.
DataLayout
[DataLayout enum definition]
Describe a multidimensional execution window.
void init_image(T &image, Format format)
Initialise an image's metadata with the dimensions of the image file currently open.
PPM Image loader concrete implementation.
void open(const std::string &filename) override
Open an image file and reads its metadata (Width, height)
void release()
Explicitly Releases the memory of the loaded data.