Fawkes API  Fawkes Development Version
writer.cpp
1 
2 /***************************************************************************
3  * writer.cpp - Writer interface
4  *
5  * Generated: Tue Mar 27 17:24:55 2007
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
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/writer.h>
25 
26 #include <core/exception.h>
27 #include <core/exceptions/system.h>
28 
29 #include <cstring>
30 #include <cstdlib>
31 #include <cstdio>
32 
33 namespace firevision {
34 #if 0 /* just to make Emacs auto-indent happy */
35 }
36 #endif
37 
38 /** @class Writer <fvutils/writers/writer.h>
39  * Interface to write images.
40  * The writer interface defines the general API for image writers. These
41  * writers are used to write images to files on your harddrive (like JPEGs,
42  * PNGs etc.).
43  *
44  * @author Tim Niemueller
45  */
46 
47 /** @fn void Writer::write()
48  * Write to file.
49  */
50 
51 /** @var Writer::filename
52  * The complete filename.
53  */
54 
55 /** @var Writer::basename
56  * The basename of the file.
57  */
58 /** @var Writer::extension
59  * The extension of the file.
60  */
61 /** @var Writer::width
62  * The width of the image.
63  */
64 /** @var Writer::height
65  * The height of the image.
66  */
67 /** @var Writer::cspace
68  * The colorspace of the image.
69  */
70 /** @var Writer::buffer
71  * The image-buffer.
72  */
73 
74 /** Constructor.
75  * @param extension the file extension
76  */
77 Writer::Writer(const char *extension)
78 {
79  basename = 0;
80  filename = 0;
81 
82  this->extension = 0;
83  if (0 != extension) {
84  this->extension = strdup(extension);
85  }
86 
87  width = 0;
88  height = 0;
89  cspace = CS_UNKNOWN;
90  buffer = 0;
91 }
92 
93 /** Virtual empty destructor. */
95 {
96  free(filename);
97  free(basename);
98  free(extension);
99 }
100 
101 /** Set filename.
102  * @param filename name of file to write to. This can either be the complete filename
103  * (including) extension or the basename only in which case the extension is added.
104  */
105 void
107 {
108  free(this->filename);
109 
110  if ( 0 != strstr(filename, ".") ) {
111  this->filename = strdup(filename);
112  } else {
113  free(this->basename);
114  this->basename = strdup(filename);
115 
116  // re-generate complete filename
117  if (0 == extension) {
118  throw fawkes::Exception("Extension not set");
119  }
120 
121  if (asprintf(&(this->filename), "%s.%s", basename, extension) == -1) {
122  throw fawkes::OutOfMemoryException("Writer::set_filename(): asprintf() failed");
123  }
124  }
125 }
126 
127 /** Set dimensions of image in pixels.
128  * @param width width of image in pixels
129  * @param height height of image in pixels.
130  */
131 void
132 Writer::set_dimensions(unsigned int width, unsigned int height)
133 {
134  this->width = width;
135  this->height = height;
136 }
137 
138 /** Set image buffer.
139  * @param cspace color space of image
140  * @param buffer buffer of image
141  */
142 void
143 Writer::set_buffer(colorspace_t cspace, unsigned char *buffer)
144 {
145  this->cspace = cspace;
146  this->buffer = buffer;
147 }
148 
149 /** Set the filename extension for file written by this writer.
150  * @param extension the extension
151  */
152 void
154 {
155  free(this->extension);
156  this->extension = strdup(extension);
157 
158  // re-generate complete filename
159  free(this->filename);
160  this->filename = (char *) malloc( strlen(basename) + strlen(extension) + 1 );
161  strcpy(filename, basename);
162  strcat(this->filename, ".");
163  strcat(filename, extension);
164 }
165 
166 } // end namespace firevision
colorspace_t cspace
The colorspace of the image.
Definition: writer.h:55
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: writer.cpp:143
virtual void set_extension(const char *extension)
Set the filename extension for file written by this writer.
Definition: writer.cpp:153
Writer(const char *extension=0)
Constructor.
Definition: writer.cpp:77
virtual ~Writer()
Virtual empty destructor.
Definition: writer.cpp:94
Base class for exceptions in Fawkes.
Definition: exception.h:36
char * basename
The basename of the file.
Definition: writer.h:49
virtual void set_filename(const char *filename)
Set filename.
Definition: writer.cpp:106
char * extension
The extension of the file.
Definition: writer.h:50
unsigned int width
The width of the image.
Definition: writer.h:52
unsigned char * buffer
The image-buffer.
Definition: writer.h:57
unsigned int height
The height of the image.
Definition: writer.h:53
virtual void set_dimensions(unsigned int width, unsigned int height)
Set dimensions of image in pixels.
Definition: writer.cpp:132
char * filename
The complete filename.
Definition: writer.h:48
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32