00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <fvutils/writers/seq_writer.h>
00025 #include <core/exceptions/system.h>
00026
00027 #include <time.h>
00028 #include <sys/time.h>
00029
00030 #include <cstring>
00031 #include <cstdlib>
00032 #include <cstdio>
00033
00034 using namespace fawkes;
00035
00036 namespace firevision {
00037 #if 0
00038 }
00039 #endif
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 SeqWriter::SeqWriter(Writer* writer)
00051 {
00052 this->writer = writer;
00053
00054 frame_number = 0;
00055
00056 cspace = CS_UNKNOWN;
00057
00058 filename = 0;
00059 img_path = 0;
00060 }
00061
00062
00063
00064
00065 SeqWriter::~SeqWriter()
00066 {
00067 delete writer;
00068 writer = 0;
00069
00070 free(filename);
00071 free(img_path);
00072 }
00073
00074
00075
00076
00077 void SeqWriter::set_path(const char* img_path)
00078 {
00079 free(this->img_path);
00080 this->img_path = strdup(img_path);
00081 printf("SeqWriter: img path set to %s\n", this->img_path);
00082 }
00083
00084
00085
00086
00087
00088
00089 void SeqWriter::set_filename(const char* filename)
00090 {
00091 free(this->filename);
00092 this->filename = strdup(filename);
00093 }
00094
00095
00096
00097
00098
00099 void SeqWriter::set_dimensions(unsigned int width, unsigned int height)
00100 {
00101 writer->set_dimensions(width, height);
00102 }
00103
00104
00105
00106
00107 void SeqWriter::set_colorspace(colorspace_t cspace)
00108 {
00109 this->cspace = cspace;
00110 }
00111
00112
00113
00114
00115
00116 void SeqWriter::write(unsigned char *buffer)
00117 {
00118 ++frame_number;
00119 char* fn;
00120
00121 time_t now = time(NULL);
00122 struct tm now_tm;
00123 struct timeval now_tv;
00124
00125 gettimeofday(&now_tv, NULL);
00126 localtime_r(&now, &now_tm);
00127
00128 char* timestring;
00129 if (asprintf(×tring, "%04d%02d%02d_%02d%02d%02d_%06ld", now_tm.tm_year + 1900,
00130 now_tm.tm_mon + 1, now_tm.tm_mday, now_tm.tm_hour, now_tm.tm_min,
00131 now_tm.tm_sec, now_tv.tv_usec) == -1)
00132 {
00133 throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (1)");
00134 }
00135
00136 if (filename)
00137 {
00138
00139 if (img_path)
00140 {
00141 if (asprintf(&fn, "%s/%s_%s-%04u", img_path, timestring, filename, frame_number) == -1)
00142 {
00143 throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (2)");
00144 }
00145 }
00146 else
00147 {
00148 if (asprintf(&fn, "%s_%s-%04u", timestring, filename, frame_number) == -1)
00149 {
00150 throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (2)");
00151 }
00152 }
00153 }
00154 else
00155 {
00156
00157 if (img_path)
00158 {
00159 if (asprintf(&fn, "%s/%s-%04u", img_path, timestring, frame_number) == -1)
00160 {
00161 throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (3)");
00162 }
00163 }
00164 else
00165 {
00166 if (asprintf(&fn, "%s-%04u", timestring, frame_number) == -1)
00167 {
00168 throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (4)");
00169 }
00170 }
00171 }
00172
00173 writer->set_filename(fn);
00174 free(fn);
00175
00176 try {
00177 writer->set_buffer(cspace, buffer);
00178 writer->write();
00179 } catch (Exception &e) {
00180 throw;
00181 }
00182 }
00183
00184 }