-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.cpp
305 lines (276 loc) · 10.4 KB
/
Script.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
#include <iostream>
#include <fstream>
#include "Script.hpp"
#include "PNG.hpp"
#include "XPM2.hpp"
using namespace std;
namespace prog
{
// Use to read color values from a script file.
istream &operator>>(istream &input, Color &c)
{
int r, g, b;
input >> r >> g >> b;
c.red() = r;
c.green() = g;
c.blue() = b;
return input;
}
Script::Script(const string &filename) : image(nullptr), input(filename)
{
}
void Script::clear_image_if_any()
{
if (image != nullptr)
{
delete image;
image = nullptr;
}
}
Script::~Script()
{
clear_image_if_any();
}
void Script::run()
{
string command;
while (input >> command)
{
cout << "Executing command '" << command << "' ..." << endl;
if (command == "open")
{
open();
continue;
}
if (command == "blank")
{
blank();
continue;
}
// Other commands require an image to be previously loaded.
if (command == "save")
{
save();
continue;
}
if (command == "invert")
{
invert();
continue;
}
if (command == "to_gray_scale")
{
to_gray_scale();
continue;
}
if (command == "replace")
{
replace();
continue;
}
if (command == "fill")
{
fill();
continue;
}
if (command == "h_mirror")
{
h_mirror();
continue;
}
if (command == "v_mirror")
{
v_mirror();
continue;
}
if (command == "add")
{
add();
continue;
}
if (command == "crop")
{
crop();
continue;
}
if (command == "rotate_left")
{
rotate_left();
continue;
}
if (command == "rotate_right")
{
rotate_right();
continue;
;
}
}
}
void Script::open()
{
// Replace current image (if any) with image read from PNG file.
clear_image_if_any();
string filename;
input >> filename;
image = loadFromPNG(filename);
}
void Script::blank()
{
// Replace current image (if any) with blank image.
clear_image_if_any();
int w, h;
Color fill;
input >> w >> h >> fill;
image = new Image(w, h, fill);
}
void Script::save()
{
// Save current image to PNG file.
string filename;
input >> filename;
saveToPNG(filename, image);
}
void Script::invert() // Inverts the intensity of the color in the current image.
{
for (int i = 0; i < image->width(); i++) // One cycle to go through all pixels in width.
{
for (int j = 0; j < image->height(); j++) // One cycle to go through all pixels in height.
{
Color inv(255 - image->at(i, j).red(), 255 - image->at(i, j).green(), 255 - image->at(i, j).blue()); // Inverts all values of color in the current image.
image->at(i, j) = inv; // The inverted color is distributed to each pixel.
}
}
}
void Script::to_gray_scale() // Turns the current image's color into tones of gray.
{
for (int i = 0; i < image->width(); i++) // One cycle to go through all pixels in width.
{
for (int j = 0; j < image->height(); j++) // One cycle to go through all pixels in height.
{
// Creation of a value that corresponds to the average of the color values according to the cycles above.
rgb_value v = (image->at(i, j).red() + image->at(i, j).green() + image->at(i, j).blue()) / 3;
image->at(i, j) = Color(v, v, v); // Turns every pixel's color into the corresponding average.
}
}
}
void Script::replace() // Replaces one color for another.
{
Color c1, c2;
input >> c1 >> c2; // Insertion of the two desired colors.
for (int i = 0; i < image->width(); i++) // One cycle to go through all pixels in width.
{
for (int j = 0; j < image->height(); j++) // One cycle to go through all pixels in height.
{
// Verification if each pixel corresponds to the first color according to the cycles above.
if (image->at(i, j).red() == c1.red() && image->at(i, j).green() == c1.green() && image->at(i, j).blue() == c1.blue())
image->at(i, j) = c2; // Replacement of the first color to the second color.
}
}
}
void Script::fill() // Fills the the desired area with a especific color.
{
int x, y, w, h;
Color c;
// Insertion of the values that determine the coordinates of the origin, the width, the height and the color that fills the desired area, respectively.
input >> x >> y >> w >> h >> c;
for (int i = x; i < x + w; i++) // One cycle to go through all pixels in the width of the area.
{
for (int j = y; j < y + h; j++) // One cycle to go through all pixels in the height of the area.
{
image->at(i, j) = c; // Redefinition of the color in each pixel inside the desired area.
}
}
}
void Script::h_mirror() // Inverts the current image horizontally.
{
for (int i = 0; i < image->width() / 2; i++) // One cycle to go through all pixels in one half of the width.
{
for (int j = 0; j < image->height(); j++) // One cycle to go through all pixels in height.
{
Color *c = new Color; // Copy of the color of every pixel according to the cycles above using dinamic memory.
*c = image->at(i, j);
image->at(i, j) = image->at(image->width() - 1 - i, j); // Reverts the order of every pixel according to the corresponding width.
image->at(image->width() - 1 - i, j) = *c; // The color is distributed to each inverted pixel according to the width.
delete c; // Delete the copy.
}
}
}
void Script::v_mirror() // Inverts the current image vertically.
{
for (int i = 0; i < image->width(); i++) // One cycle to go through all pixels in width.
{
for (int j = 0; j < image->height() / 2; j++) // One cycle to go through all pixels in one half of the height.
{
Color *c = new Color; // Copy of the color of every pixel according to the cycles above using dinamic memory.
*c = image->at(i, j);
image->at(i, j) = image->at(i, image->height() - 1 - j); // Reverts the order of every pixel according to the corresponding height.
image->at(i, image->height() - 1 - j) = *c; // The color is distributed to each inverted pixel according to the height.
delete c; // Delete the copy.
}
}
}
void Script::add()
{
string filename;
Color neutral;
int x, y;
input >> filename >> neutral >> x >> y;
Image *file = loadFromPNG(filename);
int x_file = 0;
int y_file = 0;
for (int i = x; i < x + file->width(); i++)
{
for (int j = y; j < y + file->height(); j++)
{
if (file->at(x_file, y_file) != neutral)
image->at(i, j) = file->at(x_file, y_file);
y_file++;
}
y_file = 0;
x_file++;
}
delete file;
}
void Script::crop()
{
int x, y, w, h;
input >> x >> y >> w >> h;
Image *new_image = new Image(w, h);
int x_new = 0;
int y_new = 0;
for (int i = x; i < x + w; i++) // One cycle to go through all pixels in the width of the area.
{
for (int j = y; j < y + h; j++) // One cycle to go through all pixels in the height of the area.
{
new_image->at(x_new, y_new) = image->at(i, j);
y_new++;
}
y_new = 0;
x_new++;
}
delete image; // Delete the current image.
image = new_image; // The copy turns into the current image.
}
void Script::rotate_left() // Rotates the current image ninety degrees to the left.
{
Image *new_image = new Image(image->height(), image->width()); // Copy of the current image using dinamic memory.
for (int i = 0; i < image->width(); i++) // One cycle to go through all pixels in width.
{
for (int j = 0; j < image->height(); j++) // One cycle to go through all pixels in height.
new_image->at(j, image->width() - i - 1) = image->at(i, j); // The coordinates of every pixel invert and the height becomes dependent of the width of each pixel.
}
delete image; // Delete the current image.
image = new_image; // The copy turns into the current image.
}
void Script::rotate_right() // Rotates the current image ninety degrees to the right.
{
Image *new_image = new Image(image->height(), image->width()); // Copy of the current image using dinamic memory.
for (int i = 0; i < image->width(); i++) // One cycle to go through all pixels in width.
{
for (int j = 0; j < image->height(); j++) // One cycle to g through all pixels in height.
new_image->at(image->height() - j - 1, i) = image->at(i, j); // The coordinates of every pixel invert and the width becomes dependent of the height of each pixel.
}
delete image; // Delete the current image.
image = new_image; // The copy turns into the current image.
}
}