# AVSLib :: sample script # Copyright (c) 2007 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" LoadModule("avslib", "base", "core") LoadModule("avslib", "array", "core") LoadModule("avslib", "array", "operators") LoadModule("avslib", "numeric", "rounding") LoadModule("avslib", "filters", "utility") Function ImgSource(string file, clip template, int "target_w", \ int "target_h", int "frames", bool "pc_range") { frames = Default(frames, template.Framecount) target_w = Default(target_w, template.Width) target_h = Default(target_h, template.Height) pc_range = Default(pc_range, false) ret = ImageSource(file, start=0, end=frames-1).AssumeFPS(template.Framerate) ret = ret.ConvertToTarget(template).BilinearResize(target_w, target_h) return pc_range ? ret.ScaleToPC() : ret } Function MakeSolidPen(clip brush, int pen_color) { return brush.BlankClip(color=pen_color) } # create a clip (640x480, 5 sec) global clp = BlankClip(length=120, color=color_darkslateBlue, \ fps=24, width=640, height=480, pixel_type="YV12") global pen_canvas = clp.BlankClip(color=color_black, length=1) # pens draw here global brs_canvas = pen_canvas.ScaleToPC() # brushes draw here # create solid color brushes Function LoadBrush(int idx, clip tpl, int w, int h) { fname = "brush" + String(idx) + ".jpg" return ImgSource(fname, tpl, w, h, pc_range=true) } global brushes = ArrayRange(1, 6).ArrayOpFunc("LoadBrush", "clp, 24, 24") colors = "color_gold, color_beige, color_crimson, color_forestgreen, color_lightskyblue, color_firebrick" global pens = ArrayOpArrayFunc(brushes, colors, "MakeSolidPen") # define the curves (they assume a 640x480 canvas) Function NormDist(float x, float m, float s) { return (1.0/(Sqrt(2*Pi)*s))*Exp(-Pow(x-m,2)/(2*Pow(s,2))) } Function SinPulse(float x, float xc, float peak, float spread) { return peak*NormDist(x, xc, spread)*Sin((x-xc)/(2*Pi)) } Function Curve(float x) { return 240 - 120 * Sin(0.65*x*Pi/320) + SinPulse(x, 520, 14000, 120) } # Draw on x,y; x belongs to [x_start, x_end) # assumes brush, bmask have same dimensions Function DrawItems(clip pen, clip brush, int x_start, int y_start) { dx = Round(brush.Width / 2.0) dy = Round(brush.Height / 2.0) xr = x_start + Round(Rand(StrokeRadius*2) - StrokeRadius) # shuffle x,y based on radius yr = y_start + Round(Rand(StrokeRadius*2) - StrokeRadius) global pen_canvas = Overlay(pen_canvas, pen, x=xr-dx, y=yr-dy, mask=brush, opacity=StrokeOpacity) global brs_canvas = Overlay(brs_canvas, brush, x=xr-dx, y=yr-dy, mask=brush, opacity=StrokeOpacity, \ pc_range=true) return 1 } Function DrawCurve(clip c, string curve, int x_start, int x_end, int step) { dummy2 = (x_start < x_end && x_start > xlast) ? Eval(""" y_start = Round(Apply(curve, x_start)) dummy = ArrayOpArrayFunc(pens, brushes, "DrawItems", String(x_start) + "," + String(y_start)) global xlast = x_start """) : (x_start <= xlast ? Eval(""" x_start = IntBs(xlast, step) + step """) : NOP) return x_start < x_end \ ? DrawCurve(c, curve, x_start + step, x_end, step) \ : Overlay(c, pen_canvas, mask=brs_canvas, opacity=1.0, ignore_conditional=true) } # begin draw global xlast = 0 global xs = 0 global xe = 0 xd = Int(clp.Width / clp.Framecount) global xdelta = clp.Framecount * xd < clp.Width ? xd + 1 : xd global StrokeRadius = 20 global StrokeOpacity = 1.0 ScriptClip(clp, """ global xe = Min2(xe + xdelta, last.Width) DrawCurve(last, "Curve", xs, xe, 4) """) ConditionalReader("radius.txt", "StrokeRadius") ConditionalReader("opac.txt", "StrokeOpacity")