]> git.sesse.net Git - mlt/blob - src/modules/gtk2/filter_rescale.c
7639b05bed9141d3fe268e5c512a64da290868a9
[mlt] / src / modules / gtk2 / filter_rescale.c
1 /*
2  * filter_rescale.c -- scale the producer video frame size to match the consumer
3  * Copyright (C) 2003-2004 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 "pixops.h"
22
23 #include <framework/mlt_filter.h>
24 #include <framework/mlt_frame.h>
25 #include <framework/mlt_factory.h>
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <gdk-pixbuf/gdk-pixbuf.h>
31
32 static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight )
33 {
34         // Get the properties
35         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
36
37         // Get the requested interpolation method
38         char *interps = mlt_properties_get( properties, "rescale.interp" );
39
40         // Convert to the GTK flag
41         int interp = PIXOPS_INTERP_BILINEAR;
42
43         if ( strcmp( interps, "nearest" ) == 0 )
44                 interp = PIXOPS_INTERP_NEAREST;
45         else if ( strcmp( interps, "tiles" ) == 0 )
46                 interp = PIXOPS_INTERP_TILES;
47         else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
48                 interp = PIXOPS_INTERP_HYPER;
49
50         // Carry out the rescaling
51         switch ( *format )
52         {
53         case mlt_image_yuv422:
54         {
55                 // Create the output image
56                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
57
58                 // Calculate strides
59                 int istride = iwidth * 2;
60                 int ostride = owidth * 2;
61
62                 yuv422_scale_simple( output, owidth, oheight, ostride, *image, iwidth, iheight, istride, interp );
63                 
64                 // Now update the frame
65                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
66                 mlt_properties_set_int( properties, "width", owidth );
67                 mlt_properties_set_int( properties, "height", oheight );
68
69                 // Return the output
70                 *image = output;
71                 break;
72         }
73         case mlt_image_rgb24:
74         case mlt_image_rgb24a:
75         case mlt_image_opengl:
76         {
77                 int bpp = ( *format == mlt_image_rgb24 ? 3 : 4 );
78
79                 // Create the output image
80                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * bpp );
81
82                 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
83                 {
84                         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data( *image, GDK_COLORSPACE_RGB,
85                                 ( *format == mlt_image_rgb24a || *format == mlt_image_opengl ), 8, iwidth, iheight,
86                                 iwidth * bpp, NULL, NULL );
87                         GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf, owidth, oheight, interp );
88                         g_object_unref( pixbuf );
89
90                         int src_stride = gdk_pixbuf_get_rowstride( scaled );
91                         int dst_stride = owidth * bpp;
92                         if ( src_stride != dst_stride )
93                         {
94                                 int y = oheight;
95                                 uint8_t *src = gdk_pixbuf_get_pixels( scaled );
96                                 uint8_t *dst = output;
97                                 while ( y-- )
98                                 {
99                                         memcpy( dst, src, dst_stride );
100                                         dst += dst_stride;
101                                         src += src_stride;
102                                 }
103                         }
104                         else
105                         {
106                                 memcpy( output, gdk_pixbuf_get_pixels( scaled ), owidth * oheight * bpp );
107                         }
108
109                         g_object_unref( scaled );
110
111                         // Now update the frame
112                         mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * bpp, ( mlt_destructor )mlt_pool_release, NULL );
113                         mlt_properties_set_int( properties, "width", owidth );
114                         mlt_properties_set_int( properties, "height", oheight );
115         
116                         // Return the output
117                         *image = output;
118                 }
119                 break;
120         }
121         default:
122                 break;
123         }
124
125         return 0;
126 }
127
128 /** Constructor for the filter.
129 */
130
131 mlt_filter filter_rescale_init( mlt_profile profile, char *arg )
132 {
133         // Create a new scaler
134         mlt_filter this = mlt_factory_filter( profile, "rescale", arg );
135
136         // If successful, then initialise it
137         if ( this != NULL )
138         {
139                 // Get the properties
140                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
141
142                 // Set the inerpolation
143                 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
144
145                 // Set the method
146                 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );
147         }
148
149         return this;
150 }
151