-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
244 lines (235 loc) · 11 KB
/
main.cpp
File metadata and controls
244 lines (235 loc) · 11 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
#include <iostream>
#include <cstdlib>
#include <list>
#include <random>
#include <ctime>
#include <unistd.h>
#include "colour.h"
#include "rainbow_renderer.h"
/// Program entrypoint
/// \param argc
/// \param argv
/// \return
int main(int argc, char *argv[]) {
opterr = 0;
RainbowRenderer::StartType start_type;
RainbowRenderer::FillMode fill_mode;
auto rainbow_renderer = new RainbowRenderer();
int c;
while ((c = getopt(argc, argv, "h:w:H:c:d:r:f:o:l:L:s:S:p:")) != -1) {
switch (c) {
case 'w': { // Width
int pixelsWide = (int) strtol(optarg, nullptr, 0);
if (pixelsWide <= 0) {
std::cout << "Invalid width argument " << optarg << std::endl;
return 1;
}
std::cout << "Setting renderer width to " << pixelsWide << std::endl;
rainbow_renderer->setPixelsWide(pixelsWide);
break;
}
case 'h': { // Height
int pixelsHigh = (int) strtol(optarg, nullptr, 0);
if (pixelsHigh <= 0) {
std::cout << "Invalid height argument " << optarg << std::endl;
return 1;
}
std::cout << "Setting renderer height to " << pixelsHigh << std::endl;
rainbow_renderer->setPixelsHigh(pixelsHigh);
break;
}
case 'H': { // Starting hues
int hue = (int) strtol(optarg, nullptr, 0);
if (hue < 0 || hue > 360) {
std::cout << "Starting hue must be between 0 and 360 " << std::endl;
return 1;
}
std::cout << "Adding a starting hue of " << hue << std::endl;
rainbow_renderer->addStartingHue(hue);
break;
}
case 'l': { // Minimum luminosity
float minLuminosity = std::stof(optarg);
if (minLuminosity <= 0 || minLuminosity >= 1.0) {
std::cerr << "Minimum luminosity must be between 0 and 1 " << std::endl;
return 1;
}
std::cout << "Setting the minimum luminosity to " << minLuminosity << std::endl;
rainbow_renderer->setMinimumLuminosity(minLuminosity);
break;
}
case 'L': { // Maximum luminosity
float maxLuminosity = strtof(optarg, nullptr);
if (maxLuminosity <= 0 || maxLuminosity >= 1.0) {
std::cerr << "Minimum luminosity must be between 0 and 1" << std::endl;
return 1;
}
std::cout << "Setting the maximum luminosity to " << maxLuminosity << std::endl;
rainbow_renderer->setMaximumLuminosity(maxLuminosity);
break;
}
case 's': { // Minimum saturation
float minSaturation = std::stof(optarg);
if (minSaturation <= 0 || minSaturation >= 1.0) {
std::cerr << "Minimum saturation must be between 0 and 1 " << std::endl;
return 1;
}
std::cout << "Setting the minimum saturation to " << minSaturation << std::endl;
rainbow_renderer->setMinimumSaturation(minSaturation);
break;
}
case 'S': { // Maximum saturation
float maxSaturation = strtof(optarg, nullptr);
if (maxSaturation <= 0 || maxSaturation >= 1.0) {
std::cerr << "Maximum saturation must be between 0 and 1" << std::endl;
return 1;
}
std::cout << "Setting the maximum saturation to " << maxSaturation << std::endl;
rainbow_renderer->setMaximumSaturation(maxSaturation);
break;
}
case 'c': { // Colour depth
int colourDepth = (int) strtol(optarg, nullptr, 0);
if (colourDepth <= 0) {
std::cout << "Invalid colour depth argument " << optarg << std::endl;
return 1;
}
std::cout << "Setting the colour depth to " << colourDepth << std::endl;
rainbow_renderer->setColourDepth(colourDepth);
break;
}
case 'd': { // Colour difference function
std::string diff_type = optarg;
float (*difference_func)(const Colour &, const Colour &);
if (diff_type == "lum") {
difference_func = getColourLuminosityDiff;
} else if (diff_type == "colour" || diff_type == "color") {
difference_func = getColourAbsoluteDiff;
} else if (diff_type == "natural") {
difference_func = getNaturalColourDiff;
} else if (diff_type == "hue") {
difference_func = getColourHueDiff;
} else {
std::cout << "Unknown difference type " << diff_type << std::endl;
return 1;
}
rainbow_renderer->setDifferenceFunction(difference_func);
break;
}
case 'p': { // Starting positions
std::string start_type_str = optarg;
if (start_type_str == "center" || start_type_str == "centre") {
std::cout << "Setting start position to centre" << std::endl;
start_type = RainbowRenderer::START_TYPE_CENTRE;
} else if (start_type_str == "corner") {
std::cout << "Setting start position too corner(s)" << std::endl;
start_type = RainbowRenderer::START_TYPE_CORNER;
} else if (start_type_str == "horizontal") {
start_type = RainbowRenderer::START_TYPE_HORIZONTAL_LINE;
} else if (start_type_str == "vertical") {
start_type = RainbowRenderer::START_TYPE_VERTICAL_LINE;
} else if (start_type_str == "edge") {
start_type = RainbowRenderer::START_TYPE_EDGE;
} else if (start_type_str == "random") {
start_type = RainbowRenderer::START_TYPE_RANDOM;
} else if (start_type_str == "circle") {
start_type = RainbowRenderer::START_TYPE_CIRCLE;
} else {
std::cout << "Unknown start type " << start_type_str << std::endl;
return 1;
}
rainbow_renderer->setStartType(start_type);
break;
}
case 'f': { // Fill mode
std::string fill_mode_str = optarg;
if (fill_mode_str == "edge") { // Fastest
std::cout << "Setting fill mode to \"edge\"" << std::endl;
fill_mode = RainbowRenderer::FILL_MODE_EDGE;
} else if (fill_mode_str == "neighbour" || fill_mode_str == "neighbor") { // Slower, smoother
std::cout << "Setting fill mode to \"closest neighbour\"" << std::endl;
fill_mode = RainbowRenderer::FILL_MODE_NEIGHBOUR;
} else if (fill_mode_str == "average") { // Really slow, "coral" effect
std::cout << "Setting fill mode to \"average neighbour\"" << std::endl;
fill_mode = RainbowRenderer::FILL_MODE_NEIGHBOUR_AVERAGE;
} else {
std::cerr << "Unknown fill mode " << fill_mode_str << std::endl;
return 1;
}
rainbow_renderer->setFillMode(fill_mode);
break;
}
case 'o': { // Initial colour ordering
std::string colour_order_string = optarg;
for (char &it: colour_order_string) {
switch (it) {
case 'r':
case 'R':
rainbow_renderer->addColourOrder({COLOUR_ORDER_RANDOM, false});
std::cout << "Randomising colours" << std::endl;
break;
case 'h':
case 'H':
rainbow_renderer->addColourOrder({COLOUR_ORDER_HUE, it == 'H'});
std::cout << "Sorting colours by hue " << (it == 'h' ? "ascending" : "descending")
<< std::endl;
break;
case 's':
case 'S':
rainbow_renderer->addColourOrder({COLOUR_ORDER_SAT, it == 'S'});
std::cout << "Sorting colours by saturation " << (it == 's' ? "ascending" : "descending")
<< std::endl;
break;
case 'l':
case 'L':
rainbow_renderer->addColourOrder({COLOUR_ORDER_LUM, it == 'L'});
std::cout << "Sorting colours by luminosity " << (it == 'l' ? "ascending" : "descending")
<< std::endl;
break;
default:
std::cerr << "Unknown colour ordering " << it << std::endl;
exit(1);
}
}
break;
}
case 'r': {
int seed = (int) strtol(optarg, nullptr, 0);
if (seed <= 0) {
std::cout << "Invalid seed argument " << optarg << std::endl;
return 1;
}
rainbow_renderer->setSeed(seed);
break;
}
case '?': {
if (optopt == 'h' || optopt == 'w' || optopt == 'c' || optopt == 'd' || optopt == 'f' ||
optopt == 'p') {
std::cerr << "Option -" << char(optopt) << " requires an argument" << std::endl;
} else if (isprint(optopt)) {
std::cerr << "Unknown option -" << char(optopt) << std::endl;
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
} else {
std::cerr << "Unknown option character" << optopt << std::endl;
}
return 1;
}
default:
abort();
}
}
for (; optind < argc; optind++) {
int num_start_points = (int) strtol(argv[optind], nullptr, 0);
if (num_start_points > 0) {
rainbow_renderer->setNumStartPoints(num_start_points);
}
}
time_t start_time = time(nullptr);
rainbow_renderer->init();
rainbow_renderer->fill();
time_t end_time = time(nullptr);
std::cout << "Completed in " << (end_time - start_time) << "s" << std::endl;
rainbow_renderer->writeToFile("output.bmp");
delete rainbow_renderer;
return 0;
}