-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
250 lines (225 loc) · 9.52 KB
/
main.cpp
File metadata and controls
250 lines (225 loc) · 9.52 KB
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
#include "TGA.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring> // milestone 2
Image trackingImage;
bool hasError = false;
void runMilestone1() {
// Task 1
Image layer1 = readTGA("input/layer1.tga");
Image pattern1 = readTGA("input/pattern1.tga");
Image part1 = multiply(layer1, pattern1);
writeTGA("output/part1.tga", part1);
// Task 2
Image car = readTGA("input/car.tga");
Image layer2 = readTGA("input/layer2.tga");
Image part2 = subtract(car, layer2); // not sure if subtract order is right tbh
writeTGA("output/part2.tga", part2);
// Task 3
Image pattern2 = readTGA("input/pattern2.tga");
Image temp = multiply(layer1, pattern2);
Image text = readTGA("input/text.tga");
Image part3 = screen(text, temp); // tried swapping args and this worked better?
writeTGA("output/part3.tga", part3);
// Task 4
Image circles = readTGA("input/circles.tga");
Image multiplied = multiply(layer2, circles);
Image pattern2_4 = readTGA("input/pattern2.tga");
Image part4 = subtract(multiplied, pattern2_4); // might flip this later
writeTGA("output/part4.tga", part4);
// Task 5
Image part5 = overlay(layer1, pattern1);
writeTGA("output/part5.tga", part5);
// Task 6
Image car6 = readTGA("input/car.tga");
for (int i = 0; i < car6.pixels.size(); i++) {
car6.pixels[i].green = clamp(car6.pixels[i].green + 200);
}
writeTGA("output/part6.tga", car6);
// Task 7
Image car7 = readTGA("input/car.tga");
for (auto& px : car7.pixels) {
px.red *= 4;
px.red = clamp(px.red); // separated this to test clamp alone
px.blue = 0;
}
writeTGA("output/part7.tga", car7);
// Task 8
Image car8 = readTGA("input/car.tga");
Image redChannel = car8, greenChannel = car8, blueChannel = car8;
for (size_t i = 0; i < car8.pixels.size(); i++) {
auto& p = car8.pixels[i];
redChannel.pixels[i].green = redChannel.pixels[i].blue = p.red;
greenChannel.pixels[i].red = greenChannel.pixels[i].blue = p.green;
blueChannel.pixels[i].red = blueChannel.pixels[i].green = p.blue;
}
writeTGA("output/part8_r.tga", redChannel);
writeTGA("output/part8_g.tga", greenChannel);
writeTGA("output/part8_b.tga", blueChannel);
// Task 9
Image red = readTGA("input/layer_red.tga");
Image green = readTGA("input/layer_green.tga");
Image blue = readTGA("input/layer_blue.tga");
Image combined = red; // just using red as base
for (size_t i = 0; i < combined.pixels.size(); ++i) {
combined.pixels[i].green = green.pixels[i].green;
combined.pixels[i].blue = blue.pixels[i].blue;
}
writeTGA("output/part9.tga", combined);
// Task 10
Image text2 = readTGA("input/text2.tga");
std::reverse(text2.pixels.begin(), text2.pixels.end());
writeTGA("output/part10.tga", text2);
}
void error(const std::string& msg) {
std::cerr << "[ERROR] " << msg << std::endl;
hasError = true;
}
void validateFile(const std::string& filename) {
if (filename.size() < 5 || filename.substr(filename.size() - 4) != ".tga") {
throw std::invalid_argument("Invalid file name.");
}
std::ifstream test(filename);
if (!test.good()) {
throw std::invalid_argument("File does not exist.");
}
}
void processCommands(int argc, char* argv[]) {
try {
if (argc < 3) {
std::cerr << "Invalid file name." << std::endl;
throw std::invalid_argument("");
}
std::string outputFile = argv[1];
std::string inputFile = argv[2];
if (outputFile.size() < 5 || outputFile.substr(outputFile.size() - 4) != ".tga") {
throw std::invalid_argument("Invalid file name.");
}
validateFile(inputFile);
trackingImage = readTGA(inputFile);
int i = 3;
while (i < argc && !hasError) {
std::string cmd = argv[i++];
try {
if (cmd == "multiply" || cmd == "subtract" || cmd == "screen" || cmd == "overlay") {
if (i >= argc) {
std::cerr << "Missing argument." << std::endl;
hasError = true;
return;
}
std::string file = argv[i++];
if (file.size() < 5 || file.substr(file.size() - 4) != ".tga") {
throw std::invalid_argument("Invalid argument, invalid file name.");
}
std::ifstream test(file);
if (!test.good()) {
throw std::invalid_argument("Invalid argument, file does not exist.");
}
Image img = readTGA(file);
if (cmd == "multiply") trackingImage = multiply(trackingImage, img);
else if (cmd == "subtract") trackingImage = subtract(trackingImage, img);
else if (cmd == "screen") trackingImage = screen(img, trackingImage);
else trackingImage = overlay(trackingImage, img);
}
else if (cmd == "combine") {
if (i + 1 >= argc) {
std::cerr << "Missing argument." << std::endl;
hasError = true;
return;
}
std::string greenFile = argv[i++];
std::string blueFile = argv[i++];
if (greenFile.size() < 5 || greenFile.substr(greenFile.size() - 4) != ".tga") {
throw std::invalid_argument("Invalid argument, invalid file name.");
}
std::ifstream testGreen(greenFile);
if (!testGreen.good()) {
throw std::invalid_argument("Invalid argument, file does not exist.");
}
if (blueFile.size() < 5 || blueFile.substr(blueFile.size() - 4) != ".tga") {
throw std::invalid_argument("Invalid argument, invalid file name.");
}
std::ifstream testBlue(blueFile);
if (!testBlue.good()) {
throw std::invalid_argument("Invalid argument, file does not exist.");
}
Image green = readTGA(greenFile);
Image blue = readTGA(blueFile);
trackingImage = combine(trackingImage, green, blue);
}
else if (cmd == "flip") {
trackingImage = flip(trackingImage);
}
else if (cmd == "onlyred") {
trackingImage = onlyRed(trackingImage);
}
else if (cmd == "onlygreen") {
trackingImage = onlyGreen(trackingImage);
}
else if (cmd == "onlyblue") {
trackingImage = onlyBlue(trackingImage);
}
else if (cmd == "addred" || cmd == "addgreen" || cmd == "addblue") {
if (i >= argc) {
std::cerr << "Missing argument." << std::endl;
hasError = true;
return;
}
try {
int val = std::stoi(argv[i++]);
if (cmd == "addred") addRed(trackingImage, val);
else if (cmd == "addgreen") addGreen(trackingImage, val);
else addBlue(trackingImage, val);
} catch (...) {
std::cerr << "Invalid argument, expected number." << std::endl;
hasError = true;
return;
}
}
else if (cmd == "scalered" || cmd == "scalegreen" || cmd == "scaleblue") {
if (i >= argc) {
std::cerr << "Missing argument." << std::endl;
hasError = true;
return;
}
try {
float factor = std::stof(argv[i++]);
if (cmd == "scalered") scaleRed(trackingImage, factor);
else if (cmd == "scalegreen") scaleGreen(trackingImage, factor);
else scaleBlue(trackingImage, factor);
} catch (...) {
std::cerr << "Invalid argument, expected number." << std::endl;
hasError = true;
return;
}
}
else {
std::cerr << "Invalid method name." << std::endl;
hasError = true;
return;
}
} catch (const std::invalid_argument& e) { // CHANGE
std::cerr << e.what() << std::endl; // CHANGE
hasError = true;
return;
}
}
if (!hasError) {
writeTGA(outputFile, trackingImage);
}
} catch (const std::invalid_argument& e) {
std::cerr << e.what() << std::endl;
hasError = true;
}
}
int main(int argc, char* argv[]) {
if (argc == 1 || (argc == 2 && std::strcmp(argv[1], "--help") == 0)) {
std::cout << "Project 2: Image Processing, Spring 2025\n\n";
std::cout << "Usage:\n";
std::cout << "\t./project2.out [output] [firstImage] [method] [...]\n";
return 0;
}
processCommands(argc, argv);
return hasError ? 1 : 0;
}