-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathpgmread.cpp
More file actions
257 lines (237 loc) · 8.36 KB
/
pgmread.cpp
File metadata and controls
257 lines (237 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright 2016, Simula Research Laboratory
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/trim.hpp>
#include "pgmread.h"
#define RGB2GRAY_IN_INT
#ifdef RGB2GRAY_IN_INT
// #define RATE_SHIFT 24
// #define R_RATE (uint32_t)(0.298912F * (float)(1<<RATE_SHIFT))
// #define G_RATE (uint32_t)(0.586611F * (float)(1<<RATE_SHIFT))
// #define B_RATE (uint32_t)(0.114478F * (float)(1<<RATE_SHIFT))
// the following are OpenCV's numbers
#define RATE_SHIFT 14
#define R_RATE (uint32_t)4899
#define G_RATE (uint32_t)9617
#define B_RATE (uint32_t)1868
#else
#define R_RATE 0.298912f
#define G_RATE 0.586611f
#define B_RATE 0.114478f
#endif
using namespace std;
unsigned char* readPGMfile( const string& filename, int& w, int& h )
{
boost::filesystem::path input_file( filename );
if( ! boost::filesystem::exists( input_file ) ) {
cerr << "File " << input_file << " does not exist" << endl;
return nullptr;
}
ifstream pgmfile( filename.c_str(), ios::binary );
if( ! pgmfile.is_open() ) {
cerr << "File " << input_file << " could not be opened for reading" << endl;
return nullptr;
}
string pgmtype;
do {
getline( pgmfile, pgmtype ); // this is the string version of getline()
if( pgmfile.fail() ) {
cerr << "File " << input_file << " is too short" << endl;
return nullptr;
}
boost::algorithm::trim_left( pgmtype ); // nice because of trim
} while( pgmtype.at(0) == '#' );
int type;
if( pgmtype.substr(0,2) == "P2" ) type = 2;
else if( pgmtype.substr(0,2) == "P3" ) type = 3;
else if( pgmtype.substr(0,2) == "P5" ) type = 5;
else if( pgmtype.substr(0,2) == "P6" ) type = 6;
else {
cerr << "File " << input_file << " can only contain P2, P3, P5 or P6 PGM images" << endl;
return nullptr;
}
const int maxLineSize{1000};
char line[maxLineSize];
char* parse{nullptr};
int maxval{};
do {
pgmfile.getline( line, maxLineSize );
if( pgmfile.fail() ) {
cerr << "File " << input_file << " is too short" << endl;
return nullptr;
}
int num = pgmfile.gcount();
parse = line;
while( (num-- > 0) && isspace( *parse ) ) {
parse++;
}
if( *parse == '#' ) continue;
const int ct = sscanf( parse, "%d %d", &w, &h );
if( ct != 2 ) {
cerr << "Error in " << __FILE__ << ":" << __LINE__ << endl
<< "File " << input_file << " PGM type header (" << type << ") must be followed by comments and WxH info" << endl
<< "but line contains " << parse << endl;
return nullptr;
}
} while( *parse == '#' );
if( w <= 0 || h <= 0 ) {
cerr << "File " << input_file << " has meaningless image size" << endl;
return nullptr;
}
do {
pgmfile.getline( line, maxLineSize );
if( pgmfile.fail() ) {
cerr << "File " << input_file << " is too short" << endl;
return nullptr;
}
int num = pgmfile.gcount();
parse = line;
while( (num-- > 0) && isspace( *parse ) ) {
parse++;
}
if( *parse == '#' ) continue;
const int ct = sscanf( parse, "%d", &maxval );
if( ct != 1 ) {
cerr << "File " << input_file << " PGM dimensions must be followed by comments and max value info" << endl;
return nullptr;
}
} while( *parse == '#' );
auto input_data = new unsigned char[ w * h ];
switch( type )
{
case 2 :
for( int i=0; i<w*h; i++ ) {
int input;
pgmfile >> input;
if( maxval == 255 ) {
input_data[i] = input;
} else {
input_data[i] = (unsigned char)(input * 255.0 / maxval );
}
if( pgmfile.fail() ) {
cerr << "File " << input_file << " file too short" << endl;
delete [] input_data;
return nullptr;
}
}
break;
case 3 :
{
auto i2 = new unsigned char[ w * h * 3 ];
unsigned char* src = i2;
for( int i=0; i<w*h*3; i++ ) {
int input;
pgmfile >> input;
if( maxval == 255 ) {
i2[i] = input;
} else {
i2[i] = (unsigned char)(input * 255.0 / maxval );
}
if( pgmfile.fail() ) {
cerr << "File " << input_file << " file too short" << endl;
delete [] i2;
delete [] input_data;
return nullptr;
}
}
for( int i=0; i<w*h; i++ ) {
#ifdef RGB2GRAY_IN_INT
const unsigned int r = *src; src++;
const unsigned int g = *src; src++;
const unsigned int b = *src; src++;
const unsigned int res = ( ( R_RATE*r+G_RATE*g+B_RATE*b ) >> RATE_SHIFT );
input_data[i] = (unsigned char)res;
#else // RGB2GRAY_IN_INT
const float r = *src; src++;
const float g = *src; src++;
const float b = *src; src++;
input_data[i] = (unsigned char)( R_RATE*r+G_RATE*g+B_RATE*b );
#endif // RGB2GRAY_IN_INT
}
delete [] i2;
}
break;
case 5 :
if( maxval < 256 ) {
pgmfile.read( (char*)input_data, w*h );
} else {
auto i2 = new unsigned short[ w * h ];
pgmfile.read( (char*)i2, w*h*2 );
if( pgmfile.fail() ) {
cerr << "File " << input_file << " file too short" << endl;
delete [] i2;
delete [] input_data;
return nullptr;
}
for( int i=0; i<w*h; i++ ) {
input_data[i] = (unsigned char)(i2[i] * 255.0 / maxval );
}
delete [] i2;
}
break;
case 6 :
if( maxval < 256 ) {
auto i2 = new unsigned char[ w * h * 3 ];
unsigned char* src = i2;
pgmfile.read( (char*)i2, w*h*3 );
if( pgmfile.fail() ) {
cerr << "File " << input_file << " file too short" << endl;
delete [] i2;
delete [] input_data;
return nullptr;
}
for( int i=0; i<w*h; i++ ) {
#ifdef RGB2GRAY_IN_INT
unsigned int r = *src; src++;
unsigned int g = *src; src++;
unsigned int b = *src; src++;
unsigned int res = ( ( R_RATE*r+G_RATE*g+B_RATE*b ) >> RATE_SHIFT );
input_data[i] = (unsigned char)res;
#else // RGB2GRAY_IN_INT
float r = *src; src++;
float g = *src; src++;
float b = *src; src++;
input_data[i] = (unsigned char)( R_RATE*r+G_RATE*g+B_RATE*b );
#endif // RGB2GRAY_IN_INT
}
delete [] i2;
} else {
auto i2 = new unsigned short[ w * h * 2 * 3 ];
unsigned short* src = i2;
pgmfile.read( (char*)i2, w*h*2*3 );
if( pgmfile.fail() ) {
cerr << "File " << input_file << " file too short" << endl;
delete [] i2;
delete [] input_data;
return 0;
}
for( int i=0; i<w*h; i++ ) {
#ifdef RGB2GRAY_IN_INT
unsigned int r = *src; src++;
unsigned int g = *src; src++;
unsigned int b = *src; src++;
unsigned int res = ( ( R_RATE*r+G_RATE*g+B_RATE*b ) >> RATE_SHIFT );
input_data[i] = (unsigned char)res;
#else // RGB2GRAY_IN_INT
float r = *src; src++;
float g = *src; src++;
float b = *src; src++;
input_data[i] = (unsigned char)( R_RATE*r+G_RATE*g+B_RATE*b );
#endif // RGB2GRAY_IN_INT
}
delete [] i2;
}
break;
default:
throw std::runtime_error("unsupported type " + std::to_string(type));
}
return input_data;
}