-
Notifications
You must be signed in to change notification settings - Fork 4
/
dither.c
333 lines (274 loc) · 9.65 KB
/
dither.c
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
static unsigned char nearestPaletteColor(unsigned char pixel) {
if(pixel > BPP_CLIP)
return 255;
return ((pixel + BPP_BIAS) / BPP_MUL) * BPP_MUL;
}
static unsigned char clippedAdd(unsigned char base, int8_t bias) {
int16_t intermediate = bias + base;
if(intermediate > 255)
return 255;
else if(intermediate < 0)
return 0;
else
return (unsigned char) intermediate;
}
static int8_t quantizePixel(unsigned char *buf, uint idx) {
unsigned char oldpixel = buf[idx];
unsigned char newpixel = nearestPaletteColor(oldpixel);
buf[idx] = newpixel;
return (int8_t) oldpixel - newpixel;
}
static void diffuseError(unsigned char *buf, uint idx, int8_t error, int8_t weight, int8_t base) {
buf[idx] = clippedAdd(buf[idx], (int8_t) ((((int) error) * weight) / base));
}
static uint8_t* loadNoise() {
FILE *in = fopen("bluenoise.bin", "r");
if (!in) {
printf("Failed to open noise file bluenoise.bin\n");
exit(0);
}
uint8_t *buffer = malloc(128 * 128 * sizeof(uint8_t));
fread(buffer, sizeof(uint8_t), 128 * 128, in);
fclose(in);
return buffer;
}
// Floyd-Steinberg dithering
static void floydSteinberg(unsigned char *frameBuf, int linesize, int width, int height) {
uint32_t i,j,idx;
int8_t quantError;
for(j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
idx = j * linesize + i;
quantError = quantizePixel(frameBuf, idx);
if(i != width-1)
diffuseError(frameBuf, idx + 1, quantError, 7, 16);
if(j != height-1) {
diffuseError(frameBuf, idx + linesize, quantError, 5, 16);
if(i != width-1)
diffuseError(frameBuf, idx + linesize + 1, quantError, 1, 16);
if(i != 0)
diffuseError(frameBuf, idx + linesize - 1, quantError, 3, 16);
}
}
}
}
// Serpentine Floyd-Steinberg dithering
// Essentially reverses dithering direction every line for more even texture
static void floydSteinbergSerpentine(unsigned char *frameBuf, int linesize, int width, int height) {
uint32_t i,j,idx;
int8_t quantError;
for(j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
idx = j * linesize + i;
quantError = quantizePixel(frameBuf, idx);
if(i != width-1)
diffuseError(frameBuf, idx + 1, quantError, 7, 16);
if(j != height-1) {
diffuseError(frameBuf, idx + linesize, quantError, 5, 16);
if(i != width-1)
diffuseError(frameBuf, idx + linesize + 1, quantError, 1, 16);
if(i != 0)
diffuseError(frameBuf, idx + linesize - 1, quantError, 3, 16);
}
}
j++;
if(j == height)
continue;
for(i = width; i > 0; i--) {
idx = j * linesize + i - 1;
quantError = quantizePixel(frameBuf, idx); // For cool glitch, quantize everything to 0 here
if(i != 0)
diffuseError(frameBuf, idx - 1, quantError, 7, 16);
if(j != height-1) {
diffuseError(frameBuf, idx + linesize, quantError, 5, 16);
if(i != 0)
diffuseError(frameBuf, idx + linesize - 1, quantError, 1, 16);
if(i != width - 1)
diffuseError(frameBuf, idx + linesize + 1, quantError, 3, 16);
}
}
}
}
static void interleavedGradient(unsigned char *frameBuf, int linesize, int width, int height) {
const float c1 = 52.9829189;
const float cx = 0.06711056;
const float cy = 0.00583715;
uint32_t i,j,idx;
uint8_t noise;
float inner, outer;
for(j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
idx = j * linesize + i;
inner = cx * i + cy * j;
outer = c1 * (inner - (int) inner);
noise = (uint8_t) ((outer - (int) outer) * BPP_MUL);
frameBuf[idx] = clippedAdd(frameBuf[idx], noise - BPP_BIAS);
quantizePixel(frameBuf, idx);
}
}
}
static void blueNoise(unsigned char *frameBuf, int linesize, int width, int height) {
static uint8_t initialized = 0;
static uint8_t *noiseBuf;
// uhh pretty sure that's not how you're supposed to do things
// allocating memory that's never freed? I'm sure it'll be fine...
if(!initialized) {
noiseBuf = loadNoise();
initialized = 1;
}
uint32_t i,j,idx,noiseidx;
uint8_t noise;
for(j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
idx = j * linesize + i;
noiseidx = (j % 128) * 128 + (i % 128);
noise = (((int) noiseBuf[noiseidx]) * BPP_MUL) >> 8;
frameBuf[idx] = clippedAdd(frameBuf[idx], noise - BPP_BIAS);
quantizePixel(frameBuf, idx);
}
}
}
static void whiteNoise(unsigned char *frameBuf, int linesize, int width, int height) {
uint32_t i,j,idx;
uint8_t noise;
int8_t *randomMask = (int8_t *) malloc(width * height * sizeof(int8_t));
FILE *devRandom = fopen("/dev/urandom", "r");
fread(randomMask, sizeof(int8_t), width * height, devRandom);
fclose(devRandom);
for(j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
idx = j * linesize + i;
noise = (((int) randomMask[j * width + i]) * BPP_MUL) >> 8;
frameBuf[idx] = clippedAdd(frameBuf[idx], noise - BPP_BIAS);
quantizePixel(frameBuf, idx);
}
}
free(randomMask);
}
static void atkinson(unsigned char *frameBuf, int linesize, int width, int height) {
uint32_t i,j,idx;
int8_t quantError;
for(j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
idx = j * linesize + i;
quantError = quantizePixel(frameBuf, idx);
// Same row diffusion
if(i != width-1)
diffuseError(frameBuf, idx+1, quantError, 1, 8);
if(i < width-2)
diffuseError(frameBuf, idx+2, quantError, 1, 8);
if(j == height-1)
continue;
// Next row diffusion
diffuseError(frameBuf, idx + linesize, quantError, 1, 8);
if(i != 0)
diffuseError(frameBuf, idx + linesize - 1, quantError, 1, 8);
if(i != width-1)
diffuseError(frameBuf, idx + linesize + 1, quantError, 1, 8);
// Third row diffusion
if(j != height-2)
diffuseError(frameBuf, idx + linesize * 2, quantError, 1, 8);
}
}
}
static void fullSierra(unsigned char *frameBuf, int linesize, int width, int height) {
uint32_t i,j,idx;
int8_t quantError;
for(j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
idx = j * linesize + i;
quantError = quantizePixel(frameBuf, idx);
// Same row diffusion
if(i != width-1)
diffuseError(frameBuf, idx+1, quantError, 5, 32);
if(i < width-2)
diffuseError(frameBuf, idx+2, quantError, 3, 32);
if(j == height-1)
continue;
// Next row diffusion
diffuseError(frameBuf, idx + linesize, quantError, 5, 32);
if(i != 0)
diffuseError(frameBuf, idx + linesize - 1, quantError, 4, 32);
if(i > 1)
diffuseError(frameBuf, idx + linesize - 2, quantError, 2, 32);
if(i != width-1)
diffuseError(frameBuf, idx + linesize + 1, quantError, 4, 32);
if(i < width-2)
diffuseError(frameBuf, idx + linesize + 2, quantError, 2, 32);
if(j == height-2)
continue;
// Third row diffusion
diffuseError(frameBuf, idx + linesize * 2, quantError, 3, 32);
if(i != 0)
diffuseError(frameBuf, idx + linesize * 2 - 1, quantError, 2, 32);
if(i != width-1)
diffuseError(frameBuf, idx + linesize * 2 + 1, quantError, 2, 32);
}
}
}
static void twoRowSierra(unsigned char *frameBuf, int linesize, int width, int height) {
uint32_t i,j,idx;
int8_t quantError;
for(j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
idx = j * linesize + i;
quantError = quantizePixel(frameBuf, idx);
// Same row diffusion
if(i != width-1)
diffuseError(frameBuf, idx+1, quantError, 4, 16);
if(i < width-2)
diffuseError(frameBuf, idx+2, quantError, 3, 16);
if(j == height-1)
continue;
// Next row diffusion
diffuseError(frameBuf, idx + linesize, quantError, 3, 16);
if(i != 0)
diffuseError(frameBuf, idx + linesize - 1, quantError, 2, 16);
if(i > 1)
diffuseError(frameBuf, idx + linesize - 2, quantError, 1, 16);
if(i != width-1)
diffuseError(frameBuf, idx + linesize + 1, quantError, 2, 16);
if(i < width-2)
diffuseError(frameBuf, idx + linesize + 2, quantError, 1, 16);
}
}
}
static void stucki(unsigned char *frameBuf, int linesize, int width, int height) {
uint32_t i,j,idx;
int8_t quantError;
for(j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
idx = j * linesize + i;
quantError = quantizePixel(frameBuf, idx);
// Same row diffusion
if(i != width-1)
diffuseError(frameBuf, idx+1, quantError, 8, 42);
if(i < width-2)
diffuseError(frameBuf, idx+2, quantError, 4, 42);
if(j == height-1)
continue;
// Next row diffusion
diffuseError(frameBuf, idx + linesize, quantError, 8, 42);
if(i != 0)
diffuseError(frameBuf, idx + linesize - 1, quantError, 4, 42);
if(i > 1)
diffuseError(frameBuf, idx + linesize - 2, quantError, 2, 42);
if(i != width-1)
diffuseError(frameBuf, idx + linesize + 1, quantError, 4, 42);
if(i < width-2)
diffuseError(frameBuf, idx + linesize + 2, quantError, 2, 42);
if(j == height-2)
continue;
// Third row diffusion
diffuseError(frameBuf, idx + linesize * 2, quantError, 4, 42);
if(i != 0)
diffuseError(frameBuf, idx + linesize * 2 - 1, quantError, 2, 42);
if(i > 1)
diffuseError(frameBuf, idx + linesize * 2 - 2, quantError, 1, 42);
if(i != width-1)
diffuseError(frameBuf, idx + linesize * 2 + 1, quantError, 2, 42);
if(i < width-2)
diffuseError(frameBuf, idx + linesize * 2 + 2, quantError, 1, 42);
}
}
}