#!/usr/bin/env python

import bootstrap
import sys
import openpluginlib
import openimagelib
import openmedialib
import common

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

	def assign( self, properties, position ):
		offset = position % 224
		cycle = ( position / 224 ) % 2
		if cycle == 0:
			properties.get_property( "u" ).set( 16 + offset )
			properties.get_property( "v" ).set( 240 - offset )
		else:
			properties.get_property( "u" ).set( 240 - offset )
			properties.get_property( "v" ).set( 16 + offset )

openpluginlib.init( "" )

if len( sys.argv ) > 1:
	sys.argv.pop( 0 )
	clips = common.parse_args( sys.argv )
	for clip in clips:
		input = common.input_wrapper( openmedialib.create_input( clip[ 0 ] ) )
		if input.valid( ):
			input_name = clip.pop( 0 )
			input.init( clip )
			filter = openmedialib.create_filter( "chroma" )
			filter.connect( input, 0 )
			filter.register( filter_thing( ) )
			frame = filter.fetch( )
			if frame is not None:
				preview = common.preview( frame )
				i = 0
				while i < filter.get_frames( ):
					filter.seek( i, False )
					frame = filter.fetch( )
					if not preview.push( frame ): break
					i += 1
				preview.complete( )
else:
	print "Usage: filter.py <input> ..."
	
openpluginlib.uninit( )
