]> git.sesse.net Git - vlc/blob - modules/video_filter/colorthres.c
Merge branch 1.0-bugfix
[vlc] / modules / video_filter / colorthres.c
1 /*****************************************************************************
2  * colorthres.c: Theshold color based on similarity to reference color
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal <dnumgis@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <errno.h>
33 #include <math.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_sout.h>
38
39 #include "vlc_filter.h"
40 #include "filter_picture.h"
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  Create    ( vlc_object_t * );
46 static void Destroy   ( vlc_object_t * );
47
48 static picture_t *Filter( filter_t *, picture_t * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 #define COLOR_TEXT N_("Color")
54 #define COLOR_LONGTEXT N_("Colors similar to this will be kept, others will be "\
55     "grayscaled. This must be an hexadecimal (like HTML colors). The first two "\
56     "chars are for red, then green, then blue. #000000 = black, #FF0000 = red,"\
57     " #00FF00 = green, #FFFF00 = yellow (red + green), #FFFFFF = white" )
58 static const int pi_color_values[] = {
59   0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x0000FF00, 0x000000FF, 0x0000FFFF };
60
61 static const char *const ppsz_color_descriptions[] = {
62   N_("Red"), N_("Fuchsia"), N_("Yellow"), N_("Lime"), N_("Blue"), N_("Aqua") };
63
64 #define CFG_PREFIX "colorthres-"
65
66 vlc_module_begin ()
67     set_description( N_("Color threshold filter") )
68     set_shortname( N_("Color threshold" ))
69     set_category( CAT_VIDEO )
70     set_subcategory( SUBCAT_VIDEO_VFILTER )
71     set_capability( "video filter2", 0 )
72     add_integer( CFG_PREFIX "color", 0x00FF0000, NULL, COLOR_TEXT,
73                  COLOR_LONGTEXT, false )
74         change_integer_list( pi_color_values, ppsz_color_descriptions, NULL )
75     add_integer( CFG_PREFIX "saturationthres", 20, NULL,
76                  N_("Saturaton threshold"), "", false )
77     add_integer( CFG_PREFIX "similaritythres", 15, NULL,
78                  N_("Similarity threshold"), "", false )
79     set_callbacks( Create, Destroy )
80 vlc_module_end ()
81
82 static const char *const ppsz_filter_options[] = {
83     "color", "saturationthes", "similaritythres", NULL
84 };
85
86 /*****************************************************************************
87  * filter_sys_t: adjust filter method descriptor
88  *****************************************************************************/
89 struct filter_sys_t
90 {
91 };
92
93 /*****************************************************************************
94  * Create: allocates adjust video thread output method
95  *****************************************************************************
96  * This function allocates and initializes a adjust vout method.
97  *****************************************************************************/
98 static int Create( vlc_object_t *p_this )
99 {
100     filter_t *p_filter = (filter_t *)p_this;
101
102     switch( p_filter->fmt_in.video.i_chroma )
103     {
104         CASE_PLANAR_YUV
105             break;
106
107         default:
108             msg_Err( p_filter, "Unsupported input chroma (%4s)",
109                      (char*)&(p_filter->fmt_in.video.i_chroma) );
110             return VLC_EGENERIC;
111     }
112
113     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
114     {
115         msg_Err( p_filter, "Input and output chromas don't match" );
116         return VLC_EGENERIC;
117     }
118
119     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
120                        p_filter->p_cfg );
121     var_Create( p_filter, CFG_PREFIX "color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
122     var_Create( p_filter, CFG_PREFIX "similaritythres", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
123     var_Create( p_filter, CFG_PREFIX "saturationthres", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
124
125     /* Allocate structure */
126     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
127     if( p_filter->p_sys == NULL )
128         return VLC_ENOMEM;
129
130     p_filter->pf_video_filter = Filter;
131
132     return VLC_SUCCESS;
133 }
134
135 /*****************************************************************************
136  * Destroy: destroy adjust video thread output method
137  *****************************************************************************
138  * Terminate an output method created by adjustCreateOutputMethod
139  *****************************************************************************/
140 static void Destroy( vlc_object_t *p_this )
141 {
142     filter_t *p_filter = (filter_t *)p_this;
143     free( p_filter->p_sys );
144 }
145
146 /*****************************************************************************
147  * Render: displays previously rendered output
148  *****************************************************************************
149  * This function send the currently rendered image to adjust modified image,
150  * waits until it is displayed and switch the two rendering buffers, preparing
151  * next frame.
152  *****************************************************************************/
153 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
154 {
155     picture_t *p_outpic;
156     uint8_t *p_in_y, *p_in_u, *p_in_v, *p_in_end_u;
157     uint8_t *p_out_y, *p_out_u, *p_out_v;
158     int i_simthres = var_GetInteger( p_filter, "colorthres-similaritythres" );
159     int i_satthres = var_GetInteger( p_filter, "colorthres-saturationthres" );
160     int i_color = var_GetInteger( p_filter, "colorthres-color" );
161
162     if( !p_pic ) return NULL;
163
164     p_outpic = filter_NewPicture( p_filter );
165     if( !p_outpic )
166     {
167         picture_Release( p_pic );
168         return NULL;
169     }
170
171     p_in_u = p_pic->p[U_PLANE].p_pixels;
172     p_in_v = p_pic->p[V_PLANE].p_pixels;
173     p_in_y = p_pic->p[Y_PLANE].p_pixels;
174     p_in_end_u = p_in_u + p_pic->p[U_PLANE].i_visible_lines
175                         * p_pic->p[U_PLANE].i_pitch - 8;
176
177     p_out_y = p_outpic->p[Y_PLANE].p_pixels;
178     p_out_u = p_outpic->p[U_PLANE].p_pixels;
179     p_out_v = p_outpic->p[V_PLANE].p_pixels;
180
181     /* Create grayscale version of input */
182     vlc_memcpy( p_out_y, p_in_y, p_pic->p[Y_PLANE].i_visible_lines
183                * p_pic->p[Y_PLANE].i_pitch - 8 );
184     vlc_memset( p_out_u, 0x80, p_pic->p[U_PLANE].i_visible_lines
185                * p_pic->p[U_PLANE].i_pitch - 8 );
186     vlc_memset( p_out_v, 0x80, p_pic->p[U_PLANE].i_visible_lines
187                * p_pic->p[U_PLANE].i_pitch - 8 );
188
189     /*
190      * Do the U and V planes
191      */
192     int i_red = ( i_color & 0xFF0000 ) >> 16;
193     int i_green = ( i_color & 0xFF00 ) >> 8;
194     int i_blue = i_color & 0xFF;
195     int i_u = (int8_t)(( -38 * i_red - 74 * i_green +
196                      112 * i_blue + 128) >> 8) + 128;
197     int i_v = (int8_t)(( 112 * i_red  -  94 * i_green -
198                       18 * i_blue + 128) >> 8) + 128;
199     int refu = i_u - 0x80;         /*bright red*/
200     int refv = i_v - 0x80;
201     int reflength = sqrt(refu*refu+refv*refv);
202
203     while( p_in_u < p_in_end_u ) {
204         /* Length of color vector */
205         int inu = (*p_in_u) - 0x80;
206         int inv = (*p_in_v) - 0x80;
207         int length = sqrt(inu*inu+inv*inv);
208
209         int diffu = refu * length - inu *reflength;
210         int diffv = refv * length - inv *reflength;
211         long long int difflen2=diffu*diffu;
212         difflen2 +=diffv*diffv;
213         long long int thres = length*reflength;
214         thres *= thres;
215         if( length > i_satthres && (difflen2*i_simthres< thres ) ) {
216             *p_out_u = *p_in_u;
217             *p_out_v = *p_in_v;
218 //        fprintf(stderr,"keeping color %d %d\n", length, difflen2);
219         }
220         p_in_u++;
221         p_in_v++;
222         p_out_u++;
223         p_out_v++;
224     }
225
226     return CopyInfoAndRelease( p_outpic, p_pic );
227 }