]> git.sesse.net Git - mlt/blob - src/modules/core/filter_crop.c
Add support for cropping RGB(A).
[mlt] / src / modules / core / filter_crop.c
1 /*
2  * filter_crop.c -- cropping filter
3  * Copyright (C) 2009 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_log.h>
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <math.h>
29
30 static void crop( uint8_t *src, uint8_t *dest, int bpp, int width, int height, int left, int right, int top, int bottom )
31 {
32         int stride = ( width - left - right ) * bpp;
33         int y      = height - top - bottom + 1;
34         uint8_t *s = &src[ ( ( top * width ) + left ) * bpp ];
35
36         while ( --y )
37         {
38                 memcpy( dest, s, stride );
39                 dest += stride;
40                 s += stride + ( right + left ) * bpp;
41         }
42 }
43
44 /** Do it :-).
45 */
46
47 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
48 {
49         int error = 0;
50
51         // Get the properties from the frame
52         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
53
54         // Correct Width/height if necessary
55         if ( *width == 0 || *height == 0 )
56         {
57                 *width  = mlt_properties_get_int( properties, "normalised_width" );
58                 *height = mlt_properties_get_int( properties, "normalised_height" );
59         }
60
61         int left    = mlt_properties_get_int( properties, "crop.left" );
62         int right   = mlt_properties_get_int( properties, "crop.right" );
63         int top     = mlt_properties_get_int( properties, "crop.top" );
64         int bottom  = mlt_properties_get_int( properties, "crop.bottom" );
65
66         // Now get the image
67         error = mlt_frame_get_image( this, image, format, width, height, writable );
68
69         int owidth  = *width - left - right;
70         int oheight = *height - top - bottom;
71         owidth = owidth < 0 ? 0 : owidth;
72         oheight = oheight < 0 ? 0 : oheight;
73
74         if ( ( owidth != *width || oheight != *height ) &&
75                 error == 0 && *image != NULL && owidth > 0 && oheight > 0 )
76         {
77                 int bpp;
78
79                 switch ( *format )
80                 {
81                         case mlt_image_yuv422:
82                                 bpp = 2;
83                                 break;
84                         case mlt_image_rgb24:
85                                 bpp = 3;
86                                 break;
87                         case mlt_image_rgb24a:
88                         case mlt_image_opengl:
89                                 bpp = 4;
90                                 break;
91                         default:
92                                 // XXX: we only know how to crop packed formats
93                                 return 1;
94                 }
95
96                 // Provides a manual override for misreported field order
97                 if ( mlt_properties_get( properties, "meta.top_field_first" ) )
98                 {
99                         mlt_properties_set_int( properties, "top_field_first", mlt_properties_get_int( properties, "meta.top_field_first" ) );
100                         mlt_properties_set_int( properties, "meta.top_field_first", 0 );
101                 }
102
103                 if ( top % 2 )
104                         mlt_properties_set_int( properties, "top_field_first", !mlt_properties_get_int( properties, "top_field_first" ) );
105                 
106                 // Create the output image
107                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * bpp );
108                 if ( output )
109                 {
110                         // Call the generic resize
111                         crop( *image, output, bpp, *width, *height, left, right, top, bottom );
112
113                         // Now update the frame
114                         *image = output;
115                         mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
116                         mlt_properties_set_int( properties, "width", owidth );
117                         mlt_properties_set_int( properties, "height", oheight );
118                 }
119
120                 // We should resize the alpha too
121                 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
122                 if ( alpha != NULL )
123                 {
124                         uint8_t *newalpha = mlt_pool_alloc( owidth * oheight );
125                         if ( newalpha )
126                         {
127                                 crop( alpha, newalpha, 1, *width, *height, left, right, top, bottom );
128                                 mlt_properties_set_data( properties, "alpha", newalpha, owidth * oheight, ( mlt_destructor )mlt_pool_release, NULL );
129                                 this->get_alpha_mask = NULL;
130                         }
131                 }
132                 *width = owidth;
133                 *height = oheight;
134         }
135
136         return error;
137 }
138
139 /** Filter processing.
140 */
141
142 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
143 {
144         if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "active" ) )
145         {
146                 // Push the get_image method on to the stack
147                 mlt_frame_push_get_image( frame, filter_get_image );
148         }
149         else
150         {
151                 mlt_properties filter_props = MLT_FILTER_PROPERTIES( this );
152                 mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
153                 int left   = mlt_properties_get_int( filter_props, "left" );
154                 int right  = mlt_properties_get_int( filter_props, "right" );
155                 int top    = mlt_properties_get_int( filter_props, "top" );
156                 int bottom = mlt_properties_get_int( filter_props, "bottom" );
157                 int width  = mlt_properties_get_int( frame_props, "real_width" );
158                 int height = mlt_properties_get_int( frame_props, "real_height" );
159
160                 left  -= left % 2;
161                 right -= right % 2;
162                 if ( width - left - right < 8 )
163                         left = right = 0;
164                 if ( height - top - bottom < 8 )
165                         top = bottom = 0;
166                 mlt_properties_set_int( frame_props, "crop.left", left );
167                 mlt_properties_set_int( frame_props, "crop.right", right );
168                 mlt_properties_set_int( frame_props, "crop.top", top );
169                 mlt_properties_set_int( frame_props, "crop.bottom", bottom );
170                 mlt_properties_set_int( frame_props, "real_width", width - left - right );
171                 mlt_properties_set_int( frame_props, "real_height", height - top - bottom );
172         }
173         return frame;
174 }
175
176 /** Constructor for the filter.
177 */
178
179 mlt_filter filter_crop_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
180 {
181         mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
182         if ( mlt_filter_init( this, this ) == 0 )
183         {
184                 this->process = filter_process;
185                 if ( arg )
186                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "active", atoi( arg ) );
187         }
188         return this;
189 }