Debian Codegraph @

src/filter/filmgrain/filmgrain.c

raw symbols SCIP
1 #include <stdlib.h>
2 #include "frei0r.h"
3 #include "frei0r/math.h"
4
5
6 typedef struct flimgrain_instance
7 {
8 // image dimensions
9 int width;
10 int height;
11
12 // parameters
13 double grain_amt;
14 double grain_r;
15 double grain_g;
16 double grain_b;
17 double blur_amt;
18 double dust_amt;
19 double flicker_amt;
20
21 } filmgrain_instance_t;
22
23
24 // these functions are for the effect
25 static inline uint8_t random_range_uint8(uint8_t x)
26 {
27 // never divide by zero
28 if(x < 1)
29 {
30 return 0;
31 }
32 return rand() % x;
33 }
34
35 static inline uint32_t reduce_color_range(uint32_t color, uint8_t threshold, int flicker)
36 {
37 return CLAMP0255(CLAMP(color, threshold >> 1, 255 - threshold) + flicker);
38 }
39
40 #define DUST_RAND_LIMIT 1000000000
41 static inline int big_rand()
42 {
43 #if RAND_MAX > DUST_RAND_LIMIT
44 return rand() % DUST_RAND_LIMIT;
45 #else
46 return (rand() * (RAND_MAX + 1) + rand()) % DUST_RAND_LIMIT;
47 #endif
48 }
49
50
51 // these functions are for frei0r
52 // mostly copy/paste/slightly modified from the other frei0r effects
53 int f0r_init()
54 {
55 return 1;
56 }
57
58 void f0r_deinit()
59 {}
60
61 void f0r_get_plugin_info(f0r_plugin_info_t* info)
62 {
63 info->name = "Film Grain";
64 info->author = "esmane";
65 info->explanation = "Adds film-like grain and softens the picture.";
66 info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
67 info->color_model = F0R_COLOR_MODEL_RGBA8888;
68 info->frei0r_version = FREI0R_MAJOR_VERSION;
69 info->major_version = 0;
70 info->minor_version = 1;
71 info->num_params = 7;
72 }
73
74 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
75 {
76 switch(param_index)
77 {
78 case 0:
79 info->name = "Grain Amount";
80 info->explanation = "The intensity of the grain.";
81 info->type = F0R_PARAM_DOUBLE;
82 break;
83
84 case 1:
85 info->name = "Red Grain";
86 info->explanation = "The percentage of grain applied to the red channel.";
87 info->type = F0R_PARAM_DOUBLE;
88 break;
89
90 case 2:
91 info->name = "Green Grain";
92 info->explanation = "The percentage of grain applied to the green channel.";
93 info->type = F0R_PARAM_DOUBLE;
94 break;
95
96 case 3:
97 info->name = "Blue Grain";
98 info->explanation = "The percentage of grain applied to the blue channel.";
99 info->type = F0R_PARAM_DOUBLE;
100 break;
101
102 case 4:
103 info->name = "Blur Amount";
104 info->explanation = "The intensity of the blur.";
105 info->type = F0R_PARAM_DOUBLE;
106 break;
107
108 case 5:
109 info->name = "Dust Amount";
110 info->explanation = "The amount of dust particles on the image.";
111 info->type = F0R_PARAM_DOUBLE;
112 break;
113
114 case 6:
115 info->name = "Flicker";
116 info->explanation = "The amount of variation in brightness between frames.";
117 info->type = F0R_PARAM_DOUBLE;
118 break;
119 }
120 }
121
122 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
123 {
124 filmgrain_instance_t* inst = (filmgrain_instance_t*)calloc(1, sizeof(*inst));
125
126 inst->width = width;
127 inst->height = height;
128 inst->grain_amt = 0.5;
129 inst->grain_r = 0.75;
130 inst->grain_g = 1.0;
131 inst->grain_b = 0.5;
132 inst->blur_amt = 0.5;
133 inst->dust_amt = 0.2;
134 inst->flicker_amt = 0.5;
135
136 return (f0r_instance_t)inst;
137 }
138
139 void f0r_destruct(f0r_instance_t instance)
140 {
141 filmgrain_instance_t* inst = (filmgrain_instance_t*)instance;
142 free(instance);
143 }
144
145
146 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
147 {
148 filmgrain_instance_t* inst = (filmgrain_instance_t*)instance;
149 switch(param_index)
150 {
151 case 0:
152 inst->grain_amt = *((double*)param);
153 break;
154 case 1:
155 inst->grain_r = *((double*)param);
156 break;
157 case 2:
158 inst->grain_g = *((double*)param);
159 break;
160 case 3:
161 inst->grain_b = *((double*)param);
162 break;
163 case 4:
164 inst->blur_amt = *((double*)param);
165 break;
166 case 5:
167 inst->dust_amt = *((double*)param);
168 break;
169 case 6:
170 inst->flicker_amt = *((double*)param);
171 break;
172 }
173 }
174
175 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
176 {
177 filmgrain_instance_t* inst = (filmgrain_instance_t*)instance;
178 switch(param_index)
179 {
180 case 0:
181 *((double*)param) = inst->grain_amt;
182 break;
183 case 1:
184 *((double*)param) = inst->grain_r;
185 break;
186 case 2:
187 *((double*)param) = inst->grain_g;
188 break;
189 case 3:
190 *((double*)param) = inst->grain_b;
191 break;
192 case 4:
193 *((double*)param) = inst->blur_amt;
194 break;
195 case 5:
196 *((double*)param) = inst->dust_amt;
197 break;
198 case 6:
199 *((double*)param) = inst->flicker_amt;
200 break;
201 }
202 }
203
204
205 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
206 {
207 filmgrain_instance_t* inst = (filmgrain_instance_t*)instance;
208 uint32_t* buf = outframe;
209
210 uint32_t r;
211 uint32_t g;
212 uint32_t b;
213 uint8_t grain;
214 uint8_t reduce_t = random_range_uint8(inst->flicker_amt * 5) + inst->grain_amt * 40;
215 int flicker = random_range_uint8(inst->flicker_amt * 8);
216
217 if(rand() % 2)
218 {
219 flicker *= -1;
220 }
221
222 // first grain
223 if(inst->blur_amt != 0.0)
224 {
225 // only need a buf if blur is > 0
226 buf = (uint32_t*)calloc(inst->width * inst->height, sizeof(uint32_t));
227 }
228
229 for(unsigned int i = 0; i < inst->height * inst->width; i++)
230 {
231 // dust
232 if(big_rand() < inst->dust_amt * 1000)
233 {
234 if(rand() % 2 == 0)
235 {
236 r = 0;
237 g = 0;
238 b = 0;
239 }
240 else
241 {
242 r = 255;
243 g = 255;
244 b = 255;
245 }
246 }
247 else
248 {
249 // reducing the range of each color helps look more "filmish"
250 b = reduce_color_range((*(inframe + i) & 0x00FF0000) >> 16, reduce_t, flicker);
251 g = reduce_color_range((*(inframe + i) & 0x0000FF00) >> 8, reduce_t, flicker);
252 r = reduce_color_range( *(inframe + i) & 0x000000FF , reduce_t, flicker);
253
254 grain = random_range_uint8(inst->grain_amt * (40 + ((r + g + b) >> 5)));
255
256 b = CLAMP0255(b - (grain * inst->grain_b));
257 g = CLAMP0255(g - (grain * inst->grain_g));
258 r = CLAMP0255(r - (grain * inst->grain_r));
259 }
260
261 *(buf + i) = (*(buf + i) & 0xFFFFFF00) | r;
262 *(buf + i) = (*(buf + i) & 0xFFFF00FF) | (g << 8);
263 *(buf + i) = (*(buf + i) & 0xFF00FFFF) | (b << 16);
264
265 // alpha channel is preserved and no grain is applied to it
266 *(outframe + i) = (*(outframe + i) & 0x00FFFFFF) | (*(inframe + i) & 0xFF000000);
267 }
268
269 // then blur
270 if(inst->blur_amt != 0.0)
271 {
272 unsigned int pixel_count;
273 int blur_range;
274 for(int i = 0; i < inst->height * inst->width; i++)
275 {
276 pixel_count = 1;
277 b = ((*(buf + i) & 0x00FF0000) >> 16);
278 g = ((*(buf + i) & 0x0000FF00) >> 8);
279 r = ((*(buf + i) & 0x000000FF) );
280
281 blur_range = random_range_uint8(inst->blur_amt * 4);
282 for(int xx = -blur_range - 1; xx < blur_range; xx++)
283 {
284 for(int yy = -blur_range - 1; yy < blur_range; yy++)
285 {
286 if((i + xx + (yy * inst->width)) > 0 && (i + xx + (yy * inst->width)) < (inst->width * inst->height) - 1)
287 {
288 b += (*(buf + i + xx + (yy * inst->width)) & 0x00FF0000) >> 16;
289 g += (*(buf + i + xx + (yy * inst->width)) & 0x0000FF00) >> 8;
290 r += (*(buf + i + xx + (yy * inst->width)) & 0x000000FF);
291 pixel_count++;
292 }
293 }
294 }
295
296 b = b / pixel_count;
297 g = g / pixel_count;
298 r = r / pixel_count;
299
300 *(outframe + i) = (*(outframe + i) & 0xFFFFFF00) | r;
301 *(outframe + i) = (*(outframe + i) & 0xFFFF00FF) | (g << 8);
302 *(outframe + i) = (*(outframe + i) & 0xFF00FFFF) | (b << 16);
303 }
304 free(buf);
305 }
306 }