Fawkes API
Fawkes Development Version
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
All
Classes
Namespaces
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
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
*/
67
FvRawWriter::FvRawWriter
(
const
char
*filename,
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
*/
89
FvRawWriter::FvRawWriter
(
const
char
*filename,
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. */
106
FvRawWriter::~FvRawWriter
()
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
128
FvRawWriter::write
()
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
*
174
FvRawWriter::get_write_buffer
()
175
{
176
return
buffer;
177
}
178
179
}
// end namespace firevision
src
libs
fvutils
writers
fvraw.cpp
Generated by
1.8.1.2