-
Notifications
You must be signed in to change notification settings - Fork 0
/
PGM.cpp
246 lines (219 loc) · 8.26 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
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
#include "PGM.h"
PGM::PGM() :Formats(), maxValue(0) {}
PGM::~PGM() {}
void PGM::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 == "P2");//Ïðîâåðêà äàëè å êîðåêòåí ñïðÿìî ðàçøèðåíèåòî íà ôàéëà.
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;
for (int i = 0; i < this->height; i++)
{
int x;
std::vector<int> helper;//Ïîìîùíà ïðîìåíëèâà, â êîÿòî çàïàçâàìå ïèêñåëèòå íà âñåêè ðåä.
for (int j = 0; j < this->width; j++)
{
input >> x;//×åòåì âñåêè ïèêñåë.
assert(x >= 0 && x <= this->maxValue);
helper.push_back(x);//Äîáàâÿìå âñåêè ïèêñåë.
}
this->pixels.push_back(helper);//Êîãàòî îáõîäèì ðåäà, äîáàâÿìå ïèêñåëèòå îò öåëèÿ ðåä.
}
input.close();//Çàòâàðÿìå ôàéëà.
}
void PGM::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++)
{
//Çàïèñâàìå âñåêè ïèêñåë.
assert(this->pixels[i][j] >= 0 && this->pixels[i][j] <= this->maxValue);
if(Formats::isOneDigit(this->pixels[i][j])) saveas << " " << this->pixels[i][j] << " ";
else if (Formats::isTwoDigit(this->pixels[i][j])) saveas << " " << this->pixels[i][j] << " ";
else if (Formats::isThreeDigit(this->pixels[i][j])) saveas << this->pixels[i][j] << " ";
}
saveas << '\n';
}
saveas.close();//Çàòâàðÿìå ôàéëà.
}
void PGM::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++)
{
if (Formats::isOneDigit(this->pixels[i][j]))
{
out << " " << this->pixels[i][j] << " ";
}
else if (Formats::isTwoDigit(this->pixels[i][j]))
{
out << " " << this->pixels[i][j] << " ";
}
else if (Formats::isThreeDigit(this->pixels[i][j]))
{
out << this->pixels[i][j] << " ";
}
}
out << '\n';
}
}
std::vector<std::vector<int>> PGM::getPixels() const
{
return this->pixels;
}
void PGM::negative() //Öâåòîâî îáðúùàíå.
{
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
this->pixels[i][j] = this->maxValue - this->pixels[i][j];
}
}
}
void PGM::rotation(std::string direction) //Çàâúðòàíå íà èçîáðàæåíèåòî íà 90° â çàâèñèìîñò îò çàäàäåíàòà ïîñîêà.
{
std::vector<std::vector<int>> saver;//Ïîìîùíà ïðîìåíëèâà, â êîÿòî çàïàçâàìå ïèêñåëèòå íà öÿëîòî èçîáðàæåíèå.
if (direction == "left")
{
std::swap(this->width, this->height);//Ðàçìåíÿìå ñòîéíîñòèòå íà âèñî÷èíàòà è øèðèíàòà.
for (int i = this->height - 1; i >= 0; i--)
{
std::vector<int> 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<int> 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 PGM::collage(std::string direction, std::string image1, std::string image2, std::string outimage)
{
/*Ñúçäàâàíå íà êîëàæ îò äâå èçîáðàæåíèÿ (<image1>, <image2>), êîèòî ñà îò åäèí è ñúù ôîðìàò è åäíàêâà ðàçìåðíîñò â çàâèñèìîñò îò çàäàäåíà ïîñîêà -
õîðèçîíòàëíà èëè âåðòèêàëíà, êàòî ïîëó÷åíîòî èçàáðàæåíèå ñå çàïèñâà â íîâî èçîáðàæåíèå <outimage>*/
std::vector<std::vector<int>> saver;//Ïîìîùíà ïðîìåíëèâà, â êîÿòî çàïàçâàìå ïèêñåëèòå íà öÿëîòî èçîáðàæåíèå.
PGM firstImage;//Îáåêò îò òåêóùèÿ êëàñ, â êîéòî ùå ñúõðàíÿâàìå äàííèòå çà ïúðâîòî èçîáðàæåíèå.
firstImage.load(image1);
PGM 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<int> 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<int> 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";
}
}