Fawkes API  Fawkes Development Version
pnm.cpp
1 
2 /***************************************************************************
3  * pnm.cpp - PNM reader
4  *
5  * Generated: Sun Jan 13 16:23:08 2008
6  * Copyright 2007 Daniel Beck
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 <fvutils/readers/pnm.h>
25 #include <fvutils/color/colorspaces.h>
26 #include <fvutils/color/conversions.h>
27 #include <core/exception.h>
28 #include <core/exceptions/system.h>
29 
30 #include <cstdlib>
31 #include <cstring>
32 
33 using namespace fawkes;
34 
35 namespace firevision {
36 #if 0 /* just to make Emacs auto-indent happy */
37 }
38 #endif
39 
40 /** @class PNMReader <fvutils/readers/pnm.h>
41  * PNM file reader.
42  *
43  * @author Daniel Beck
44  */
45 
46 /** Constructor.
47  * @param filename name of the PNM file
48  */
49 PNMReader::PNMReader(const char* filename)
50 {
51  m_filename = strdup(filename);
52  m_pnmfile = fopen(m_filename, "rb");
53 
54  if ( m_pnmfile == NULL )
55  {
56  throw Exception("PNMReader::ctor: cannot open PNM file");
57  }
58 
59  // read header
60  char* line = (char*) malloc(80);
61 
62  // magic value
63  if (fgets(line, 80, m_pnmfile) == NULL)
64  {
65  throw FileReadException(m_filename, "Failed to read magic value");
66  }
67 
68  if ( strcmp("P6", line) > 0 )
69  {
70  throw Exception("PNMReader::ctor: unknown magic value");
71  }
72 
73  // comments
74  do
75  {
76  if (fgets(line, 80, m_pnmfile) == NULL)
77  {
78  throw FileReadException(m_filename, "Failed to read comments");
79  }
80  } while ( strncmp("#", line, 1) == 0);
81 
82  // width & height
83  char* tmp = (char*) malloc(10);
84  char* token;
85  token = strtok(line, " ");
86  if ( atoi(token) >= 0 ) { m_img_width = (unsigned int) atoi(token); }
87  else { throw Exception("PNMReader::ctor: could not read out image width"); };
88  token = strtok(NULL, " ");
89  if ( atoi(token) >= 0 ) { m_img_height = (unsigned int) atoi(token); }
90  else { throw Exception("PNMReader::ctor: could not read out image height"); };
91  free(tmp);
92 
93  // depth
94  if (fgets(line, 80, m_pnmfile) == NULL)
95  {
96  throw FileReadException(m_filename, "Failed to read depth");
97  }
98  int max = atoi(line);
99  free(line);
100  if ( max >= 0)
101  {
102  switch(max)
103  {
104  case 1:
105  m_img_depth = 1;
106  break;
107 
108  case 15:
109  m_img_depth = 2;
110  break;
111 
112  case 255:
113  m_img_depth = 3;
114  break;
115 
116  default:
117  break;
118  }
119  }
120  else
121  {
122  throw Exception("PNMReader::ctor: unknown color depth");
123  }
124 
125  size_t img_size = m_img_width * m_img_height * m_img_depth;
126  m_pnm_buffer = (unsigned char*) malloc(img_size);
127 }
128 
129 /** Destructor. */
130 PNMReader::~PNMReader()
131 {
132  fclose(m_pnmfile);
133  free(m_filename);
134  free(m_pnm_buffer);
135 }
136 
137 void
138 PNMReader::set_buffer(unsigned char* buffer)
139 {
140  m_yuv_buffer = buffer;
141 }
142 
143 colorspace_t
144 PNMReader::colorspace()
145 {
146  return YUV422_PLANAR;
147 }
148 
149 unsigned int
150 PNMReader::pixel_width()
151 {
152  return m_img_width;
153 }
154 
155 unsigned int
156 PNMReader::pixel_height()
157 {
158  return m_img_height;
159 }
160 
161 void
162 PNMReader::read()
163 {
164  if (m_yuv_buffer == NULL)
165  {
166  throw Exception("PNMReader::read: buffer = NULL");
167  }
168 
169  if (fread(m_pnm_buffer, m_img_depth, m_img_width * m_img_height, m_pnmfile) != m_img_width * m_img_height)
170  {
171  throw fawkes::FileReadException(m_filename, "Failed to read data");
172  }
173  convert(RGB, YUV422_PLANAR, m_pnm_buffer, m_yuv_buffer, m_img_width, m_img_height);
174 }
175 
176 } // end namespace firevision
File could not be read.
Definition: system.h:61
Fawkes library namespace.
Base class for exceptions in Fawkes.
Definition: exception.h:36