]> git.sesse.net Git - vlc/blob - modules/video_filter/gradient.c
Remove stdlib.h
[vlc] / modules / video_filter / gradient.c
1 /*****************************************************************************
2  * gradient.c : Gradient and edge detection video effects plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Antoine Cellerier <dionoea -at- videolan -dot- org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <string.h>
29
30 #include <math.h>                                            /* sin(), cos() */
31
32 #include <vlc/vlc.h>
33 #include <vlc_sout.h>
34 #include <vlc_vout.h>
35
36 #include "vlc_filter.h"
37
38 enum { GRADIENT, EDGE, HOUGH };
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create    ( vlc_object_t * );
44 static void Destroy   ( vlc_object_t * );
45
46 static picture_t *Filter( filter_t *, picture_t * );
47 static int GradientCallback( vlc_object_t *, char const *,
48                              vlc_value_t, vlc_value_t,
49                              void * );
50
51 static void FilterGradient( filter_t *, picture_t *, picture_t * );
52 static void FilterEdge    ( filter_t *, picture_t *, picture_t * );
53 static void FilterHough   ( filter_t *, picture_t *, picture_t * );
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 #define MODE_TEXT N_("Distort mode")
59 #define MODE_LONGTEXT N_("Distort mode, one of \"gradient\", \"edge\" and \"hough\".")
60
61 #define GRADIENT_TEXT N_("Gradient image type")
62 #define GRADIENT_LONGTEXT N_("Gradient image type (0 or 1). 0 will " \
63         "turn the image to white while 1 will keep colors." )
64
65 #define CARTOON_TEXT N_("Apply cartoon effect")
66 #define CARTOON_LONGTEXT N_("Apply cartoon effect. It is only used by " \
67     "\"gradient\" and \"edge\".")
68
69 static const char *mode_list[] = { "gradient", "edge", "hough" };
70 static const char *mode_list_text[] = { N_("Gradient"), N_("Edge"), N_("Hough") };
71
72 #define FILTER_PREFIX "gradient-"
73
74 vlc_module_begin();
75     set_description( _("Gradient video filter") );
76     set_shortname( _( "Gradient" ));
77     set_capability( "video filter2", 0 );
78     set_category( CAT_VIDEO );
79     set_subcategory( SUBCAT_VIDEO_VFILTER );
80
81     add_string( FILTER_PREFIX "mode", "gradient", NULL,
82                 MODE_TEXT, MODE_LONGTEXT, VLC_FALSE );
83         change_string_list( mode_list, mode_list_text, 0 );
84
85     add_integer_with_range( FILTER_PREFIX "type", 0, 0, 1, NULL,
86                 GRADIENT_TEXT, GRADIENT_LONGTEXT, VLC_FALSE );
87     add_bool( FILTER_PREFIX "cartoon", 1, NULL,
88                 CARTOON_TEXT, CARTOON_LONGTEXT, VLC_FALSE );
89
90     add_shortcut( "gradient" );
91     set_callbacks( Create, Destroy );
92 vlc_module_end();
93
94 static const char *ppsz_filter_options[] = {
95     "mode", "type", "cartoon", NULL
96 };
97
98 /*****************************************************************************
99  * vout_sys_t: Distort video output method descriptor
100  *****************************************************************************
101  * This structure is part of the video output thread descriptor.
102  * It describes the Distort specific properties of an output thread.
103  *****************************************************************************/
104 struct filter_sys_t
105 {
106     int i_mode;
107
108     /* For the gradient mode */
109     int i_gradient_type;
110     vlc_bool_t b_cartoon;
111
112     uint32_t *p_buf32;
113     uint32_t *p_buf32_bis;
114     uint8_t *p_buf8;
115
116     /* For hough mode */
117     int *p_pre_hough;
118 };
119
120 /*****************************************************************************
121  * Create: allocates Distort video thread output method
122  *****************************************************************************
123  * This function allocates and initializes a Distort vout method.
124  *****************************************************************************/
125 static int Create( vlc_object_t *p_this )
126 {
127     filter_t *p_filter = (filter_t *)p_this;
128     char *psz_method;
129
130     /* Allocate structure */
131     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
132     if( p_filter->p_sys == NULL )
133     {
134         msg_Err( p_filter, "out of memory" );
135         return VLC_ENOMEM;
136     }
137
138     p_filter->pf_video_filter = Filter;
139
140     p_filter->p_sys->p_pre_hough = NULL;
141
142     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
143                    p_filter->p_cfg );
144
145     if( !(psz_method =
146         var_CreateGetNonEmptyStringCommand( p_filter, FILTER_PREFIX "mode" )) )
147     {
148         msg_Err( p_filter, "configuration variable "
149                  FILTER_PREFIX "mode empty" );
150         p_filter->p_sys->i_mode = GRADIENT;
151     }
152     else
153     {
154         if( !strcmp( psz_method, "gradient" ) )
155         {
156             p_filter->p_sys->i_mode = GRADIENT;
157         }
158         else if( !strcmp( psz_method, "edge" ) )
159         {
160             p_filter->p_sys->i_mode = EDGE;
161         }
162         else if( !strcmp( psz_method, "hough" ) )
163         {
164             p_filter->p_sys->i_mode = HOUGH;
165         }
166         else
167         {
168             msg_Err( p_filter, "no valid gradient mode provided (%s)", psz_method );
169             p_filter->p_sys->i_mode = GRADIENT;
170         }
171     }
172     free( psz_method );
173
174     p_filter->p_sys->i_gradient_type =
175         var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "type" );
176     p_filter->p_sys->b_cartoon =
177         var_CreateGetBoolCommand( p_filter, FILTER_PREFIX "cartoon" );
178
179     var_AddCallback( p_filter, FILTER_PREFIX "mode",
180                      GradientCallback, p_filter->p_sys );
181     var_AddCallback( p_filter, FILTER_PREFIX "type",
182                      GradientCallback, p_filter->p_sys );
183     var_AddCallback( p_filter, FILTER_PREFIX "cartoon",
184                      GradientCallback, p_filter->p_sys );
185
186     p_filter->p_sys->p_buf32 = NULL;
187     p_filter->p_sys->p_buf32_bis = NULL;
188     p_filter->p_sys->p_buf8 = NULL;
189
190     return VLC_SUCCESS;
191 }
192
193 /*****************************************************************************
194  * Destroy: destroy Distort video thread output method
195  *****************************************************************************
196  * Terminate an output method created by DistortCreateOutputMethod
197  *****************************************************************************/
198 static void Destroy( vlc_object_t *p_this )
199 {
200     filter_t *p_filter = (filter_t *)p_this;
201
202     free( p_filter->p_sys->p_buf32 );
203     free( p_filter->p_sys->p_buf32_bis );
204     free( p_filter->p_sys->p_buf8 );
205     free( p_filter->p_sys->p_pre_hough );
206
207     free( p_filter->p_sys );
208 }
209
210 /*****************************************************************************
211  * Render: displays previously rendered output
212  *****************************************************************************
213  * This function send the currently rendered image to Distort image, waits
214  * until it is displayed and switch the two rendering buffers, preparing next
215  * frame.
216  *****************************************************************************/
217 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
218 {
219     picture_t *p_outpic;
220
221     if( !p_pic ) return NULL;
222
223     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
224     if( !p_outpic )
225     {
226         msg_Warn( p_filter, "can't get output picture" );
227         if( p_pic->pf_release )
228             p_pic->pf_release( p_pic );
229         return NULL;
230     }
231
232     switch( p_filter->p_sys->i_mode )
233     {
234         case EDGE:
235             FilterEdge( p_filter, p_pic, p_outpic );
236             break;
237
238         case GRADIENT:
239             FilterGradient( p_filter, p_pic, p_outpic );
240             break;
241
242         case HOUGH:
243             FilterHough( p_filter, p_pic, p_outpic );
244             break;
245
246         default:
247             break;
248     }
249
250     p_outpic->date = p_pic->date;
251     p_outpic->b_force = p_pic->b_force;
252     p_outpic->i_nb_fields = p_pic->i_nb_fields;
253     p_outpic->b_progressive = p_pic->b_progressive;
254     p_outpic->b_top_field_first = p_pic->b_top_field_first;
255
256     if( p_pic->pf_release )
257         p_pic->pf_release( p_pic );
258
259     return p_outpic;
260 }
261
262 /*****************************************************************************
263  * Gaussian Convolution
264  *****************************************************************************
265  *    Gaussian convolution ( sigma == 1.4 )
266  *
267  *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
268  *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
269  *    |  5 12 15 12  5  | ~ |  4 12 16 12  4 |
270  *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
271  *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
272  *****************************************************************************/
273 static void GaussianConvolution( picture_t *p_inpic, uint32_t *p_smooth )
274 {
275     const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
276     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
277     const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
278     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
279
280     int x,y;
281     for( y = 2; y < i_num_lines - 2; y++ )
282     {
283         for( x = 2; x < i_src_visible - 2; x++ )
284         {
285             p_smooth[y*i_src_visible+x] = (uint32_t)(
286               /* 2 rows up */
287                 ( p_inpix[(y-2)*i_src_pitch+x-2] )
288               + ((p_inpix[(y-2)*i_src_pitch+x-1]
289               +   p_inpix[(y-2)*i_src_pitch+x]
290               +   p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
291               + ( p_inpix[(y-2)*i_src_pitch+x+2] )
292               /* 1 row up */
293               + ((p_inpix[(y-1)*i_src_pitch+x-2]
294               + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
295               + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
296               + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
297               +   p_inpix[(y-1)*i_src_pitch+x+2]
298               /* */
299               +   p_inpix[y*i_src_pitch+x-2]
300               + ( p_inpix[y*i_src_pitch+x-1]*3 )
301               + ( p_inpix[y*i_src_pitch+x]<<2 )
302               + ( p_inpix[y*i_src_pitch+x+1]*3 )
303               +   p_inpix[y*i_src_pitch+x+2]
304               /* 1 row down */
305               +   p_inpix[(y+1)*i_src_pitch+x-2]
306               + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
307               + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
308               + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
309               +   p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
310               /* 2 rows down */
311               + ( p_inpix[(y+2)*i_src_pitch+x-2] )
312               + ((p_inpix[(y+2)*i_src_pitch+x-1]
313               +   p_inpix[(y+2)*i_src_pitch+x]
314               +   p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
315               + ( p_inpix[(y+2)*i_src_pitch+x+2] )
316               ) >> 6 /* 115 */;
317         }
318     }
319 }
320
321 /*****************************************************************************
322  * FilterGradient: Sobel
323  *****************************************************************************/
324 static void FilterGradient( filter_t *p_filter, picture_t *p_inpic,
325                                                 picture_t *p_outpic )
326 {
327     int x, y;
328     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
329     const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
330     const int i_dst_pitch = p_outpic->p[Y_PLANE].i_pitch;
331     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
332
333     const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
334     uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;
335
336     uint32_t *p_smooth;
337     if( !p_filter->p_sys->p_buf32 )
338         p_filter->p_sys->p_buf32 =
339         (uint32_t *)malloc( i_num_lines * i_src_visible * sizeof(uint32_t));
340     p_smooth = p_filter->p_sys->p_buf32;
341
342     if( !p_smooth ) return;
343
344     if( p_filter->p_sys->b_cartoon )
345     {
346         p_filter->p_libvlc->pf_memcpy( p_outpic->p[U_PLANE].p_pixels,
347             p_inpic->p[U_PLANE].p_pixels,
348             p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
349         p_filter->p_libvlc->pf_memcpy( p_outpic->p[V_PLANE].p_pixels,
350             p_inpic->p[V_PLANE].p_pixels,
351             p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
352     }
353     else
354     {
355         p_filter->p_libvlc->pf_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
356             p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
357         p_filter->p_libvlc->pf_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
358             p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
359     }
360
361     GaussianConvolution( p_inpic, p_smooth );
362
363     /* Sobel gradient
364
365      | -1 0 1 |     |  1  2  1 |
366      | -2 0 2 | and |  0  0  0 |
367      | -1 0 1 |     | -1 -2 -1 | */
368
369 #define FOR                                                     \
370     for( y = 1; y < i_num_lines - 1; y++ )                      \
371     {                                                           \
372         for( x = 1; x < i_src_visible - 1; x++ )                \
373         {                                                       \
374             const uint32_t a =                                  \
375             (                                                   \
376               abs(                                              \
377                  ( p_smooth[(y-1)*i_src_visible+x-1]            \
378                    - p_smooth[(y+1)*i_src_visible+x-1] )        \
379                + ( ( p_smooth[(y-1)*i_src_visible+x]            \
380                     - p_smooth[(y+1)*i_src_visible+x] ) <<1 )   \
381                + ( p_smooth[(y-1)*i_src_visible+x+1]            \
382                    - p_smooth[(y+1)*i_src_visible+x+1] )        \
383               )                                                 \
384             +                                                   \
385               abs(                                              \
386                  ( p_smooth[(y-1)*i_src_visible+x-1]            \
387                    - p_smooth[(y-1)*i_src_visible+x+1] )        \
388                + ( ( p_smooth[y*i_src_visible+x-1]              \
389                     - p_smooth[y*i_src_visible+x+1] ) <<1 )     \
390                + ( p_smooth[(y+1)*i_src_visible+x-1]            \
391                    - p_smooth[(y+1)*i_src_visible+x+1] )        \
392               )                                                 \
393             );
394     if( p_filter->p_sys->i_gradient_type )
395     {
396         if( p_filter->p_sys->b_cartoon )
397         {
398             FOR
399             if( a > 60 )
400             {
401                 p_outpix[y*i_dst_pitch+x] = 0x00;
402             }
403             else
404             {
405                 if( p_smooth[y*i_src_visible+x] > 0xa0 )
406                     p_outpix[y*i_dst_pitch+x] =
407                         0xff - ((0xff - p_inpix[y*i_src_pitch+x] )>>2);
408                 else if( p_smooth[y*i_src_visible+x] > 0x70 )
409                     p_outpix[y*i_dst_pitch+x] =
410                         0xa0 - ((0xa0 - p_inpix[y*i_src_pitch+x] )>>2);
411                 else if( p_smooth[y*i_src_visible+x] > 0x28 )
412                     p_outpix[y*i_dst_pitch+x] =
413                         0x70 - ((0x70 - p_inpix[y*i_src_pitch+x] )>>2);
414                 else
415                     p_outpix[y*i_dst_pitch+x] =
416                         0x28 - ((0x28 - p_inpix[y*i_src_pitch+x] )>>2);
417             }
418             }}
419         }
420         else
421         {
422             FOR
423             p_outpix[y*i_dst_pitch+x] = clip_uint8_vlc( a );
424             }}
425         }
426     }
427     else
428     {
429         FOR
430         if( a>>8 )
431             p_outpix[y*i_dst_pitch+x] = 0;
432         else
433             p_outpix[y*i_dst_pitch+x] = 0xff-(uint8_t)a;
434         }}
435     }
436 #undef FOR
437 }
438
439 /*****************************************************************************
440  * FilterEdge: Canny edge detection algorithm
441  *****************************************************************************
442  * http://fourier.eng.hmc.edu/e161/lectures/canny/node1.html
443  * (well ... my implementation isn't really the canny algorithm ... but some
444  * ideas are the same)
445  *****************************************************************************/
446 /* angle : | */
447 #define THETA_Y 0
448 /* angle : - */
449 #define THETA_X 1
450 /* angle : / */
451 #define THETA_P 2
452 /* angle : \ */
453 #define THETA_M 3
454 static void FilterEdge( filter_t *p_filter, picture_t *p_inpic,
455                                             picture_t *p_outpic )
456 {
457     int x, y;
458
459     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
460     const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
461     const int i_dst_pitch = p_outpic->p[Y_PLANE].i_pitch;
462     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
463
464     const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
465     uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;
466
467     uint32_t *p_smooth;
468     uint32_t *p_grad;
469     uint8_t *p_theta;
470
471     if( !p_filter->p_sys->p_buf32 )
472         p_filter->p_sys->p_buf32 =
473         (uint32_t *)malloc( i_num_lines * i_src_visible * sizeof(uint32_t));
474     p_smooth = p_filter->p_sys->p_buf32;
475
476     if( !p_filter->p_sys->p_buf32_bis )
477         p_filter->p_sys->p_buf32_bis =
478         (uint32_t *)malloc( i_num_lines * i_src_visible * sizeof(uint32_t));
479     p_grad = p_filter->p_sys->p_buf32_bis;
480
481     if( !p_filter->p_sys->p_buf8 )
482         p_filter->p_sys->p_buf8 =
483         (uint8_t *)malloc( i_num_lines * i_src_visible * sizeof(uint8_t));
484     p_theta = p_filter->p_sys->p_buf8;
485
486     if( !p_smooth || !p_grad || !p_theta ) return;
487
488     if( p_filter->p_sys->b_cartoon )
489     {
490         p_filter->p_libvlc->pf_memcpy( p_outpic->p[U_PLANE].p_pixels,
491             p_inpic->p[U_PLANE].p_pixels,
492             p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
493         p_filter->p_libvlc->pf_memcpy( p_outpic->p[V_PLANE].p_pixels,
494             p_inpic->p[V_PLANE].p_pixels,
495             p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
496     }
497     else
498     {
499         p_filter->p_libvlc->pf_memset( p_outpic->p[Y_PLANE].p_pixels, 0xff,
500               p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
501         p_filter->p_libvlc->pf_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
502             p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
503         p_filter->p_libvlc->pf_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
504             p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
505     }
506
507     GaussianConvolution( p_inpic, p_smooth );
508
509     /* Sobel gradient
510
511      | -1 0 1 |     |  1  2  1 |
512      | -2 0 2 | and |  0  0  0 |
513      | -1 0 1 |     | -1 -2 -1 | */
514
515     for( y = 1; y < i_num_lines - 1; y++ )
516     {
517         for( x = 1; x < i_src_visible - 1; x++ )
518         {
519
520             const int gradx =
521                  ( p_smooth[(y-1)*i_src_visible+x-1]
522                    - p_smooth[(y+1)*i_src_visible+x-1] )
523                + ( ( p_smooth[(y-1)*i_src_visible+x]
524                     - p_smooth[(y+1)*i_src_visible+x] ) <<1 )
525                + ( p_smooth[(y-1)*i_src_visible+x+1]
526                    - p_smooth[(y+1)*i_src_visible+x+1] );
527             const int grady =
528                  ( p_smooth[(y-1)*i_src_visible+x-1]
529                    - p_smooth[(y-1)*i_src_visible+x+1] )
530                + ( ( p_smooth[y*i_src_visible+x-1]
531                     - p_smooth[y*i_src_visible+x+1] ) <<1 )
532                + ( p_smooth[(y+1)*i_src_visible+x-1]
533                    - p_smooth[(y+1)*i_src_visible+x+1] );
534
535             p_grad[y*i_src_visible+x] = (uint32_t)(abs( gradx ) + abs( grady ));
536
537             /* tan( 22.5 ) = 0,414213562 .. * 128 = 53
538              * tan( 26,565051177 ) = 0.5
539              * tan( 45 + 22.5 ) = 2,414213562 .. * 128 = 309
540              * tan( 63,434948823 ) 2 */
541             if( (grady<<1) > gradx )
542                 p_theta[y*i_src_visible+x] = THETA_P;
543             else if( (grady<<1) < -gradx )
544                 p_theta[y*i_src_visible+x] = THETA_M;
545             else if( !gradx || abs(grady) > abs(gradx)<<1 )
546                 p_theta[y*i_src_visible+x] = THETA_Y;
547             else
548                 p_theta[y*i_src_visible+x] = THETA_X;
549         }
550     }
551
552     /* edge computing */
553     for( y = 1; y < i_num_lines - 1; y++ )
554     {
555         for( x = 1; x < i_src_visible - 1; x++ )
556         {
557             if( p_grad[y*i_src_visible+x] > 40 )
558             {
559                 switch( p_theta[y*i_src_visible+x] )
560                 {
561                     case THETA_Y:
562                         if(    p_grad[y*i_src_visible+x] > p_grad[(y-1)*i_src_visible+x]
563                             && p_grad[y*i_src_visible+x] > p_grad[(y+1)*i_src_visible+x] )
564                         {
565                             p_outpix[y*i_dst_pitch+x] = 0;
566                             break;
567                         } else goto colorize;
568                     case THETA_P:
569                         if(    p_grad[y*i_src_visible+x] > p_grad[(y-1)*i_src_visible+x-1]
570                             && p_grad[y*i_src_visible+x] > p_grad[(y+1)*i_src_visible+x+1] )
571                         {
572                             p_outpix[y*i_dst_pitch+x] = 0;
573                             break;
574                         } else goto colorize;
575                     case THETA_M:
576                         if(    p_grad[y*i_src_visible+x] > p_grad[(y-1)*i_src_visible+x+1]
577                             && p_grad[y*i_src_visible+x] > p_grad[(y+1)*i_src_visible+x-1] )
578                         {
579                             p_outpix[y*i_dst_pitch+x] = 0;
580                             break;
581                         } else goto colorize;
582                     case THETA_X:
583                         if(    p_grad[y*i_src_visible+x] > p_grad[y*i_src_visible+x-1]
584                             && p_grad[y*i_src_visible+x] > p_grad[y*i_src_visible+x+1] )
585                         {
586                             p_outpix[y*i_dst_pitch+x] = 0;
587                             break;
588                         } else goto colorize;
589                 }
590             }
591             else
592             {
593                 colorize:
594                 if( p_filter->p_sys->b_cartoon )
595                 {
596                     if( p_smooth[y*i_src_visible+x] > 0xa0 )
597                         p_outpix[y*i_dst_pitch+x] = (uint8_t)
598                             0xff - ((0xff - p_inpix[y*i_src_pitch+x] )>>2);
599                     else if( p_smooth[y*i_src_visible+x] > 0x70 )
600                         p_outpix[y*i_dst_pitch+x] =(uint8_t)
601                             0xa0 - ((0xa0 - p_inpix[y*i_src_pitch+x] )>>2);
602                     else if( p_smooth[y*i_src_visible+x] > 0x28 )
603                         p_outpix[y*i_dst_pitch+x] =(uint8_t)
604                             0x70 - ((0x70 - p_inpix[y*i_src_pitch+x] )>>2);
605                     else
606                         p_outpix[y*i_dst_pitch+x] =(uint8_t)
607                             0x28 - ((0x28 - p_inpix[y*i_src_pitch+x] )>>2);
608                 }
609             }
610         }
611     }
612 }
613
614 /*****************************************************************************
615  * FilterHough
616  *****************************************************************************/
617 #define p_pre_hough p_filter->p_sys->p_pre_hough
618 static void FilterHough( filter_t *p_filter, picture_t *p_inpic,
619                                              picture_t *p_outpic )
620 {
621     int x, y, i;
622     int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
623     int i_dst_pitch = p_outpic->p[Y_PLANE].i_pitch;
624     int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
625
626     uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;
627
628     int i_diag = sqrt( i_num_lines * i_num_lines +
629                         i_src_visible * i_src_visible);
630     int i_max, i_phi_max, i_rho, i_rho_max;
631     int i_nb_steps = 90;
632     double d_step = M_PI / i_nb_steps;
633     double d_sin;
634     double d_cos;
635     uint32_t *p_smooth;
636     int *p_hough = malloc( i_diag * i_nb_steps * sizeof(int) );
637     if( ! p_hough ) return;
638     p_smooth = (uint32_t *)malloc( i_num_lines*i_src_visible*sizeof(uint32_t));
639     if( !p_smooth ) return;
640
641     if( ! p_pre_hough )
642     {
643         msg_Dbg(p_filter, "Starting precalculation");
644         p_pre_hough = malloc( i_num_lines*i_src_visible*i_nb_steps*sizeof(int));
645         if( ! p_pre_hough ) return;
646         for( i = 0 ; i < i_nb_steps ; i++)
647         {
648             d_sin = sin(d_step * i);
649             d_cos = cos(d_step * i);
650             for( y = 0 ; y < i_num_lines ; y++ )
651                 for( x = 0 ; x < i_src_visible ; x++ )
652                 {
653                     p_pre_hough[(i*i_num_lines+y)*i_src_visible + x] =
654                         ceil(x*d_sin + y*d_cos);
655                 }
656         }
657         msg_Dbg(p_filter, "Precalculation done");
658     }
659
660     p_filter->p_libvlc->pf_memset( p_hough, 0,
661                                    i_diag * i_nb_steps * sizeof(int) );
662
663     p_filter->p_libvlc->pf_memcpy(
664         p_outpic->p[Y_PLANE].p_pixels, p_inpic->p[Y_PLANE].p_pixels,
665         p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
666     p_filter->p_libvlc->pf_memcpy(
667         p_outpic->p[U_PLANE].p_pixels, p_inpic->p[U_PLANE].p_pixels,
668         p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
669     p_filter->p_libvlc->pf_memcpy(
670         p_outpic->p[V_PLANE].p_pixels, p_inpic->p[V_PLANE].p_pixels,
671         p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
672
673     GaussianConvolution( p_inpic, p_smooth );
674
675     /* Sobel gradient
676
677      | -1 0 1 |     |  1  2  1 |
678      | -2 0 2 | and |  0  0  0 |
679      | -1 0 1 |     | -1 -2 -1 | */
680
681     i_max = 0;
682     i_rho_max = 0;
683     i_phi_max = 0;
684     for( y = 4; y < i_num_lines - 4; y++ )
685     {
686         for( x = 4; x < i_src_visible - 4; x++ )
687         {
688             uint32_t a =
689             (
690               abs(
691                 ( ( p_smooth[(y-1)*i_src_visible+x]
692                     - p_smooth[(y+1)*i_src_visible+x] ) <<1 )
693                + ( p_smooth[(y-1)*i_src_visible+x-1]
694                    - p_smooth[(y+1)*i_src_visible+x-1] )
695                + ( p_smooth[(y-1)*i_src_visible+x+1]
696                    - p_smooth[(y+1)*i_src_visible+x+1] )
697               )
698             +
699               abs(
700                 ( ( p_smooth[y*i_src_visible+x-1]
701                     - p_smooth[y*i_src_visible+x+1] ) <<1 )
702                + ( p_smooth[(y-1)*i_src_visible+x-1]
703                    - p_smooth[(y-1)*i_src_visible+x+1] )
704                + ( p_smooth[(y+1)*i_src_visible+x-1]
705                    - p_smooth[(y+1)*i_src_visible+x+1] )
706               )
707             );
708             if( a>>8 )
709             {
710                 for( i = 0 ; i < i_nb_steps ; i ++ )
711                 {
712                     i_rho = p_pre_hough[(i*i_num_lines+y)*i_src_visible + x];
713                     if( p_hough[i_rho + i_diag/2 + i * i_diag]++ > i_max )
714                     {
715                         i_max = p_hough[i_rho + i_diag/2 + i * i_diag];
716                         i_rho_max = i_rho;
717                         i_phi_max = i;
718                     }
719                 }
720             }
721         }
722     }
723
724     d_sin = sin(i_phi_max*d_step);
725     d_cos = cos(i_phi_max*d_step);
726     if( d_cos != 0 )
727     {
728         for( x = 0 ; x < i_src_visible ; x++ )
729         {
730             y = (i_rho_max - x * d_sin) / d_cos;
731             if( y >= 0 && y < i_num_lines )
732                 p_outpix[y*i_dst_pitch+x] = 255;
733         }
734     }
735
736     if( p_hough ) free( p_hough );
737     if( p_smooth ) free( p_smooth );
738 }
739 #undef p_pre_hough
740
741
742 static int GradientCallback( vlc_object_t *p_this, char const *psz_var,
743                              vlc_value_t oldval, vlc_value_t newval,
744                              void *p_data )
745 {
746     filter_sys_t *p_sys = (filter_sys_t *)p_data;
747     if( !strcmp( psz_var, FILTER_PREFIX "mode" ) )
748     {
749         if( !strcmp( newval.psz_string, "gradient" ) )
750         {
751             p_sys->i_mode = GRADIENT;
752         }
753         else if( !strcmp( newval.psz_string, "edge" ) )
754         {
755             p_sys->i_mode = EDGE;
756         }
757         else if( !strcmp( newval.psz_string, "hough" ) )
758         {
759             p_sys->i_mode = HOUGH;
760         }
761         else
762         {
763             msg_Err( p_this, "no valid gradient mode provided (%s)", newval.psz_string );
764             p_sys->i_mode = GRADIENT;
765         }
766     }
767     else if( !strcmp( psz_var, FILTER_PREFIX "type" ) )
768     {
769         p_sys->i_gradient_type = newval.i_int;
770     }
771     else if( !strcmp( psz_var, FILTER_PREFIX "cartoon" ) )
772     {
773         p_sys->b_cartoon = newval.b_bool;
774     }
775     return VLC_SUCCESS;
776 }