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