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