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 <core/exceptions/system.h>
25 #include <fvutils/writers/seq_writer.h>
26 #include <sys/time.h>
27 
28 #include <cstdio>
29 #include <cstdlib>
30 #include <cstring>
31 #include <time.h>
32 
33 using namespace fawkes;
34 
35 namespace firevision {
36 
37 /** @class SeqWriter <fvutils/writers/seq_writer.h>
38  * Writes a sequence of images to disk.
39  *
40  * @author Daniel Beck
41  */
42 
43 /** Constructor.
44  * @param writer the actual image writer
45  */
46 SeqWriter::SeqWriter(Writer *writer)
47 {
48  this->writer = writer;
49 
50  frame_number = 0;
51 
52  cspace = CS_UNKNOWN;
53 
54  filename = 0;
55  img_path = 0;
56 }
57 
58 /** Destructor.
59  */
60 SeqWriter::~SeqWriter()
61 {
62  delete writer;
63  writer = 0;
64 
65  free(filename);
66  free(img_path);
67 }
68 
69 /** Set the path to where the images are stored.
70  * @param img_path the image path
71  */
72 void
73 SeqWriter::set_path(const char *img_path)
74 {
75  free(this->img_path);
76  this->img_path = strdup(img_path);
77  printf("SeqWriter: img path set to %s\n", this->img_path);
78 }
79 
80 /** Set a (base-) filename.
81  * If a filename is set the name of the files will look like this:
82  * filename_index.ext .
83  * @param filename the (base-) filename
84  */
85 void
86 SeqWriter::set_filename(const char *filename)
87 {
88  free(this->filename);
89  this->filename = strdup(filename);
90 }
91 
92 /** Set the image dimensions.
93  * @param width the width of the image
94  * @param height the height of the image
95  */
96 void
97 SeqWriter::set_dimensions(unsigned int width, unsigned int height)
98 {
99  writer->set_dimensions(width, height);
100 }
101 
102 /** Set the colorspace of the image.
103  * @param cspace the colospace
104  */
105 void
106 SeqWriter::set_colorspace(colorspace_t cspace)
107 {
108  this->cspace = cspace;
109 }
110 
111 /** Write a single image to disk.
112  * A running number is added to the filename
113  * @param buffer the image buffer that is written to disk
114  */
115 void
116 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,
130  "%04d%02d%02d_%02d%02d%02d_%06ld",
131  now_tm.tm_year + 1900,
132  now_tm.tm_mon + 1,
133  now_tm.tm_mday,
134  now_tm.tm_hour,
135  now_tm.tm_min,
136  now_tm.tm_sec,
137  now_tv.tv_usec)
138  == -1) {
139  throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (1)");
140  }
141 
142  if (filename) {
143  // filename: YYYYMMDD-hhmmss_uuuuuu_name_index.ext
144  if (img_path) {
145  if (asprintf(&fn, "%s/%s_%s-%04u", img_path, timestring, filename, frame_number) == -1) {
146  throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (2)");
147  }
148  } else {
149  if (asprintf(&fn, "%s_%s-%04u", timestring, filename, frame_number) == -1) {
150  throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (2)");
151  }
152  }
153  } else {
154  // filename: YYYYMMDD-hhmmss_uuuuuu_index.ext
155  if (img_path) {
156  if (asprintf(&fn, "%s/%s-%04u", img_path, timestring, frame_number) == -1) {
157  throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (3)");
158  }
159  } else {
160  if (asprintf(&fn, "%s-%04u", timestring, frame_number) == -1) {
161  throw OutOfMemoryException("SeqWriter::write(): asprintf() failed (4)");
162  }
163  }
164  }
165 
166  writer->set_filename(fn);
167  free(fn);
168 
169  try {
170  writer->set_buffer(cspace, buffer);
171  writer->write();
172  } catch (Exception &e) {
173  throw;
174  }
175 }
176 
177 } // end namespace firevision
Fawkes library namespace.
Interface to write images.
Definition: writer.h:31
Base class for exceptions in Fawkes.
Definition: exception.h:35
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:31