-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgm.cpp
127 lines (109 loc) · 2.13 KB
/
pgm.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "pgm.h"
#include <iostream>
#include <vector>
#include <fstream>
#include "grayPixel.h"
using namespace std;
Pgm::Pgm(string fileName, int no_of_rotations){
ifstream fin(fileName, ifstream::binary);
if(!fin.is_open())
{
cerr<<"Failed to open file";
return;
}
fin>> magicNumber;
fin.seekg(1,fin.cur);
char c;
fin.get(c);
if (c == '#')
{
while (c != '\n')
{
fin.get(c);
}
}
else
{
fin.seekg(-1, fin.cur);
}
fin >> width >> height >> max;
this->no_of_rotations = no_of_rotations;
if(magicNumber == "P2") {
grayScale = vector<vector<GrayPixel>> (height, vector<GrayPixel>(width));
for(int i =0; i<height; i++){
for(int j=0; j<width; j++){
int g;
fin >> g;
GrayPixel gray(g);
grayScale[i][j] = gray;
}
}
}
else{
grayScale = vector<vector<GrayPixel>> (height, vector<GrayPixel>(width));
fin.seekg(1, fin.cur);
for(int i =0; i<height; i++){
for(int j=0; j<width; j++){
char* a = new char[1];
fin.read(a,1);
grayScale[i][j] = GrayPixel((int)a[0]);
}
}
}
};
void Pgm::write(string filename, vector<vector<GrayPixel>> &arr) {
ofstream outfile(filename, ofstream::binary);
if (outfile.fail())
{
std::cout << "Failed to write " << endl;
return;
}
int width_w = width, height_w = height;
if(this->no_of_rotations & 1)
{
width_w = height;
height_w = width;
}
outfile << magicNumber << "\n" << width_w << " " << height_w << "\n" << max << "\n";
if(magicNumber == "P2")
{
for (int i = 0; i < height_w; i ++)
{
for (int j = 0; j < width_w; j ++)
{
outfile << arr[i][j].getGray() << "\n";
}
outfile << "\n";
}
}
else {
for(int i=0;i<height_w;i++)
{
for(int j=0;j<width_w;j++)
{
char* c = new char[1];
c[0] = (char)arr[i][j].getGray();
outfile.write(c,1);
}
}
}
return;
}
Pgm::~Pgm(){
grayScale.clear();
};
int Pgm::getWidth(){
return width;
}
int Pgm::getHeight(){
return height;
}
int Pgm::getMax(){
return max;
}
string Pgm::getMagicNumber(){
return magicNumber;
}
vector<vector<GrayPixel>> Pgm::getGray(){
return grayScale;
}