# AVSLib :: 'filters' package :: 'multiedit' module
# Copyright (c) 2005 George Zarkadas (gzarkadas@users.sourceforge.net)

# This program is free software; you can redistribute it and/or modify it under the terms of 
# the GNU General Public License as published by the Free Software Foundation; either 
# version 2 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program; 
# if not, write to the "Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA  02111-1307  USA"

__lib__ = "avslib" #$$ [__lib__ = "avslib-debug"]

DefineModule(__lib__, "filters", "edit")

global __load__ = Eval("""
	LoadModule(__lib__, "base", "core")
	LoadModule(__lib__, "array", "operators")
	LoadModule(__lib__, "filters", "edit")
	""")

# Trims a number of ranges of frames [fs..fe) and returns them as a single clip.
# joining is performed by EditJoin, supporting all of its features.
# If 'fe' is a scalar value:
#     Assume it is the number of frames to extract from each range, 
#     starting from fs, ie the ith range will be [fs(i)..fs(i)+fe).
# Else:
#     Assume it is an array defining the end-frames of each range (actually the
#     first frames past the ranges), ie the ith range will be [fs(i)..fe(i)).

	Function __edit_dotrim(int fs, int fe, clip c)
	{
		return c.EditTrim(fs, fe)
	}

Function EditTrimRange(clip c, string fs, val "fe", int "op", string "extra_info")
{
	# if fe is not supplied extract one frame from each range 
	# (effectively, just the frames supplied in fs)
	fe = Default(fe, 1)
	op = Default(op, EDOP_ADD)
	extra_info = Default(extra_info, "")
	_fe = fe.IsString ? fe : fs.ArrayOpValue(fe, "+")
	# 1st global is needed for ArrayOpArrayFunc
	# 2nd global is needed because ArraySum chokes when strings with 
	# keyword arguments that contain strings are passed in ( Eval() 
	# cannot handle them correctly).
	global __edit_tr_tmp = c
	global __edit_tr_xti = extra_info
	parts = ArrayOpArrayFunc(fs, _fe, "__edit_dotrim", "__edit_tr_tmp")
	# free global copy of c to reclaim memory (hopefully)
	global __edit_tr_tmp = Null(c)
	return parts.ArraySum(sum_func="EditJoin", \
		sum_args="op=" + String(op) + (extra_info == "" \
		? "" : ",extra_info=__edit_tr_xti") )
}
