Debian Codegraph @

src/filter/rgbnoise/rgbnoise.c

raw symbols SCIP
1 /*
2 * This file is a modified port of RGB Noise plug-in from Gimp.
3 * It contains code from plug-ins/common/noise-rgb.c, see that for copyrights.
4 *
5 * rgbnoise.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
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <math.h>
29 #include "frei0r.h"
30 #include "frei0r/math.h"
31
32 #define MY_MAX_RAND 32767 // assume RAND_MAX to be at least this big.
33
34 typedef struct rgbnoise_instance
35 {
36 unsigned int width;
37 unsigned int height;
38 double noise;
39 double gaussian_lookup[MY_MAX_RAND];
40 int table_inited;
41 int next_gaussian_index;
42 int last_in_range;
43 } rgbnoise_instance_t;
44
45
46
47 void f0r_deinit()
48 {}
49
50 void f0r_get_plugin_info(f0r_plugin_info_t* rgbnoiseInfo)
51 {
52 rgbnoiseInfo->name = "rgbnoise";
53 rgbnoiseInfo->author = "Janne Liljeblad";
54 rgbnoiseInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER;
55 rgbnoiseInfo->color_model = F0R_COLOR_MODEL_RGBA8888;
56 rgbnoiseInfo->frei0r_version = FREI0R_MAJOR_VERSION;
57 rgbnoiseInfo->major_version = 0;
58 rgbnoiseInfo->minor_version = 9;
59 rgbnoiseInfo->num_params = 1;
60 rgbnoiseInfo->explanation = "Adds RGB noise to image.";
61 }
62
63 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
64 {
65 switch ( param_index ) {
66 case 0:
67 info->name = "noise";
68 info->type = F0R_PARAM_DOUBLE;
69 info->explanation = "Amount of noise added";
70 break;
71 }
72 }
73
74 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
75 {
76 rgbnoise_instance_t* inst = (rgbnoise_instance_t*)calloc(1, sizeof(*inst));
77 inst->width = width;
78 inst->height = height;
79 inst->noise = 0.2;
80 inst->table_inited = 0;
81 inst->next_gaussian_index = 0;
82 inst->last_in_range = MY_MAX_RAND;
83 return (f0r_instance_t)inst;
84 }
85
86 void f0r_destruct(f0r_instance_t instance)
87 {
88 free(instance);
89 }
90
91 void f0r_set_param_value(f0r_instance_t instance,
92 f0r_param_t param, int param_index)
93 {
94 rgbnoise_instance_t* inst = (rgbnoise_instance_t*)instance;
95 switch (param_index)
96 {
97 case 0:
98 inst->noise = *((double*)param);
99 break;
100 }
101 }
102
103 void f0r_get_param_value(f0r_instance_t instance,
104 f0r_param_t param, int param_index)
105 {
106 rgbnoise_instance_t* inst = (rgbnoise_instance_t*)instance;
107 switch (param_index)
108 {
109 case 0:
110 *((double*)param) = inst->noise;
111 break;
112 }
113 }
114
115 //-------------------------------------------------------- filter methods
116 static inline double nextDouble()
117 {
118 double val = ((double) rand()) / ((double) RAND_MAX);
119 return val;
120 }
121
122 static inline double gauss()
123 {
124 double u, v, x;
125 do
126 {
127 v = nextDouble();
128
129 do u = nextDouble();
130 while (u == 0);
131
132 x = 1.71552776992141359295 * (v - 0.5) / u;
133 }
134 while ( x * x > -4.0 * log(u) );
135
136 return x;
137 }
138
139 static void create_new_lookup_range(rgbnoise_instance_t* inst)
140 {
141 int first, last, tmp;
142 first = rand() % (MY_MAX_RAND - 1);
143 last = rand() % (MY_MAX_RAND - 1);
144 if (first > last)
145 {
146 tmp = last;
147 last = first;
148 first = tmp;
149 }
150 inst->next_gaussian_index = first;
151 inst->last_in_range = last;
152 }
153
154 static inline double next_gauss(rgbnoise_instance_t* inst)
155 {
156 inst->next_gaussian_index++;
157 if (inst->next_gaussian_index >= inst->last_in_range)
158 {
159 create_new_lookup_range(inst);
160 }
161 return inst->gaussian_lookup[inst->next_gaussian_index];
162 }
163
164 static inline int addNoise(rgbnoise_instance_t* inst, int sample, double noise)
165 {
166 int byteNoise = 0;
167 int noiseSample = 0;
168
169 byteNoise = (int) (noise * next_gauss(inst));
170 noiseSample = sample + byteNoise;
171 noiseSample = CLAMP(noiseSample, 0, 255);
172 return noiseSample;
173 }
174
175 int f0r_init()
176 {
177 return 1;
178 }
179
180 void rgb_noise(f0r_instance_t instance, double time,
181 const uint32_t* inframe, uint32_t* outframe)
182 {
183 rgbnoise_instance_t* inst = (rgbnoise_instance_t*)instance;
184 unsigned int len = inst->width * inst->height;
185
186 // Initialize the gaussian lookup table if not already done
187 if (inst->table_inited == 0)
188 {
189 int i;
190 for( i = 0; i < MY_MAX_RAND; i++)
191 {
192 inst->gaussian_lookup[i] = gauss() * 127.0;
193 }
194 inst->table_inited = 1;
195 inst->next_gaussian_index = 0;
196 inst->last_in_range = MY_MAX_RAND;
197 }
198
199 unsigned char* dst = (unsigned char*)outframe;
200 const unsigned char* src = (unsigned char*)inframe;
201
202 int sample;
203 double noise = inst->noise;
204 while (len--)
205 {
206 sample = *src++;
207 *dst++ = addNoise(inst, sample, noise);
208 sample = *src++;
209 *dst++ = addNoise(inst, sample, noise);
210 sample = *src++;
211 *dst++ = addNoise(inst, sample, noise);
212 *dst++ = *src++;
213 }
214 }
215
216 //---------------------------------------------------- update
217 void f0r_update(f0r_instance_t instance, double time,
218 const uint32_t* inframe, uint32_t* outframe)
219 {
220 assert(instance);
221 rgb_noise(instance, time, inframe, outframe);
222 }