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 )
88 msg_Err( p_filter, "out of memory" );
92 p_filter->pf_video_filter = Filter;
93 p_filter->p_sys->p_energy = NULL;
94 p_filter->p_sys->p_grad = NULL;
95 p_filter->p_sys->i_crop = 0;
97 var_Create( p_filter, "crop", VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
98 var_AddCallback( p_filter, "crop", CropCallback, p_filter->p_sys );
103 static void Destroy( vlc_object_t *p_this )
105 filter_t *p_filter = (filter_t *)p_this;
107 free( p_filter->p_sys->p_energy );
108 free( p_filter->p_sys->p_grad );
110 free( p_filter->p_sys );
113 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
117 if( !p_pic ) return NULL;
119 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
122 msg_Warn( p_filter, "can't get output picture" );
123 if( p_pic->pf_release )
124 p_pic->pf_release( p_pic );
128 FilterSeamCarving( p_filter, p_pic, p_outpic );
130 return CopyInfoAndRelease( p_outpic, p_pic );
133 static inline int my_min3( int a, int b, int c );
134 static inline int my_min( int a, int b );
135 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible );
137 //#define DRAW_GRADIENT
138 //#define DRAW_ENERGY
141 static void FilterSeamCarving( filter_t *p_filter, picture_t *p_inpic,
142 picture_t *p_outpic )
144 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
145 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
147 int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
149 if( !p_filter->p_sys->p_energy )
150 p_filter->p_sys->p_energy = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
151 if( !p_filter->p_sys->p_grad )
152 p_filter->p_sys->p_grad = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
154 //#if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
155 vlc_memcpy( p_outpic->p[Y_PLANE].p_pixels, p_inpic->p[Y_PLANE].p_pixels,
156 p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
158 // vlc_memset( p_outpix, 0x80,
159 // p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
161 vlc_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
162 p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
163 vlc_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
164 p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
166 #if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
167 i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
172 if( p_filter->p_sys->i_crop != 0 )
173 j = p_filter->p_sys->i_crop;
174 for( i = 0; i < j; i++ )
175 i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
177 for( y = 0; y < p_outpic->p[Y_PLANE].i_lines; y++ )
178 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 );
180 if( j == 100 ) k = -1;
185 static int ComputeGradient( filter_t *p_filter, picture_t *p_inpic, int i_src_visible )
188 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
189 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
191 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
192 int *p_grad = p_filter->p_sys->p_grad;
194 for( y = 1; y < i_num_lines - 1;
195 y++, p_grad += i_src_pitch )
197 /* Compute line y's gradient */
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] \
204 - p_inpix[(y+1)*i_src_pitch+x] ) <<1 ) \
205 + ( p_inpix[(y-1)*i_src_pitch+x+1] \
206 - 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+1] ) \
212 + ( ( p_inpix[y*i_src_pitch+x-1] \
213 - p_inpix[y*i_src_pitch+x+1] ) <<1 ) \
214 + ( p_inpix[(y+1)*i_src_pitch+x-1] \
215 - p_inpix[(y+1)*i_src_pitch+x+1] ) \
218 for( x = 1; x < i_src_visible - 1; x++ )
220 p_grad[x] = GRADx( x );
222 p_outpix[y*i_src_pitch+x] = p_grad[x]>>3;
229 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible )
232 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
233 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
235 uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;
237 int *p_energy = p_filter->p_sys->p_energy;
238 int *p_grad = p_filter->p_sys->p_grad;
240 ComputeGradient( p_filter, p_inpic, i_src_visible );
242 /** Compute the image's energy (using a sobel gradient as the base energy
244 /* Set the first energy line to 0 */
245 memset( p_energy, 0, i_src_pitch*sizeof(int));
247 int *p_energy_prev = p_energy;
248 for( y = 1; y < i_num_lines - 1;
249 y++, p_energy_prev = p_energy, p_energy += i_src_pitch,
250 p_grad += i_src_pitch )
252 /* Compute line y's minimum energy value for paths ending on
255 p_energy[x] = my_min( p_energy_prev[x ]+p_grad[x ],
256 p_energy_prev[x+1]+p_grad[x+1] );
257 for( x = 2; x < i_src_visible - 2; x++ )
259 p_energy[x] = my_min3( p_energy_prev[x-1]+p_grad[x-1],
260 p_energy_prev[x ]+p_grad[x ],
261 p_energy_prev[x+1]+p_grad[x+1] );
263 p_energy[x] = my_min( p_energy_prev[x-1]+p_grad[x-1],
264 p_energy_prev[x ]+p_grad[x ] );
267 int max = p_energy[1];
268 for( x = 1; x < i_src_visible - 1; x++ )
269 if( p_energy[x] > max ) max = p_energy[x];
270 for( x = 1; x < i_src_visible - 1; x++ )
271 p_outpix[y*i_src_pitch+x] = p_energy[x]*0xff/max;
275 /* Find the minimum energy point on the last line */
277 p_energy -= i_src_pitch;
278 p_grad -= i_src_pitch;
282 for( x = 1; x < i_src_visible - 1; x++ )
284 if( p_energy[x] < m )
292 p_outpix[y*i_src_pitch+xmin] = 0xff;
293 p_outpix[(y+1)*i_src_pitch+xmin] = 0xff;
295 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
296 memmove( p_outpix+(y+1)*i_src_pitch+xmin, p_outpix+(y+1)*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
299 p_energy -= i_src_pitch;
300 for( ; y>1; y--, p_energy -= i_src_pitch, p_grad -= i_src_pitch )
302 if( m != p_energy[xmin]+p_grad[xmin] )
304 if( xmin > 1 && m == p_energy[xmin-1]+p_grad[xmin-1] )
308 else if( xmin < i_src_visible - 2 && m == p_energy[xmin+1]+p_grad[xmin+1] )
314 printf("Alarm! %d\n" ,y);
320 p_outpix[y*i_src_pitch+xmin] = 0xff;
322 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
326 p_outpix[y*i_src_pitch+xmin] = 0xff;
328 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
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) );
337 #if defined( DRAW_SEAM )
338 return i_src_visible;
340 return i_src_visible-1;
344 static inline int my_min3( int a, int b, int c )
348 if( a < c ) return a;
351 if( b < c ) return b;
354 static inline int my_min( int a, int b )
356 return a < b ? a : b;
359 static int CropCallback( vlc_object_t *p_this, char const *psz_var,
360 vlc_value_t oldval, vlc_value_t newval,
363 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
364 filter_sys_t *p_sys = (filter_sys_t *)p_data;
365 p_sys->i_crop = newval.i_int;