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;
128 this->cspace = CS_UNKNOWN;
129 opened = started =
false;
131 if ( cap->
has(
"file") ) {
132 this->filename = strdup(cap->
get(
"file").c_str());
133 if (cap->
has(
"width")) {
136 if (cap->
has(
"height")) {
137 height = cap->
get_int(
"height");
139 if (cap->
has(
"colorspace")) {
140 cspace = colorspace_by_name(cap->
get(
"colorspace").c_str());
142 }
else if ( cap->
has(
"dir") ) {
143 this->dirname = strdup( cap->
get(
"dir").c_str() );
144 if ( cap->
has(
"ext") ) {
145 this->extension = strdup( cap->
get(
"ext").c_str() );
164 FileLoader::FileLoader(colorspace_t cspace,
const char *filename,
165 unsigned int width,
unsigned int height)
167 started = opened =
false;
168 this->cspace = cspace;
170 this->height = height;
171 this->filename = strdup(filename);
172 this->dirname = NULL;
173 this->extension = NULL;
174 this->file_list = NULL;
182 FileLoader::~FileLoader()
184 for (
int i = 0; i < num_files; ++i) {
200 num_files = scandir(dirname, &file_list, file_select, alphasort);
202 if ( -1 == num_files ) {
203 throw Exception(
"Error while scanning directory %s", dirname);
218 throw Exception(
"Trying to start closed file");
232 FileLoader::print_info()
238 FileLoader::capture()
240 if (0 != num_files) {
247 if (++cur_file == num_files) {
262 FileLoader::buffer_size()
271 if (file_buffer != NULL) {
280 FileLoader::dispose_buffer()
299 FileLoader::set_image_number(
unsigned int n)
305 FileLoader::pixel_width()
312 FileLoader::pixel_height()
319 FileLoader::colorspace()
329 FileLoader::set_colorspace(colorspace_t c)
339 FileLoader::set_pixel_width(
unsigned int w)
349 FileLoader::set_pixel_height(
unsigned int h)
355 FileLoader::read_file()
358 if (0 != num_files) {
359 if (asprintf(&fn,
"%s/%s", dirname, file_list[cur_file]->d_name) == -1) {
363 fn = strdup(filename);
366 std::string ft = fv_filetype_file( fn );
368 if ( ft ==
"FvRaw" ) {
369 FvRawReader *fvrr =
new FvRawReader( fn );
370 cspace = fvrr->colorspace();
371 width = fvrr->pixel_width();
372 height = fvrr->pixel_height();
373 _buffer_size = colorspace_buffer_size( cspace, width, height );
374 file_buffer = (
unsigned char*)malloc(_buffer_size);
375 fvrr->set_buffer( file_buffer );
380 e.
append(
"FileLoader::open() failed");
386 }
else if ( ft.find(
"JPEG" ) != std::string::npos ) {
387 JpegReader *jr =
new JpegReader( fn );
388 cspace = jr->colorspace();
389 width = jr->pixel_width();
390 height = jr->pixel_height();
391 _buffer_size = colorspace_buffer_size( cspace, width, height );
392 file_buffer = (
unsigned char*)malloc(_buffer_size);
393 jr->set_buffer( file_buffer );
398 e.
append(
"FileLoader::open() failed");
405 }
else if ( ft.find(
"PNG" ) != std::string::npos ) {
406 PNGReader *pr =
new PNGReader( fn ); cspace = pr->colorspace();
407 width = pr->pixel_width();
408 height = pr->pixel_height();
409 _buffer_size = colorspace_buffer_size( cspace, width, height );
410 file_buffer = (
unsigned char*)malloc(_buffer_size);
411 pr->set_buffer( file_buffer );
416 e.
append(
"FileLoader::open() failed for PNG");
422 }
else if ( ft ==
"FvColormap" ) {
426 Colormap *colormap = cmf.get_colormap();
427 cspace = YUV422_PLANAR;
428 width = colormap->width() * 2;
429 height = colormap->height() * 2;
430 _buffer_size = colorspace_buffer_size( cspace, width, height );
431 file_buffer = (
unsigned char*)malloc(_buffer_size);
432 colormap->to_image(file_buffer);
437 _buffer_size = colorspace_buffer_size( cspace, width, height );
439 if (_buffer_size > 0) {
441 f = fopen( fn,
"rb" );
442 file_buffer = (
unsigned char*)malloc(_buffer_size);
443 if (fread(file_buffer, _buffer_size, 1, f) != 1) {
450 throw Exception(
"Invalid color space (buffer size is 0)");
Fawkes library namespace.
bool has(std::string s) const
Check if an parameter was given.
Base class for exceptions in Fawkes.
long int get_int(std::string s) const
Get the value of the given parameter as integer.
std::string get(std::string s) const
Get the value of the given parameter.
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.