Guest User

y_gamma_converter

a guest
Mar 20th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # base on dithertools avsi functions by cretindesalpes
  2. # !!!!!!!!!!!! work only with native avs+ 16 bit !!!!!!!!!!!! for now
  3.  
  4. Function y_gamma_to_linear (clip src,
  5. \   bool "tv_range_in", bool "tv_range_out", string "curve", int "u", int "v",
  6. \   float "gcor", bool "sigmoid", float "thr", float "cont")
  7. {
  8.     src
  9.     linear_and_gamma (false,
  10. \       tv_range_in, tv_range_out, curve, u, v,
  11. \       gcor, sigmoid, thr, cont
  12. \   )
  13. }
  14.  
  15. Function y_linear_to_gamma (clip src,
  16. \   bool "tv_range_in", bool "tv_range_out", string "curve", int "u", int "v",
  17. \   float "gcor", bool "sigmoid", float "thr", float "cont")
  18. {
  19.     src
  20.     linear_and_gamma (true,
  21. \       tv_range_in, tv_range_out, curve, u, v,
  22. \       gcor, sigmoid, thr, cont
  23. \   )
  24. }
  25.  
  26. Function linear_and_gamma (clip src, bool l2g_flag,
  27. \   bool "tv_range_in", bool "tv_range_out", string "curve", int "u", int "v",
  28. \   float "gcor", bool "sigmoid", float "thr", float "cont")
  29. {
  30.     tv_range_in  = Default (tv_range_in,  true)
  31.     tv_range_out = Default (tv_range_out, true)
  32.     curve        = Default (curve,      "srgb")
  33.     u            = Default (u,               2)
  34.     v            = Default (v,               2)
  35.     gcor         = Default (gcor,          1.0)
  36.     sigmoid      = Default (sigmoid,     false)
  37.  
  38.     c_num =
  39. \     (curve == "srgb" ) ? 0
  40. \   : (curve == "709"  ) ? 1
  41. \   : (curve == "601"  ) ? 1
  42. \   : (curve == "170"  ) ? 1
  43. \   : (curve == "240"  ) ? 2
  44. \   : (curve == "2020" ) ? 3
  45. \   : (curve == "1886" ) ? 4
  46. \   : (curve == "1886a") ? 5
  47. \   : Assert (false, "linear_and_gamma: wrong curve value.")
  48.  
  49.     #                                  BT-709/601
  50.     #                         sRGB     SMPTE 170M  SMPTE 240M    BT-2020    BT-1886    BT-1886a
  51.     k0    = Select (c_num, " 0.04045", " 0.081  ", " 0.0912 ", " 0.08145", "0      ", "0.35   ")
  52.     phi   = Select (c_num, "12.92   ", " 4.5    ", " 4.0    ", " 4.5    ", "4.5    ", "0.65709357") # 0.35 ^ (3.0-2.6)
  53.     gam2  = Select (c_num, " 1      ", " 1      ", " 1      ", " 1      ", "1      ", "3.0    ")
  54.     alpha = Select (c_num, " 0.055  ", " 0.099  ", " 0.1115 ", " 0.0993 ", "0      ", "0      ")
  55.     gamma = Select (c_num, " 2.4    ", " 2.22222", " 2.22222", " 2.22222", "2.4    ", "2.6    ")
  56.  
  57.     expr = (tv_range_in) ? "x 4096 - 56064 /" : "x 65536 /"
  58.  
  59.     # E = (E' <= k0)   ?   (E' ^ gam2) / phi   :   ((E' + alpha) / (1 + alpha)) ^ gamma
  60.     g2l = expr
  61.     g2l =   g2l + " " + k0 +" <= "
  62. \         + g2l + " " + gam2 + " ^ " + phi +" / "
  63. \         + g2l + " " + alpha + " + 1 " + alpha + " + / " + gamma + " ^   ?"
  64.     g2l = (gcor != 1.0) ? g2l + " 0 >=   " + g2l + " " + String (gcor) + " ^   " + g2l + "   ?" : g2l
  65.     g2l = (sigmoid) ? build_sigmoid_expr (g2l , true , thr, cont) : g2l
  66.  
  67.     l2g = (sigmoid) ? build_sigmoid_expr (expr, false, thr, cont) : expr
  68.     l2g = (gcor != 1.0) ? l2g + " 0 >=   " + l2g + " " + String (gcor) + " ^   " + l2g + "   ?" : l2g
  69.     # E' = (E <= k0 / phi)   ?   (E * phi) ^ (1 / gam2)   :   (E ^ (1 / gamma)) * (alpha + 1) - alpha
  70.     l2g =   l2g + " " + k0 + " " + phi + " / <= "
  71. \         + l2g + " " + phi + " * 1 " + gam2 + " / ^ "
  72. \         + l2g + " 1 " + gamma + " / ^ " + alpha + " 1 + * " + alpha + " -   ?"
  73.  
  74.     expr = (l2g_flag) ? l2g : g2l
  75.     expr = expr + ((tv_range_out) ? " 56064 * 4096 +" : " 65536 *")
  76.     src.mt_lut (expr=expr, y=3, u=u, v=v)
  77. }
  78.  
  79. # Sigmoidal functions:
  80. # x0 = 1 / (1 + exp (cont *  thr     ))
  81. # x1 = 1 / (1 + exp (cont * (thr - 1)))
  82. # y  = (1 / (1 + exp (cont * (thr - x))) - x0) / (x1 - x0)
  83. # x  = thr - log (1 / (y * (x1 - x0) + x0) - 1) / cont
  84. Function build_sigmoid_expr (string in, bool inv, float "thr", float "cont")
  85. {
  86.     thr   = Default (thr,  0.5)
  87.     cont  = Default (cont, 6.5)
  88.     Assert ((cont > 0), "build_sigmoid_expr: cont must be strictly positive.")
  89.     Assert ((thr >= 0 && thr <= 1), "build_sigmoid_expr: thr must be in the 0-1 range.")
  90.     x0v = 1 / (1 + exp (cont *  thr     ))
  91.     x1v = 1 / (1 + exp (cont * (thr - 1)))
  92.  
  93.     x0   = String (      x0v)
  94.     x1m0 = String (x1v - x0v)
  95.     cont = String (cont)
  96.     thr  = String (thr)
  97.  
  98.     expr = (inv)
  99. \   ? thr + " 1 " + in + " " + x1m0 + " * " + x0 + " + 0.000001 max / 1 - 0.000001 max log " + cont + " / -"
  100. \   : "1 1 " + cont + " " + thr + " " + in + " - * exp + / " + x0 + " - " + x1m0 + " /"
  101.  
  102.     return (expr)
  103. }
Advertisement
Add Comment
Please, Sign In to add comment