$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
pnm/load_header.hh
1 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_IO_PNM_LOAD_HEADER_HH
27 # define MLN_IO_PNM_LOAD_HEADER_HH
28 
32 
33 # include <cstdlib>
34 # include <iostream>
35 # include <fstream>
36 # include <string>
37 
38 
39 namespace mln
40 {
41 
42  namespace io
43  {
44 
45  namespace pnm
46  {
47 
48 # ifndef MLN_INCLUDE_ONLY
49 
50  inline
51  bool read_header(std::ifstream& istr,
52  char& type,
53  int& nrows, int& ncols,
54  unsigned int& maxval,
55  bool test = false)
56  {
57  // check magic
58  if (istr.get() != 'P' )
59  goto err;
60  type = static_cast<char>(istr.get());
61 
62  if (type < '1' || type > '6')
63  goto err;
64  if (istr.get() != '\n')
65  goto err;
66 
67  // skip comments
68  while (istr.peek() == '#')
69  {
70  std::string line;
71  std::getline(istr, line);
72  }
73 
74  // get size
75  istr >> ncols >> nrows;
76  if (nrows <= 0 || ncols <= 0)
77  goto err;
78 
79  // get maxvalue
80  if (istr.get() != '\n')
81  goto err;
82  if (type != '1' && type != '4')
83  {
84  istr >> maxval;
85  if (istr.get() != '\n')
86  goto err;
87  }
88  return true;
89 
90  err:
91  if (! test)
92  {
93  std::cerr << "error: badly formed header!";
94  std::abort();
95  }
96  return false;
97  }
98 
99  inline
100  void read_header(char ascii, char raw,
101  std::ifstream& istr,
102  char& type,
103  int& nrows, int& ncols,
104  unsigned int& maxval)
105  {
106  read_header(istr, type, nrows, ncols, maxval);
107  if (! (type == ascii || type == raw))
108  {
109  std::cerr << "error: bad pnm type; "
110  << "expected P" << ascii
111  << " or P" << raw
112  << ", get P" << type << "!";
113  std::abort();
114  }
115  }
116 
117  inline
118  void read_header(char ascii, char raw,
119  std::ifstream& istr,
120  char& type,
121  int& nrows, int& ncols)
122  {
123  unsigned int maxval;
124  read_header(ascii, raw, istr, type,
125  nrows, ncols, maxval);
126  }
127 
128 # endif // ! MLN_INCLUDE_ONLY
129 
130  } // end of namespace mln::io::pnm
131 
132  } // end of namespace mln::io
133 
134 } // end of namespace mln
135 
136 
137 #endif // ! MLN_IO_PNM_LOAD_HEADER_HH