Fawkes API  Fawkes Development Version
fvraw.cpp
1 
2 /***************************************************************************
3  * fvraw.cpp - writer for FireVision raw files
4  *
5  * Generated: Sat Mar 25 00:15:47 2006
6  * Copyright 2005-2009 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 <fvutils/writers/fvraw.h>
26 
27 #include <string.h>
28 #include <stdlib.h>
29 
30 #include <cstdio>
31 #include <cerrno>
32 
33 using namespace fawkes;
34 
35 namespace firevision {
36 #if 0 /* just to make Emacs auto-indent happy */
37 }
38 #endif
39 
40 /** File identifier for FvRaw images. */
41 const unsigned int FvRawWriter::FILE_IDENTIFIER = 0x17559358; // 16
42 
43 /** @class FvRawWriter <fvutils/writers/fvraw.h>
44  * FvRaw Writer implementation.
45  * This class allows for writing FvRaw images to a file.
46  * @author Tim Niemueller
47  */
48 
49 /** Constructor. */
50 FvRawWriter::FvRawWriter()
51  : Writer("raw")
52 {
53  header.file_id = FILE_IDENTIFIER;
54  header.width = 0;
55  header.height = 0;
56  header.colorspace = CS_UNKNOWN;
57 
58  buffer = NULL;
59 }
60 
61 
62 /** Constructor.
63  * @param filename file name to write to
64  * @param width width of image
65  * @param height height of image
66  */
68  unsigned int width, unsigned int height)
69  : Writer("raw")
70 {
71  set_filename(filename);
72 
73  header.file_id = FILE_IDENTIFIER;
74  header.width = width;
75  header.height = height;
76  header.colorspace = CS_UNKNOWN;
77 
78  buffer = NULL;
79 }
80 
81 
82 /** Constructor.
83  * @param filename file name to write to
84  * @param width width of image
85  * @param height height of image
86  * @param colorspace colorspace
87  * @param buffer buffer
88  */
90  unsigned int width, unsigned int height,
91  colorspace_t colorspace, unsigned char *buffer)
92  : Writer("raw")
93 {
94  set_filename(filename);
95 
96  header.file_id = FILE_IDENTIFIER;
97  header.width = width;
98  header.height = height;
99  header.colorspace = colorspace;
100 
101  this->buffer = buffer;
102 }
103 
104 
105 /** Destructor. */
107 {
108 }
109 
110 
111 void
112 FvRawWriter::set_dimensions(unsigned int width, unsigned int height)
113 {
114  header.width = width;
115  header.height = height;
116 }
117 
118 
119 void
120 FvRawWriter::set_buffer(colorspace_t cspace, unsigned char *buffer)
121 {
122  header.colorspace = cspace;
123  this->buffer = buffer;
124 }
125 
126 
127 void
129 {
130  if ( strlen(filename) == 0 ) {
131  throw Exception("Cannot write if no file name given");
132  }
133  if ( header.width == 0 ) {
134  throw Exception("Cannot write if width = 0");
135  }
136  if ( header.height == 0 ) {
137  throw Exception("Cannot write if height = 0");
138  }
139  if ( header.colorspace == CS_UNKNOWN ) {
140  throw Exception("Cannot write if colorspace unknown");
141  }
142  if ( buffer == NULL ) {
143  throw Exception("Cannot write if no buffer set");
144  }
145 
146  FILE *imagefile=fopen(filename, "w");
147  if( imagefile == NULL) {
148  throw Exception("Cannot not open file for writing");
149  }
150 
151  unsigned int buffer_size = colorspace_buffer_size(header.colorspace,
152  header.width,
153  header.height);
154 
155  if ( fwrite((const char *)&header, 1, sizeof(header), imagefile) != sizeof(header) ) {
156  throw Exception("Cannot write header to file", errno);
157  fclose(imagefile);
158  }
159 
160  if ( fwrite((const char *)buffer, 1, buffer_size, imagefile) != buffer_size ) {
161  throw Exception("Cannot write data to file", errno);
162  fclose(imagefile);
163  }
164 
165  fclose(imagefile);
166 
167 }
168 
169 
170 /** Get write buffer.
171  * @return write buffer
172  */
173 unsigned char *
175 {
176  return buffer;
177 }
178 
179 } // end namespace firevision
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: fvraw.cpp:120
colorspace_t cspace
The colorspace of the image.
Definition: writer.h:55
Fawkes library namespace.
Interface to write images.
Definition: writer.h:34
static const unsigned int FILE_IDENTIFIER
File identifier for FvRaw images.
Definition: fvraw.h:50
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void set_dimensions(unsigned int width, unsigned int height)
Set dimensions of image in pixels.
Definition: fvraw.cpp:112
unsigned int file_id
file id
Definition: fvraw.h:54
unsigned int height
height of image in pixels
Definition: fvraw.h:57
virtual void write()
Write to file.
Definition: fvraw.cpp:128
virtual void set_filename(const char *filename)
Set filename.
Definition: writer.cpp:106
colorspace_t colorspace
color space
Definition: fvraw.h:55
unsigned int width
width of image in pixels
Definition: fvraw.h:56
virtual ~FvRawWriter()
Destructor.
Definition: fvraw.cpp:106
virtual unsigned char * get_write_buffer()
Get write buffer.
Definition: fvraw.cpp:174
unsigned int width
The width of the image.
Definition: writer.h:52
unsigned int height
The height of the image.
Definition: writer.h:53
FvRawWriter()
Constructor.
Definition: fvraw.cpp:50
char * filename
The complete filename.
Definition: writer.h:48