-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPM.cpp
306 lines (276 loc) · 10.2 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include"PPM.h"
#include<cassert>
PPM::PPM() : Formats(), maxValue(0), pixels({ }) {};
PPM::~PPM(){}
void PPM::load(std::string path)//Ôóíêöèÿ çà ïðî÷èòàíå íà èíôîðìàöèÿ îò èçîáðàæåíèÿ.
{
std::ifstream input(path.c_str());//Ïîòîê çà ÷åòåíå.
if (input.fail())//Ïðîâåðêà çà ñúñòîÿíèåòî íà ïîòîêà.
{
std::cout << "Error!\n";
return;
}
input >> this->ASCIInum;//×åòå ASCII êîäà.
assert(this->ASCIInum == "P3");//Ïðîâåðêà äàëè å êîðåêòåí ñïðÿìî ðàçøèðåíèåòî íà ôàéëà.
this->path = path;//Ïðèñâîÿâà èìåòî íà ôàéëà.
//Íóæíî íè å äà çíàåì äàëè âúâ ôàéëà èìà êîìåíòàð, çà äà å ÿñíî êàêâî äà ÷åòåì.
char x;
input >> x;
if (x == '#')//Êîìåíòàðèòå çàïî÷âàò ñ #.
{
input.unget();//Âðúùà get-óêàçàòåëÿ åäèí ñèìâîë íàçàä, ò.å. ïðåäè #.
std::getline(input, this->comment);//Ïðî÷èòà êîìåíòàðà.
}
else//Àêî íÿìà êîìåíòàð, òî çíà÷è ñëåäâà äà ñå ïðî÷åòå èíôîðìàöèÿ çà øèðèíàòà è âèñî÷èíàòà.
{
input.unget();
}
input >> this->width;
input >> this->height;
input >> this->maxValue;
int red, green, blue;
for (int i = 0; i < this->height; i++)
{
std::vector<RGB> helper;//Ïîìîùíà ïðîìåíëèâà, â êîÿòî çàïàçâàìå ïèêñåëèòå íà âñåêè ðåä.
for (int j = 0; j < this->width; j++)
{
RGB saver;//Â òàçè ïðîìåíëèâà çàïàçâàìå åäèí ïèêñåë.
input.ignore();
input >> red >> green >> blue;
saver.setColor(red, green, blue);
helper.push_back(saver);//Äîáàâÿìå âñåêè ïèêñåë.
}
this->pixels.push_back(helper);//Êîãàòî îáõîäèì ðåäà, äîáàâÿìå ïèêñåëèòå îò öåëèÿ ðåä.
}
input.close();//Çàòâàðÿìå ôàéëà.
}
void PPM::saveas(std::string path) const//Ôóíêöèÿ çà çàïàçâàíå íà èíôîðìàöèÿ îò èçîáðàæåíèå âúâ ôàéë, ðàçëè÷åí îò èçõîäíèÿ.
{
std::ofstream saveas(path.c_str());//Ïîòîê çà çàïèñ.
saveas << this->ASCIInum << '\n';//Çàïèñâàìå ASCII êîäà.
saveas << this->comment << '\n';//Çàïèñâàìå êîìåíòàðà.
saveas << this->width << " " << this->height << '\n';//Çàïèñâàìå øèðèíà è âèñî÷èíà.
saveas << this->maxValue << '\n';//Çàïèñâàìå ìàêñèìàëíàòà ñòîéíîñò.
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
saveas << this->pixels[i][j];//Çàïèñâàìå âñåêè ïèêñåë.
}
saveas << '\n';
}
saveas.close();//Çàòâàðÿìå ôàéëà.
}
void PPM::print(std::ostream& out) const//Ôóíêöèÿ çà èçâåæäàíå íà èíôîðìàöèÿ çà äàäåí ôîðìàò.
{
out << this->path << '\n';
out << "ASCII number: " << this->ASCIInum << '\n';
out << this->comment << '\n';
out << "Width: " << this->width << " Height:" << this->height <<'\n';
out << "Max value: " << this->maxValue <<'\n';
out << "Pixels:\n";
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
out << this->pixels[i][j];
}
out << '\n';
}
}
void PPM::grayscale()//Ïðîìåíÿ ïèêñåëèòå äà ñà â ñèâ íþàíñ.
{
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
if ((this->pixels[i][j].getRed() != this->pixels[i][j].getGreen() || this->pixels[i][j].getRed() != this->pixels[i][j].getBlue()))
{
//Ôîðìóëà îò tutorialspoint.com
this->pixels[i][j].setColor((int)(0.3 * this->pixels[i][j].getRed()), (int)(0.59 * this->pixels[i][j].getGreen()), (int)(0.11 * this->pixels[i][j].getBlue()));
}
}
}
}
void PPM::monochrome()//Ïðîìåíÿ ïèêñåëèòå äà ñà ÷åðíè èëè áåëè.
{
/*Àëãîðèòúìúò å ñëåäíèÿ:
1.Ïðîìåíÿìå èçàáðàæåíèåòî ÷ðåç grayscale, çà äà ñòàíå ñàìî ñ ñèâè íþàíñè.
2.Èçáèðàìå ñè íÿêàêâî ÷èñëî, êîåòî ùå áúäå ãðàíèöà.  ñëó÷àÿ ñúì ñè èçáðàëà 123.
3.Àêî ÷åðâåíèÿò ïèêñåë å >= 123, òî ïèêñåëèòå ñòàâàò {255, 255, 255}, ò.å. áåëè.
Àêî ÷åðâåíèÿò ïèêñåë å <123, òî ïèêñåëèòå ñòàâàò {0, 0, 0}, ò.å. ÷åðíè.*/
(*this).grayscale();
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
if (this->pixels[i][j].getRed() != this->pixels[i][j].getGreen() || this->pixels[i][j].getRed() != this->pixels[i][j].getBlue())
{
if (this->pixels[i][j].getRed() >= 123)
{
this->pixels[i][j].setColor(255,255,255);
}
else if (this->pixels[i][j].getRed() < 123)
{
this->pixels[i][j].setColor(0, 0, 0);
}
}
}
}
}
void PPM::negative()//Öâåòîâî îáðúùàíå.
{
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
this->pixels[i][j].setColor(this->maxValue - this->pixels[i][j].getRed(),
this->maxValue - this->pixels[i][j].getGreen(), this->maxValue - this->pixels[i][j].getBlue());
}
}
}
void PPM::rotation(std::string direction)//Çàâúðòàíå íà èçîáðàæåíèåòî íà 90° â çàâèñèìîñò îò çàäàäåíàòà ïîñîêà.
{
std::vector<std::vector<RGB>> saver;//Ïîìîùíà ïðîìåíëèâà, â êîÿòî çàïàçâàìå ïèêñåëèòå íà öÿëîòî èçîáðàæåíèå.
if (direction == "left")
{
std::swap(this->width, this->height);//Ðàçìåíÿìå ñòîéíîñòèòå íà âèñî÷èíàòà è øèðèíàòà.
for (int i = this->height - 1; i >= 0; i--)
{
std::vector<RGB> helper;//Ïîìîùíà ïðîìåíëèâà, â êîÿòî çàïàçâàìå ïèêñåëèòå íà âñåêè ðåä.
for (int j = 0; j < this->width; j++)
{
helper.push_back(this->pixels[j][i]);
}
saver.push_back(helper);
}
}
else if (direction == "right")
{
std::swap(this->width, this->height);
for (int i = 0; i < this->height; i++)
{
std::vector<RGB> helper;//Ïîìîùíà ïðîìåíëèâà, â êîÿòî çàïàçâàìå ïèêñåëèòå íà âñåêè ðåä.
for (int j = this->width - 1; j >= 0; j--)
{
helper.push_back(this->pixels[j][i]);
}
saver.push_back(helper);
}
}
else
{
std::cout << "Wrong direction!\n";
return;
}
this->pixels = saver;//Ïðèñâîÿâàìå ñòîéíîñòèòå íà ïèêñåëèòå.
}
void PPM::collage(std::string direction, std::string image1, std::string image2, std::string outimage)
{
/*Ñúçäàâàíå íà êîëàæ îò äâå èçîáðàæåíèÿ (<image1>, <image2>), êîèòî ñà îò åäèí è ñúù ôîðìàò è åäíàêâà ðàçìåðíîñò â çàâèñèìîñò îò çàäàäåíà ïîñîêà -
õîðèçîíòàëíà èëè âåðòèêàëíà, êàòî ïîëó÷åíîòî èçàáðàæåíèå ñå çàïèñâà â íîâî èçîáðàæåíèå <outimage>*/
std::vector<std::vector<RGB>> saver;//Ïîìîùíà ïðîìåíëèâà, â êîÿòî çàïàçâàìå ïèêñåëèòå íà öÿëîòî èçîáðàæåíèå.
PPM firstImage;//Îáåêò îò òåêóùèÿ êëàñ, â êîéòî ùå ñúõðàíÿâàìå äàííèòå çà ïúðâîòî èçîáðàæåíèå.
firstImage.load(image1);
PPM secondImage;//Îáåêò îò òåêóùèÿ êëàñ, â êîéòî ùå ñúõðàíÿâàìå äàííèòå çà âòîðîòî èçîáðàæåíèå.
secondImage.load(image2);
if (firstImage.height == secondImage.height && firstImage.width == secondImage.width)//Óñëîâèå, êîåòî òðÿáâà äà èçïúëíÿâàò.
{
if (direction == "horizontal")
{
this->path = outimage;
this->ASCIInum = firstImage.ASCIInum;//ASCII íîìåðàòà íà 2òå èçîáðàæåíèÿ ñúâïàäàò, çàùîòî ñà îò åäèí ôîðìàò.
this->comment = firstImage.comment + secondImage.comment;//Êîíêàòåíàöèÿ íà äâàòà êîìåíòàðà.
this->width = 2 * firstImage.width;//Øèðèíàòà ñå óâåëè÷àâà äâà ïúòè.
this->height = firstImage.height;//Âèñî÷èíàòà îñòàâà ñúùàòà.
this->maxValue = (firstImage.maxValue + secondImage.maxValue) / 2;//Ìàêñèìàëíàòà ñòîéíîñò å ñðåäíîàðèòìåòè÷íî íà äâåòå.
for (int i = 0; i < this->height; i++)
{
std::vector<RGB> helper;//Ïîìîùíà ïðîìåíëèâà, â êîÿòî çàïàçâàìå ïèêñåëèòå íà âñåêè ðåä.
for (int j = 0; j < firstImage.width; j++)//Ïúðâî îáõîæäàìå ðåäà íà ïúðâîòî èçîáðàæåíèå.
{
helper.push_back(firstImage.pixels[i][j]);//Äîáàâÿìå ñòîéíîñòèòå.
}
for (int t = 0; t < secondImage.width; t++)//Ñëåä òîâà îáõîæäàìå ðåäà íà âòîðîòî èçîáðàæåíèå.
{
helper.push_back(secondImage.pixels[i][t]);//Äîáàâÿìå ñòîéíîñòèòå.
}
saver.push_back(helper);//Ñëåä ïðåìèíàâàíå ïî öåëèÿ ðåä, äîáàâÿìå ñòîéíîñòèòå â ïîìîùíàòà ïðîìåíëèâà.
}
}
else if (direction == "vertical")
{
this->path = outimage;
this->ASCIInum = firstImage.ASCIInum;//ASCII íîìåðàòà íà 2òå èçîáðàæåíèÿ ñúâïàäàò, çàùîòî ñà îò åäèí ôîðìàò.
this->comment = firstImage.comment + secondImage.comment;//Êîíêàòåíàöèÿ íà äâàòà êîìåíòàðà.
this->width = firstImage.width;//Øèðèíàòà îñòàâà ñúùàòà.
this->height = 2 * firstImage.height;//Âèñî÷èíàòà ñå óâåëè÷àâà äâà ïúòè.
this->maxValue = (firstImage.maxValue + secondImage.maxValue) / 2;//Ìàêñèìàëíàòà ñòîéíîñò å ñðåäíîàðèòìåòè÷íî íà äâåòå.
//Îáõîæäàìå âñè÷êè ðåäîâå íà ïúðâîòî èçîáðàæåíèå, à ñëåä òîâà âñè÷êè ðåäîâå íà âòîðîòî.
size_t k = 0;//Òðÿáâà íè äîïúëíèòåëåí áðîÿ÷ ïðè îáõîæäàíå íà âòîðîòî èçîáðàæåíèå.
for (int i = 0; i < this->height; i++)
{
std::vector<RGB> helper;//Ïîìîùíà ïðîìåíëèâà, â êîÿòî çàïàçâàìå ïèêñåëèòå íà âñåêè ðåä.
if (i < firstImage.height)
{
//Îáõîæäàíå íà ïúðâî èçàáðàæåíèå.
for (int j = 0; j < firstImage.width; j++)
{
helper.push_back(firstImage.pixels[i][j]);
}
}
else if (i >= firstImage.height)
{
//Îáõîæäàíå íà âòîðî èçîáðàæåíèå.
for (int j = 0; j < this->width; j++)
{
helper.push_back(secondImage.pixels[k][j]);
}
k++;//Óâåëè÷àâàìå áðîÿ÷à, ò.å. ïðåìèíàâàìå íà ñëåäâàùèÿ ðåä.
}
saver.push_back(helper);//Ñëåä ïðåìèíàâàíå ïî öåëèÿ ðåä, äîáàâÿìå ñòîéíîñòèòå â ïîìîùíàòà ïðîìåíëèâà.
}
}
else
{
std::cout << "Wrong direction!\n";
}
this->pixels = saver;//Ïðèñâîÿâàìå ñòîéíîñòèòå íà ïèêñåëèòå.
this->saveas(outimage);//Çàïèñâàìå ïîëó÷åíîòî èçîáðàæåíèå.
}
else
{
std::cout << "Unable to create collage!\n";
}
}
void PPM::undoGrayscale()//Ôóíêöèÿ çà ïðåìàõâàíå íà ñèâè íþàíñè.
{
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
if ((this->pixels[i][j].getRed() != this->pixels[i][j].getGreen() || this->pixels[i][j].getRed() != this->pixels[i][j].getBlue()))
{
//Ôîðìóëà îò tutorialspoint.com
this->pixels[i][j].setRed((int)(this->pixels[i][j].getRed()/0.3));
this->pixels[i][j].setGreen((int)(this->pixels[i][j].getGreen()/ 0.59));
this->pixels[i][j].setBlue((int)(this->pixels[i][j].getBlue()/ 0.11));
}
}
}
}
void PPM::undoMonochrome()//Ôóíêöèÿ çà ïðåìàõâàíå íà ÷åðíî/áåëè íþàíñè.
{
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
if (this->pixels[i][j].getRed() != this->pixels[i][j].getGreen() || this->pixels[i][j].getRed() != this->pixels[i][j].getBlue())
{
this->pixels[i][j].setRed(255);
this->pixels[i][j].setGreen(255);
this->pixels[i][j].setBlue(255);
}
}
}
}