26 #include <core/exception.h> 27 #include <core/exceptions/software.h> 28 #include <core/exceptions/system.h> 29 #include <fvcams/fileloader.h> 30 #include <fvutils/writers/fvraw.h> 31 #include <fvutils/system/filetype.h> 32 #include <fvutils/system/camargp.h> 33 #include <fvutils/colormap/cmfile.h> 34 #include <fvutils/colormap/colormap.h> 36 #include <fvutils/readers/fvraw.h> 38 #include <fvutils/readers/jpeg.h> 41 #include <fvutils/readers/png.h> 48 #include <sys/types.h> 66 char* FileLoader::extension = NULL;
68 #if defined(__GLIBC__) || defined(__FreeBSD__) 69 int file_select(
const struct dirent* ent)
71 int file_select(
struct dirent *ent)
74 if ( !FileLoader::extension ) {
return 1; }
78 if ( NULL != strstr(ent->d_name, FileLoader::extension) ) {
88 FileLoader::FileLoader(
const char *filename)
90 this->filename = strdup(filename);
92 this->extension = NULL;
93 this->file_list = NULL;
96 opened = started =
false;
99 this->cspace = CS_UNKNOWN;
117 if ( cap->
has(
"file") ) {
118 this->filename = strdup(cap->
get(
"file").c_str());
119 }
else if ( cap->
has(
"dir") ) {
120 this->dirname = strdup( cap->
get(
"dir").c_str() );
121 if ( cap->
has(
"ext") ) {
122 this->extension = strdup( cap->
get(
"ext").c_str() );
133 this->cspace = CS_UNKNOWN;
134 opened = started =
false;
148 FileLoader::FileLoader(colorspace_t cspace,
const char *filename,
149 unsigned int width,
unsigned int height)
151 started = opened =
false;
152 this->cspace = cspace;
154 this->height = height;
155 this->filename = strdup(filename);
156 this->dirname = NULL;
157 this->extension = NULL;
158 this->file_list = NULL;
166 FileLoader::~FileLoader()
168 for (
int i = 0; i < num_files; ++i) {
184 num_files = scandir(dirname, &file_list, file_select, alphasort);
186 if ( -1 == num_files ) {
187 throw Exception(
"Error while scanning directory %s", dirname);
202 throw Exception(
"Trying to start closed file");
216 FileLoader::print_info()
222 FileLoader::capture()
224 if (0 != num_files) {
231 if (++cur_file == num_files) {
246 FileLoader::buffer_size()
255 if (file_buffer != NULL) {
264 FileLoader::dispose_buffer()
283 FileLoader::set_image_number(
unsigned int n)
289 FileLoader::pixel_width()
296 FileLoader::pixel_height()
303 FileLoader::colorspace()
313 FileLoader::set_colorspace(colorspace_t c)
323 FileLoader::set_pixel_width(
unsigned int w)
333 FileLoader::set_pixel_height(
unsigned int h)
339 FileLoader::read_file()
342 if (0 != num_files) {
343 if (asprintf(&fn,
"%s/%s", dirname, file_list[cur_file]->d_name) == -1) {
347 fn = strdup(filename);
350 std::string ft = fv_filetype_file( fn );
352 if ( ft ==
"FvRaw" ) {
357 _buffer_size = colorspace_buffer_size( cspace, width, height );
358 file_buffer = (
unsigned char*)malloc(_buffer_size);
364 e.
append(
"FileLoader::open() failed");
370 }
else if ( ft.find(
"JPEG" ) != std::string::npos ) {
375 _buffer_size = colorspace_buffer_size( cspace, width, height );
376 file_buffer = (
unsigned char*)malloc(_buffer_size);
382 e.
append(
"FileLoader::open() failed");
389 }
else if ( ft.find(
"PNG" ) != std::string::npos ) {
393 _buffer_size = colorspace_buffer_size( cspace, width, height );
394 file_buffer = (
unsigned char*)malloc(_buffer_size);
400 e.
append(
"FileLoader::open() failed for PNG");
406 }
else if ( ft ==
"FvColormap" ) {
411 cspace = YUV422_PLANAR;
412 width = colormap->
width() * 2;
413 height = colormap->
height() * 2;
414 _buffer_size = colorspace_buffer_size( cspace, width, height );
415 file_buffer = (
unsigned char*)malloc(_buffer_size);
421 _buffer_size = colorspace_buffer_size( cspace, width, height );
423 if (_buffer_size > 0) {
425 f = fopen( fn,
"rb" );
426 file_buffer = (
unsigned char*)malloc(_buffer_size);
427 if (fread(file_buffer, _buffer_size, 1, f) != 1) {
434 throw Exception(
"Invalid color space (buffer size is 0)");
virtual unsigned int pixel_height()
Get height of read image in pixels.
std::string get(std::string s) const
Get the value of the given parameter.
virtual void to_image(unsigned char *yuv422_planar_buffer, unsigned int level=0)
Create image from LUT.
virtual void set_buffer(unsigned char *yuv422planar_buffer)
Set buffer that the read image should be written to.
Fawkes library namespace.
virtual unsigned int pixel_width()
Get width of read image in pixels.
virtual void read()
Read data from file.
virtual void set_buffer(unsigned char *yuv422planar_buffer)
Set buffer that the read image should be written to.
virtual void read()
Read data from file.
Colormap * get_colormap()
Get a freshly generated colormap based on current file content.
virtual unsigned int pixel_width()
Get width of read image in pixels.
virtual void read(const char *file_name)
Read file.
virtual unsigned int width() const =0
Get width of colormap.
Base class for exceptions in Fawkes.
virtual unsigned int pixel_height()
Get height of read image in pixels.
virtual unsigned int pixel_width()
Get width of read image in pixels.
virtual colorspace_t colorspace()
Get colorspace from the just read image.
virtual unsigned int height() const =0
Get height of colormap.
virtual colorspace_t colorspace()
Get colorspace from the just read image.
virtual unsigned int pixel_height()
Get height of read image in pixels.
virtual void set_buffer(unsigned char *yuv422planar_buffer)
Set buffer that the read image should be written to.
FvRaw image reader implementation.
virtual colorspace_t colorspace()
Get colorspace from the just read image.
Expected parameter is missing.
virtual void read()
Read data from file.
System ran out of memory and desired operation could not be fulfilled.
void append(const char *format,...)
Append messages to the message list.
bool has(std::string s) const
Check if an parameter was given.