]> git.sesse.net Git - nageru/blobdiff - theme.lua
Make a Lua function for enumerating the cartesian products of parameters, instead...
[nageru] / theme.lua
index e6338087fbaf2ab00ae9b8aec3d8886f530bf60d..b623541c2a1b38bfa8024e1983be6e097ed27850 100644 (file)
--- a/theme.lua
+++ b/theme.lua
@@ -33,69 +33,96 @@ local STATIC_SIGNAL_NUM = 3
 -- to the next.
 local FADE_SIGNAL_NUM = 4
 
--- The main live chain.
-function make_sbs_chain(input0_deint, input1_deint, hq)
-       local chain = EffectChain.new(16, 9)
-       local input0 = chain:add_live_input(not input0_deint, input0_deint)  -- Override bounce only if not deinterlacing.
-       input0:connect_signal(0)
-       local input0_wb_effect = chain:add_effect(WhiteBalanceEffect.new())
-       local input1 = chain:add_live_input(not input1_deint, input1_deint)
-       input1:connect_signal(1)
-       local input1_wb_effect = chain:add_effect(WhiteBalanceEffect.new())
+-- Utility function to help creating many similar chains that can differ
+-- in a free set of chosen parameters.
+function make_cartesian_product(parms, callback)
+       return make_cartesian_product_internal(parms, callback, 1, {})
+end
+
+function make_cartesian_product_internal(parms, callback, index, args)
+       if index > #parms then
+               return callback(unpack(args))
+       end
+       local ret = {}
+       for _, value in ipairs(parms[index]) do
+               args[index] = value
+               ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
+       end
+       return ret
+end
+
+function make_sbs_input(chain, signal, deint, hq)
+       local input = chain:add_live_input(not deint, deint)  -- Override bounce only if not deinterlacing.
+       input:connect_signal(signal)
+       local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
 
        local resample_effect = nil
        local resize_effect = nil
        if (hq) then
-               resample_effect = chain:add_effect(ResampleEffect.new(), input0_wb_effect)
+               resample_effect = chain:add_effect(ResampleEffect.new())
        else
-               resize_effect = chain:add_effect(ResizeEffect.new(), input0_wb_effect)
+               resize_effect = chain:add_effect(ResizeEffect.new())
        end
 
        local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
-       padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
 
-       local resample2_effect = nil
-       local resize2_effect = nil
-       if (hq) then
-               resample2_effect = chain:add_effect(ResampleEffect.new(), input1_wb_effect)
-       else
-               resize2_effect = chain:add_effect(ResizeEffect.new(), input1_wb_effect)
-       end
-       local padding2_effect = chain:add_effect(IntegralPaddingEffect.new())
+       return {
+               input = input,
+               wb_effect = wb_effect,
+               resample_effect = resample_effect,
+               resize_effect = resize_effect,
+               padding_effect = padding_effect
+       }
+end
+
+-- The main live chain.
+function make_sbs_chain(input0_deint, input1_deint, hq)
+       local chain = EffectChain.new(16, 9)
+
+       local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_deint, hq)
+       local input1 = make_sbs_input(chain, INPUT1_SIGNAL_NUM, input1_deint, hq)
+
+       input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
+       input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
 
-       chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect)
+       chain:add_effect(OverlayEffect.new(), input0.padding_effect, input1.padding_effect)
        chain:finalize(hq)
 
        return {
                chain = chain,
-               input0 = {
-                       input = input0,
-                       wb_effect = input0_wb_effect,
-                       resample_effect = resample_effect,
-                       resize_effect = resize_effect,
-                       padding_effect = padding_effect
-               },
-               input1 = {
-                       input = input1,
-                       wb_effect = input1_wb_effect,
-                       resample_effect = resample2_effect,
-                       resize_effect = resize2_effect,
-                       padding_effect = padding2_effect
-               }
+               input0 = input0,
+               input1 = input1
        }
 end
 
 -- Make all possible combinations of side-by-side chains.
