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