FilePnm.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25  */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30  */
31 #ifndef FILE_PNM_H_
32 #define FILE_PNM_H_
33 
34 
35 #include <string>
36 #include <iostream>
37 #include <fstream>
38 #include <exception>
39 
40 
41 #include "drain/util/FileInfo.h"
42 //#include "drain/util/RegExp.h"
43 #include "Image.h"
44 
45 
46 namespace drain
47 {
48 namespace image
49 {
50 
51 // using namespace std;
52 
54 
59 {
60 public:
61 
62  enum FileType {
63  UNDEFINED=0,
64  PBM_ASC=1,
65  PGM_ASC=2,
66  PPM_ASC=3,
67  PBM_RAW=4,
68  PGM_RAW=5,
69  PPM_RAW=6
70  };
71 
73  //static
74  //const drain::RegExp fileNameRegExp;
75  static
77 
79 
88  //template <class T>
89  static // , const CommentReader & commentReader = CommentReader()
90  void read(Image & image, const std::string & path);
91 
92 
94  static
95  FileType readHeader(drain::image::ImageConf & conf, drain::FlexVariableMap & properties, std::istream & infile);
96 
97 
99  static
100  void readFrame(ImageFrame & image, const std::string & path);
101 
102 
104  static
105  void readFrame(ImageFrame & image, std::istream & infile, FileType fileType = PGM_RAW);
106 
108  //static
109  //void readFrameASCII(ImageFrame & image, std::istream & infile);
110 
112 
120  static
121  void write(const ImageFrame &image, const std::string &path);
122 
123 
124 protected:
125 
126 
127 
128 };
129 
130 
131 /*
132 template <class T>
133 void FilePnm::read(T & image, const std::string & path, const CommentReader & commentReader) {
134 
135  drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
136 
137  mout.info("path='" , path , "'" );
138 
139  std::ifstream infile;
140  infile.open(path.c_str(), std::ios::in);
141 
142  if (!infile){
143  mout.warn("opening file '" , path , "' failed" );
144  return;
145  }
146 
147  //std::string magic;
148  //infile >> magic;
149 
150  if (infile.get() != 'P'){
151  mout.warn("file does not start with 'P' (magic code)" );
152  mout.error("not an PNM file" );
153  return;
154  }
155 
156  FileType pt = UNDEFINED;
157  int width, height, channels, maxValue;
158 
159  char c = infile.get();
160 
161  switch (c){
162  case '1':
163  pt = PBM_ASC;
164  channels = 1;
165  break;
166  case '2':
167  pt = PGM_ASC;
168  channels = 1;
169  break;
170  case '3':
171  pt = PPM_ASC;
172  channels = 3;
173  break;
174  case '4':
175  pt = PBM_RAW;
176  channels = 1;
177  break;
178  case '5':
179  pt = PGM_RAW;
180  channels = 1;
181  break;
182  case '6':
183  pt = PPM_RAW;
184  channels = 3;
185  break;
186  default:
187  mout.error("unrecognized PPM type" );
188  }
189 
190  mout.note("PNM type: P" , c , " (" , channels , " channels)" );
191 
192  while ((c = infile.get()) != '\n'){
193  // ?
194  }
195 
196  std::stringstream sstr;
197  while (infile.peek() == '#'){
198  while ((c = infile.get()) !='\n' ){
199  // std::cout << c;
200  if (infile.eof())
201  mout.error("Premature end of file" );
202  }
203  std::list<std::string> assignment;
204  drain::StringTools::split(sstr.str(), assignment, "=", " \t'\"");
205  if (assignment.size() > 1){
206 
207  }
208  sstr.str("");
209  }
210 
211  infile >> width;
212  infile >> height;
213  if ((pt != PBM_ASC) && (pt != PBM_RAW))
214  infile >> maxValue;
215 
216  image.initialize(typeid(unsigned char), width, height, channels);
217 
218  mout.debug(image );
219 
220  readFrame(image, infile);
221 
222  infile.close();
223 
224 
225 }
226 */
227 
228 
229 }
230 
231 }
232 
233 #endif /*FILEPng_H_*/
234 
235 // Drain
Definition: FileInfo.h:97
Definition: FileInfo.h:48
A map of FlexVariable:s.
Definition: VariableMap.h:138
For reading and writing images in PNM format.
Definition: FilePnm.h:59
static void write(const ImageFrame &image, const std::string &path)
Read image array after essential metadata (type and geometry) has been processed.
Definition: FilePnm.cpp:439
static void read(Image &image, const std::string &path)
Reads a pnm file to an image.
Definition: FilePnm.cpp:189
static void readFrame(ImageFrame &image, const std::string &path)
Read image array, assuming type and geometry already set.
Definition: FilePnm.cpp:218
static FileType readHeader(drain::image::ImageConf &conf, drain::FlexVariableMap &properties, std::istream &infile)
Read image array after essential metadata (type and geometry) has been processed.
Definition: FilePnm.cpp:54
static const FileInfo fileInfo
Syntax for recognising pnm files.
Definition: FilePnm.h:76
Struct for image (excluding data)
Definition: ImageConf.h:333
Image with static geometry.
Definition: ImageFrame.h:67
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition: Image.h:184
Definition: DataSelector.cpp:1277