+#include <math.h>
+
+/** Swapbytes inline.
+*/
+
+static inline void swap_bytes( uint8_t *upper, uint8_t *lower )
+{
+ uint8_t t = *lower;
+ *lower = *upper;
+ *upper = t;
+}
+
+static uint8_t *resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight, uint8_t alpha_value )
+{
+ uint8_t *output = NULL;
+
+ if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
+ {
+ uint8_t *out_line;
+ int offset_x = ( owidth - iwidth ) / 2;
+ int offset_y = ( oheight - iheight ) / 2;
+ int iused = iwidth;
+
+ output = mlt_pool_alloc( owidth * oheight );
+ memset( output, alpha_value, owidth * oheight );
+
+ offset_x -= offset_x % 2;
+
+ out_line = output + offset_y * owidth;
+ out_line += offset_x;
+
+ // Loop for the entirety of our output height.
+ while ( iheight -- )
+ {
+ // We're in the input range for this row.
+ memcpy( out_line, input, iused );
+
+ // Move to next input line
+ input += iwidth;
+
+ // Move to next output line
+ out_line += owidth;
+ }
+ }
+
+ return output;
+}
+
+static void resize_image( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight, int bpp )
+{
+ // Calculate strides
+ int istride = iwidth * bpp;
+ int ostride = owidth * bpp;
+ int offset_x = ( owidth - iwidth ) / 2 * bpp;
+ int offset_y = ( oheight - iheight ) / 2;
+ uint8_t *in_line = input;
+ uint8_t *out_line;
+ int size = owidth * oheight;
+ uint8_t *p = output;
+
+ // Optimisation point
+ if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
+ {
+ return;
+ }
+ else if ( iwidth == owidth && iheight == oheight )
+ {
+ memcpy( output, input, iheight * istride );
+ return;
+ }
+
+ if ( bpp == 2 )
+ {
+ while( size -- )
+ {
+ *p ++ = 16;
+ *p ++ = 128;
+ }
+ offset_x -= offset_x % 4;
+ }
+ else
+ {
+ size *= bpp;
+ while ( size-- )
+ *p ++ = 0;
+ }
+
+ out_line = output + offset_y * ostride;
+ out_line += offset_x;
+
+ // Loop for the entirety of our output height.
+ while ( iheight -- )
+ {
+ // We're in the input range for this row.
+ memcpy( out_line, in_line, istride );
+
+ // Move to next input line
+ in_line += istride;
+
+ // Move to next output line
+ out_line += ostride;
+ }
+}
+
+/** A padding function for frames - this does not rescale, but simply
+ resizes.
+*/
+
+static uint8_t *frame_resize_image( mlt_frame this, int owidth, int oheight, int bpp )
+{
+ // Get properties
+ mlt_properties properties = MLT_FRAME_PROPERTIES( this );
+
+ // Get the input image, width and height
+ uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
+ uint8_t *alpha = mlt_frame_get_alpha_mask( this );
+ int alpha_size = 0;
+ mlt_properties_get_data( properties, "alpha", &alpha_size );
+
+ int iwidth = mlt_properties_get_int( properties, "width" );
+ int iheight = mlt_properties_get_int( properties, "height" );
+
+ // If width and height are correct, don't do anything
+ if ( iwidth < owidth || iheight < oheight )
+ {
+ uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" );
+
+ // Create the output image
+ uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * bpp );
+
+ // Call the generic resize
+ resize_image( output, owidth, oheight, input, iwidth, iheight, bpp );
+
+ // Now update the frame
+ mlt_frame_set_image( this, output, owidth * ( oheight + 1 ) * bpp, mlt_pool_release );
+
+ // We should resize the alpha too
+ if ( alpha && alpha_size >= iwidth * iheight )
+ {
+ alpha = resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
+ if ( alpha )
+ {
+ mlt_frame_set_alpha( this, alpha, owidth * oheight, mlt_pool_release );
+ this->get_alpha_mask = NULL;
+ }
+ }
+
+ // Return the output
+ return output;
+ }
+ // No change, return input
+ return input;
+}