seq_writer.cpp
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
00037
00038
00039
00040
00041
00042
00043
00044
00045 SeqWriter::SeqWriter(Writer* writer)
00046 {
00047 this->writer = writer;
00048
00049 frame_number = 0;
00050
00051 cspace = CS_UNKNOWN;
00052
00053 filename = 0;
00054 img_path = 0;
00055 }
00056
00057
00058
00059
00060 SeqWriter::~SeqWriter()
00061 {
00062 delete writer;
00063 writer = 0;
00064
00065 free(filename);
00066 free(img_path);
00067 }
00068
00069
00070
00071
00072 void SeqWriter::set_path(const char* img_path)
00073 {
00074 free(this->img_path);
00075 this->img_path = strdup(img_path);
00076 printf("SeqWriter: img path set to %s\n", this->img_path);
00077 }
00078
00079
00080
00081
00082
00083
00084 void SeqWriter::set_filename(const char* filename)
00085 {
00086 free(this->filename);
00087 this->filename = strdup(filename);
00088 }
00089
00090
00091
00092
00093
00094 void SeqWriter::set_dimensions(unsigned int width, unsigned int height)
00095 {
00096 writer->set_dimensions(width, height);
00097 }
00098
00099
00100
00101
00102 void SeqWriter::set_colorspace(colorspace_t cspace)
00103 {
00104 this->cspace = cspace;
00105 }
00106
00107
00108
00109
00110
00111 void SeqWriter::write(unsigned char *buffer)
00112 {
00113 ++frame_number;
00114 char* fn;
00115
00116 time_t now = time(NULL);
00117 struct tm now_tm;
00118 struct timeval now_tv;
00119
00120 gettimeofday(&now_tv, NULL);
00121 localtime_r(&now, &now_tm);
00122
00123 char* timestring;
00124 if (asprintf(×tring, "%04d%02d%02d_%02d%02d%02d_%06ld", now_tm.tm_year + 1900,
00125 now_tm.tm_mon + 1, now_tm.tm_mday, now_tm.tm_hour, now_tm.tm_min,
00126 now_tm.tm_sec, now_tv.tv_usec) == -1)
00127 {
00128 throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (1)");
00129 }
00130
00131 if (filename)
00132 {
00133
00134 if (img_path)
00135 {
00136 if (asprintf(&fn, "%s/%s_%s-%04u", img_path, timestring, filename, frame_number) == -1)
00137 {
00138 throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (2)");
00139 }
00140 }
00141 else
00142 {
00143 if (asprintf(&fn, "%s_%s-%04u", timestring, filename, frame_number) == -1)
00144 {
00145 throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (2)");
00146 }
00147 }
00148 }
00149 else
00150 {
00151
00152 if (img_path)
00153 {
00154 if (asprintf(&fn, "%s/%s-%04u", img_path, timestring, frame_number) == -1)
00155 {
00156 throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (3)");
00157 }
00158 }
00159 else
00160 {
00161 if (asprintf(&fn, "%s-%04u", timestring, frame_number) == -1)
00162 {
00163 throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (4)");
00164 }
00165 }
00166 }
00167
00168 writer->set_filename(fn);
00169 free(fn);
00170
00171 try {
00172 writer->set_buffer(cspace, buffer);
00173 writer->write();
00174 } catch (Exception &e) {
00175 throw;
00176 }
00177 }
00178