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