View Single Post
Old 17th May 2010, 03:32   #42  |  Link
WorBry
Registered User
 
Join Date: Jan 2004
Location: Here, there and everywhere
Posts: 1,197
Quote:
Originally Posted by Gavino View Post
I think it would make the formulae more readable and checkable if you defined the opacity expressions as variables, eg
Opacity = String(Opac)+" * x 1 "+String(Opac)+" - * + "
instead of repeating them everywhere.
So, I guess you are suggesting something like........(edited)

Edit: Script modified to incorporate changes as suggested by Gavino (below). Now Blend_MT_alpha3. The Appendix 1 'Explanation of the Blend Modes' (Post# 27) remains unchanged.
Edit2: Corrected Overlay and Softlight expressions, and changed the base clip for the BLMode2 operation from Clp1 to the BLMode product (as per post# 46 below)

Code:
function Blend_MT_alpha3(clip clp1 ,clip "clp2", string "BLMode", int "LMode", int "CMode", float "Opac", float "Opac1", float "Opac2", 
\                    bool "BL2", string "BLMode2", float "SOpac", float "SOpac1", float "SOpac2", bool "Preview", bool "GSGrad")
{

# Assembled by WorBry. Thanks to Didee for the Multiply, Screen and '128-Centred Contrast Multiply' computations, and to
# Gavino for valuable advice on syntax. 
                                          
# DESCRIPTION:

# Blend_MT aims to emulate a number of the blend modes available in commercial imaging software (After Effects etc). Whilst
# the existing core filters, Layer and Overlay, incorporate some of these modes, both require color space conversion to process 
# YV12 sources; Overlay accepts YV12 input, but internally converts to YUV 4:4:4 for processing. Blend_MT uses MaskTools v2 
# only and so allows processing exclusively in YV12. The function was created as one of set of routines to assist in the 
# simulation of certain "filmic looks", but perhaps has general application.
 
# Requires YV12 input, assumes full (pc) range 0-255 and assumes that the source is progressive (native or deinterlaced). 
# Requires MaskTools v2 only.          

# PARAMETER DEFINITIONS:
#
#Clp1          - Input clip that serves as the lower (base) clip in the specified blend operation. 
#Clp2          - Input clip that serves as the upper (overlay) clip in the specified blend operation. If not defined, clp2 
#                 defaults to clp1
#BLMode        - The specific blend operation that will be applied to the clips. At default, the operation is applied to the 
#                luma plane only; chroma is retaind from Clp1. The currently available modes are Blend, Multiply, Screen,
#                Hardlight, Overlay, Softlight, Burn, Dodge,  Reflect, Glow, Darken, Lighten, Additive, Subtractive and 
#                Difference. See Appendix 1 for explanation of these modes. 
#LMode         - Determines whether the luma plane (Y) will be processed by the blend operation (3) or retained from Clp1 (2) 
#                or Clp2 (4). Default is LMode=3. 
#CMode         - (2,3,4 or 0). Determines whether the chroma planes (U,V) will be processed by the blend operation (3), retained 
#                from Clp1 (2) retained Clp2 (4), or excluded i.e. greyscale (0). Note, aside from 'Blend' mode, Cmode=3 has 
#                limited application since the transformations cause marked U and V shifts with undesirable color casts. 
#                However, when applied to the 'contrast' modes (HardLight, Overlay, Softlight), with identical source clips, 
#                CMode=3 may produce an acceptable image in which the U and V profiles are broadened to create a richer mix of 
#                like hues. Depending on the source, this can take on a 'technicolor' film like appearance.                                        
#BL2           - True/False. Whilst the strength of the BLMode operation can be regulated by opacity adjustment, it may 
#                be desirable to apply a second blend operation to the first. True, activates this option. Default is, False. 
#BL2Mode       - Selects the second blend mode operation. All of the blend modes available in BLMode are available in BL2Mode. 
#                The BLMode2 operation is applied as a 'self-blend' i.e. the BLMode product is the top and base clip. For 
#                simplicity, the LMode and CMode settings are kept the same as for BLMode.     
#Opac          - Sets the opacity of the BLMode product relative to Clp1. Range 0.0 - 1.0. At 1.0 (default) all of the blend 
#                product is represented. 
#Opac1, Opac2  - Apply only to Hardlight, Overlay and Softlight. Opac1 sets the opacity of the BLMode product for pixel 
#                values <128 and Opac2 sets the opacity for values >128. This makes it possible to alter, in some way, 
#                the symetry of the S-curve. Note though, this can introduce some undesirable luma effects with non-identical 
#                source clips. Range 0.0 - 1.0. If not separately defined, defaults to Opac value.   
#SOpac         - Sets the opacity of the BLMode2 product relative to the BLMode product. Range 0.0 - 1.0. At 1.0 (default) all 
                  of the BLMode2 product is represented.
#SOpac1, SOpac2- Apply to Hardlight, Overlay and Softlight. SOpac1 sets the opacity of the BLMode2 product for pixel 
#                values <128 and SOpac2 sets the opacity for values >128. In this way it is possible to further 
#                manipulate the shape of the curve. Range 0.0 - 1.0. If not separately defined, default to SOpac value.  
#Preview       - True/False. True gives a composite view of the input clips and final output. Default is False. 
#GSGrad        - True/False. True demonstrates the effect of the blend operation as applied to a greyscale gradient; histogram 
#                reveals the luma curve. Default is False.
#   
#Defaults:
#clp2    = default(clp2, clp1)
BLMode  = default(BLMode, "Blend")
LMode   = default(LMode, 3) 
CMode   = default(Cmode, 2 ) 
Opac    = default(Opac,  1.0)
Opac1   = default(Opac1, Opac)
Opac2   = default(Opac2, Opac)
BL2     = default(BL2, False)
BLMode2 = default(BLMode2, "Blend")
SOpac   = default(SOpac, 1.0)
SOpac1  = default(SOpac1, SOpac)
SOpac2  = default(SOpac2, SOpac)
Preview = default(Preview, False)
GSGrad  = default(GSGrad, False)
#
#Limits:
Opac    = Opac <0.0 ? 0.0 : Opac >1.0 ? 1.0 : Opac
Opac1   = Opac1 <0.0 ? 0.0 : Opac1 >1.0 ? 1.0 : Opac1
Opac2   = Opac2 <0.0 ? 0.0 : Opac2 >1.0 ? 1.0 : Opac2
SOpac   = SOpac <0.0 ? 0.0 : SOpac >1.0 ? 1.0 : SOpac
SOpac1  = SOpac1 <0.0 ? 0.0 : SOpac1 >1.0 ? 1.0 : SOpac1
SOpac2  = SOpac2 <0.0 ? 0.0 : SOpac2 >1.0 ? 1.0 : SOpac2
#Switch for CMode=0 greyscale:
CMode  = (Cmode == 0) ? 128 : CMode
#Create greyscale gradients for GSGrad:
GSG    =  colorbars().converttoyv12().mt_lutspa(relative=true,expr="x 255 *").greyscale()
GSG    =  GSG.TurnLeft().LanczosResize(640,360)
Clp1   = (GSGrad == False) ? Clp1 : GSG
clp2   = default(clp2, clp1)
Clp2   = (GSGrad == False) ? Clp2 : GSG
# Create mt_lutxy expressions for the blend modes:
Opac   = " "+String(Opac)+" * x 1 "+String(Opac)+" - * + "
Opac1  = " "+String(Opac1)+" * x 1 "+String(Opac1)+" - * + "
Opac2  = " "+String(Opac2)+" * x 1 "+String(Opac2)+" - * + "
SOpac  = " "+String(SOpac)+" * x 1 "+String(SOpac)+" - * + "
SOpac1 = " "+String(SOpac1)+" * x 1 "+String(SOpac1)+" - * + "
SOpac2 = " "+String(SOpac2)+" * x 1 "+String(SOpac2)+" - * + "
# Didee's '128-Centred Contrast Multiply' (HardLight):
HDL    =  " y 128 < x y * 128 / " + Opac1
HDL    =  HDL + " y 128 > 256 256 x - 128 y 128 - - abs * 128 / - " + Opac2
HDL    =  HDL + " x ? ? "
HDL2   =  " y 128 < x y * 128 / " + SOpac1
HDL2   =  HDL2 + " y 128 > 256 256 x - 128 y 128 - - abs * 128 / - " + SOpac2
HDL2   =  HDL2 + " x ? ? "
#Didee's '128-centred contrast multiply' commuted (Overlay):
OVE    =  " x 128 < x y * 128 / " + Opac1
OVE    =  OVE + " x 128 > 256 256 x - 128 y 128 - - abs * 128 / - " + Opac2
OVE    =  OVE + " y ? ? "
OVE2   =  " x 128 < x y * 128 / " + SOpac1
OVE2   =  OVE2 + " x 128 > 256 256 x - 128 y 128 - - abs * 128 / - " + SOpac2
OVE2   =  OVE2 + " y ? ? "
#Softlight: Based on Gimp and Delphi formula
SFL    = " y 128 < 255 x - x y * 255 / * x 255 255 x - 255 y - * 255 / - * + 255 / " + Opac1
SFL    = SFL + " y 128 > 255 x - x y * 255 / * x 255 255 x - 255 y - * 255 / - * + 255 / " + Opac2
SFL    = SFL + " x ? ? "
SFL2   = " y 128 < 255 x - x y * 255 / * x 255 255 x - 255 y - * 255 / - * + 255 / " + SOPac1
SFL2   = SFL2 + " y 128 > 255 x - x y * 255 / * x 255 255 x - 255 y - * 255 / - * + 255 / " + SOPac2
SFL2   = SFL2 + " x ? ? "
#
Bl1Expr = (BLMode=="Blend")       ? "y " + Opac
 \      : (BLMode=="Multiply")    ? "x y * 255 / " + Opac
 \      : (BLMode=="Screen")      ? "256 256 x - 256 y - * 256 / - " + Opac
 \      : (BLMode=="Hardlight")   ? HDL
 \      : (BLMode=="Overlay")     ? OVE
 \      : (BLMode=="Softlight")   ? SFL
 \      : (BLMode=="Burn")        ? "255 255 x - 256 * y 1 + / - " + Opac
 \      : (BLMode=="Dodge")       ? " x 256 * 256 y - / " + Opac
 \      : (BLMode=="Glow")        ? "x 255 == x 255 y y * 255 x - / min ? " + Opac
 \      : (BLMode=="Reflect")     ? "y 255 == y 255 x x * 255 y - / min ? " + Opac
 \      : (BLMode=="Lighten")     ? "x y > x y ? " + Opac
 \      : (BLMode=="Darken")      ? "x y < x y ? " + Opac
 \      : (BLMode=="Additive")    ? "x y + 255 > 255 x y + ? " + Opac
 \      : (BLMode=="Subtractive") ? "x y + 256 - 0 < 0 x y + 256 - ? " + Opac
 \      : (BLMode=="Difference")  ? "x y - abs " + Opac
 \      : Assert(false, "Invalid BLMode String")
#
Bl2Expr = (BLMode2=="Blend")      ? "y " + SOpac
 \      : (BLMode2=="Multiply")   ? "x y * 255 / " + SOpac
 \      : (BLMode2=="Screen")     ? "256 256 x - 256 y - * 256 / - " + SOpac
 \      : (BLMode2=="Hardlight")  ? HDL2
 \      : (BLMode2=="Overlay")    ? OVE2
 \      : (BLMode2=="Softlight")  ? SFL2
 \      : (BLMode2=="Burn")       ? "255 255 x - 256 * y 1 + / - " + SOpac
 \      : (BLMode2=="Dodge")      ? "x 256 * 256 y - / " + SOpac
 \      : (BLMode2=="Glow")       ? "x 255 == x 255 y y * 255 x - / min ? " + SOpac
 \      : (BLMode2=="Reflect")    ? "y 255 == y 255 x x * 255 y - / min ? " + SOpac
 \      : (BLMode2=="Lighten")    ? "x y > x y ? " + SOpac
 \      : (BLMode2=="Darken")     ? "x y < x y ? " + SOpac
 \      : (BLMode2=="Additive")   ? "x y + 255 > 255 x y + ? " + SOpac
 \      : (BLMode2=="Subtractive") ? "x y + 256 - 0 < 0 x y + 256 - ? " + SOpac
 \      : (BLMode2=="Difference") ? "x y - abs " + SOpac
 \      : Assert(false, "Invalid BLMode String")

#Apply blend operation:
BLOut  = mt_lutxy(clp1,clp2, Bl1Expr, Y=LMode, U=CMode, V=CMode)
Output = (BL2 == False) ? BLOut : mt_lutxy(BLOut, BLOut, Bl2Expr, Y=LMode, U=CMode, V=CMode)
#Preview set-up:
Clp1s  = Clp1.LanczosResize(640,360).Subtitle("Clp1 (Base)", size=14)
Clp2s  = Clp2.LanczosResize(640,360).Subtitle("Clp2 (Top)", size=14)
Outs   = Output.LanczosResize(640,360).Subtitle("Output", size=14)
Outgs  = Output.Greyscale().LanczosResize(640,360).Subtitle("Output (Greyscale)", size=14) 
Clpsv  = StackVertical(Clp2s, Clp1s)
Outsv  = StackVertical(Outs, Outgs)
Prev   = StackHorizontal(Clpsv, Outsv)
#GSGrad view set-up:
GSGH   = GSG.Histogram().Subtitle("Original")
GSGoH  = Output.LanczosResize(640,360).Histogram().Subtitle("Blend operation applied")
GSGout = StackVertical(GSGH, GSGoH)
#Output
Final  = (Preview == False && GSGrad == False) ? Output
\      : Preview ? Prev : GSGout 
#
Return Final
}
__________________
Nostalgia's not what it used to be

Last edited by WorBry; 18th May 2010 at 21:49.
WorBry is offline   Reply With Quote