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 namespace firevision {
00037 #if 0 /* just to make Emacs auto-indent happy */
00038 }
00039 #endif
00040 
00041 /** @class SeqWriter <fvutils/writers/seq_writer.h>
00042  * Writes a sequence of images to disk.
00043  *
00044  * @author Daniel Beck
00045  */
00046 
00047 /** Constructor.
00048  * @param writer the actual image writer
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 /** Destructor.
00064  */
00065 SeqWriter::~SeqWriter()
00066 {
00067   delete writer;
00068   writer = 0;
00069   
00070   free(filename);
00071   free(img_path);
00072 }
00073 
00074 /** Set the path to where the images are stored.
00075  * @param img_path the image path
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 /** Set a (base-) filename.
00085  * If a filename is set the name of the files will look like this:
00086  * filename_index.ext .
00087  * @param filename the (base-) filename
00088  */
00089 void SeqWriter::set_filename(const char* filename)
00090 {
00091   free(this->filename);
00092   this->filename = strdup(filename);
00093 }
00094 
00095 /** Set the image dimensions.
00096  * @param width the width of the image
00097  * @param height the height of the image
00098  */
00099 void SeqWriter::set_dimensions(unsigned int width, unsigned int height)
00100 {
00101   writer->set_dimensions(width, height);
00102 }
00103 
00104 /** Set the colorspace of the image.
00105  * @param cspace the colospace
00106  */
00107 void SeqWriter::set_colorspace(colorspace_t cspace)
00108 {
00109   this->cspace = cspace;
00110 }
00111 
00112 /** Write a single image to disk.
00113  * A running number is added to the filename
00114  * @param buffer the image buffer that is written to disk
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(&timestring, "%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       // filename: YYYYMMDD-hhmmss_uuuuuu_name_index.ext
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       // filename: YYYYMMDD-hhmmss_uuuuuu_index.ext
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 } // end namespace firevision

Generated on Tue Feb 22 13:32:16 2011 for Fawkes API by  doxygen 1.4.7