Fawkes API  Fawkes Development Version
seq_writer.cpp
1 
2 /***************************************************************************
3  * seq_writer.cpp - Writes sequences of images
4  *
5  * Generated: Fri Jul 06 11:10:08 2007
6  * Copyright 2007 Daniel Beck
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvutils/writers/seq_writer.h>
25 #include <core/exceptions/system.h>
26 
27 #include <time.h>
28 #include <sys/time.h>
29 
30 #include <cstring>
31 #include <cstdlib>
32 #include <cstdio>
33 
34 using namespace fawkes;
35 
36 namespace firevision {
37 #if 0 /* just to make Emacs auto-indent happy */
38 }
39 #endif
40 
41 /** @class SeqWriter <fvutils/writers/seq_writer.h>
42  * Writes a sequence of images to disk.
43  *
44  * @author Daniel Beck
45  */
46 
47 /** Constructor.
48  * @param writer the actual image writer
49  */
50 SeqWriter::SeqWriter(Writer* writer)
51 {
52  this->writer = writer;
53 
54  frame_number = 0;
55 
56  cspace = CS_UNKNOWN;
57 
58  filename = 0;
59  img_path = 0;
60 }
61 
62 
63 /** Destructor.
64  */
65 SeqWriter::~SeqWriter()
66 {
67  delete writer;
68  writer = 0;
69 
70  free(filename);
71  free(img_path);
72 }
73 
74 /** Set the path to where the images are stored.
75  * @param img_path the image path
76  */
77 void SeqWriter::set_path(const char* img_path)
78 {
79  free(this->img_path);
80  this->img_path = strdup(img_path);
81  printf("SeqWriter: img path set to %s\n", this->img_path);
82 }
83 
84 /** Set a (base-) filename.
85  * If a filename is set the name of the files will look like this:
86  * filename_index.ext .
87  * @param filename the (base-) filename
88  */
89 void SeqWriter::set_filename(const char* filename)
90 {
91  free(this->filename);
92  this->filename = strdup(filename);
93 }
94 
95 /** Set the image dimensions.
96  * @param width the width of the image
97  * @param height the height of the image
98  */
99 void SeqWriter::set_dimensions(unsigned int width, unsigned int height)
100 {
101  writer->set_dimensions(width, height);
102 }
103 
104 /** Set the colorspace of the image.
105  * @param cspace the colospace
106  */
107 void SeqWriter::set_colorspace(colorspace_t cspace)
108 {
109  this->cspace = cspace;
110 }
111 
112 /** Write a single image to disk.
113  * A running number is added to the filename
114  * @param buffer the image buffer that is written to disk
115  */
116 void SeqWriter::write(unsigned char *buffer)
117 {
118  ++frame_number;
119  char* fn;
120 
121  time_t now = time(NULL);
122  struct tm now_tm;
123  struct timeval now_tv;
124 
125  gettimeofday(&now_tv, NULL);
126  localtime_r(&now, &now_tm);
127 
128  char* timestring;
129  if (asprintf(&timestring, "%04d%02d%02d_%02d%02d%02d_%06ld", now_tm.tm_year + 1900,
130  now_tm.tm_mon + 1, now_tm.tm_mday, now_tm.tm_hour, now_tm.tm_min,
131  now_tm.tm_sec, now_tv.tv_usec) == -1)
132  {
133  throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (1)");
134  }
135 
136  if (filename)
137  {
138  // filename: YYYYMMDD-hhmmss_uuuuuu_name_index.ext
139  if (img_path)
140  {
141  if (asprintf(&fn, "%s/%s_%s-%04u", img_path, timestring, filename, frame_number) == -1)
142  {
143  throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (2)");
144  }
145  }
146  else
147  {
148  if (asprintf(&fn, "%s_%s-%04u", timestring, filename, frame_number) == -1)
149  {
150  throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (2)");
151  }
152  }
153  }
154  else
155  {
156  // filename: YYYYMMDD-hhmmss_uuuuuu_index.ext
157  if (img_path)
158  {
159  if (asprintf(&fn, "%s/%s-%04u", img_path, timestring, frame_number) == -1)
160  {
161  throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (3)");
162  }
163  }
164  else
165  {
166  if (asprintf(&fn, "%s-%04u", timestring, frame_number) == -1)
167  {
168  throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (4)");
169  }
170  }
171  }
172 
173  writer->set_filename(fn);
174  free(fn);
175 
176  try {
177  writer->set_buffer(cspace, buffer);
178  writer->write();
179  } catch (Exception &e) {
180  throw;
181  }
182 }
183 
184 } // end namespace firevision
Fawkes library namespace.
Interface to write images.
Definition: writer.h:34
Base class for exceptions in Fawkes.
Definition: exception.h:36
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32