-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
198 lines (169 loc) · 5.96 KB
/
main.cpp
File metadata and controls
198 lines (169 loc) · 5.96 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
#include "video.h"
#include "videoFunction.h"
using namespace std;
int main(int argc, char * argv[]) {
Video video;
// Ensure the correct number of command-line arguments are provided
if (argc < 4) {
cerr << "Invalid number of arguments. Expected 3 or more arguments but got "
<< (argc - 1) << "\nUsage: ./runme "
<< "<inputPath> <outputPath> [optimisation] <function> [options]"
<< "\n";
return 1;
}
// Define valid function names and optimisation flags
const set<string> validFunctions = {
"reverse",
"swap_channel",
"clip_channel",
"scale_channel",
"invert_channel"
};
const set<string> validOptimisationFlag = {
"-S",
"-M"};
string inputFilePath = argv[1];
string outputFilePath = argv[2];
string optimisationFlag = "";
string functionName;
vector<string> functionOptions;
// Detect if a optimisation flag is presetned, then parse the arguments
if (argc >= 5) {
string potentialFlag = argv[3];
if (validFunctions.count(potentialFlag) == 1) {
functionName = potentialFlag;
} else {
optimisationFlag = potentialFlag;
functionName = argv[4];
}
} else {
functionName = argv[3];
}
if (validFunctions.count(functionName) == 0) {
cerr << "Invalid function name: " << functionName
<< "\nExpected one of the following:\n";
for (const auto & function : validFunctions) {
cerr << " - " << function << "\n";
}
return 1;
}
int optionsStartIndex;
if (optimisationFlag.empty()) {
optionsStartIndex = 4;
} else {
if (validOptimisationFlag.count(optimisationFlag) == 0) {
cerr << "Invalid optimisation flag: " << optimisationFlag
<< "\nExpected one of the following:\n";
for (const auto & flag : validOptimisationFlag) {
cerr << " - " << flag << "\n";
}
return 1;
}
optionsStartIndex = 5;
}
// Extract all left over aguement for options for functions
for (int argIndex = optionsStartIndex; argIndex < argc; argIndex++) {
functionOptions.push_back(argv[argIndex]);
}
// Initialise video object
// Initialise input/output streams to read/write from/to
if (!readVideoFromFile(video, inputFilePath, optimisationFlag)) {
cerr << "Failed to open or read file: "
<< inputFilePath << endl;
return 1;
}
if (!setUpOutputStream(video, outputFilePath)) {
cerr << "Failed to setup output stream: "
<< outputFilePath << endl;
return 1;
}
if (!writeHeaderToFile(video)) {
cerr << "Failed to write header for output file: "
<< outputFilePath << endl;
return 1;
}
try {
if (functionName == "reverse") {
if (!functionOptions.empty()) {
throw invalid_argument(
"Function 'reverse' "
"does not accept additional options.");
}
reverseVideo(video, optimisationFlag);
} else if (functionName == "swap_channel") {
if (functionOptions.size() != 1) {
throw invalid_argument(
"Function 'swap_channel' "
"expects exactly 1 option: 'channel1,channel2'.");
}
string channelOption = functionOptions[0];
size_t commaPosition = channelOption.find(",");
if (commaPosition == string::npos) {
throw invalid_argument(
"Invalid option for 'swap_channel'."
"Expected '[channel1,channel2]'.");
}
// Parse channels arguments for swap_channel, assumming format 'int,int'
int channel1 = stoi(channelOption.substr(0, commaPosition));
int channel2 = stoi(channelOption.substr(commaPosition + 1));
swapChannel(video, optimisationFlag, channel1, channel2);
} else if (functionName == "clip_channel") {
if (functionOptions.size() != 2) {
throw invalid_argument(
"Function 'clip_channel' "
"expects exactly 2 options: <channel> '<minClip,maxClip>'.");
}
// Parse channel and clip range arguments
// Assumming format 'int [int,int]'
int channel = stoi(functionOptions[0]);
string clipRange = functionOptions[1];
size_t commaPosition = clipRange.find(",");
if (commaPosition == string::npos) {
throw invalid_argument(
"Invalid option for 'swap_channel. "
"Expected format '<minClip,maxClip>'.");
}
int minClip = stoi(clipRange.substr(1, commaPosition));
int maxClip = stoi(clipRange.substr(commaPosition + 1,
clipRange.size() - 1));
clipChannel(video, optimisationFlag, channel, minClip, maxClip);
} else if (functionName == "scale_channel") {
if (functionOptions.size() != 2) {
throw invalid_argument(
"Function 'scale_channel' "
"xpects exactly 2 options: <channel> <scale>.");
}
// Parse channel and scale arguments
// Assumming format 'int float'
int channel = stoi(functionOptions[0]);
float scale = stof(functionOptions[1]);
if (scale <= 0) {
throw invalid_argument("Scale value must be greater than 0.");
}
scaleChannel(video, optimisationFlag, channel, scale);
} else if (functionName == "invert_channel") {
if (functionOptions.size() != 1) {
throw invalid_argument(
"Invalid option for 'invert_channel. "
"Expected format '<scale>'.");
}
// Parse channel argument, assumming format 'int'
int channel = stoi(functionOptions[0]);
InvertChannel(video, optimisationFlag, channel);
}
} catch (...) {
cerr << "An unexpected error occurred.\n";
return 1;
}
// If not -M optimisation flag
// Write the modified Video object to the output file
if (!(optimisationFlag == "-M")) {
if (!writeVideoToFile(video)) {
cerr << "Failed to write video to: "
<< outputFilePath << "\n";
return 1;
}
}
cout << "Successfully wrote modified video to: " << outputFilePath << "\n";
return 0;
}