View Single Post
Old 26th March 2013, 09:13   #1  |  Link
fvisagie
Registered User
 
Join Date: Aug 2008
Location: Isle of Man
Posts: 588
Multiple statements on a single line

This is probably old hat to the old-timers here, but I stumbled across something quite exciting. Avisynth does support multiple statements on a single line, in contradiction to Multiple Avisynth statements on a single line can only be achieved in the context of OOP notation or embedding filters as parameters of another function.

This seems to be supported for normal statements, by Eval() etc. and opens up a whole new world (for me at least ) of things like simple conditional block statements. This is with 2.6.0, I haven't tested with earlier versions.

Here's an example of a trivial function returning multiple values

Code:
#-------------------------------------------
# ColourCoefficients 1.0.0
#   Generates specified colour matrix coefficient values
#
#   Useful when
#
#       Doing YUV<->RGB colourspace calculations or conversions
#       Doing colour coefficient calculations or conversions
#
#   ColourCoefficients(string "matrix")
#
#       <matrix>    Colour coefficient matrix which to generate coefficients for - "Rec601" / "Rec709" / "FCC", default "Rec601"
#
#   Usage
#
#       coeffs = ColourCoefficients("Rec709")   # ColourCoefficients returns a multi-statement string with matrix coefficient value assignments
#       Eval(coeffs)                            # This defines Kr containing Red channel coefficient, Kg with Green channel coefficient and Kb for Blue channel
#       y      = Kr*r + Kg*g + Kb*b
#       ...
#
#   References
#
#       http://avisynth.org/mediawiki/Color_conversions
#
#   Version history
#
#       1.0.0 Francois Visagie
#

function ColourCoefficients(string "matrix") {
matrix      = Default(matrix, "Rec601")
matrixnum   = (matrix == "Rec601") ? 0 : \
              (matrix == "Rec709") ? 1 : \
              (matrix == "FCC"   ) ? 2 : 3
Assert(matrixnum < 3, """'matrix' value """" + matrix + """" is invalid""")
# Channel coefficients          Rec.601 Rec.709 FCC
Kr          = Select(matrixnum, 0.299,  0.2125, 0.3 )
Kg          = Select(matrixnum, 0.587,  0.7154, 0.59)
Kb          = Select(matrixnum, 0.114,  0.0721, 0.11)
return("Kr=" + string(Kr) + " Kg=" + string(Kg) + " Kb=" + string(Kb))
}
To what extent has anyone else played with this, and what gotchas have been discovered so far?
fvisagie is offline   Reply With Quote