Fawkes API Fawkes Development Version
fvraw.cpp
1
2/***************************************************************************
3 * fvraw.h - FvRaw Reader
4 *
5 * Generated: Sun Jun 05 01:22:35 2006 (watching Terminator 2)
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 <fvutils/color/colorspaces.h>
26#include <fvutils/readers/fvraw.h>
27#include <fvutils/writers/fvraw.h>
28
29#include <cstdio>
30#include <errno.h>
31
32using namespace fawkes;
33
34namespace firevision {
35
36/** @class FvRawReader <fvutils/readers/fvraw.h>
37 * FvRaw image reader implementation.
38 * @author Tim Niemueller
39 */
40
41/** Constructor.
42 * @param filename filename to read from.
43 */
44FvRawReader::FvRawReader(const char *filename)
45{
46 opened = false;
47 buffer = NULL;
48
49 infile = fopen(filename, "r");
50
51 if (infile == NULL) {
52 throw Exception("Could not open file for reading");
53 }
54
55 if (fread((char *)&header, sizeof(header), 1, infile) != 1) {
56 throw Exception("Could not read header");
57 } else {
58 if (header.file_id != FvRawWriter::FILE_IDENTIFIER) {
59 throw("Invalid file identifier");
60 } else {
61 buffer_size = colorspace_buffer_size(header.colorspace, header.width, header.height);
62 opened = true;
63 }
64 }
65}
66
67/** Destructor. */
69{
70 fclose(infile);
71 opened = false;
72}
73
74void
75FvRawReader::set_buffer(unsigned char *yuv422planar_buffer)
76{
77 buffer = yuv422planar_buffer;
78}
79
80colorspace_t
82{
83 if (opened) {
84 return header.colorspace;
85 } else {
86 return CS_UNKNOWN;
87 }
88}
89
90unsigned int
92{
93 if (opened) {
94 return header.width;
95 } else {
96 return 0;
97 }
98}
99
100unsigned int
102{
103 if (opened) {
104 return header.height;
105 } else {
106 return 0;
107 }
108}
109
110void
112{
113 if (buffer == NULL) {
114 throw Exception("Read failed: buffer == NULL");
115 }
116 if (buffer_size == 0) {
117 throw Exception("Read failed: buffer_size == 0");
118 }
119
120 if (fread(buffer, buffer_size, 1, infile) != 1) {
121 throw Exception("Failed to read data", errno);
122 }
123}
124
125/** Check if given file contains FvRaw image.
126 * @param filename file to check
127 * @return true if file contains FvRaw image, false otherwise
128 */
129bool
130FvRawReader::is_FvRaw(const char *filename)
131{
132 FILE *f;
133 f = fopen(filename, "r");
134 if (f != NULL) {
136 if (fread((char *)&header, sizeof(header), 1, f) == 1) {
138 fclose(f);
139 return true;
140 }
141 }
142 fclose(f);
143 }
144 return false;
145}
146
147} // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void read()
Read data from file.
Definition: fvraw.cpp:111
static bool is_FvRaw(const char *filename)
Check if given file contains FvRaw image.
Definition: fvraw.cpp:130
FvRawReader(const char *filename)
Constructor.
Definition: fvraw.cpp:44
virtual ~FvRawReader()
Destructor.
Definition: fvraw.cpp:68
virtual unsigned int pixel_width()
Get width of read image in pixels.
Definition: fvraw.cpp:91
virtual colorspace_t colorspace()
Get colorspace from the just read image.
Definition: fvraw.cpp:81
virtual void set_buffer(unsigned char *yuv422planar_buffer)
Set buffer that the read image should be written to.
Definition: fvraw.cpp:75
virtual unsigned int pixel_height()
Get height of read image in pixels.
Definition: fvraw.cpp:101
static const unsigned int FILE_IDENTIFIER
File identifier for FvRaw images.
Definition: fvraw.h:48
Fawkes library namespace.
FvRaw image file header.
Definition: fvraw.h:52
unsigned int file_id
file id
Definition: fvraw.h:53
colorspace_t colorspace
color space
Definition: fvraw.h:54
unsigned int height
height of image in pixels
Definition: fvraw.h:56
unsigned int width
width of image in pixels
Definition: fvraw.h:55