1 /*****************************************************************************
2 * noise.c : "add noise to image" video filter
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
36 #include "filter_picture.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Create ( vlc_object_t * );
42 static picture_t *Filter( filter_t *, picture_t * );
44 #define FILTER_PREFIX "noise-"
46 /*****************************************************************************
48 *****************************************************************************/
50 set_description( N_("Noise video filter") )
51 set_shortname( N_( "Noise" ))
52 set_capability( "video filter2", 0 )
53 set_category( CAT_VIDEO )
54 set_subcategory( SUBCAT_VIDEO_VFILTER )
56 add_shortcut( "noise" )
57 set_callbacks( Create, NULL )
60 /*****************************************************************************
61 * Create: allocates Distort video thread output method
62 *****************************************************************************
63 * This function allocates and initializes a Distort vout method.
64 *****************************************************************************/
65 static int Create( vlc_object_t *p_this )
67 filter_t *p_filter = (filter_t *)p_this;
68 p_filter->pf_video_filter = Filter;
73 /*****************************************************************************
74 * Render: displays previously rendered output
75 *****************************************************************************
76 * This function send the currently rendered image to Distort image, waits
77 * until it is displayed and switch the two rendering buffers, preparing next
79 *****************************************************************************/
80 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
85 if( !p_pic ) return NULL;
87 p_outpic = filter_NewPicture( p_filter );
90 msg_Warn( p_filter, "can't get output picture" );
91 picture_Release( p_pic );
95 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
97 uint8_t *p_in = p_pic->p[i_index].p_pixels;
98 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
100 const int i_num_lines = p_pic->p[i_index].i_visible_lines;
101 const int i_num_cols = p_pic->p[i_index].i_visible_pitch;
102 const int i_pitch = p_pic->p[i_index].i_pitch;
106 for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
110 /* line isn't noisy */
111 vlc_memcpy( p_out+i_line*i_pitch, p_in+i_line*i_pitch,
116 /* this line is noisy */
117 int noise_level = rand()%8+2;
118 for( i_col = 0; i_col < i_num_cols ; i_col++ )
120 if( rand()%noise_level )
122 p_out[i_line*i_pitch+i_col] =
123 p_in[i_line*i_pitch+i_col];
127 p_out[i_line*i_pitch+i_col] = (rand()%3)*0x7f;
134 return CopyInfoAndRelease( p_outpic, p_pic );