1 /*****************************************************************************
2 * seamcarving.c: "Seam Carving for Content-Aware Image Resizing"
3 * Based on paper by Shai Avidan and Ariel Shamir.
4 *****************************************************************************
5 * Copyright (C) 2007 the VideoLAN team
8 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include "vlc_filter.h"
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Create ( vlc_object_t * );
41 static void Destroy ( vlc_object_t * );
43 static picture_t *Filter( filter_t *, picture_t * );
44 static int CropCallback( vlc_object_t *, char const *,
45 vlc_value_t, vlc_value_t,
48 static void FilterSeamCarving( filter_t *, picture_t *, picture_t * );
50 /*****************************************************************************
52 *****************************************************************************/
54 #define FILTER_PREFIX "seamcarving-"
57 set_description( _("Seam Carving video filter") );
58 set_shortname( _( "Seam Carvinf" ));
59 set_capability( "video filter2", 0 );
60 set_category( CAT_VIDEO );
61 set_subcategory( SUBCAT_VIDEO_VFILTER );
63 set_callbacks( Create, Destroy );
66 static const char *ppsz_filter_options[] = {
78 static int Create( vlc_object_t *p_this )
80 filter_t *p_filter = (filter_t *)p_this;
82 /* Allocate structure */
83 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
84 if( p_filter->p_sys == NULL )
86 msg_Err( p_filter, "out of memory" );
90 p_filter->pf_video_filter = Filter;
91 p_filter->p_sys->p_energy = NULL;
92 p_filter->p_sys->p_grad = NULL;
93 p_filter->p_sys->i_crop = 0;
95 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
98 var_Create( p_filter, "crop", VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
99 var_AddCallback( p_filter, "crop", CropCallback, p_filter->p_sys );
104 static void Destroy( vlc_object_t *p_this )
106 filter_t *p_filter = (filter_t *)p_this;
108 free( p_filter->p_sys->p_energy );
109 free( p_filter->p_sys->p_grad );
111 free( p_filter->p_sys );
114 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
118 if( !p_pic ) return NULL;
120 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
123 msg_Warn( p_filter, "can't get output picture" );
124 if( p_pic->pf_release )
125 p_pic->pf_release( p_pic );
129 FilterSeamCarving( p_filter, p_pic, p_outpic );
131 p_outpic->date = p_pic->date;
132 p_outpic->b_force = p_pic->b_force;
133 p_outpic->i_nb_fields = p_pic->i_nb_fields;
134 p_outpic->b_progressive = p_pic->b_progressive;
135 p_outpic->b_top_field_first = p_pic->b_top_field_first;
137 if( p_pic->pf_release )
138 p_pic->pf_release( p_pic );
143 static inline int min3( int a, int b, int c );
144 static inline int min( int a, int b );
145 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible );
147 //#define DRAW_GRADIENT
148 //#define DRAW_ENERGY
151 static void FilterSeamCarving( filter_t *p_filter, picture_t *p_inpic,
152 picture_t *p_outpic )
154 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
155 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
157 int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
159 if( !p_filter->p_sys->p_energy )
160 p_filter->p_sys->p_energy = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
161 if( !p_filter->p_sys->p_grad )
162 p_filter->p_sys->p_grad = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
164 //#if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
165 p_filter->p_libvlc->pf_memcpy( p_outpic->p[Y_PLANE].p_pixels,
166 p_inpic->p[Y_PLANE].p_pixels,
167 p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
169 // p_filter->p_libvlc->pf_memset( p_outpix, 0x80,
170 // p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
172 p_filter->p_libvlc->pf_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
173 p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
174 p_filter->p_libvlc->pf_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
175 p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
177 #if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
178 i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
183 if( p_filter->p_sys->i_crop != 0 )
184 j = p_filter->p_sys->i_crop;
185 for( i = 0; i < j; i++ )
186 i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
188 for( y = 0; y < p_outpic->p[Y_PLANE].i_lines; y++ )
189 p_filter->p_libvlc->pf_memset( p_outpic->p[Y_PLANE].p_pixels + y*p_outpic->p[Y_PLANE].i_pitch + i_src_visible, 0x00, p_outpic->p[Y_PLANE].i_pitch - i_src_visible );
191 if( j == 100 ) k = -1;
196 static int ComputeGradient( filter_t *p_filter, picture_t *p_inpic, int i_src_visible )
199 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
200 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
202 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
203 int *p_grad = p_filter->p_sys->p_grad;
205 for( y = 1; y < i_num_lines - 1;
206 y++, p_grad += i_src_pitch )
208 /* Compute line y's gradient */
212 ( p_inpix[(y-1)*i_src_pitch+x-1] \
213 - p_inpix[(y+1)*i_src_pitch+x-1] ) \
214 + ( ( p_inpix[(y-1)*i_src_pitch+x] \
215 - p_inpix[(y+1)*i_src_pitch+x] ) <<1 ) \
216 + ( p_inpix[(y-1)*i_src_pitch+x+1] \
217 - p_inpix[(y+1)*i_src_pitch+x+1] ) \
221 ( p_inpix[(y-1)*i_src_pitch+x-1] \
222 - p_inpix[(y-1)*i_src_pitch+x+1] ) \
223 + ( ( p_inpix[y*i_src_pitch+x-1] \
224 - p_inpix[y*i_src_pitch+x+1] ) <<1 ) \
225 + ( p_inpix[(y+1)*i_src_pitch+x-1] \
226 - p_inpix[(y+1)*i_src_pitch+x+1] ) \
229 for( x = 1; x < i_src_visible - 1; x++ )
231 p_grad[x] = GRADx( x );
233 p_outpix[y*i_src_pitch+x] = p_grad[x]>>3;
240 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible )
243 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
244 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
246 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
247 uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;
249 int *p_energy = p_filter->p_sys->p_energy;
250 int *p_grad = p_filter->p_sys->p_grad;
252 ComputeGradient( p_filter, p_inpic, i_src_visible );
254 /** Compute the image's energy (using a sobel gradient as the base energy
256 /* Set the first energy line to 0 */
257 memset( p_energy, 0, i_src_pitch*sizeof(int));
259 int *p_energy_prev = p_energy;
260 for( y = 1; y < i_num_lines - 1;
261 y++, p_energy_prev = p_energy, p_energy += i_src_pitch,
262 p_grad += i_src_pitch )
264 /* Compute line y's minimum energy value for paths ending on
267 p_energy[x] = min( p_energy_prev[x ]+p_grad[x ],
268 p_energy_prev[x+1]+p_grad[x+1] );
269 for( x = 2; x < i_src_visible - 2; x++ )
271 p_energy[x] = min3( p_energy_prev[x-1]+p_grad[x-1],
272 p_energy_prev[x ]+p_grad[x ],
273 p_energy_prev[x+1]+p_grad[x+1] );
275 p_energy[x] = min( p_energy_prev[x-1]+p_grad[x-1],
276 p_energy_prev[x ]+p_grad[x ] );
279 int max = p_energy[1];
280 for( x = 1; x < i_src_visible - 1; x++ )
281 if( p_energy[x] > max ) max = p_energy[x];
282 for( x = 1; x < i_src_visible - 1; x++ )
283 p_outpix[y*i_src_pitch+x] = p_energy[x]*0xff/max;
287 /* Find the minimum energy point on the last line */
289 p_energy -= i_src_pitch;
290 p_grad -= i_src_pitch;
294 for( x = 1; x < i_src_visible - 1; x++ )
296 if( p_energy[x] < m )
304 p_outpix[y*i_src_pitch+xmin] = 0xff;
305 p_outpix[(y+1)*i_src_pitch+xmin] = 0xff;
307 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
308 memmove( p_outpix+(y+1)*i_src_pitch+xmin, p_outpix+(y+1)*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
311 p_energy -= i_src_pitch;
312 for( ; y>1; y--, p_energy -= i_src_pitch, p_grad -= i_src_pitch )
314 if( m != p_energy[xmin]+p_grad[xmin] )
316 if( xmin > 1 && m == p_energy[xmin-1]+p_grad[xmin-1] )
320 else if( xmin < i_src_visible - 2 && m == p_energy[xmin+1]+p_grad[xmin+1] )
326 printf("Alarm! %d\n" ,y);
332 p_outpix[y*i_src_pitch+xmin] = 0xff;
334 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
338 p_outpix[y*i_src_pitch+xmin] = 0xff;
340 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
344 p_outpix[y*i_src_pitch+xmin] = 0xff;
346 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
349 #if defined( DRAW_SEAM )
350 return i_src_visible;
352 return i_src_visible-1;
356 static inline int min3( int a, int b, int c )
360 if( a < c ) return a;
363 if( b < c ) return b;
366 static inline int min( int a, int b )
368 return a < b ? a : b;
371 static int CropCallback( vlc_object_t *p_this, char const *psz_var,
372 vlc_value_t oldval, vlc_value_t newval,
375 filter_sys_t *p_sys = (filter_sys_t *)p_data;
376 p_sys->i_crop = newval.i_int;