|
7 | 7 | #include "ppm.h"
|
8 | 8 |
|
9 | 9 | //init with default values
|
10 |
| -void ppm::init(){ |
11 |
| - flag_alloc = false; |
12 |
| - width=0; |
13 |
| - height=0; |
14 |
| - max_col_val=255; |
| 10 | + |
| 11 | +void ppm::init() { |
| 12 | + flag_alloc = false; |
| 13 | + width = 0; |
| 14 | + height = 0; |
| 15 | + max_col_val = 255; |
15 | 16 | }
|
16 | 17 |
|
17 | 18 | //create a PPM object
|
18 |
| -ppm::ppm(){ |
19 |
| - init(); |
| 19 | + |
| 20 | +ppm::ppm() { |
| 21 | + init(); |
20 | 22 | }
|
21 | 23 |
|
22 | 24 | //create a PPM object and fill it with data stored in fname
|
23 |
| -ppm::ppm(const std::string &fname){ |
24 |
| - init(); |
25 |
| - read(fname); |
| 25 | + |
| 26 | +ppm::ppm(const std::string &fname) { |
| 27 | + init(); |
| 28 | + read(fname); |
26 | 29 | }
|
27 | 30 |
|
28 | 31 | //create an "epmty" PPM image with a given width and height;the R,G,B arrays are filled with zeros
|
29 |
| -ppm::ppm(const unsigned int _width, const unsigned int _height){ |
30 |
| - init(); |
31 |
| - width = _width; |
32 |
| - height = _height; |
33 |
| - nr_lines = height; |
34 |
| - nr_columns = width; |
35 |
| - size = width*height; |
36 |
| - |
37 |
| - r = new unsigned char[size]; |
38 |
| - g = new unsigned char[size]; |
39 |
| - b = new unsigned char[size]; |
40 |
| - flag_alloc = true; |
41 |
| - |
42 |
| - for(unsigned int i = 0; i < size; i++){ |
43 |
| - r[i] = 0; |
44 |
| - g[i] = 0; |
45 |
| - b[i] = 0; |
46 |
| - } |
| 32 | + |
| 33 | +ppm::ppm(const unsigned int _width, const unsigned int _height) { |
| 34 | + init(); |
| 35 | + width = _width; |
| 36 | + height = _height; |
| 37 | + nr_lines = height; |
| 38 | + nr_columns = width; |
| 39 | + size = width*height; |
| 40 | + |
| 41 | + r = new unsigned char[size]; |
| 42 | + g = new unsigned char[size]; |
| 43 | + b = new unsigned char[size]; |
| 44 | + flag_alloc = true; |
| 45 | + |
| 46 | + for (unsigned int i = 0; i < size; ++i) { |
| 47 | + r[i] = 0; |
| 48 | + g[i] = 0; |
| 49 | + b[i] = 0; |
| 50 | + } |
47 | 51 | }
|
48 | 52 |
|
49 | 53 | //free the memory used by the R,G,B vectors when the object is destroyed
|
50 |
| -ppm::~ppm(){ |
51 |
| - if(flag_alloc){ |
52 |
| - delete [] r; |
53 |
| - delete [] g; |
54 |
| - delete [] b; |
55 |
| - } |
| 54 | + |
| 55 | +ppm::~ppm() { |
| 56 | + if (flag_alloc) { |
| 57 | + delete [] r; |
| 58 | + delete [] g; |
| 59 | + delete [] b; |
| 60 | + } |
56 | 61 | }
|
57 | 62 |
|
58 | 63 | //read the PPM image from fname
|
59 |
| -void ppm::read(const std::string &fname){ |
60 |
| - std::ifstream inp(fname.c_str(), std::ios::in|std::ios::binary); |
61 |
| - if(inp.is_open()){ |
62 |
| - std::string line; |
63 |
| - std::getline(inp,line); |
64 |
| - if(line != "P6"){ |
65 |
| - std::cout<<"Error. Unrecognized file format."<<std::endl; |
66 |
| - return; |
67 |
| - } |
68 |
| - std::getline(inp,line); |
69 |
| - while(line[0]=='#'){ |
70 |
| - std::getline(inp,line); |
71 |
| - } |
72 |
| - std::stringstream dimensions(line); |
73 |
| - |
74 |
| - try{ |
75 |
| - dimensions>>width; |
76 |
| - dimensions>>height; |
77 |
| - nr_lines = height; |
78 |
| - nr_columns = width; |
79 |
| - } |
80 |
| - catch(std::exception &e){ |
81 |
| - std::cout<<"Header file format error. "<<e.what()<<std::endl; |
82 |
| - return; |
83 |
| - } |
84 |
| - |
85 |
| - std::getline(inp,line); |
86 |
| - std::stringstream max_val(line); |
87 |
| - try{ |
88 |
| - max_val>>max_col_val; |
89 |
| - } |
90 |
| - catch(std::exception &e){ |
91 |
| - std::cout<<"Header file format error. "<<e.what()<<std::endl; |
92 |
| - return; |
93 |
| - } |
94 |
| - |
95 |
| - size = width*height; |
96 |
| - |
97 |
| - r = new unsigned char[size]; |
98 |
| - g = new unsigned char[size]; |
99 |
| - b = new unsigned char[size]; |
100 |
| - flag_alloc = true; |
101 |
| - |
102 |
| - char aux; |
103 |
| - for(unsigned int i = 0; i < size; i++){ |
104 |
| - inp.read(&aux,1); |
105 |
| - r[i] = (unsigned char) aux; |
106 |
| - inp.read(&aux,1); |
107 |
| - g[i] = (unsigned char) aux; |
108 |
| - inp.read(&aux,1); |
109 |
| - b[i] = (unsigned char) aux; |
110 |
| - } |
111 |
| - } |
112 |
| - else{ |
113 |
| - std::cout<<"Error. Unable to open "<<fname<<std::endl; |
114 |
| - } |
115 |
| - inp.close(); |
| 64 | + |
| 65 | +void ppm::read(const std::string &fname) { |
| 66 | + std::ifstream inp(fname.c_str(), std::ios::in | std::ios::binary); |
| 67 | + if (inp.is_open()) { |
| 68 | + std::string line; |
| 69 | + std::getline(inp, line); |
| 70 | + if (line != "P6") { |
| 71 | + std::cout << "Error. Unrecognized file format." << std::endl; |
| 72 | + return; |
| 73 | + } |
| 74 | + std::getline(inp, line); |
| 75 | + while (line[0] == '#') { |
| 76 | + std::getline(inp, line); |
| 77 | + } |
| 78 | + std::stringstream dimensions(line); |
| 79 | + |
| 80 | + try { |
| 81 | + dimensions >> width; |
| 82 | + dimensions >> height; |
| 83 | + nr_lines = height; |
| 84 | + nr_columns = width; |
| 85 | + } catch (std::exception &e) { |
| 86 | + std::cout << "Header file format error. " << e.what() << std::endl; |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + std::getline(inp, line); |
| 91 | + std::stringstream max_val(line); |
| 92 | + try { |
| 93 | + max_val >> max_col_val; |
| 94 | + } catch (std::exception &e) { |
| 95 | + std::cout << "Header file format error. " << e.what() << std::endl; |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + size = width*height; |
| 100 | + |
| 101 | + r = new unsigned char[size]; |
| 102 | + g = new unsigned char[size]; |
| 103 | + b = new unsigned char[size]; |
| 104 | + flag_alloc = true; |
| 105 | + |
| 106 | + char aux; |
| 107 | + for (unsigned int i = 0; i < size; ++i) { |
| 108 | + inp.read(&aux, 1); |
| 109 | + r[i] = (unsigned char) aux; |
| 110 | + inp.read(&aux, 1); |
| 111 | + g[i] = (unsigned char) aux; |
| 112 | + inp.read(&aux, 1); |
| 113 | + b[i] = (unsigned char) aux; |
| 114 | + } |
| 115 | + } else { |
| 116 | + std::cout << "Error. Unable to open " << fname << std::endl; |
| 117 | + } |
| 118 | + inp.close(); |
116 | 119 | }
|
117 | 120 |
|
118 | 121 | //write the PPM image in fname
|
119 |
| -void ppm::write(const std::string &fname){ |
120 |
| - std::ofstream inp(fname.c_str(), std::ios::out|std::ios::binary); |
121 |
| - if(inp.is_open()){ |
122 |
| - |
123 |
| - inp<<"P6\n"; |
124 |
| - inp<<width; |
125 |
| - inp<<" "; |
126 |
| - inp<<height<<"\n"; |
127 |
| - inp<<max_col_val<<"\n"; |
128 |
| - |
129 |
| - char aux; |
130 |
| - for(unsigned int i = 0; i < size; i++){ |
131 |
| - aux = (char)r[i]; |
132 |
| - inp.write(&aux,1); |
133 |
| - aux = (char)g[i]; |
134 |
| - inp.write(&aux,1); |
135 |
| - aux = (char)b[i]; |
136 |
| - inp.write(&aux,1); |
137 |
| - } |
138 |
| - } |
139 |
| - else{ |
140 |
| - std::cout<<"Error. Unable to open "<<fname<<std::endl; |
141 |
| - } |
142 |
| - inp.close(); |
143 |
| -} |
144 | 122 |
|
| 123 | +void ppm::write(const std::string &fname) { |
| 124 | + std::ofstream inp(fname.c_str(), std::ios::out | std::ios::binary); |
| 125 | + if (inp.is_open()) { |
| 126 | + |
| 127 | + inp << "P6\n"; |
| 128 | + inp << width; |
| 129 | + inp << " "; |
| 130 | + inp << height << "\n"; |
| 131 | + inp << max_col_val << "\n"; |
| 132 | + |
| 133 | + char aux; |
| 134 | + for (unsigned int i = 0; i < size; ++i) { |
| 135 | + aux = (char) r[i]; |
| 136 | + inp.write(&aux, 1); |
| 137 | + aux = (char) g[i]; |
| 138 | + inp.write(&aux, 1); |
| 139 | + aux = (char) b[i]; |
| 140 | + inp.write(&aux, 1); |
| 141 | + } |
| 142 | + } else { |
| 143 | + std::cout << "Error. Unable to open " << fname << std::endl; |
| 144 | + } |
| 145 | + inp.close(); |
| 146 | +} |
0 commit comments