seq_writer.cpp

00001
00002 /***************************************************************************
00003  *  seq_writer.cpp - Writes sequences of images
00004  *
00005  *  Generated: Fri Jul 06 11:10:08 2007
00006  *  Copyright  2007  Daniel Beck
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
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 /** @class SeqWriter <fvutils/writers/seq_writer.h>
00037  * Writes a sequence of images to disk.
00038  *
00039  * @author Daniel Beck
00040  */
00041 
00042 /** Constructor.
00043  * @param writer the actual image writer
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 /** Destructor.
00059  */
00060 SeqWriter::~SeqWriter()
00061 {
00062   delete writer;
00063   writer = 0;
00064
00065   free(filename);
00066   free(img_path);
00067 }
00068 
00069 /** Set the path to where the images are stored.
00070  * @param img_path the image path
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 /** Set a (base-) filename.
00080  * If a filename is set the name of the files will look like this:
00081  * filename_index.ext .
00082  * @param filename the (base-) filename
00083  */
00084 void SeqWriter::set_filename(const char* filename)
00085 {
00086   free(this->filename);
00087   this->filename = strdup(filename);
00088 }
00089 
00090 /** Set the image dimensions.
00091  * @param width the width of the image
00092  * @param height the height of the image
00093  */
00094 void SeqWriter::set_dimensions(unsigned int width, unsigned int height)
00095 {
00096   writer->set_dimensions(width, height);
00097 }
00098 
00099 /** Set the colorspace of the image.
00100  * @param cspace the colospace
00101  */
00102 void SeqWriter::set_colorspace(colorspace_t cspace)
00103 {
00104   this->cspace = cspace;
00105 }
00106 
00107 /** Write a single image to disk.
00108  * A running number is added to the filename
00109  * @param buffer the image buffer that is written to disk
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(&timestring, "%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       // filename: YYYYMMDD-hhmmss_uuuuuu_name_index.ext
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       // filename: YYYYMMDD-hhmmss_uuuuuu_index.ext
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