#!/usr/bin/env python

import bootstrap
import sys
import openpluginlib
import openimagelib
import openmedialib
import common
import stacks
import balance

class filter_thing( openmedialib.callback_delegate ):
	def __init__( self ):
		openmedialib.callback_delegate.__init__( self )

	def assign( self, properties, position ):
		position %= 400
		if position >= 200:
			position = 200 - ( position % 200 )
		if position < 25:
			properties.get_property( "mode" ).set( "fill" )
			properties.get_property( "rx" ).set( 0.48 - 0.02 * position )
			properties.get_property( "ry" ).set( 0.48 - 0.02 * position )
			properties.get_property( "rw" ).set( 0.04 + 0.04 * position )
			properties.get_property( "rh" ).set( 0.04 + 0.04 * position )
		elif position < 50:
			properties.get_property( "mode" ).set( "fill" )
		elif position < 75:
			properties.get_property( "mix" ).set( 1.0 - 0.04 * ( position - 49 ) )
		elif position < 100:
			properties.get_property( "mix" ).set( 0.04 * ( position - 74 ) )
		elif position < 125:
			properties.get_property( "mode" ).set( "letterbox" )
		elif position < 150:
			properties.get_property( "mode" ).set( "pillarbox" )
		elif position < 175:
			properties.get_property( "mode" ).set( "fill" )
			position -= 150
			properties.get_property( "rx" ).set( 0.02 * position )
			properties.get_property( "ry" ).set( 0.02 * position )
			properties.get_property( "rw" ).set( 1.0 - 0.04 * position )
			properties.get_property( "rh" ).set( 1.0 - 0.04 * position )

openpluginlib.init( "" )

if len( sys.argv ) > 1:
	sys.argv.pop( 0 )
	rpn = stacks.stack( )
	rpn.push_args( [ "colour:", "width=320", "height=240", "sar_num=1", "sar_den=1" ] )
	rpn.push_args( sys.argv )
	rpn.push_args( [ "filter:threader" ] )
	rpn.push_args( [ "filter:composite", "@0.out=@1.rpn_length" ] )
	rpn.push_callback( filter_thing( ) )
	input = rpn.pop( )
	if input is not None:
		balance.activate( input )
		common.play( input )
		balance.deactivate( input )
else:
	print "Usage: resize.py <input> ..."

openpluginlib.uninit( )
