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
|
#include "config.h"
#include <filesystem>
#include <iostream>
#include <random>
#include <stdexcept>
#include <vector>
#include <cmath>
#include <SFML/Graphics.hpp>
#include "brushes.h"
namespace fs = std::filesystem;
namespace {
bool seeded = false;
std::mt19937 engine;
std::uniform_int_distribution<sf::Uint32> color_dist;
std::uniform_real_distribution<float> white_chance(0, 1.0);
sf::Color random_color() {
if (!seeded) {
engine.seed(std::random_device{}());
seeded = true;
}
#define COLOR_MODE 2
#if (COLOR_MODE == 1)
sf::Color c = sf::Color(color_dist(engine));
c.a = 255;
c.g = 0;
return c;
#elif (COLOR_MODE == 2)
sf::Color c =
white_chance(engine) > 0.5 ? sf::Color::Yellow : sf::Color::Green;
float f = white_chance(engine);
c.r *= f;
c.g *= f;
c.b *= f;
c.a = 255;
return c;
#elif (COLOR_MODE == 3)
if (white_chance(engine) > 0.5) {
return sf::Color::White;
} else {
return sf::Color::Black;
}
#elif (COLOR_MODE == 4)
float f = white_chance(engine);
return sf::Color(255 * f, 255 * f, 255 * f, 255);
#endif
}
// Distort a black-white color. assume color_mode == 3 or 4.
sf::Color distort(const sf::Color& c, float p = 2) {
return sf::Color(255 * std::pow(c.r / 255.0, p),
255 * std::pow(c.g / 255.0, p),
255 * std::pow(c.b / 255.0, p), 255);
}
bool in_range(const sf::Vector2u& size, const sf::Vector2u& v) {
return v.x < size.x && v.y < size.y;
}
sf::Color avg_color(const sf::Image& im, unsigned i, unsigned j,
const std::vector<sf::Vector2i>& d,
bool black_border = false) {
sf::Vector2u p(i, j);
unsigned long ct = 0, r = 0, g = 0, b = 0, a = 0;
for (int k = 0; k < d.size(); ++k) {
sf::Vector2u q = p + sf::Vector2u(d[k]);
if (in_range(im.getSize(), q)) {
r += im.getPixel(q.x, q.y).r * (p == q ? 2 : 1);
g += im.getPixel(q.x, q.y).g * (p == q ? 2 : 1);
b += im.getPixel(q.x, q.y).b * (p == q ? 2 : 1);
a += im.getPixel(q.x, q.y).a;
ct += (p == q ? 2 : 1);
} else if (black_border) {
++ct;
}
}
r /= ct;
g /= ct;
b /= ct;
a /= ct;
return sf::Color(r, g, b, a);
}
sf::Color avg_card(const sf::Image& im, unsigned i, unsigned j) {
const std::vector<sf::Vector2i> d = {
{0, 0}, {1, 0}, {0, -1}, {-1, 0}, {0, 1}};
return avg_color(im, i, j, d);
}
void noise(sf::Image& im, int mode) {
sf::Image im2, im3;
if (mode == 1) {
im2.create(im.getSize().x * 2, im.getSize().y * 2);
for (unsigned i = 0; i < im.getSize().x; ++i) {
for (unsigned j = 0; j < im.getSize().y; ++j) {
sf::Color c = im.getPixel(i, j);
im2.setPixel(i * 2, j * 2, c);
im2.setPixel(i * 2 + 1, j * 2, c);
im2.setPixel(i * 2 + 1, j * 2 + 1, c);
im2.setPixel(i * 2, j * 2 + 1, c);
}
}
im3.create(im.getSize().x * 2, im.getSize().y * 2);
for (unsigned i = 0; i < im2.getSize().x; ++i) {
for (unsigned j = 0; j < im2.getSize().y; ++j) {
im3.setPixel(i, j, brushes::CardinalAverage(im2, i, j));
}
}
} else if (mode == 2) {
im3.create(im.getSize().x, im.getSize().y);
for (unsigned i = 0; i < im.getSize().x; ++i) {
for (unsigned j = 0; j < im.getSize().y; ++j) {
im3.setPixel(i, j, brushes::CardinalAverage(im, i, j));
}
}
} else if (mode == 3) {
im3.create(im.getSize().x * 2, im.getSize().y * 2);
for (unsigned i = 0; i < im.getSize().x; ++i) {
for (unsigned j = 0; j < im.getSize().y; ++j) {
sf::Color c = im.getPixel(i, j);
im3.setPixel(i * 2, j * 2, c);
im3.setPixel(i * 2 + 1, j * 2,
avg_color(im, i, j, {{0, 0}, {1, 0}}, true));
im3.setPixel(i * 2, j * 2 + 1,
avg_color(im, i, j, {{0, 0}, {0, 1}}, true));
im3.setPixel(i * 2 + 1, j * 2 + 1,
avg_color(im3, i * 2 + 1, j * 2 + 1,
{{-1, 0}, {1, 0}, {0, -1}, {0, 1},
{-1,-1},{-1,1},{1,-1},{1,1}}, true));
}
}
} else {
throw std::invalid_argument("Bad noise mode.");
}
im = im3;
}
sf::Color flip(const sf::Color& c) {
return sf::Color(255 - c.r, 255 - c.g, 255 - c.b, c.a);
}
} // namespace
int main(int argc, char* argv[]) {
sf::RenderWindow window(sf::VideoMode(640, 400), "Noise");
sf::Font font;
if (!font.loadFromFile((fs::path(argv[0]).parent_path() / "OpenSans.ttf").string())) {
std::cerr << "FATAL: Failed to load font OpenSans.ttf." << std::endl;
return -1;
}
#ifdef HAVE_FONT_SMOOTHING
font.setSmooth(true);
#endif
sf::Text counter("Iteration 0 Blur 0 Brightness 0", font);
counter.setPosition(5, 5);
sf::Text help(
"1 - Zoom\tQ - Quit\n"
"2 - Average blur\n"
"3 - Quadratic darken\n"
"4 - Quadratic brighten\n"
"F - Flip colors\n"
"R - randomize pixels\n"
"0 - Reset texture",
font, 15);
help.setPosition(5, window.getSize().y - help.getLocalBounds().height - 10);
int icount = 0, bcount = 0, dcount = 0;
sf::Texture text;
sf::Image im;
sf::RectangleShape rect(sf::Vector2f(400, 200));
rect.setPosition(100, 50);
const sf::Vector2u initSize(32, 16);
im.create(initSize.x, initSize.y, sf::Color::White);
for (unsigned i = 0; i < im.getSize().x; ++i) {
for (unsigned j = 0; j < im.getSize().y; ++j) {
im.setPixel(i, j, random_color());
}
}
text.loadFromImage(im);
rect.setTexture(&text);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
} else if (event.type == sf::Event::KeyReleased) {
if (event.key.code == sf::Keyboard::Num1 && im.getSize().x < 2 << 8) {
++icount;
noise(im, 1);
} else if (event.key.code == sf::Keyboard::Num2) {
++bcount;
if (event.key.shift) {
} else {
noise(im, 2);
}
} else if (event.key.code == sf::Keyboard::Num3) {
--dcount;
for (unsigned i = 0; i < im.getSize().x; ++i) {
for (unsigned j = 0; j < im.getSize().y; ++j) {
im.setPixel(i, j, distort(im.getPixel(i, j), 1.5));
}
}
} else if (event.key.code == sf::Keyboard::Num4) {
++dcount;
for (unsigned i = 0; i < im.getSize().x; ++i) {
for (unsigned j = 0; j < im.getSize().y; ++j) {
im.setPixel(i, j, distort(im.getPixel(i, j), 2.0 / 3));
}
}
} else if (event.key.code == sf::Keyboard::F) {
for (unsigned i = 0; i < im.getSize().x; ++i) {
for (unsigned j = 0; j < im.getSize().y; ++j) {
im.setPixel(i, j, flip(im.getPixel(i, j)));
}
}
} else if (event.key.code == sf::Keyboard::Num0) {
bcount = icount = 0, dcount = 0;
im.create(initSize.x, initSize.y, sf::Color::White);
for (unsigned i = 0; i < im.getSize().x; ++i) {
for (unsigned j = 0; j < im.getSize().y; ++j) {
im.setPixel(i, j, random_color());
}
}
} else if (event.key.code == sf::Keyboard::R) {
bcount = 0, dcount = 0;
for (unsigned i = 0; i < im.getSize().x; ++i) {
for (unsigned j = 0; j < im.getSize().y; ++j) {
im.setPixel(i, j, random_color());
}
}
} else {
continue;
}
counter.setString("Iteration " + std::to_string(icount) + " Blur " +
std::to_string(bcount) + " Brightness " +
std::to_string(dcount));
text.loadFromImage(im);
rect.setTexture(&text, true);
}
}
window.clear();
window.draw(help);
window.draw(counter);
window.draw(rect);
window.display();
sf::sleep(sf::milliseconds(1));
}
return 0;
}
|