Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # base on dithertools avsi functions by cretindesalpes
- # !!!!!!!!!!!! work only with native avs+ 16 bit !!!!!!!!!!!! for now
- Function y_gamma_to_linear (clip src,
- \ bool "tv_range_in", bool "tv_range_out", string "curve", int "u", int "v",
- \ float "gcor", bool "sigmoid", float "thr", float "cont")
- {
- src
- linear_and_gamma (false,
- \ tv_range_in, tv_range_out, curve, u, v,
- \ gcor, sigmoid, thr, cont
- \ )
- }
- Function y_linear_to_gamma (clip src,
- \ bool "tv_range_in", bool "tv_range_out", string "curve", int "u", int "v",
- \ float "gcor", bool "sigmoid", float "thr", float "cont")
- {
- src
- linear_and_gamma (true,
- \ tv_range_in, tv_range_out, curve, u, v,
- \ gcor, sigmoid, thr, cont
- \ )
- }
- Function linear_and_gamma (clip src, bool l2g_flag,
- \ bool "tv_range_in", bool "tv_range_out", string "curve", int "u", int "v",
- \ float "gcor", bool "sigmoid", float "thr", float "cont")
- {
- tv_range_in = Default (tv_range_in, true)
- tv_range_out = Default (tv_range_out, true)
- curve = Default (curve, "srgb")
- u = Default (u, 2)
- v = Default (v, 2)
- gcor = Default (gcor, 1.0)
- sigmoid = Default (sigmoid, false)
- c_num =
- \ (curve == "srgb" ) ? 0
- \ : (curve == "709" ) ? 1
- \ : (curve == "601" ) ? 1
- \ : (curve == "170" ) ? 1
- \ : (curve == "240" ) ? 2
- \ : (curve == "2020" ) ? 3
- \ : (curve == "1886" ) ? 4
- \ : (curve == "1886a") ? 5
- \ : Assert (false, "linear_and_gamma: wrong curve value.")
- # BT-709/601
- # sRGB SMPTE 170M SMPTE 240M BT-2020 BT-1886 BT-1886a
- k0 = Select (c_num, " 0.04045", " 0.081 ", " 0.0912 ", " 0.08145", "0 ", "0.35 ")
- phi = Select (c_num, "12.92 ", " 4.5 ", " 4.0 ", " 4.5 ", "4.5 ", "0.65709357") # 0.35 ^ (3.0-2.6)
- gam2 = Select (c_num, " 1 ", " 1 ", " 1 ", " 1 ", "1 ", "3.0 ")
- alpha = Select (c_num, " 0.055 ", " 0.099 ", " 0.1115 ", " 0.0993 ", "0 ", "0 ")
- gamma = Select (c_num, " 2.4 ", " 2.22222", " 2.22222", " 2.22222", "2.4 ", "2.6 ")
- expr = (tv_range_in) ? "x 4096 - 56064 /" : "x 65536 /"
- # E = (E' <= k0) ? (E' ^ gam2) / phi : ((E' + alpha) / (1 + alpha)) ^ gamma
- g2l = expr
- g2l = g2l + " " + k0 +" <= "
- \ + g2l + " " + gam2 + " ^ " + phi +" / "
- \ + g2l + " " + alpha + " + 1 " + alpha + " + / " + gamma + " ^ ?"
- g2l = (gcor != 1.0) ? g2l + " 0 >= " + g2l + " " + String (gcor) + " ^ " + g2l + " ?" : g2l
- g2l = (sigmoid) ? build_sigmoid_expr (g2l , true , thr, cont) : g2l
- l2g = (sigmoid) ? build_sigmoid_expr (expr, false, thr, cont) : expr
- l2g = (gcor != 1.0) ? l2g + " 0 >= " + l2g + " " + String (gcor) + " ^ " + l2g + " ?" : l2g
- # E' = (E <= k0 / phi) ? (E * phi) ^ (1 / gam2) : (E ^ (1 / gamma)) * (alpha + 1) - alpha
- l2g = l2g + " " + k0 + " " + phi + " / <= "
- \ + l2g + " " + phi + " * 1 " + gam2 + " / ^ "
- \ + l2g + " 1 " + gamma + " / ^ " + alpha + " 1 + * " + alpha + " - ?"
- expr = (l2g_flag) ? l2g : g2l
- expr = expr + ((tv_range_out) ? " 56064 * 4096 +" : " 65536 *")
- src.mt_lut (expr=expr, y=3, u=u, v=v)
- }
- # Sigmoidal functions:
- # x0 = 1 / (1 + exp (cont * thr ))
- # x1 = 1 / (1 + exp (cont * (thr - 1)))
- # y = (1 / (1 + exp (cont * (thr - x))) - x0) / (x1 - x0)
- # x = thr - log (1 / (y * (x1 - x0) + x0) - 1) / cont
- Function build_sigmoid_expr (string in, bool inv, float "thr", float "cont")
- {
- thr = Default (thr, 0.5)
- cont = Default (cont, 6.5)
- Assert ((cont > 0), "build_sigmoid_expr: cont must be strictly positive.")
- Assert ((thr >= 0 && thr <= 1), "build_sigmoid_expr: thr must be in the 0-1 range.")
- x0v = 1 / (1 + exp (cont * thr ))
- x1v = 1 / (1 + exp (cont * (thr - 1)))
- x0 = String ( x0v)
- x1m0 = String (x1v - x0v)
- cont = String (cont)
- thr = String (thr)
- expr = (inv)
- \ ? thr + " 1 " + in + " " + x1m0 + " * " + x0 + " + 0.000001 max / 1 - 0.000001 max log " + cont + " / -"
- \ : "1 1 " + cont + " " + thr + " " + in + " - * exp + / " + x0 + " - " + x1m0 + " /"
- return (expr)
- }
Advertisement
Add Comment
Please, Sign In to add comment