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