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/colormap/cmfile.h>
31 #include <fvutils/colormap/colormap.h>
32 #include <fvutils/readers/fvraw.h>
33 #include <fvutils/system/camargp.h>
34 #include <fvutils/system/filetype.h>
35 #include <fvutils/writers/fvraw.h>
37 # include <fvutils/readers/jpeg.h>
40 # include <fvutils/readers/png.h>
43 #include <sys/types.h>
51 namespace firevision {
62 char *FileLoader::extension = NULL;
64 #if defined(__GLIBC__) || defined(__FreeBSD__)
66 file_select(
const struct dirent *ent)
69 file_select(
struct dirent *ent)
72 if (!FileLoader::extension) {
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;
127 this->cspace = CS_UNKNOWN;
128 opened = started =
false;
130 if (cap->
has(
"file")) {
131 this->filename = strdup(cap->
get(
"file").c_str());
132 if (cap->
has(
"width")) {
135 if (cap->
has(
"height")) {
136 height = cap->
get_int(
"height");
138 if (cap->
has(
"colorspace")) {
139 cspace = colorspace_by_name(cap->
get(
"colorspace").c_str());
141 }
else if (cap->
has(
"dir")) {
142 this->dirname = strdup(cap->
get(
"dir").c_str());
143 if (cap->
has(
"ext")) {
144 this->extension = strdup(cap->
get(
"ext").c_str());
161 FileLoader::FileLoader(colorspace_t cspace,
162 const char * filename,
166 started = opened =
false;
167 this->cspace = cspace;
169 this->height = height;
170 this->filename = strdup(filename);
171 this->dirname = NULL;
172 this->extension = NULL;
173 this->file_list = NULL;
180 FileLoader::~FileLoader()
182 for (
int i = 0; i < num_files; ++i) {
198 num_files = scandir(dirname, &file_list, file_select, alphasort);
200 if (-1 == num_files) {
201 throw Exception(
"Error while scanning directory %s", dirname);
216 throw Exception(
"Trying to start closed file");
229 FileLoader::print_info()
234 FileLoader::capture()
236 if (0 != num_files) {
243 if (++cur_file == num_files) {
256 FileLoader::buffer_size()
264 if (file_buffer != NULL) {
272 FileLoader::dispose_buffer()
288 FileLoader::set_image_number(
unsigned int n)
293 FileLoader::pixel_width()
299 FileLoader::pixel_height()
305 FileLoader::colorspace()
314 FileLoader::set_colorspace(colorspace_t c)
323 FileLoader::set_pixel_width(
unsigned int w)
332 FileLoader::set_pixel_height(
unsigned int h)
338 FileLoader::read_file()
341 if (0 != num_files) {
342 if (asprintf(&fn,
"%s/%s", dirname, file_list[cur_file]->d_name) == -1) {
346 fn = strdup(filename);
349 std::string ft = fv_filetype_file(fn);
352 FvRawReader *fvrr =
new FvRawReader(fn);
353 cspace = fvrr->colorspace();
354 width = fvrr->pixel_width();
355 height = fvrr->pixel_height();
356 _buffer_size = colorspace_buffer_size(cspace, width, height);
357 file_buffer = (
unsigned char *)malloc(_buffer_size);
358 fvrr->set_buffer(file_buffer);
363 e.
append(
"FileLoader::open() failed");
369 }
else if (ft.find(
"JPEG") != std::string::npos) {
370 JpegReader *jr =
new JpegReader(fn);
371 cspace = jr->colorspace();
372 width = jr->pixel_width();
373 height = jr->pixel_height();
374 _buffer_size = colorspace_buffer_size(cspace, width, height);
375 file_buffer = (
unsigned char *)malloc(_buffer_size);
376 jr->set_buffer(file_buffer);
381 e.
append(
"FileLoader::open() failed");
388 }
else if (ft.find(
"PNG") != std::string::npos) {
389 PNGReader *pr =
new PNGReader(fn);
390 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) {
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)");
Base class for exceptions in Fawkes.
void append(const char *format,...) noexcept
Append messages to the message list.
Expected parameter is missing.
System ran out of memory and desired operation could not be fulfilled.
long int get_int(std::string s) const
Get the value of the given parameter as integer.
bool has(std::string s) const
Check if an parameter was given.
std::string get(std::string s) const
Get the value of the given parameter.
Fawkes library namespace.