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>
50 using namespace fawkes;
52 namespace firevision {
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" ) {
353 FvRawReader *fvrr =
new FvRawReader( fn );
354 cspace = fvrr->colorspace();
355 width = fvrr->pixel_width();
356 height = fvrr->pixel_height();
357 _buffer_size = colorspace_buffer_size( cspace, width, height );
358 file_buffer = (
unsigned char*)malloc(_buffer_size);
359 fvrr->set_buffer( file_buffer );
364 e.
append(
"FileLoader::open() failed");
370 }
else if ( ft.find(
"JPEG" ) != std::string::npos ) {
371 JpegReader *jr =
new JpegReader( fn );
372 cspace = jr->colorspace();
373 width = jr->pixel_width();
374 height = jr->pixel_height();
375 _buffer_size = colorspace_buffer_size( cspace, width, height );
376 file_buffer = (
unsigned char*)malloc(_buffer_size);
377 jr->set_buffer( file_buffer );
382 e.
append(
"FileLoader::open() failed");
389 }
else if ( ft.find(
"PNG" ) != std::string::npos ) {
390 PNGReader *pr =
new PNGReader( fn ); cspace = pr->colorspace();
391 width = pr->pixel_width();
392 height = pr->pixel_height();
393 _buffer_size = colorspace_buffer_size( cspace, width, height );
394 file_buffer = (
unsigned char*)malloc(_buffer_size);
395 pr->set_buffer( file_buffer );
400 e.
append(
"FileLoader::open() failed for PNG");
406 }
else if ( ft ==
"FvColormap" ) {
410 Colormap *colormap = cmf.get_colormap();
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);
416 colormap->to_image(file_buffer);
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)");
std::string get(std::string s) const
Get the value of the given parameter.
Base class for exceptions in Fawkes.
Expected parameter is missing.
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.