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 
32 using namespace fawkes;
33 
34 namespace 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  */
44 FvRawReader::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. */
68 FvRawReader::~FvRawReader()
69 {
70  fclose(infile);
71  opened = false;
72 }
73 
74 void
75 FvRawReader::set_buffer(unsigned char *yuv422planar_buffer)
76 {
77  buffer = yuv422planar_buffer;
78 }
79 
80 colorspace_t
81 FvRawReader::colorspace()
82 {
83  if (opened) {
84  return header.colorspace;
85  } else {
86  return CS_UNKNOWN;
87  }
88 }
89 
90 unsigned int
91 FvRawReader::pixel_width()
92 {
93  if (opened) {
94  return header.width;
95  } else {
96  return 0;
97  }
98 }
99 
100 unsigned int
101 FvRawReader::pixel_height()
102 {
103  if (opened) {
104  return header.height;
105  } else {
106  return 0;
107  }
108 }
109 
110 void
111 FvRawReader::read()
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  */
129 bool
130 FvRawReader::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) {
137  if (header.file_id == FvRawWriter::FILE_IDENTIFIER) {
138  fclose(f);
139  return true;
140  }
141  }
142  fclose(f);
143  }
144  return false;
145 }
146 
147 } // end namespace firevision
FvRaw image file header.
Definition: fvraw.h:51
Fawkes library namespace.
Base class for exceptions in Fawkes.
Definition: exception.h:35