-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppm.cpp
executable file
·119 lines (107 loc) · 3.03 KB
/
ppm.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
#include "ppm.hpp"
#include "image.hpp"
#include <iostream>
#include <sstream>
#include <fstream>
void getline_ignore_comments(std::istream& in, std::string& l){
char c = '#';
do{
std::getline(in, l);
} while(l[0] == c); //loop until <line> is not a comment.
}
image openppm(std::string fname){
std::filebuf infile;
image img;
if (infile.open(fname, std::ios::in)){
std::istream is(&infile);
img = readppm(is);
infile.close();
}
else {
std::cerr << "Unable to open file.\n";
exit(1);
}
return img;
}
image readppm(std::istream& in){
std::string line;
//Get the magic number
getline_ignore_comments(in, line);
std::string format = line;
//Get the width and height of the image
getline_ignore_comments(in, line);
std::stringstream linestream(line);
int width, height;
linestream >> width >> height;
//get the max brightness value
getline_ignore_comments(in, line);
double max = std::stod(line);
std::vector<std::vector<pixel> > pixdata;
std::string lines = "";
while(std::getline(in, line)){
if(line[0] != '#') lines += line + " ";
}
linestream = std::stringstream(lines);
std::vector<pixel> temp;
double r, g, b, y;
//Grayscale image
if(format == "P2"){
while(linestream >> y && pixdata.size() < height){
temp.push_back(pixel(y/max));
if(temp.size() == width){
pixdata.push_back(temp);
temp.clear();
}
}
}
//Color Image
else if(format == "P3"){
while(linestream >> r >> g >> b && pixdata.size() < height){
temp.push_back(pixel(r/max, g/max, b/max));
if(temp.size() == width){
pixdata.push_back(temp);
temp.clear();
}
}
}
else{
std::cerr << "Unknown file type.\n";
exit(2);
}
image img(pixdata);
img.set_format(format);
return img;
}
int printppm(const image& img){
if(img.get_format() == "P1"){
std::cout << "P1\n" << img.c() << ' ' << img.r() << "\n";
for(int i = 0; i < img.r(); i++){
for(pixel pix : img[i]){
std::cout << int(1-pix.y) << ' ';
}
std::cout << std::endl;
}
return 0;
}
if(img.get_format() == "P2"){
std::cout << "P2\n" << img.c() << ' ' << img.r() << "\n255\n";
for(int i = 0; i < img.r(); i++){
for(pixel pix : img[i]){
std::cout << int(pix.y * 255) << ' ';
}
std::cout << std::endl;
}
return 0;
}
if(img.get_format() == "P3"){
std::cout << "P3\n" << img.c() << ' ' << img.r() << "\n255\n";
for(int i = 0; i < img.r(); i++){
for(pixel pix : img[i]){
std::cout << int(pix.r * 255) << ' ' << int(pix.g * 255) << ' ' << int(pix.b * 255) << ' ';
}
std::cout << std::endl;
}
return 0;
}
return 1;
}