-
Notifications
You must be signed in to change notification settings - Fork 0
/
rot_dither.c
425 lines (326 loc) · 9.69 KB
/
rot_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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "cwalk/include/cwalk.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
int8_t matrix[] =
{
0, 8, 2, 10,
12, 4, 14, 6,
3, 11, 1, 9,
15, 7, 13, 5
};
int32_t matrix_size[] = { 4, 4 };
int8_t matrix_max_value = 15;
typedef struct _pixel {
short r;
short g;
short b;
} pixel;
typedef struct _palette {
unsigned char colors[256][3];
unsigned char size;
} palette;
typedef unsigned char rgb_color[3];
typedef struct _rectangle {
int8_t *matrix;
int32_t size[2];
} rectangle;
typedef struct _histo_cell {
int32_t start, end, size;
} histo_cell;
int32_t repeat_matrix_size[] = { 8000, 8000 };
//int32_t wrapping_matrix_size[] = { 8000, 8000 };
double _A = 4.0;
double _B = 3.0;
double _C = 5.0;
int get_position(int32_t x, int32_t y, int32_t stride)
{
return y * stride + x;
}
rectangle create_matrix(int8_t *base_matrix, int32_t xsize, int32_t ysize)
{
int8_t *ret_matrix = calloc(xsize * ysize, sizeof(uint8_t));
int32_t index = 0;
for (int32_t y = 0; y < ysize; y++) {
for (int32_t x = 0; x < xsize; x++) {
int8_t val = base_matrix[index + (x % matrix_size[0])];
ret_matrix[get_position(x, y, xsize)] = val;
}
index += matrix_size[0];
if (index >= (matrix_size[1] * matrix_size[0])) {
index = 0;
}
}
rectangle rect;
rect.matrix = ret_matrix;
rect.size[0] = xsize;
rect.size[1] = ysize;
return rect;
}
//int compare_rectangle(int8_t *matrix, int8_t *matrix_size, int32_t x, int32_t y, int32_t x2,
// int32_t y2, int32_t size_x, int32_t size_y)
//{
// for (int yy = 0; yy < size_y; yy++) {
// for (int xx = 0; xx < size_x; xx++) {
//
// if (matrix[get_position(x + xx, y + yy, matrix_size[0])] == -1) return 0;
// if ((x + xx) > matrix_size[0] || (x2 + xx) > matrix_size[0]) return 0;
// if ((y + yy) > matrix_size[1] || (y2 + yy) > matrix_size[1]) return 0;
//
// if (matrix[get_position(x + xx, y + yy, matrix_size[0])]
// != matrix[get_position(x2 + xx, y2 + yy, matrix_size[0])]) {
// return 0;
// }
// }
// }
// return 1;
//}
rectangle center_matrix(rectangle rect, int32_t size_x, int32_t size_y)
{
int32_t center_x = rect.size[0] / 2;
int32_t center_y = rect.size[1] / 2;
int8_t *new_matrix = calloc(size_x * size_y, sizeof(uint8_t));
memset(new_matrix, -1, size_x * size_y);
int32_t x_start = size_x / 2 - center_x;
int32_t y_start = size_y / 2 - center_y;
int32_t xc = 0;
int32_t yc = 0;
for (int32_t y = y_start; y < y_start + rect.size[1]; y++) {
for (int32_t x = x_start; x < x_start + rect.size[0]; x++) {
// flip matrix vertically (0,0 is bottom left for discrete rotation)
new_matrix[get_position(x, y, size_y)] = rect.matrix[get_position(xc, rect.size[1] - 1 - yc, rect.size[0])];
xc++;
}
yc++;
xc = 0;
}
rectangle ret;
ret.matrix = new_matrix;
ret.size[0] = size_x;
ret.size[1] = size_y;
return ret;
}
rectangle rotate_matrix(rectangle rect, float a, float b, float c)
{
int32_t around_x = rect.size[0] / 2;
int32_t around_y = rect.size[1] / 2;
int8_t *new_matrix = calloc(rect.size[0] * rect.size[1], sizeof(uint8_t));
memset(new_matrix, -1, rect.size[0] * rect.size[1]);
for (int32_t y = 0; y < rect.size[1]; y++) {
for (int32_t x = 0; x < rect.size[0]; x++) {
if (rect.matrix[get_position(x, y, rect.size[0])] >= 0) {
int32_t new_x = (int) round((a / c) * (x - around_x) - (b / c) * (y - around_y) + around_x);
int32_t new_y = rect.size[1] - (int) round((b / c) * (x - around_x) + (a / c) * (y - around_y) + around_y);
if (new_x < rect.size[0] && new_y < rect.size[1] && new_x >= 0 && new_y >= 0) {
new_matrix[get_position(new_x, new_y, rect.size[0])] = rect.matrix[get_position(x, y, rect.size[0])];
}
}
}
}
rectangle ret_rect;
ret_rect.matrix = new_matrix;
ret_rect.size[0] = rect.size[0];
ret_rect.size[1] = rect.size[1];
return ret_rect;
}
void display_matrix(int8_t *m, int32_t xsize, int32_t ysize)
{
printf("\n");
for (int32_t y = 0; y < ysize; y++) {
for (int32_t x = 0; x < xsize; x++) {
if (m[get_position(x, y, xsize)] >= 0) {
printf("%2d ", m[get_position(x, y, xsize)]);
} else {
printf(" . ");
}
}
printf("\n");
}
}
rectangle find_max_rectangle_in_matrix(rectangle rect)
{
// find upper and bottom line
int bottom = 0;
int top = -1;
for (int y = 0; y < rect.size[1] ; y++) {
int val_bottom = 0;
int full_line = 0;
for (int x = 0; x < rect.size[0]; x++) {
if (rect.matrix[get_position(x, y, rect.size[0])] >= 0) {
full_line++;
}
}
if (full_line == rect.size[0] && top == -1) {
top = y;
}
if (full_line == rect.size[0] && y > bottom) {
bottom = y;
}
}
uint32_t size = bottom - top;
int8_t *rect_matrix = calloc(size * rect.size[0], sizeof(int8_t));
int line = 0;
for (int y = top; y < bottom; y++) {
for (int x = 0; x < rect.size[0]; x++) {
rect_matrix[get_position(x, line, rect.size[0])] = rect.matrix[get_position(x, y, rect.size[0])];
}
line++;
}
rectangle r;
r.matrix = rect_matrix;
r.size[0] = rect.size[0];
r.size[1] = size;
return r;
}
void convert_pixel(pixel *source, pixel *target)
{
target->r = max(min(source->r, 255), 0);
target->g = max(min(source->g, 255), 0);
target->b = max(min(source->b, 255), 0);
}
float color_delta_f_ccir601(unsigned char c1[3], unsigned char c2[3])
{
int dr = abs((c1[0]) - (c2[0]));
int dg = abs((c1[1]) - (c2[1]));
int db = abs((c1[2]) - (c2[2]));
return sqrt(30 * (dr * dr) + 59 * (dg * dg) + 11 * (db * db));
}
float color_delta_f(unsigned char c1[3], unsigned char c2[3])
{
int dr = abs((c1[0]) - (c2[0]));
int dg = abs((c1[1]) - (c2[1]));
int db = abs((c1[2]) - (c2[2]));
return sqrt((dr * dr) + (dg * dg) + (db * db));
}
void find_closest_color_rgb(pixel *pixel, palette *palette, rgb_color *color)
{
float d_plt;
unsigned char c_current[3];
float diff = FLT_MAX;
unsigned char plt = 0;
c_current[0] = pixel->r;
c_current[1] = pixel->g;
c_current[2] = pixel->b;
for (int i = 0; i < palette->size; i++) {
d_plt = color_delta_f_ccir601(palette->colors[i], c_current);
if (d_plt < diff) {
plt = i;
diff = d_plt;
}
}
(*color)[0] = palette->colors[plt][0];
(*color)[1] = palette->colors[plt][1];
(*color)[2] = palette->colors[plt][2];
}
short linear_space(int8_t x)
{
float y;
float fx = (float) x / 255.0;
if (fx <= 0.04045) {
y = fx / 12.92;
} else {
y = pow(((fx + 0.055) / 1.055), 2.4);
}
return (short) round(y * 255.0);
}
unsigned char *ordered_dither(unsigned char *source, int width, int height, int channels,
palette *palette, int8_t *matrix, int32_t *matrix_size)
{
pixel d, d2;
short index_color;
float map_value;
uint8_t value;
unsigned char *target = calloc(width * height * channels, sizeof(char));
printf("buffer size: %d\n", width * height * channels);
if (target == NULL) {
fprintf(stderr, "ordered_dither - not enough memory to create image");
return NULL;
}
pixel p;
rgb_color c;
int ptr_x = 0;
for (short y = 0; y < height; y++) {
ptr_x = 0;
for (short x = 0; x < width * 3; x += channels) {
p.r = (short)source[y * width * 3 + x];
p.g = (short)source[y * width * 3 + x + 1];
p.b = (short)source[y * width * 3 + x + 2];
map_value = matrix[matrix_size[0] * y + ptr_x];
value = (uint8_t) round(map_value * 255 / matrix_max_value);
d2.r = p.r + value - 127;
d2.g = p.g + value - 127;
d2.b = p.b + value - 127;
// TODO test linear space
// d2.r = linear_space(p.r) + value - 127;
// d2.g = linear_space(p.g) + value - 127;
// d2.b = linear_space(p.b) + value - 127;
convert_pixel(&d2, &d);
find_closest_color_rgb(&d, palette, &c);
target[y * width * 3 + x] = c[0];
target[y * width * 3 + x + 1] = c[1];
target[y * width * 3 + x + 2] = c[2];
ptr_x++;
}
}
return target;
}
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("Image filename as argument...\n");
return 0;
}
printf("Generating a big matrix from \n");
display_matrix(matrix, 4, 4);
printf("\n");
rectangle repeat_matrix = create_matrix(matrix, repeat_matrix_size[0], repeat_matrix_size[1]);
rectangle rotated_matrix = rotate_matrix(repeat_matrix, _A, _B, _C);
rectangle the_big_matrix = find_max_rectangle_in_matrix(rotated_matrix);
printf("Loading image\n");
int width, height, channels;
unsigned char *img = stbi_load(/*"images/lenna_.jpg"*/ argv[1], &width, &height, &channels, 0);
if (img == NULL) {
printf("Error in loading the image\n");
exit(1);
}
printf("Loaded image with a width of %dpx, a height of %dpx and %d channels\n", width, height, channels);
if (width > the_big_matrix.size[0] || height > the_big_matrix.size[1]) {
printf("Picture size is too high - use a %dx%d max picture size\n", the_big_matrix.size[0], the_big_matrix.size[1]);
exit(1);
}
palette p;
p.colors[0][0] = 0;
p.colors[0][1] = 0;
p.colors[0][2] = 0;
p.colors[1][0] = 255;
p.colors[1][1] = 255;
p.colors[1][2] = 255;
p.size = 2;
printf("Dithering image\n");
unsigned char *result = ordered_dither(img, width, height, channels, &p, the_big_matrix.matrix, the_big_matrix.size);
char new_name[1024] = {0};
size_t length;
const char *base;
cwk_path_get_basename(argv[1], &base, &length);
const char *path = argv[1];
cwk_path_get_dirname(path, &length);
strncpy(new_name, path, length);
strcat(new_name, "_");
strcat(new_name, base);
printf("Saving image as %s\n", new_name);
stbi_write_png(new_name, width, height, channels, result, width * channels);
free(repeat_matrix.matrix);
free(rotated_matrix.matrix);
free(the_big_matrix.matrix);
free(result);
return 0;
}