-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.cpp
316 lines (260 loc) · 8.66 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
305
306
307
308
309
310
311
312
313
314
315
316
/* File: Script.cpp
██████╗ ██████╗ ██████╗ ██████╗
██╔══██╗██╔══██╗██╔═══██╗██╔════╝
██████╔╝██████╔╝██║ ██║██║ ███╗
██╔═══╝ ██╔══██╗██║ ██║██║ ██║
██║ ██║ ██║╚██████╔╝╚██████╔╝
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝
Class:
- FEUP-L.EIC009-2022/2023-2S (Programação)
Group:
- 13.10
Students:
- Gonçalo Morais Magalhães
- Rodrigo Albergaria Coelho e Silva
- Tiago Morais de Pimentel Teixeira
Tip: the implementation of the functions on the .cpp files are in the same order as they appear defined in the corresponding .hpp header file
*/
#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;
}
// Implemented commands
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;
}
if (command == "median_filter"){
median_filter();
continue;
}
if (command == "xpm2_open"){
xpm2_open();
continue;
}
if (command == "xpm2_save"){
xpm2_save();
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);
}
// Image Manipulations (Maintaining Size)
void Script::invert() {
// Inverts the value of each Color in the Image (255 - rgb_value)
image->invert();
}
void Script::to_gray_scale(){
// Grayscales each Color of the Image
image->to_gray_scale();
}
void Script::replace(){
// Replaces every Color in the Image that matches the initial_color by the final_color
Color initial_color, final_color;
input >> initial_color >> final_color;
// Loads the necessary parameters
image->replace(initial_color, final_color);
}
void Script::fill(){
// Fills a rectangle on top of the current Image with a Color
int x, y, w, h;
Color fill_color;
input >> x >> y >> w >> h >> fill_color;
// Loads the necessary parameters
image->fill(x, y, w, h, fill_color);
}
void Script::h_mirror(){
// Mirrors the image horizontally
image->h_mirror();
}
void Script::v_mirror(){
// Mirrors the image vertically
image->v_mirror();
}
void Script::add(){
// Pastes image from filename on top of current image (everything is else is kept)
string filename;
Image *image_copy;
input >> filename;
image_copy = loadFromPNG(filename);
// Loads the new Image
Color neutral_color; int x, y;
input >> neutral_color >> x >> y;
// Loads the necessary parameters
image->add(image_copy, neutral_color, x, y);
if (image_copy != nullptr) {
delete image_copy;
image_copy = nullptr;
}
// Deletes the new Image (already pasted)
}
// Image Manipulations (Changing Size)
void Script::crop(){
// Crop the current Image into a smaller rectangle
int x, y, w, h;
input >> x >> y >> w >> h;
Image *image_cropped = new Image(w, h);
// Loads the necessary parameters and creates a new Image to store the cropped image
image->crop(x, y, image_cropped);
if (image != nullptr) {
delete image;
image = image_cropped;
image_cropped = nullptr;
}
// Deletes the previous Image and makes the cropped Image the new current Image
}
void Script::rotate_left(){
// Rotate the Image 90 degrees to the left
Image *image_rotated = new Image(image->height(), image->width());
// Creates a new Image to store the rotated Image
image->rotate_left(image_rotated);
if (image != nullptr) {
delete image;
image = image_rotated;
image_rotated = nullptr;
}
// Deletes the previous Image and makes the rotated Image the new current Image
}
void Script::rotate_right(){
// Rotate the image 90 degrees to the right
Image *image_rotated = new Image(image->height(), image->width());
// Creates a new Image to store the rotated Image
image->rotate_right(image_rotated);
if (image != nullptr) {
delete image;
image = image_rotated;
image_rotated = nullptr;
}
// Deletes the previous Image and makes the rotated Image the new current Image
}
// Advanced Functionality
void Script::median_filter(){
// Apply a median filter of n to the Image
Image *image_filtered = new Image(image->width(), image->height());
// Creates a new Image to store the filtered Image
int n;
input >> n;
// Loads the necessary parameters
image->median_filter(n, image_filtered);
if (image != nullptr) {
delete image;
image = image_filtered;
image_filtered = nullptr;
}
// Deletes the previous Image and makes the filtered Image the new current Image
}
// Processing XPM2 Files
void Script::xpm2_open(){
// Open XPM2 files to an image object
clear_image_if_any();
// Deletes the current image
string inputFilename;
input >> inputFilename;
// Loads the necessary parameters
image = loadFromXPM2(inputFilename);
}
void Script::xpm2_save(){
// Save image object into XPM2 file
string outputFilename;
input >> outputFilename;
// Loads the necessary parameters
saveToXPM2(outputFilename, image);
}
}