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
70 static
72
74
83 //template <class T>
84 static // , const CommentReader & commentReader = CommentReader()
85 void read(Image & image, const std::string & path);
86
87
89 static
90 FileType readHeader(drain::image::ImageConf & conf, drain::FlexVariableMap & properties, std::istream & infile);
91
92
94 static
95 void readFrame(ImageFrame & image, const std::string & path);
96
97
99 static
100 void readFrame(ImageFrame & image, std::istream & infile, FileType fileType = PGM_RAW);
101
103 //static
104 //void readFrameASCII(ImageFrame & image, std::istream & infile);
105
107
115 static
116 void write(const ImageFrame &image, const std::string &path);
117
118
119protected:
120
121
122
123};
124
125
126/*
127template <class T>
128void FilePnm::read(T & image, const std::string & path, const CommentReader & commentReader) {
129
130 drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
131
132 mout.info("path='" , path , "'" );
133
134 std::ifstream infile;
135 infile.open(path.c_str(), std::ios::in);
136
137 if (!infile){
138 mout.warn("opening file '" , path , "' failed" );
139 return;
140 }
141
142 //std::string magic;
143 //infile >> magic;
144
145 if (infile.get() != 'P'){
146 mout.warn("file does not start with 'P' (magic code)" );
147 mout.error("not an PNM file" );
148 return;
149 }
150
151 FileType pt = UNDEFINED;
152 int width, height, channels, maxValue;
153
154 char c = infile.get();
155
156 switch (c){
157 case '1':
158 pt = PBM_ASC;
159 channels = 1;
160 break;
161 case '2':
162 pt = PGM_ASC;
163 channels = 1;
164 break;
165 case '3':
166 pt = PPM_ASC;
167 channels = 3;
168 break;
169 case '4':
170 pt = PBM_RAW;
171 channels = 1;
172 break;
173 case '5':
174 pt = PGM_RAW;
175 channels = 1;
176 break;
177 case '6':
178 pt = PPM_RAW;
179 channels = 3;
180 break;
181 default:
182 mout.error("unrecognized PPM type" );
183 }
184
185 mout.note("PNM type: P" , c , " (" , channels , " channels)" );
186
187 while ((c = infile.get()) != '\n'){
188 // ?
189 }
190
191 std::stringstream sstr;
192 while (infile.peek() == '#'){
193 while ((c = infile.get()) !='\n' ){
194 // std::cout << c;
195 if (infile.eof())
196 mout.error("Premature end of file" );
197 }
198 std::list<std::string> assignment;
199 drain::StringTools::split(sstr.str(), assignment, "=", " \t'\"");
200 if (assignment.size() > 1){
201
202 }
203 sstr.str("");
204 }
205
206 infile >> width;
207 infile >> height;
208 if ((pt != PBM_ASC) && (pt != PBM_RAW))
209 infile >> maxValue;
210
211 image.initialize(typeid(unsigned char), width, height, channels);
212
213 mout.debug(image );
214
215 readFrame(image, infile);
216
217 infile.close();
218
219
220}
221*/
222
223
224}
225
226}
227
228#endif /*FILEPng_H_*/
229
230// Drain
Definition FileInfo.h:96
Definition FileInfo.h:47
A map of FlexVariable:s.
Definition VariableMap.h:144
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 image files.
Definition FilePnm.h:71
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 Data.h:1473