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_common.h>
34 #include <vlc_plugin.h>
38 #include "vlc_filter.h"
39 #include "filter_picture.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Create ( vlc_object_t * );
47 static void Destroy ( vlc_object_t * );
49 static picture_t *Filter( filter_t *, picture_t * );
50 static int CropCallback( vlc_object_t *, char const *,
51 vlc_value_t, vlc_value_t,
54 static void FilterSeamCarving( filter_t *, picture_t *, picture_t * );
56 /*****************************************************************************
58 *****************************************************************************/
60 #define FILTER_PREFIX "seamcarving-"
63 set_description( N_("Seam Carving video filter") );
64 set_shortname( N_( "Seam Carving" ));
65 set_capability( "video filter2", 0 );
66 set_category( CAT_VIDEO );
67 set_subcategory( SUBCAT_VIDEO_VFILTER );
69 set_callbacks( Create, Destroy );
80 static int Create( vlc_object_t *p_this )
82 filter_t *p_filter = (filter_t *)p_this;
84 /* Allocate structure */
85 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
86 if( p_filter->p_sys == NULL )
89 p_filter->pf_video_filter = Filter;
90 p_filter->p_sys->p_energy = NULL;
91 p_filter->p_sys->p_grad = NULL;
92 p_filter->p_sys->i_crop = 0;
94 var_Create( p_filter, "crop", VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
95 var_AddCallback( p_filter, "crop", CropCallback, p_filter->p_sys );
100 static void Destroy( vlc_object_t *p_this )
102 filter_t *p_filter = (filter_t *)p_this;
104 free( p_filter->p_sys->p_energy );
105 free( p_filter->p_sys->p_grad );
107 free( p_filter->p_sys );
110 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
114 if( !p_pic ) return NULL;
116 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
119 msg_Warn( p_filter, "can't get output picture" );
120 if( p_pic->pf_release )
121 p_pic->pf_release( p_pic );
125 FilterSeamCarving( p_filter, p_pic, p_outpic );
127 return CopyInfoAndRelease( p_outpic, p_pic );
130 static inline int my_min3( int a, int b, int c );
131 static inline int my_min( int a, int b );
132 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible );
134 //#define DRAW_GRADIENT
135 //#define DRAW_ENERGY
138 static void FilterSeamCarving( filter_t *p_filter, picture_t *p_inpic,
139 picture_t *p_outpic )
141 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
142 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
144 int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
146 if( !p_filter->p_sys->p_energy )
147 p_filter->p_sys->p_energy = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
148 if( !p_filter->p_sys->p_grad )
149 p_filter->p_sys->p_grad = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
151 //#if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
152 vlc_memcpy( p_outpic->p[Y_PLANE].p_pixels, p_inpic->p[Y_PLANE].p_pixels,
153 p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
155 // vlc_memset( p_outpix, 0x80,
156 // p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
158 vlc_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
159 p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
160 vlc_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
161 p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
163 #if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
164 i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
169 if( p_filter->p_sys->i_crop != 0 )
170 j = p_filter->p_sys->i_crop;
171 for( i = 0; i < j; i++ )
172 i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
174 for( y = 0; y < p_outpic->p[Y_PLANE].i_lines; y++ )
175 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 );
177 if( j == 100 ) k = -1;
182 static int ComputeGradient( filter_t *p_filter, picture_t *p_inpic, int i_src_visible )
185 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
186 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
188 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
189 int *p_grad = p_filter->p_sys->p_grad;
191 for( y = 1; y < i_num_lines - 1;
192 y++, p_grad += i_src_pitch )
194 /* Compute line y's gradient */
198 ( p_inpix[(y-1)*i_src_pitch+x-1] \
199 - p_inpix[(y+1)*i_src_pitch+x-1] ) \
200 + ( ( p_inpix[(y-1)*i_src_pitch+x] \
201 - p_inpix[(y+1)*i_src_pitch+x] ) <<1 ) \
202 + ( p_inpix[(y-1)*i_src_pitch+x+1] \
203 - p_inpix[(y+1)*i_src_pitch+x+1] ) \
207 ( p_inpix[(y-1)*i_src_pitch+x-1] \
208 - p_inpix[(y-1)*i_src_pitch+x+1] ) \
209 + ( ( p_inpix[y*i_src_pitch+x-1] \
210 - p_inpix[y*i_src_pitch+x+1] ) <<1 ) \
211 + ( p_inpix[(y+1)*i_src_pitch+x-1] \
212 - p_inpix[(y+1)*i_src_pitch+x+1] ) \
215 for( x = 1; x < i_src_visible - 1; x++ )
217 p_grad[x] = GRADx( x );
219 p_outpix[y*i_src_pitch+x] = p_grad[x]>>3;
226 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible )
229 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
230 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
232 uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;
234 int *p_energy = p_filter->p_sys->p_energy;
235 int *p_grad = p_filter->p_sys->p_grad;
237 ComputeGradient( p_filter, p_inpic, i_src_visible );
239 /** Compute the image's energy (using a sobel gradient as the base energy
241 /* Set the first energy line to 0 */
242 memset( p_energy, 0, i_src_pitch*sizeof(int));
244 int *p_energy_prev = p_energy;
245 for( y = 1; y < i_num_lines - 1;
246 y++, p_energy_prev = p_energy, p_energy += i_src_pitch,
247 p_grad += i_src_pitch )
249 /* Compute line y's minimum energy value for paths ending on
252 p_energy[x] = my_min( p_energy_prev[x ]+p_grad[x ],
253 p_energy_prev[x+1]+p_grad[x+1] );
254 for( x = 2; x < i_src_visible - 2; x++ )
256 p_energy[x] = my_min3( p_energy_prev[x-1]+p_grad[x-1],
257 p_energy_prev[x ]+p_grad[x ],
258 p_energy_prev[x+1]+p_grad[x+1] );
260 p_energy[x] = my_min( p_energy_prev[x-1]+p_grad[x-1],
261 p_energy_prev[x ]+p_grad[x ] );
264 int max = p_energy[1];
265 for( x = 1; x < i_src_visible - 1; x++ )
266 if( p_energy[x] > max ) max = p_energy[x];
267 for( x = 1; x < i_src_visible - 1; x++ )
268 p_outpix[y*i_src_pitch+x] = p_energy[x]*0xff/max;
272 /* Find the minimum energy point on the last line */
274 p_energy -= i_src_pitch;
275 p_grad -= i_src_pitch;
279 for( x = 1; x < i_src_visible - 1; x++ )
281 if( p_energy[x] < m )
289 p_outpix[y*i_src_pitch+xmin] = 0xff;
290 p_outpix[(y+1)*i_src_pitch+xmin] = 0xff;
292 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
293 memmove( p_outpix+(y+1)*i_src_pitch+xmin, p_outpix+(y+1)*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
296 p_energy -= i_src_pitch;
297 for( ; y>1; y--, p_energy -= i_src_pitch, p_grad -= i_src_pitch )
299 if( m != p_energy[xmin]+p_grad[xmin] )
301 if( xmin > 1 && m == p_energy[xmin-1]+p_grad[xmin-1] )
305 else if( xmin < i_src_visible - 2 && m == p_energy[xmin+1]+p_grad[xmin+1] )
311 printf("Alarm! %d\n" ,y);
317 p_outpix[y*i_src_pitch+xmin] = 0xff;
319 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
323 p_outpix[y*i_src_pitch+xmin] = 0xff;
325 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
329 p_outpix[y*i_src_pitch+xmin] = 0xff;
331 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
334 #if defined( DRAW_SEAM )
335 return i_src_visible;
337 return i_src_visible-1;
341 static inline int my_min3( int a, int b, int c )
345 if( a < c ) return a;
348 if( b < c ) return b;
351 static inline int my_min( int a, int b )
353 return a < b ? a : b;
356 static int CropCallback( vlc_object_t *p_this, char const *psz_var,
357 vlc_value_t oldval, vlc_value_t newval,
360 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
361 filter_sys_t *p_sys = (filter_sys_t *)p_data;
362 p_sys->i_crop = newval.i_int;