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 *****************************************************************************/
34 #include <vlc_plugin.h>
38 #include "vlc_filter.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Create ( vlc_object_t * );
46 static void Destroy ( vlc_object_t * );
48 static picture_t *Filter( filter_t *, picture_t * );
49 static int CropCallback( vlc_object_t *, char const *,
50 vlc_value_t, vlc_value_t,
53 static void FilterSeamCarving( filter_t *, picture_t *, picture_t * );
55 /*****************************************************************************
57 *****************************************************************************/
59 #define FILTER_PREFIX "seamcarving-"
62 set_description( N_("Seam Carving video filter") );
63 set_shortname( N_( "Seam Carving" ));
64 set_capability( "video filter2", 0 );
65 set_category( CAT_VIDEO );
66 set_subcategory( SUBCAT_VIDEO_VFILTER );
68 set_callbacks( Create, Destroy );
79 static int Create( vlc_object_t *p_this )
81 filter_t *p_filter = (filter_t *)p_this;
83 /* Allocate structure */
84 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
85 if( p_filter->p_sys == NULL )
87 msg_Err( p_filter, "out of memory" );
91 p_filter->pf_video_filter = Filter;
92 p_filter->p_sys->p_energy = NULL;
93 p_filter->p_sys->p_grad = NULL;
94 p_filter->p_sys->i_crop = 0;
96 var_Create( p_filter, "crop", VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
97 var_AddCallback( p_filter, "crop", CropCallback, p_filter->p_sys );
102 static void Destroy( vlc_object_t *p_this )
104 filter_t *p_filter = (filter_t *)p_this;
106 free( p_filter->p_sys->p_energy );
107 free( p_filter->p_sys->p_grad );
109 free( p_filter->p_sys );
112 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
116 if( !p_pic ) return NULL;
118 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
121 msg_Warn( p_filter, "can't get output picture" );
122 if( p_pic->pf_release )
123 p_pic->pf_release( p_pic );
127 FilterSeamCarving( p_filter, p_pic, p_outpic );
129 p_outpic->date = p_pic->date;
130 p_outpic->b_force = p_pic->b_force;
131 p_outpic->i_nb_fields = p_pic->i_nb_fields;
132 p_outpic->b_progressive = p_pic->b_progressive;
133 p_outpic->b_top_field_first = p_pic->b_top_field_first;
135 if( p_pic->pf_release )
136 p_pic->pf_release( p_pic );
141 static inline int my_min3( int a, int b, int c );
142 static inline int my_min( int a, int b );
143 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible );
145 //#define DRAW_GRADIENT
146 //#define DRAW_ENERGY
149 static void FilterSeamCarving( filter_t *p_filter, picture_t *p_inpic,
150 picture_t *p_outpic )
152 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
153 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
155 int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
157 if( !p_filter->p_sys->p_energy )
158 p_filter->p_sys->p_energy = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
159 if( !p_filter->p_sys->p_grad )
160 p_filter->p_sys->p_grad = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
162 //#if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
163 vlc_memcpy( p_outpic->p[Y_PLANE].p_pixels, p_inpic->p[Y_PLANE].p_pixels,
164 p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
166 // vlc_memset( p_outpix, 0x80,
167 // p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
169 vlc_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
170 p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
171 vlc_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
172 p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
174 #if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
175 i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
180 if( p_filter->p_sys->i_crop != 0 )
181 j = p_filter->p_sys->i_crop;
182 for( i = 0; i < j; i++ )
183 i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
185 for( y = 0; y < p_outpic->p[Y_PLANE].i_lines; y++ )
186 vlc_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 );
188 if( j == 100 ) k = -1;
193 static int ComputeGradient( filter_t *p_filter, picture_t *p_inpic, int i_src_visible )
196 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
197 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
199 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
200 int *p_grad = p_filter->p_sys->p_grad;
202 for( y = 1; y < i_num_lines - 1;
203 y++, p_grad += i_src_pitch )
205 /* Compute line y's gradient */
209 ( p_inpix[(y-1)*i_src_pitch+x-1] \
210 - p_inpix[(y+1)*i_src_pitch+x-1] ) \
211 + ( ( p_inpix[(y-1)*i_src_pitch+x] \
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+1] ) \
218 ( p_inpix[(y-1)*i_src_pitch+x-1] \
219 - p_inpix[(y-1)*i_src_pitch+x+1] ) \
220 + ( ( p_inpix[y*i_src_pitch+x-1] \
221 - p_inpix[y*i_src_pitch+x+1] ) <<1 ) \
222 + ( p_inpix[(y+1)*i_src_pitch+x-1] \
223 - p_inpix[(y+1)*i_src_pitch+x+1] ) \
226 for( x = 1; x < i_src_visible - 1; x++ )
228 p_grad[x] = GRADx( x );
230 p_outpix[y*i_src_pitch+x] = p_grad[x]>>3;
237 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible )
240 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
241 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
243 uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;
245 int *p_energy = p_filter->p_sys->p_energy;
246 int *p_grad = p_filter->p_sys->p_grad;
248 ComputeGradient( p_filter, p_inpic, i_src_visible );
250 /** Compute the image's energy (using a sobel gradient as the base energy
252 /* Set the first energy line to 0 */
253 memset( p_energy, 0, i_src_pitch*sizeof(int));
255 int *p_energy_prev = p_energy;
256 for( y = 1; y < i_num_lines - 1;
257 y++, p_energy_prev = p_energy, p_energy += i_src_pitch,
258 p_grad += i_src_pitch )
260 /* Compute line y's minimum energy value for paths ending on
263 p_energy[x] = my_min( p_energy_prev[x ]+p_grad[x ],
264 p_energy_prev[x+1]+p_grad[x+1] );
265 for( x = 2; x < i_src_visible - 2; x++ )
267 p_energy[x] = my_min3( p_energy_prev[x-1]+p_grad[x-1],
268 p_energy_prev[x ]+p_grad[x ],
269 p_energy_prev[x+1]+p_grad[x+1] );
271 p_energy[x] = my_min( p_energy_prev[x-1]+p_grad[x-1],
272 p_energy_prev[x ]+p_grad[x ] );
275 int max = p_energy[1];
276 for( x = 1; x < i_src_visible - 1; x++ )
277 if( p_energy[x] > max ) max = p_energy[x];
278 for( x = 1; x < i_src_visible - 1; x++ )
279 p_outpix[y*i_src_pitch+x] = p_energy[x]*0xff/max;
283 /* Find the minimum energy point on the last line */
285 p_energy -= i_src_pitch;
286 p_grad -= i_src_pitch;
290 for( x = 1; x < i_src_visible - 1; x++ )
292 if( p_energy[x] < m )
300 p_outpix[y*i_src_pitch+xmin] = 0xff;
301 p_outpix[(y+1)*i_src_pitch+xmin] = 0xff;
303 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
304 memmove( p_outpix+(y+1)*i_src_pitch+xmin, p_outpix+(y+1)*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
307 p_energy -= i_src_pitch;
308 for( ; y>1; y--, p_energy -= i_src_pitch, p_grad -= i_src_pitch )
310 if( m != p_energy[xmin]+p_grad[xmin] )
312 if( xmin > 1 && m == p_energy[xmin-1]+p_grad[xmin-1] )
316 else if( xmin < i_src_visible - 2 && m == p_energy[xmin+1]+p_grad[xmin+1] )
322 printf("Alarm! %d\n" ,y);
328 p_outpix[y*i_src_pitch+xmin] = 0xff;
330 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
334 p_outpix[y*i_src_pitch+xmin] = 0xff;
336 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
340 p_outpix[y*i_src_pitch+xmin] = 0xff;
342 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
345 #if defined( DRAW_SEAM )
346 return i_src_visible;
348 return i_src_visible-1;
352 static inline int my_min3( int a, int b, int c )
356 if( a < c ) return a;
359 if( b < c ) return b;
362 static inline int my_min( int a, int b )
364 return a < b ? a : b;
367 static int CropCallback( vlc_object_t *p_this, char const *psz_var,
368 vlc_value_t oldval, vlc_value_t newval,
371 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
372 filter_sys_t *p_sys = (filter_sys_t *)p_data;
373 p_sys->i_crop = newval.i_int;