Debian Codegraph @

src/filter/sharpness/sharpness.c

raw symbols SCIP
1 /*
2 sharpness.c
3
4 This frei0r plugin is a port of Mplayer's unsharp mask filter
5 original by by Remi Guyomarch <[email protected]>
6
7 Version 0.1 jul 2010
8
9 Copyright (C) 2010 Marko Cebokli http://lea.hamradio.si/~s57uuu
10
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 */
27
28
29 //compile: gcc -c -fPIC -Wall sharpness.c -o sharpness.o
30 //link: gcc -shared -o sharpness.so sharpness.o
31
32 //#include <stdio.h>
33 #include <frei0r.h>
34 #include <stdlib.h>
35 //#include <math.h>
36 #include <assert.h>
37 #include <string.h>
38
39 #define MIN_MATRIX_SIZE 3
40 #define MAX_MATRIX_SIZE 63
41
42 typedef struct FilterParam {
43 int msizeX, msizeY;
44 double amount;
45 uint32_t *SC[MAX_MATRIX_SIZE-1];
46 } FilterParam;
47
48
49 //----------------------------------------
50 //struktura za instanco efekta
51 typedef struct
52 {
53 int h;
54 int w;
55
56 FilterParam fp;
57 int size,ac;
58 unsigned char *Rplani,*Gplani,*Bplani,*Rplano,*Gplano,*Bplano;
59
60 } inst;
61
62
63 //========================================================
64 //unsharp() function from Mplayer unsharp filter
65 //by Remi Guyomarch <[email protected]>
66
67 /* This code is based on :
68
69 An Efficient algorithm for Gaussian blur using finite-state machines
70 Frederick M. Waltz and John W. V. Miller
71
72 SPIE Conf. on Machine Vision Systems for Inspection and Metrology VII
73 Originally published Boston, Nov 98
74
75 */
76
77 void unsharp( uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height, FilterParam *fp ) {
78
79 uint32_t **SC = fp->SC;
80 uint32_t SR[MAX_MATRIX_SIZE-1], Tmp1, Tmp2;
81 uint8_t* src2 = src; // avoid gcc warning
82
83 int32_t res;
84 int x, y, z;
85 int amount = fp->amount * 65536.0;
86 int stepsX = fp->msizeX/2;
87 int stepsY = fp->msizeY/2;
88 int scalebits = (stepsX+stepsY)*2;
89 int32_t halfscale = 1 << ((stepsX+stepsY)*2-1);
90
91 if( !fp->amount ) {
92 if( src == dst )
93 return;
94 if( dstStride == srcStride )
95 // fast_memcpy( dst, src, srcStride*height );
96 memcpy( dst, src, srcStride*height );
97 else
98 for( y=0; y<height; y++, dst+=dstStride, src+=srcStride )
99 // fast_memcpy( dst, src, width );
100 memcpy( dst, src, width );
101 return;
102 }
103
104 for( y=0; y<2*stepsY; y++ )
105 memset( SC[y], 0, sizeof(SC[y][0]) * (width+2*stepsX) );
106
107 for( y=-stepsY; y<height+stepsY; y++ ) {
108 if( y < height ) src2 = src;
109 memset( SR, 0, sizeof(SR[0]) * (2*stepsX-1) );
110 for( x=-stepsX; x<width+stepsX; x++ ) {
111 Tmp1 = x<=0 ? src2[0] : x>=width ? src2[width-1] : src2[x];
112 for( z=0; z<stepsX*2; z+=2 ) {
113 Tmp2 = SR[z+0] + Tmp1; SR[z+0] = Tmp1;
114 Tmp1 = SR[z+1] + Tmp2; SR[z+1] = Tmp2;
115 }
116 for( z=0; z<stepsY*2; z+=2 ) {
117 Tmp2 = SC[z+0][x+stepsX] + Tmp1; SC[z+0][x+stepsX] = Tmp1;
118 Tmp1 = SC[z+1][x+stepsX] + Tmp2; SC[z+1][x+stepsX] = Tmp2;
119 }
120 if( x>=stepsX && y>=stepsY ) {
121 uint8_t* srx = src - stepsY*srcStride + x - stepsX;
122 uint8_t* dsx = dst - stepsY*dstStride + x - stepsX;
123
124 res = (int32_t)*srx + ( ( ( (int32_t)*srx - (int32_t)((Tmp1+halfscale) >> scalebits) ) * amount ) >> 16 );
125 *dsx = res>255 ? 255 : res<0 ? 0 : (uint8_t)res;
126 }
127 }
128 if( y >= 0 ) {
129 dst += dstStride;
130 src += srcStride;
131 }
132 }
133 }
134
135
136
137 //-----------------------------------------------------
138 //stretch [0...1] to parameter range [min...max] linear
139 float map_value_forward(double v, float min, float max)
140 {
141 return min+(max-min)*v;
142 }
143
144 //-----------------------------------------------------
145 //collapse from parameter range [min...max] to [0...1] linear
146 double map_value_backward(float v, float min, float max)
147 {
148 return (v-min)/(max-min);
149 }
150
151 //***********************************************
152 // OBVEZNE FREI0R FUNKCIJE
153
154 //-----------------------------------------------
155 int f0r_init()
156 {
157 return 1;
158 }
159
160 //------------------------------------------------
161 void f0r_deinit()
162 {
163 }
164
165 //-----------------------------------------------
166 void f0r_get_plugin_info(f0r_plugin_info_t* info)
167 {
168
169 info->name="Sharpness";
170 info->author="Marko Cebokli, Remi Guyomarch";
171 info->plugin_type=F0R_PLUGIN_TYPE_FILTER;
172 info->color_model=F0R_COLOR_MODEL_RGBA8888;
173 info->frei0r_version=FREI0R_MAJOR_VERSION;
174 info->major_version=0;
175 info->minor_version=2;
176 info->num_params=2;
177 info->explanation="Unsharp masking (port from Mplayer)";
178 }
179
180 //--------------------------------------------------
181 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
182 {
183 switch(param_index)
184 {
185 case 0:
186 info->name = "Amount";
187 info->type = F0R_PARAM_DOUBLE;
188 info->explanation = "";
189 break;
190 case 1:
191 info->name = "Size";
192 info->type = F0R_PARAM_DOUBLE;
193 info->explanation = "";
194 break;
195 }
196 }
197
198 //----------------------------------------------
199 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
200 {
201 inst *in;
202 int z;
203
204 in=calloc(1,sizeof(inst));
205 in->w=width;
206 in->h=height;
207
208 in->Rplani=calloc(width*height,sizeof(unsigned char));
209 in->Gplani=calloc(width*height,sizeof(unsigned char));
210 in->Bplani=calloc(width*height,sizeof(unsigned char));
211 in->Rplano=calloc(width*height,sizeof(unsigned char));
212 in->Gplano=calloc(width*height,sizeof(unsigned char));
213 in->Bplano=calloc(width*height,sizeof(unsigned char));
214
215 //defaults
216 in->fp.amount=0.0;
217 in->size=3;
218 in->fp.msizeX=3;
219 in->fp.msizeY=3;
220 in->ac=0;
221
222 memset(in->fp.SC,0,sizeof(in->fp.SC));
223 for( z=0; z<in->fp.msizeY; z++ )
224 in->fp.SC[z] = calloc(in->w+in->fp.msizeX , sizeof(*(in->fp.SC[z])));
225
226 return (f0r_instance_t)in;
227 }
228
229 //---------------------------------------------------
230 void f0r_destruct(f0r_instance_t instance)
231 {
232 inst *in;
233 int i;
234
235 in=(inst*)instance;
236
237 free(in->Rplani);
238 free(in->Gplani);
239 free(in->Bplani);
240 free(in->Rplano);
241 free(in->Gplano);
242 free(in->Bplano);
243 for (i=0;i<in->fp.msizeY;i++) free(in->fp.SC[i]);
244
245 free(instance);
246 }
247
248 //-----------------------------------------------------
249 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t parm, int param_index)
250 {
251 inst *p;
252 double tmpf;
253 int tmpi,chg,z;
254
255 p=(inst*)instance;
256
257 chg=0;
258 switch(param_index)
259 {
260 case 0:
261 tmpf=map_value_forward(*((double*)parm), -1.5, 3.5);
262 if (tmpf!=p->fp.amount) chg=1;
263 p->fp.amount=tmpf;
264 break;
265 case 1:
266 tmpi=map_value_forward(*((double*)parm), 3.0, 11.0);
267 if (p->size != tmpi) chg=1;
268 p->size=tmpi;
269 break;
270 }
271
272 if (chg==0) return;
273
274
275 for( z=0; z<p->fp.msizeY; z++ )
276 free(p->fp.SC[z]);
277
278 p->fp.msizeX=p->size;
279 p->fp.msizeY=p->size;
280
281 memset(p->fp.SC,0,sizeof(p->fp.SC));
282 for( z=0; z<p->fp.msizeY; z++ )
283 p->fp.SC[z] = calloc(p->w+p->fp.msizeX , sizeof(*(p->fp.SC[z])));
284
285 }
286
287 //--------------------------------------------------
288 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
289 {
290 inst *p;
291
292 p=(inst*)instance;
293
294 switch(param_index)
295 {
296 case 0:
297 *((double*)param)=map_value_backward(p->fp.amount, -1.5, 3.5);
298 break;
299 case 1:
300 *((double*)param)=map_value_backward(p->size, 3.0, 11.0);
301 break;
302 }
303 }
304
305 //-------------------------------------------------
306 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
307 {
308 inst *in;
309 int i;
310
311 assert(instance);
312 in=(inst*)instance;
313
314 //Frei0r works with packed color, Mplayer with planar color
315 for (i=0;i<(in->w*in->h);i++) //copy to planar
316 {
317 in->Rplani[i]=inframe[i]&255;
318 in->Gplani[i]=(inframe[i]>>8)&255;
319 in->Bplani[i]=(inframe[i]>>16)&255;
320 }
321
322 unsharp(in->Rplano, in->Rplani, in->w, in->w, in->w, in->h, &in->fp);
323 unsharp(in->Gplano, in->Gplani, in->w, in->w, in->w, in->h, &in->fp);
324 unsharp(in->Bplano, in->Bplani, in->w, in->w, in->w, in->h, &in->fp);
325
326 for (i=0;i<(in->w*in->h);i++) //copy to packed, preserve alpha
327 {
328 outframe[i]=((uint32_t)in->Rplano[i])|((uint32_t)in->Gplano[i]<<8)|((uint32_t)in->Bplano[i]<<16)|(inframe[i]&0xFF000000);
329 }
330
331
332 }
333