-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrainbow_renderer.cpp
More file actions
495 lines (444 loc) · 19.2 KB
/
rainbow_renderer.cpp
File metadata and controls
495 lines (444 loc) · 19.2 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include "rainbow_renderer.h"
void RainbowRenderer::setSeed(unsigned int _seed) {
this->seed = _seed;
}
void RainbowRenderer::setPixelsWide(int _pixels_wide) {
this->pixels_wide = _pixels_wide;
}
void RainbowRenderer::setPixelsHigh(int _pixels_high) {
this->pixels_high = _pixels_high;
}
void RainbowRenderer::setNumStartPoints(int _num_start_points) {
this->num_start_points = _num_start_points;
}
void RainbowRenderer::setColourDepth(int _colour_depth) {
this->colour_depth = _colour_depth;
}
void RainbowRenderer::setDifferenceFunction(float (*_func)(const Colour &, const Colour &)) {
this->difference_function = _func;
}
void RainbowRenderer::setStartType(StartType _start_type) {
this->start_type = _start_type;
}
void RainbowRenderer::setFillMode(FillMode _fill_mode) {
this->fill_mode = _fill_mode;
}
void RainbowRenderer::addColourOrder(ColourOrdering ordering) {
this->colour_ordering.push_back(ordering);
}
void RainbowRenderer::addStartingHue(int hue) {
this->startingHues.push_back(hue);
}
void RainbowRenderer::setMinimumLuminosity(float luminosity) {
this->minimumLuminosity = luminosity;
}
void RainbowRenderer::setMaximumLuminosity(float luminosity) {
this->maximumLuminosity = luminosity;
}
void RainbowRenderer::setMinimumSaturation(float saturation) {
this->minimumSaturation = saturation;
}
void RainbowRenderer::setMaximumSaturation(float saturation) {
this->maximumSaturation = saturation;
}
/// Initialises starting pixels
void RainbowRenderer::init() {
this->rng = std::default_random_engine(this->seed);
this->pixels.resize(this->pixels_wide * this->pixels_high);
this->fillColours();
std::vector<Point> possible_start_points;
switch (this->start_type) {
case START_TYPE_CENTRE: {
possible_start_points.emplace_back(this->pixels_wide / 2, this->pixels_high / 2);
break;
}
case START_TYPE_HORIZONTAL_LINE: {
int midY = this->pixels_high / 2;
for (int x = 0; x < this->pixels_wide; ++x) {
possible_start_points.emplace_back(x, midY);
}
break;
}
case START_TYPE_VERTICAL_LINE: {
int midX = this->pixels_wide / 2;
for (int y = 0; y < this->pixels_high; ++y) {
possible_start_points.emplace_back(midX, y);
}
break;
}
case START_TYPE_CORNER: {
possible_start_points.emplace_back(0, 0);
possible_start_points.emplace_back(this->pixels_wide - 1, 0);
possible_start_points.emplace_back(0, this->pixels_high - 1);
possible_start_points.emplace_back(this->pixels_wide - 1, this->pixels_high - 1);
break;
}
case START_TYPE_EDGE:
case START_TYPE_RANDOM:
for (int y = 0; y < this->pixels_high; ++y) {
for (int x = 0; x < this->pixels_wide; ++x) {
if (this->start_type == START_TYPE_RANDOM ||
(x == 0 || x == this->pixels_wide - 1 || y == 0 || y == this->pixels_high - 1)) {
possible_start_points.emplace_back(x, y);
}
}
}
break;
case START_TYPE_CIRCLE: {
// Make a circle such that the inner area and outer area are the same (for a square board)
int radius = int(float(std::min(this->pixels_wide, this->pixels_high)) / sqrt(2 * M_PI));
int f = 1 - radius;
int ddF_x = 0;
int ddF_y = -2 * radius;
int x_offset = 0;
int y_offset = radius;
int mid_x = this->pixels_wide / 2;
int mid_y = this->pixels_high / 2;
possible_start_points.emplace_back(mid_x, mid_y + radius);
possible_start_points.emplace_back(mid_x, mid_y - radius);
possible_start_points.emplace_back(mid_x + radius, mid_y);
possible_start_points.emplace_back(mid_x - radius, mid_y);
while (x_offset < y_offset) {
if (f >= 0) {
--y_offset;
ddF_y += 2;
f += ddF_y;
}
x_offset++;
ddF_x += 2;
f += ddF_x + 1;
possible_start_points.emplace_back(mid_x + x_offset, mid_y + y_offset);
possible_start_points.emplace_back(mid_x - x_offset, mid_y + y_offset);
possible_start_points.emplace_back(mid_x + x_offset, mid_y - y_offset);
possible_start_points.emplace_back(mid_x - x_offset, mid_y - y_offset);
possible_start_points.emplace_back(mid_x + y_offset, mid_y + x_offset);
possible_start_points.emplace_back(mid_x - y_offset, mid_y + x_offset);
possible_start_points.emplace_back(mid_x + y_offset, mid_y - x_offset);
possible_start_points.emplace_back(mid_x - y_offset, mid_y - x_offset);
}
break;
}
}
std::shuffle(possible_start_points.begin(), possible_start_points.end(), this->rng);
if (this->num_start_points > possible_start_points.size()) {
std::cout << this->num_start_points << " starting points were requested, but there are only "
<< possible_start_points.size() << " available" << std::endl;
}
for (int i = 0; i < possible_start_points.size() && i < this->num_start_points; ++i) {
std::cout << "Starting in position " << possible_start_points[i] << std::endl;
fillPoint(possible_start_points[i]);
}
std::cout << "Finished placing start points" << std::endl;
}
void RainbowRenderer::fill() {
switch (this->fill_mode) {
case FILL_MODE_EDGE:
this->edge_fill();
break;
case FILL_MODE_NEIGHBOUR:
this->neighbour_fill();
break;
case FILL_MODE_NEIGHBOUR_AVERAGE:
this->neighbour_fill(true);
break;
}
}
/// Fills remaining spaces with pixels
void RainbowRenderer::edge_fill() {
int partition = this->pixels_high * this->pixels_wide / 100;
while (true) {
if (this->available_edges.empty() || this->colour_index >= this->colours.size()) {
std::cout << "Out of edges or colours" << std::endl;
break;
}
const Colour ¤t_colour = this->colours[this->colour_index];
Point best_point;
float best_difference = std::numeric_limits<float>::max();
for (auto &available_edge: this->available_edges) {
float diff = this->difference_function(current_colour, getPixelAtPoint(available_edge)->colour);
if (diff < best_difference) {
best_point = available_edge;
best_difference = diff;
}
}
auto neighbours = getNeighboursOfPoint(best_point);
int neighbour_count = (int) neighbours.size();
std::shuffle(std::begin(neighbours), std::end(neighbours), this->rng);
int neighbour_index = 0;
for (; neighbour_index < neighbour_count; ++neighbour_index) {
Point &neighbour = neighbours[neighbour_index];
Pixel *const neighbour_pixel = getPixelAtPoint(neighbour);
if (neighbour_pixel->is_filled) {
continue;
}
neighbour_pixel->colour = current_colour;
neighbour_pixel->is_filled = true;
this->available_edges.push_back(neighbour);
++this->colour_index;
if (this->colour_index % partition == 0) {
std::cout << "Step " << this->colour_index << " with " << this->available_edges.size() << " edges ("
<< ((float) this->colour_index / float(this->pixels_high * this->pixels_wide) * 100)
<< "%)"
<< std::endl;
std::ostringstream stream;
stream << "output_" << int(this->colour_index / partition) << ".bmp";
std::cout << "Saving..." << std::flush;
this->writeToFile(stream.str());
std::cout << "Done" << std::endl;
this->cleanFilledEdges();
}
break;
}
if (neighbour_index == neighbour_count) {
this->available_edges.remove(best_point);
}
}
}
void RainbowRenderer::neighbour_fill(bool neighbour_average) {
const int total_pixels = this->pixels_high * this->pixels_wide;
const int partition = total_pixels / 100;
// List of places where pixels can be placed (next to neighbours)
std::list<Point> availablePoints;
// Find all neighbours of existing points and add them to the available list
for (int y = 0; y < this->pixels_high; ++y) {
for (int x = 0; x < this->pixels_wide; ++x) {
Point point = Point(x, y);
Pixel *pixel = this->getPixelAtPoint(point);
if (!pixel->is_filled) {
continue;
}
for (auto neighbour: this->getNeighboursOfPoint(point)) {
Pixel *neighbour_pixel = this->getPixelAtPoint(neighbour);
if (!neighbour_pixel->is_filled && !neighbour_pixel->is_available) {
neighbour_pixel->is_available = true;
availablePoints.push_back(neighbour);
}
}
}
}
// While there are colours to place and available spots to place them
for (; this->colour_index < this->colours.size() && !availablePoints.empty(); ++this->colour_index) {
Colour &colour = this->colours[colour_index];
Point best_point;
float best_difference = std::numeric_limits<float>::max();
// Find the point that has neighbours that are closest
for (auto point: availablePoints) {
float neighbourDiff = this->getNeighbourDifference(point, colour, neighbour_average);
if (neighbourDiff < best_difference) {
best_difference = neighbourDiff;
best_point = point;
}
}
Pixel *pixel = this->getPixelAtPoint(best_point);
pixel->is_filled = true;
pixel->is_available = false;
pixel->colour = colour;
availablePoints.remove(best_point);
for (auto neighbour: this->getNeighboursOfPoint(best_point)) {
Pixel *neighbourPixel = this->getPixelAtPoint(neighbour);
if (!neighbourPixel->is_filled && !neighbourPixel->is_available) {
neighbourPixel->is_available = true;
auto iter = availablePoints.begin();
std::advance(iter, this->rng() % availablePoints.size());
availablePoints.insert(iter, neighbour);
}
}
if (this->colour_index % partition == 0) {
std::cout << "Placed " << this->colour_index << "/" << total_pixels << " pixels ( "
<< ((float) this->colour_index / float(total_pixels) * 100) << "%) with "
<< availablePoints.size()
<< " spaces available"
<< std::endl;
std::ostringstream stream;
stream << "output_" << int(this->colour_index / partition) << ".bmp";
std::cout << "Saving..." << std::flush;
this->writeToFile(stream.str());
std::cout << "Done" << std::endl;
}
}
}
float RainbowRenderer::getNeighbourDifference(Point point, const Colour &colour, bool neighbour_average) {
std::vector<float> diffs;
for (auto neighbour: this->getNeighboursOfPoint(point)) {
const Pixel *pixel = this->getPixelAtPoint(neighbour);
if (!pixel->is_filled) {
continue;
}
float diff = this->difference_function(colour, pixel->colour);
diffs.push_back(diff);
}
if (diffs.empty()) {
return std::numeric_limits<float>::max();
}
if (neighbour_average) {
return (float) (std::accumulate(diffs.begin(), diffs.end(), 0.0) / float(diffs.size()));
}
return *std::min_element(diffs.begin(), diffs.end());
}
/// Writes the current content of the pixel board out to file
/// \param _filename
void RainbowRenderer::writeToFile(const std::string &_filename) {
BMP bmp = BMP(this->pixels_wide, this->pixels_high, false);
for (int y = 0; y < this->pixels_high; ++y) {
for (int x = 0; x < this->pixels_wide; ++x) {
Pixel *pixel = getPixel(x, y);
bmp.set_pixel(x, y, pixel->colour.b, pixel->colour.g, pixel->colour.r);
}
}
bmp.write(_filename.c_str());
}
void RainbowRenderer::cleanFilledEdges() {
std::cout << "Cleaning " << std::flush;
int removedEdges = 0;
auto iter = this->available_edges.begin();
while (iter != this->available_edges.end()) {
Point edgePoint = *iter;
auto neighbours = getNeighboursOfPoint(edgePoint);
bool hasOpenNeighbours = false;
for (auto &neighbour_point: neighbours) {
const Pixel *neighbour_pixel = getPixelAtPoint(neighbour_point);
if (!neighbour_pixel->is_filled) {
hasOpenNeighbours = true;
break;
}
}
if (!hasOpenNeighbours) {
iter = this->available_edges.erase(iter);
removedEdges += 1;
} else {
++iter;
}
}
std::cout << "Done (cleaned " << removedEdges << " edges)" << std::endl;
}
Pixel *RainbowRenderer::getPixel(int x, int y) {
return &this->pixels[y * this->pixels_wide + x];
}
/// Gets the pixel at the given point
/// \param point The point
/// \return The pixel at that point
Pixel *RainbowRenderer::getPixelAtPoint(const Point &point) {
return &this->pixels[point.y * this->pixels_wide + point.x];
}
/// Gets the neighbour positions of the given point (points over the edge of the board aren ot returned)
/// \param point The point to find the neighbours for
/// \return The number of neighbours
std::vector<Point> RainbowRenderer::getNeighboursOfPoint(const Point &point) const {
std::vector<Point> neighbours;
for (int y = -1; y <= 1; ++y) {
if (y + point.y < 0 || y + point.y >= this->pixels_high) {
continue;
}
for (int x = -1; x <= 1; ++x) {
if ((x == 0 && y == 0) || x + point.x < 0 || x + point.x >= this->pixels_wide) {
continue;
}
neighbours.emplace_back(x + point.x, y + point.y);
}
}
return neighbours;
}
/// Fills the list of random colours
/// \param colour_depth The number of each unique colours in each channel
void RainbowRenderer::fillColours() {
if (this->startingHues.empty()) {
if (this->colour_depth == 0) {
this->colour_depth = ceil(pow(this->pixels_wide * this->pixels_high, 1.0f / 3.0f));
}
for (int r = 0; r < this->colour_depth; ++r) {
for (int g = 0; g < this->colour_depth; ++g) {
for (int b = 0; b < this->colour_depth; ++b) {
this->colours.emplace_back(r * 255 / (this->colour_depth - 1),
g * 255 / (this->colour_depth - 1),
b * 255 / (this->colour_depth - 1));
}
}
}
std::cout << "Colour depth " << this->colour_depth << " makes " << this->colours.size() << " colours (of "
<< (this->pixels_wide * this->pixels_high) << " pixels)" << std::endl;
} else {
std::cout << "Starting hues detected, ignoring colour depth and instead start from hue points."
<< std::endl;
int offset = 0;
std::set<Colour> colourSet;
while (colourSet.size() < this->pixels_wide * this->pixels_high) {
for (int r = 0; r < 255; ++r) {
for (int g = 0; g < 255; ++g) {
for (int b = 0; b < 255; ++b) {
const auto t = rgbToHsl(r, g, b);
int hue = int(std::get<0>(t) * 360);
float sat = std::get<1>(t);
float lum = std::get<2>(t);
for (auto targetHue: this->startingHues) {
// The hue might be close to the target when wrapping around from 360
// so try both and the minimum
int hueDifference = std::abs(hue - targetHue);
if (hueDifference >= 180) {
hueDifference =
targetHue > hue ? std::abs(hue + 360 - targetHue) : std::abs(
hue + targetHue - 360);
}
if (std::abs(hueDifference - offset) <= 1 &&
lum >= this->minimumLuminosity && lum <= this->maximumLuminosity &&
sat >= this->minimumSaturation && sat <= this->maximumSaturation) {
if (colourSet.find(Colour(r, g, b)) == colourSet.end()) {
colourSet.insert(Colour(r, g, b));
break;
}
}
}
}
}
}
offset += 1;
std::cout << "Colour offset now " << offset << std::endl;
}
this->colours.insert(this->colours.end(), colourSet.begin(), colourSet.end());
}
if (this->colours.size() < this->pixels_wide * this->pixels_high) {
std::cout << "All colours were exhausted with only "
<< 100 * this->colours.size() / (this->pixels_wide * this->pixels_high)
<< "% of the image covered. Please revise input parameters" << std::endl;
exit(1);
}
// Default colour ordering if the user doesn't supply any
if (this->colour_ordering.empty()) {
this->colour_ordering.push_back({COLOUR_ORDER_LUM, false});
this->colour_ordering.push_back({COLOUR_ORDER_HUE, false});
this->colour_ordering.push_back({COLOUR_ORDER_SAT, true});
}
for (auto order: this->colour_ordering) {
bool (*compare_func)(const Colour &c1, const Colour &c2);
switch (order.ordering_type) {
case COLOUR_ORDER_RANDOM:
std::shuffle(std::begin(colours), std::end(colours), rng);
continue;
case COLOUR_ORDER_HUE:
compare_func = compareHue;
break;
case COLOUR_ORDER_SAT:
compare_func = compareSat;
break;
case COLOUR_ORDER_LUM:
compare_func = compareLum;
break;
default:
std::cerr << "Unknown colour ordering " << order.ordering_type << std::endl;
return;
}
if (order.reverse) {
std::sort(std::rbegin(colours), std::rend(colours), compare_func);
} else {
std::sort(std::begin(colours), std::end(colours), compare_func);
}
}
}
/// Fills the pixel at the given point
/// \param point The point to place the pixel at
void RainbowRenderer::fillPoint(Point &point) {
this->available_edges.push_back(point);
Pixel *pixel = getPixelAtPoint(point);
pixel->colour = this->colours[this->colour_index];
pixel->is_filled = true;
++this->colour_index;
}