-
Notifications
You must be signed in to change notification settings - Fork 0
/
PGMImage.cpp
107 lines (93 loc) · 2.52 KB
/
PGMImage.cpp
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
//
// Created by juan on 14/11/20.
//
#include "PGMImage.h"
#include <bits/stdc++.h>
#include "Tools.h"
PGMImage::PGMImage(string filename) {
ifstream file(filename);
stringstream caster;
string line;
// verificar que archivo se pueda abrir
if (!file.good()) {
cout << "ERROR OPENING FILE IMG: " << filename << endl;
return;
}
//read header
while (getline(file, line)) {
if (line.at(0) == '#') //ignore comment
continue;
if (!hasHeader) {
if (line.at(0) != 'P' && line.at(1) != '2') {
cout << "ERROR: NO HEADER " << endl;
return;
}
hasHeader = true;
continue;
}
if (nRows == -1 && nCols == -1) {
stringstream ss(line);
string splitLine;
getline(ss, splitLine, ' ');
caster << splitLine;
caster >> nCols;
caster.clear();
getline(ss, splitLine, ' ');
caster << splitLine;
caster >> nRows;
pixels.assign(nRows, vector<unsigned char>(nCols, 0));
transpose.assign(nCols, vector<unsigned char>(nRows, 0));
continue;
} else if (max == -1) {
caster.clear();
caster << line;
caster >> max;
}
break;
}
//read data
unsigned int number, i = 0, j = 0;
while(getline(file, line)){
stringstream ss(line);
string splitLine;
while(getline(ss, splitLine, ' ')){
caster.clear();
caster << splitLine;
caster >> number;
pixels[i][j] = number;
j++;
if (j%nCols == 0){
j = 0;
i++;
}
}
}
file.close();
}
void PGMImage::writeFile(string filename) {
ofstream file(filename);
file << "P2\n";
file << "# IMG by Juan Luis" << endl;
file << nCols << " " << nRows << endl;
file << max << endl;
//write data
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nCols; j++) {
file << (unsigned int) pixels[i][j] << endl;
}
}
cout << "IMG writen successful" << endl;
file.close();
}
void PGMImage::showData() {
cout << "rows: " << nRows << endl;
cout << "cols: " << nCols << endl;
cout << "max: " << max << endl;
cout << pixels << endl;
}
int PGMImage::getCols() {
return nCols;
}
int PGMImage::getRows() {
return nRows;
}