-local sbs_chains = {}
-for input0_type, input0_deint in pairs({live = false, livedeint = true}) do
-       sbs_chains[input0_type] = {}
-       for input1_type, input1_deint in pairs({live = false, livedeint = true}) do
-               sbs_chains[input0_type][input1_type] = {}
-               for _, hq in pairs({true, false}) do
-                       sbs_chains[input0_type][input1_type][hq] =
-                               make_sbs_chain(input0_deint, input1_deint, hq)
-               end
+local sbs_chains = make_cartesian_product({
+       {"live", "livedeint"},  -- input0_type
+       {"live", "livedeint"},  -- input1_type
+       {true, false}           -- hq
+}, function(input0_type, input1_type, hq)
+       local input0_deint = (input0_type == "livedeint")
+       local input1_deint = (input1_type == "livedeint")
+       return make_sbs_chain(input0_deint, input1_deint, hq)
+end)
+
+function make_fade_input(chain, signal, live, deint)
+       local input, wb_effect, last
+       if live then
+               input = chain:add_live_input(false, deint)
+               wb_effect = chain:add_effect(WhiteBalanceEffect.new())
+               input:connect_signal(signal)
+               last = wb_effect
+       else
+               input = chain:add_effect(ImageInput.new("bg.jpeg"))
+               last = input
        end
+
+       return {
+               input = input,
+               wb_effect = wb_effect,
+               last = last
+       }
 end
 
 -- A chain to fade between two inputs, of which either can be a picture
@@ -104,59 +131,32 @@ end
 function make_fade_chain(input0_live, input0_deint, input1_live, input1_deint, hq)
        local chain = EffectChain.new(16, 9)
 
-       local input0, wb0_effect, input0_last, input1, wb1_effect, input1_last
-
-       if input0_live then
-               input0 = chain:add_live_input(false, input0_deint)
-               wb0_effect = chain:add_effect(WhiteBalanceEffect.new())
-               input0:connect_signal(0)
-               input0_last = wb0_effect
-       else
-               input0 = chain:add_effect(ImageInput.new("bg.jpeg"))
-               input0_last = input0
-       end
-
-       if input1_live then
-               input1 = chain:add_live_input(false, input1_deint)
-               wb1_effect = chain:add_effect(WhiteBalanceEffect.new())
-               input1:connect_signal(1)
-               input1_last = wb1_effect
-       else
-               input1 = chain:add_effect(ImageInput.new("bg.jpeg"))
-               input1_last = input1
-       end
+       local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint)
+       local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint)
 
-       local mix_effect = chain:add_effect(MixEffect.new(), input0_last, input1_last)
+       local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
        chain:finalize(hq)
 
        return {
                chain = chain,
-               input0 = {
-                       input = input0,
-                       wb_effect = wb0_effect
-               },
-               input1 = {
-                       input = input1,
-                       wb_effect = wb1_effect
-               },
+               input0 = input0,
+               input1 = input1,
                mix_effect = mix_effect
        }
 end
 
 -- Chains to fade between two inputs, in various configurations.
-local fade_chains = {}
-for input0_type, input0_live in pairs({static = false, live = true, livedeint = true}) do
-       local input0_deint = (input0_live == "livedeint")
-       fade_chains[input0_type] = {}
-       for input1_type, input1_live in pairs({static = false, live = true, livedeint = true}) do
-               local input1_deint = (input1_live == "livedeint")
-               fade_chains[input0_type][input1_type] = {}
-               for _, hq in pairs({true, false}) do
-                       fade_chains[input0_type][input1_type][hq] =
-                               make_fade_chain(input0_live, input0_deint, input1_live, input1_deint, hq)
-               end
-       end
-end
+local fade_chains = make_cartesian_product({
+       {"static", "live", "livedeint"},  -- input0_type
+       {"static", "live", "livedeint"},  -- input1_type
+       {true, false}                     -- hq
+}, function(input0_type, input1_type, hq)
+       local input0_live = (input0_type ~= "static")
+       local input1_live = (input1_type ~= "static")
+       local input0_deint = (input0_type == "livedeint")
+       local input1_deint = (input1_type == "livedeint")
+       return make_fade_chain(input0_live, input0_deint, input1_live, input1_deint, hq)
+end)
 
 -- A chain to show a single input on screen.
 function make_simple_chain(input_deint, hq)
@@ -175,13 +175,13 @@ function make_simple_chain(input_deint, hq)
 end
 
 -- Make all possible combinations of single-input chains.
-local simple_chains = {}
-for input_type, input_deint in pairs({live = false, livedeint = true}) do
-       simple_chains[input_type] = {}
-       for _, hq in pairs({true, false}) do
-               simple_chains[input_type][hq] = make_simple_chain(input_deint, hq)
-       end
-end
+local simple_chains = make_cartesian_product({
+       {"live", "livedeint"},  -- input_type
+       {true, false}           -- hq
+}, function(input_type, hq)
+       local input_deint = (input_type == "livedeint")
+       return make_simple_chain(input_deint, hq)
+end)
 
 -- A chain to show a single static picture on screen (HQ version).
 local static_chain_hq = EffectChain.new(16, 9)