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 picture_Release( p_pic );
124 FilterSeamCarving( p_filter, p_pic, p_outpic );
126 return CopyInfoAndRelease( p_outpic, p_pic );
129 static inline int my_min3( int a, int b, int c );
130 static inline int my_min( int a, int b );
131 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible );
133 //#define DRAW_GRADIENT
134 //#define DRAW_ENERGY
137 static void FilterSeamCarving( filter_t *p_filter, picture_t *p_inpic,
138 picture_t *p_outpic )
140 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
141 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
143 int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
145 if( !p_filter->p_sys->p_energy )
146 p_filter->p_sys->p_energy = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
147 if( !p_filter->p_sys->p_grad )
148 p_filter->p_sys->p_grad = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
150 //#if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
151 vlc_memcpy( p_outpic->p[Y_PLANE].p_pixels, p_inpic->p[Y_PLANE].p_pixels,
152 p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
154 // vlc_memset( p_outpix, 0x80,
155 // p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
157 vlc_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
158 p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
159 vlc_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
160 p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
162 #if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
163 i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
168 if( p_filter->p_sys->i_crop != 0 )
169 j = p_filter->p_sys->i_crop;
170 for( i = 0; i < j; i++ )
171 i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
173 for( y = 0; y < p_outpic->p[Y_PLANE].i_lines; y++ )
174 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 );
176 if( j == 100 ) k = -1;
181 static int ComputeGradient( filter_t *p_filter, picture_t *p_inpic, int i_src_visible )
184 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
185 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
187 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
188 int *p_grad = p_filter->p_sys->p_grad;
190 for( y = 1; y < i_num_lines - 1;
191 y++, p_grad += i_src_pitch )
193 /* Compute line y's gradient */
197 ( p_inpix[(y-1)*i_src_pitch+x-1] \
198 - p_inpix[(y+1)*i_src_pitch+x-1] ) \
199 + ( ( p_inpix[(y-1)*i_src_pitch+x] \
200 - p_inpix[(y+1)*i_src_pitch+x] ) <<1 ) \
201 + ( p_inpix[(y-1)*i_src_pitch+x+1] \
202 - p_inpix[(y+1)*i_src_pitch+x+1] ) \
206 ( p_inpix[(y-1)*i_src_pitch+x-1] \
207 - p_inpix[(y-1)*i_src_pitch+x+1] ) \
208 + ( ( p_inpix[y*i_src_pitch+x-1] \
209 - p_inpix[y*i_src_pitch+x+1] ) <<1 ) \
210 + ( p_inpix[(y+1)*i_src_pitch+x-1] \
211 - p_inpix[(y+1)*i_src_pitch+x+1] ) \
214 for( x = 1; x < i_src_visible - 1; x++ )
216 p_grad[x] = GRADx( x );
218 p_outpix[y*i_src_pitch+x] = p_grad[x]>>3;
225 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible )
228 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
229 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
231 uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;
233 int *p_energy = p_filter->p_sys->p_energy;
234 int *p_grad = p_filter->p_sys->p_grad;
236 ComputeGradient( p_filter, p_inpic, i_src_visible );
238 /** Compute the image's energy (using a sobel gradient as the base energy
240 /* Set the first energy line to 0 */
241 memset( p_energy, 0, i_src_pitch*sizeof(int));
243 int *p_energy_prev = p_energy;
244 for( y = 1; y < i_num_lines - 1;
245 y++, p_energy_prev = p_energy, p_energy += i_src_pitch,
246 p_grad += i_src_pitch )
248 /* Compute line y's minimum energy value for paths ending on
251 p_energy[x] = my_min( p_energy_prev[x ]+p_grad[x ],
252 p_energy_prev[x+1]+p_grad[x+1] );
253 for( x = 2; x < i_src_visible - 2; x++ )
255 p_energy[x] = my_min3( p_energy_prev[x-1]+p_grad[x-1],
256 p_energy_prev[x ]+p_grad[x ],
257 p_energy_prev[x+1]+p_grad[x+1] );
259 p_energy[x] = my_min( p_energy_prev[x-1]+p_grad[x-1],
260 p_energy_prev[x ]+p_grad[x ] );
263 int max = p_energy[1];
264 for( x = 1; x < i_src_visible - 1; x++ )
265 if( p_energy[x] > max ) max = p_energy[x];
266 for( x = 1; x < i_src_visible - 1; x++ )
267 p_outpix[y*i_src_pitch+x] = p_energy[x]*0xff/max;
271 /* Find the minimum energy point on the last line */
273 p_energy -= i_src_pitch;
274 p_grad -= i_src_pitch;
278 for( x = 1; x < i_src_visible - 1; x++ )
280 if( p_energy[x] < m )
288 p_outpix[y*i_src_pitch+xmin] = 0xff;
289 p_outpix[(y+1)*i_src_pitch+xmin] = 0xff;
291 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
292 memmove( p_outpix+(y+1)*i_src_pitch+xmin, p_outpix+(y+1)*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
295 p_energy -= i_src_pitch;
296 for( ; y>1; y--, p_energy -= i_src_pitch, p_grad -= i_src_pitch )
298 if( m != p_energy[xmin]+p_grad[xmin] )
300 if( xmin > 1 && m == p_energy[xmin-1]+p_grad[xmin-1] )
304 else if( xmin < i_src_visible - 2 && m == p_energy[xmin+1]+p_grad[xmin+1] )
310 printf("Alarm! %d\n" ,y);
316 p_outpix[y*i_src_pitch+xmin] = 0xff;
318 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
322 p_outpix[y*i_src_pitch+xmin] = 0xff;
324 memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
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) );
333 #if defined( DRAW_SEAM )
334 return i_src_visible;
336 return i_src_visible-1;
340 static inline int my_min3( int a, int b, int c )
344 if( a < c ) return a;
347 if( b < c ) return b;
350 static inline int my_min( int a, int b )
352 return a < b ? a : b;
355 static int CropCallback( vlc_object_t *p_this, char const *psz_var,
356 vlc_value_t oldval, vlc_value_t newval,
359 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
360 filter_sys_t *p_sys = (filter_sys_t *)p_data;
361 p_sys->i_crop = newval.i_int;