Debian Codegraph @

src/filter/dither/dither.c

raw symbols SCIP
1 /*
2 * This file is a port of com.jhlabs.image.DitherFilter.java
3 * Copyright 2006 Jerry Huxtable
4 *
5 * dither.c
6 * Copyright 2012 Janne Liljeblad
7 *
8 * This file is a Frei0r plugin.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include <stdlib.h>
26 #include <assert.h>
27 #include <math.h>
28
29 #include "frei0r.h"
30 #include "frei0r/math.h"
31
32 int ditherMagic2x2Matrix[] = {
33 0, 2,
34 3, 1
35 };
36
37 int ditherMagic4x4Matrix[] = {
38 0, 14, 3, 13,
39 11, 5, 8, 6,
40 12, 2, 15, 1,
41 7, 9, 4, 10
42 };
43
44 int ditherOrdered4x4Matrix[] = {
45 0, 8, 2, 10,
46 12, 4, 14, 6,
47 3, 11, 1, 9,
48 15, 7, 13, 5
49 };
50
51 int ditherLines4x4Matrix[] = {
52 0, 1, 2, 3,
53 4, 5, 6, 7,
54 8, 9, 10, 11,
55 12, 13, 14, 15
56 };
57
58 int dither90Halftone6x6Matrix[] = {
59 29, 18, 12, 19, 30, 34,
60 17, 7, 4, 8, 20, 28,
61 11, 3, 0, 1, 9, 27,
62 16, 6, 2, 5, 13, 26,
63 25, 15, 10, 14, 21, 31,
64 33, 25, 24, 23, 33, 36
65 };
66
67 int ditherOrdered6x6Matrix[] = {
68 1, 59, 15, 55, 2, 56, 12, 52,
69 33, 17, 47, 31, 34, 18, 44, 28,
70 9, 49, 5, 63, 10, 50, 6, 60,
71 41, 25, 37, 21, 42, 26, 38, 22,
72 3, 57, 13, 53, 0, 58, 14, 54,
73 35, 19, 45, 29, 32, 16, 46, 30,
74 11, 51, 7, 61, 8, 48, 4, 62,
75 43, 27, 39, 23, 40, 24, 36, 20
76 };
77
78 int ditherOrdered8x8Matrix[] = {
79 1,235, 59,219, 15,231, 55,215, 2,232, 56,216, 12,228, 52,212,
80 129, 65,187,123,143, 79,183,119,130, 66,184,120,140, 76,180,116,
81 33,193, 17,251, 47,207, 31,247, 34,194, 18,248, 44,204, 28,244,
82 161, 97,145, 81,175,111,159, 95,162, 98,146, 82,172,108,156, 92,
83 9,225, 49,209, 5,239, 63,223, 10,226, 50,210, 6,236, 60,220,
84 137, 73,177,113,133, 69,191,127,138, 74,178,114,134, 70,188,124,
85 41,201, 25,241, 37,197, 21,255, 42,202, 26,242, 38,198, 22,252,
86 169,105,153, 89,165,101,149, 85,170,106,154, 90,166,102,150, 86,
87 3,233, 57,217, 13,229, 53,213, 0,234, 58,218, 14,230, 54,214,
88 131, 67,185,121,141, 77,181,117,128, 64,186,122,142, 78,182,118,
89 35,195, 19,249, 45,205, 29,245, 32,192, 16,250, 46,206, 30,246,
90 163, 99,147, 83,173,109,157, 93,160, 96,144, 80,174,110,158, 94,
91 11,227, 51,211, 7,237, 61,221, 8,224, 48,208, 4,238, 62,222,
92 139, 75,179,115,135, 71,189,125,136, 72,176,112,132, 68,190,126,
93 43,203, 27,243, 39,199, 23,253, 40,200, 24,240, 36,196, 20,254,
94 171,107,155, 91,167,103,151, 87,168,104,152, 88,164,100,148, 84 };
95
96 int ditherCluster3Matrix[] = {
97 9,11,10, 8, 6, 7,
98 12,17,16, 5, 0, 1,
99 13,14,15, 4, 3, 2,
100 8, 6, 7, 9,11,10,
101 5, 0, 1,12,17,16,
102 4, 3, 2,13,14,15 };
103
104 int ditherCluster4Matrix[] = {
105 18,20,19,16,13,11,12,15,
106 27,28,29,22, 4, 3, 2, 9,
107 26,31,30,21, 5, 0, 1,10,
108 23,25,24,17, 8, 6, 7,14,
109 13,11,12,15,18,20,19,16,
110 4, 3, 2, 9,27,28,29,22,
111 5, 0, 1,10,26,31,30,21,
112 8, 6, 7,14,23,25,24,17 };
113
114 int ditherCluster8Matrix[] = {
115 64, 69, 77, 87, 86, 76, 68, 67, 63, 58, 50, 40, 41, 51, 59, 60,
116 70, 94,100,109,108, 99, 93, 75, 57, 33, 27, 18, 19, 28, 34, 52,
117 78,101,114,116,115,112, 98, 83, 49, 26, 13, 11, 12, 15, 29, 44,
118 88,110,123,124,125,118,107, 85, 39, 17, 4, 3, 2, 9, 20, 42,
119 89,111,122,127,126,117,106, 84, 38, 16, 5, 0, 1, 10, 21, 43,
120 79,102,119,121,120,113, 97, 82, 48, 25, 8, 6, 7, 14, 30, 45,
121 71, 95,103,104,105, 96, 92, 74, 56, 32, 24, 23, 22, 31, 35, 53,
122 65, 72, 80, 90, 91, 81, 73, 66, 62, 55, 47, 37, 36, 46, 54, 61,
123 63, 58, 50, 40, 41, 51, 59, 60, 64, 69, 77, 87, 86, 76, 68, 67,
124 57, 33, 27, 18, 19, 28, 34, 52, 70, 94,100,109,108, 99, 93, 75,
125 49, 26, 13, 11, 12, 15, 29, 44, 78,101,114,116,115,112, 98, 83,
126 39, 17, 4, 3, 2, 9, 20, 42, 88,110,123,124,125,118,107, 85,
127 38, 16, 5, 0, 1, 10, 21, 43, 89,111,122,127,126,117,106, 84,
128 48, 25, 8, 6, 7, 14, 30, 45, 79,102,119,121,120,113, 97, 82,
129 56, 32, 24, 23, 22, 31, 35, 53, 71, 95,103,104,105, 96, 92, 74,
130 62, 55, 47, 37, 36, 46, 54, 61, 65, 72, 80, 90, 91, 81, 73, 66 };
131
132 int matrixSizes[] = {4, 16, 16, 16, 36, 64, 256, 36, 64, 256};
133 int* matrixes[] = {ditherMagic2x2Matrix, ditherMagic4x4Matrix, ditherOrdered4x4Matrix,
134 ditherLines4x4Matrix, dither90Halftone6x6Matrix, ditherOrdered6x6Matrix,
135 ditherOrdered8x8Matrix, ditherCluster3Matrix, ditherCluster4Matrix, ditherCluster8Matrix};
136
137 typedef struct dither_instance
138 {
139 unsigned int width;
140 unsigned int height;
141 double levels;
142 double matrixid;
143 } dither_instance_t;
144
145 int f0r_init()
146 {
147 return 1;
148 }
149
150 void f0r_deinit()
151 { /* no initialization required */ }
152
153 void f0r_get_plugin_info(f0r_plugin_info_t* dither_info)
154 {
155 dither_info->name = "dither";
156 dither_info->author = "Janne Liljeblad";
157 dither_info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
158 dither_info->color_model = F0R_COLOR_MODEL_RGBA8888;
159 dither_info->frei0r_version = FREI0R_MAJOR_VERSION;
160 dither_info->major_version = 0;
161 dither_info->minor_version = 1;
162 dither_info->num_params = 2;
163 dither_info->explanation = "Dithers the image and reduces the number of available colors";
164 }
165
166 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
167 {
168 switch(param_index)
169 {
170 case 0:
171 info->name = "levels";
172 info->type = F0R_PARAM_DOUBLE;
173 info->explanation = "Number of values per channel";
174 break;
175 case 1:
176 info->name = "matrixid";
177 info->type = F0R_PARAM_DOUBLE;
178 info->explanation = "Id of matrix used for dithering";
179 break;
180 }
181 }
182
183 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
184 {
185 dither_instance_t* inst = (dither_instance_t*)calloc(1, sizeof(*inst));
186 inst->width = width;
187 inst->height = height;
188 inst->levels = 5.0 / 48.0;// input range 0.0 - 1.0 will be interpreted as levels range 2 - 50
189 inst->matrixid = 1.0; // input range 0.0 - 1.0 will be interpreted as matrixid 0 - 9
190 // e.g. values 0.0, 0.12, 0.23, 0.34, 0.45, 0.56, 0.67, 0.78, 0.89, 1.0
191 // will select matrixes 0 to 9
192 return (f0r_instance_t)inst;
193 }
194
195 void f0r_destruct(f0r_instance_t instance)
196 {
197 free(instance);
198 }
199
200 void f0r_set_param_value(f0r_instance_t instance,
201 f0r_param_t param, int param_index)
202 {
203 assert(instance);
204 dither_instance_t* inst = (dither_instance_t*)instance;
205
206 switch(param_index)
207 {
208 case 0:
209 inst->levels = *((double*)param);
210 break;
211 case 1:
212 inst->matrixid = *((double*)param);
213 break;
214 }
215 }
216
217 void f0r_get_param_value(f0r_instance_t instance,
218 f0r_param_t param, int param_index)
219 {
220 assert(instance);
221 dither_instance_t* inst = (dither_instance_t*)instance;
222
223 switch(param_index)
224 {
225 case 0:
226 *((double*)param) = inst->levels;
227 break;
228 case 1:
229 *((double*)param) = inst->matrixid;
230 break;
231 }
232 }
233
234 void f0r_update(f0r_instance_t instance, double time,
235 const uint32_t* inframe, uint32_t* outframe)
236 {
237 //init and get params
238 assert(instance);
239 dither_instance_t* inst = (dither_instance_t*)instance;
240 unsigned char* dst = (unsigned char*)outframe;
241 const unsigned char* src = (unsigned char*)inframe;
242
243 double levelsInput = inst->levels * 48.0;
244 levelsInput = CLAMP(levelsInput, 0.0, 48.0) + 2.0;
245 int levels = (int)levelsInput;
246
247 double matrixIdInput = inst->matrixid * 9.0;
248 matrixIdInput = CLAMP(matrixIdInput, 0.0, 9.0);
249 int matrixid = (int)matrixIdInput;
250 int* matrix = matrixes[matrixid];
251 int matrixLength = matrixSizes[matrixid];
252
253 // init look-ups
254 int rows, cols;
255 rows = cols = (int)sqrt(matrixLength);
256 int *map = (int *) malloc(levels * sizeof(int));
257 int i,v;
258 for (i = 0; i < levels; i++)
259 {
260 v = 255 * i / (levels-1);
261 map[i] = v;
262 }
263
264 int div[256];
265 int mod[256];
266 int rc = (rows * cols + 1);
267 for (i = 0; i < 256; i++)
268 {
269 div[i] = (levels-1) * i / 256;
270 mod[i] = i * rc /256;
271 }
272
273 // filter image
274 unsigned int width = inst->width;
275 unsigned int height = inst->height;
276 unsigned int x,y,col,row;
277 unsigned char r, g, b;
278 for (y = 0; y < height; ++y)
279 {
280 for (x=0; x < width; ++x)
281 {
282 r = *src++;
283 g = *src++;
284 b = *src++;
285
286 col = x % cols;
287 row = y % rows;
288
289 v = matrix[ row * cols + col];
290 r = map[mod[r] > v ? div[r] + 1 : div[r]];
291 g = map[mod[g] > v ? div[g] + 1 : div[g]];
292 b = map[mod[b] > v ? div[b] + 1 : div[b]];
293
294 *dst++ = r;
295 *dst++ = g;
296 *dst++ = b;
297 *dst++ = *src++;//copy alpha
298 }
299 }
300
301 free(map);
302 }
303
304