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" ) {
373 _buffer_size = colorspace_buffer_size( cspace, width, height );
374 file_buffer = (
unsigned char*)malloc(_buffer_size);
380 e.
append(
"FileLoader::open() failed");
386 }
else if ( ft.find(
"JPEG" ) != std::string::npos ) {
391 _buffer_size = colorspace_buffer_size( cspace, width, height );
392 file_buffer = (
unsigned char*)malloc(_buffer_size);
398 e.
append(
"FileLoader::open() failed");
405 }
else if ( ft.find(
"PNG" ) != std::string::npos ) {
409 _buffer_size = colorspace_buffer_size( cspace, width, height );
410 file_buffer = (
unsigned char*)malloc(_buffer_size);
416 e.
append(
"FileLoader::open() failed for PNG");
422 }
else if ( ft ==
"FvColormap" ) {
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);
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)");
virtual unsigned int pixel_height()
Get height of read image in pixels.
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.
virtual unsigned int height() const =0
Get height of colormap.
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.
bool has(std::string s) const
Check if an parameter was given.
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 unsigned int width() const =0
Get width of colormap.
virtual colorspace_t colorspace()
Get colorspace from the just read image.
virtual colorspace_t colorspace()
Get colorspace from the just read image.
virtual unsigned int pixel_height()
Get height of read image in pixels.
long int get_int(std::string s) const
Get the value of the given parameter as integer.
virtual void set_buffer(unsigned char *yuv422planar_buffer)
Set buffer that the read image should be written to.
std::string get(std::string s) const
Get the value of the given parameter.
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.