Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion. Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules. |
27th July 2003, 09:04 | #1 | Link |
AviSynth Enthusiast
Join Date: Jul 2002
Location: California, U.S.
Posts: 1,267
|
Caveats of using Trim in functions
I was looking through some of the shared functions, and I saw a few cases where Trim isn't used very carefully. IMO, Trim's syntax--intended for editing convenience--is tricky to use robustly for general-purpose, user-defined functions.
For example, let's say we wanted to write our own version of ApplyRange. Here's the naive approach: Code:
# ApplyRange2 # # A version of ApplyRange that works with Avisynth 2.0x and that allows # named arguments and filters that take no arguments. # # USAGE: # c.ApplyRange2(123, 456, "Tweak(sat=1.1)") # function ApplyRange2(clip c, int start, int end, string thunk) { # NOTE: this version isn't very safe! seg1 = c.Trim(0, start - 1) seg2 = c.Trim(start, end) # filter will be applied to frames in the # range [start, end] seg3 = c.Trim(end + 1, 0) seg2 = Eval("seg2." + thunk) return seg1 + seg2 + seg3 }
(I'm not sure why, but c.Trim(c.FrameCount() + N, 0) returns the last frame of c instead of an error or instead of a 0-frame clip.) Okay, so what can we do? We can simplify Trim a lot by:
[Edit: code moved to <http://www.avisynth.org/stickboy/>] Now we safely can define ApplyRange2 as: Code:
function ApplyRange2(clip c, int start, int end, string thunk) { seg1 = c.Trim2(0, start) seg2 = c.Trim2(start, end + 1) # filter will be applied to frames in the # range [start, end] seg3 = c.Trim2(end + 1) seg2 = Eval("seg2." + thunk) return seg1 + seg2 + seg3 } Greatly relaxed the bounds-checking for Trim2; now allows start/end values that go past the end of the clip. Edit: (2003-08-20): Oops, I broke Trim2's support for negative values of end. Should be fixed now, hopefully without breaking anything else. Last edited by stickboy; 27th November 2003 at 01:47. |
Thread Tools | Search this Thread |
Display Modes | |
|
|