-
Notifications
You must be signed in to change notification settings - Fork 0
/
dithering.c
375 lines (299 loc) · 9.26 KB
/
dithering.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
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
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "image.h"
int dithering2x2[2][2] = {
{ 0, 2 },
{ 3, 1 }
};
int dithering3x3[3][3] = {
{ 0, 6, 3 },
{ 7, 5, 1 },
{ 4, 2, 8 }
};
int dithering4x4[4][4] = {
{ 0, 8, 2, 10 },
{ 12, 4, 14, 6 },
{ 3, 11, 1, 9 },
{ 15, 7, 13, 5 }
};
int dithering8x8[8][8] = {
{ 0, 32, 8, 40, 2, 34, 10, 42 },
{ 48, 16, 56, 24, 50, 18, 58, 26 },
{ 12, 44, 4, 36, 14, 46, 6, 38 },
{ 60, 28, 52, 20, 62, 30, 54, 22 },
{ 3, 35, 11, 43, 1, 33, 9, 41 },
{ 51, 19, 59, 27, 49, 17, 57, 25 },
{ 15, 47, 7, 39, 13, 45, 5, 37 },
{ 63, 31, 55, 23, 61, 29, 53, 21 }
};
Image ordered_dithering(Image input, int matrix_size, int matrix[matrix_size][matrix_size])
{
Image out_image = alloc_image(input.width, input.height);
int dx = 0;
int dy = 0;
int r, g, b;
for (int y = 0; y < input.height; ++y)
{
for (int x = 0; x < input.width; ++x)
{
get_pixel(input, x, y, &r, &g, &b);
int d = 255.0 / (matrix_size * matrix_size);
int c = r <= (d * (matrix[dy][dx] + 1)) ? 0 : 255;
set_pixel(out_image, x, y, c, c, c);
dx = (dx + 1) % matrix_size;
}
dy = (dy + 1) % matrix_size;
dx = 0;
}
return out_image;
}
Image ordered_color_dithering(Image input, int matrix_size, int matrix[matrix_size][matrix_size])
{
Image out_image = alloc_image(input.width, input.height);
int dx = 0;
int dy = 0;
int r, g, b;
for (int y = 0; y < input.height; ++y)
{
for (int x = 0; x < input.width; ++x)
{
get_pixel(input, x, y, &r, &g, &b);
r = r < 255 / (matrix[dy][dx] + 1) ? 0 : 255;
g = g < 255 / (matrix[dy][dx] + 1) ? 0 : 255;
b = b < 255 / (matrix[dy][dx] + 1) ? 0 : 255;
set_pixel(out_image, x, y, r, g, b);
dx = (dx + 1) % matrix_size;
}
dy = (dy + 1) % matrix_size;
}
return out_image;
}
Image floyd_steinberg_color_dithering(Image input)
{
#define IX(x, y) ((x) + (y) * (input.width))
Image out_image = alloc_image(input.width, input.height);
float ratio1 = 7.0 / 16.0;
float ratio2 = 3.0 / 16.0;
float ratio3 = 5.0 / 16.0;
float ratio4 = 1.0 / 16.0;
float* valuesr = calloc(input.width * input.height, sizeof(float));
float* valuesg = calloc(input.width * input.height, sizeof(float));
float* valuesb = calloc(input.width * input.height, sizeof(float));
int r, g, b;
float errorr;
float errorg;
float errorb;
for (int y = 0; y < input.height; ++y)
{
for (int x = 0; x < input.width; ++x)
{
get_pixel(input, x, y, &r, &g, &b);
valuesr[IX(x, y)] += r;
valuesg[IX(x, y)] += g;
valuesb[IX(x, y)] += b;
if (valuesr[IX(x, y)] < 255.0 / 2.0)
{
errorr = valuesr[IX(x, y)];
valuesr[IX(x, y)] = 0.0;
}
else
{
errorr = valuesr[IX(x, y)] - 255.0;
valuesr[IX(x, y)] = 255.0;
}
if (valuesg[IX(x, y)] < 255.0 / 2.0)
{
errorg = valuesg[IX(x, y)];
valuesg[IX(x, y)] = 0.0;
}
else
{
errorg = valuesg[IX(x, y)] - 255.0;
valuesg[IX(x, y)] = 255.0;
}
if (valuesb[IX(x, y)] < 255.0 / 2.0)
{
errorb = valuesb[IX(x, y)];
valuesb[IX(x, y)] = 0.0;
}
else
{
errorb = valuesb[IX(x, y)] - 255.0;
valuesb[IX(x, y)] = 255.0;
}
if (x < input.width - 1)
{
valuesr[IX(x + 1, y)] += ratio1 * errorr;
valuesg[IX(x + 1, y)] += ratio1 * errorg;
valuesb[IX(x + 1, y)] += ratio1 * errorb;
}
if (x > 0 && y < input.height - 1)
{
valuesr[IX(x - 1, y + 1)] += ratio2 * errorr;
valuesg[IX(x - 1, y + 1)] += ratio2 * errorg;
valuesb[IX(x - 1, y + 1)] += ratio2 * errorb;
}
if (y < input.height - 1)
{
valuesr[IX(x, y + 1)] += ratio3 * errorr;
valuesg[IX(x, y + 1)] += ratio3 * errorg;
valuesb[IX(x, y + 1)] += ratio3 * errorb;
}
if (x < input.width - 1 && y < input.height - 1)
{
valuesr[IX(x + 1, y + 1)] += ratio4 * errorr;
valuesg[IX(x + 1, y + 1)] += ratio4 * errorg;
valuesb[IX(x + 1, y + 1)] += ratio4 * errorb;
}
set_pixel(out_image, x, y, valuesr[IX(x, y)], valuesg[IX(x, y)], valuesb[IX(x, y)]);
}
}
free(valuesr);
free(valuesg);
free(valuesb);
return out_image;
}
Image floyd_steinberg_dithering(Image input)
{
#define IX(x, y) ((x) + (y) * (input.width))
Image out_image = alloc_image(input.width, input.height);
float ratio1 = 7.0 / 16.0;
float ratio2 = 3.0 / 16.0;
float ratio3 = 5.0 / 16.0;
float ratio4 = 1.0 / 16.0;
float* values = calloc(input.width * input.height, sizeof(float));
int r, g, b;
float error;
for (int y = 0; y < input.height; ++y)
{
for (int x = 0; x < input.width; ++x)
{
get_pixel(input, x, y, &r, &g, &b);
values[IX(x, y)] += r;
if (values[IX(x, y)] < 255.0 / 2.0)
{
error = values[IX(x, y)];
values[IX(x, y)] = 0.0;
}
else
{
error = values[IX(x, y)] - 255.0;
values[IX(x, y)] = 255.0;
}
if (x < input.width - 1)
{
values[IX(x + 1, y)] += ratio1 * error;
}
if (x > 0 && y < input.height - 1)
{
values[IX(x - 1, y + 1)] += ratio2 * error;
}
if (y < input.height - 1)
{
values[IX(x, y + 1)] += ratio3 * error;
}
if (x < input.width - 1 && y < input.height - 1)
{
values[IX(x + 1, y + 1)] += ratio4 * error;
}
set_pixel(out_image, x, y, values[IX(x, y)], values[IX(x, y)], values[IX(x, y)]);
}
}
free(values);
return out_image;
}
Image calc_luminance(Image input)
{
Image out_image = alloc_image(input.width, input.height);
int r, g, b;
for (int x = 0; x < input.width; ++x)
{
for (int y = 0; y < input.height; ++y)
{
// int ix = x + (y * input.width);
get_pixel(input, x, y, &r, &g, &b);
float lum = to_luminance(r, g, b);
set_pixel(out_image, x, y, lum * 255.0, lum * 255.0, lum * 255.0);
}
}
return out_image;
}
void process(Image input, const char *input_file_path)
{
const char *output_file_path;
Image lum = calc_luminance(input);
output_file_path = add_infix(input_file_path, ".lum");
printf("Saving %s\n", output_file_path);
write_png_file(output_file_path, lum);
Image d2x2 = ordered_dithering(lum, 2, dithering2x2);
output_file_path = add_infix(input_file_path, ".2x2");
printf("Saving %s\n", output_file_path);
write_png_file(output_file_path, d2x2);
Image d3x3 = ordered_dithering(lum, 3, dithering3x3);
output_file_path = add_infix(input_file_path, ".3x3");
printf("Saving %s\n", output_file_path);
write_png_file(output_file_path, d3x3);
Image d4x4 = ordered_dithering(lum, 4, dithering4x4);
output_file_path = add_infix(input_file_path, ".4x4");
printf("Saving %s\n", output_file_path);
write_png_file(output_file_path, d4x4);
Image d8x8 = ordered_dithering(lum, 8, dithering8x8);
output_file_path = add_infix(input_file_path, ".8x8");
printf("Saving %s\n", output_file_path);
write_png_file(output_file_path, d8x8);
Image d2x2c = ordered_color_dithering(input, 2, dithering2x2);
output_file_path = add_infix(input_file_path, ".2x2c");
printf("Saving %s\n", output_file_path);
write_png_file(output_file_path, d2x2c);
Image fs = floyd_steinberg_dithering(lum);
output_file_path = add_infix(input_file_path, ".fs");
printf("Saving %s\n", output_file_path);
write_png_file(output_file_path, fs);
Image fsc = floyd_steinberg_color_dithering(input);
output_file_path = add_infix(input_file_path, ".fsc");
printf("Saving %s\n", output_file_path);
write_png_file(output_file_path, fsc);
free_image(fsc);
free_image(fs);
free_image(d2x2c);
free_image(d8x8);
free_image(d4x4);
free_image(d3x3);
free_image(d2x2);
free_image(lum);
}
int main(int argc, char **argv)
{
srand(time(NULL));
const char *program = args_shift(&argc, &argv);
if (argc <= 0)
{
fprintf(stderr, "Usage: %s <input.png>\n", program);
fprintf(stderr, "ERROR: no input file is provided\n");
return 1;
}
const char *input_file_path = args_shift(&argc, &argv);
if (!is_png(input_file_path))
{
fprintf(stderr, "ERROR: %s is not a PNG file\n", input_file_path);
return 1;
}
printf("Reading %s\n", input_file_path);
Image input_image = read_png_file(input_file_path);
printf("Dimensions: %d x %d\n", input_image.width, input_image.height);
if (input_image.bit_depth != 8)
{
fprintf(stderr, "Only 8 bit images are supported\n");
free_image(input_image);
abort();
}
process(input_image, input_file_path);
free_image(input_image);
return 0;
}