Advertisement
Guest User

Masked_DHA

a guest
Mar 4th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 2017.03.04 1st release
  2. # Masked_DHA with high bit support, need masktools 2.2.4 or up in avs26 and avs+
  3.  
  4. function Masked_DHA(clip input, float "rx", float "ry", float "darkstr", float "brightstr", float "lowsens", float "highsens", int "msk_pull", int "msk_push", float "ss", bool "show_msk", bool "cs", int "search_rade", int "search_radi")
  5. {
  6. rx        = default( rx,         2.0 )
  7. ry        = default( ry,         2.0 )
  8. darkstr   = default( darkstr,    1.0 )
  9. brightstr = default( brightstr,  1.0 )
  10. lowsens   = default( lowsens,     50 )
  11. highsens  = default( highsens,    50 )
  12. msk_pull  = default( msk_pull,    48 )
  13. msk_push  = default( msk_push,   192 )
  14. ss        = default( ss,         1.5 )
  15. show_msk  = default( show_msk, false )
  16. cs        = default( cs,       false )
  17. sre       = default( search_rade,  1 )
  18. sri       = default( search_radi, sre )
  19.  
  20. rx        = max(rx, 1.0)
  21. ry        = max(ry, 1.0)
  22. darkstr   = max(min(darkstr, 1.0), 0.0)
  23. lowsens   = max(min(lowsens, 100), 0)
  24. highsens  = max(min(highsens, 100), 0)
  25. msk_pull  = max(min(msk_pull, 254), 0)
  26. msk_push  = max(min(msk_push, 255), msk_pull + 1)
  27. ss        = max(ss, 1.0)
  28.  
  29. ssispmt   = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  30.  
  31. sislumaonly = ssispmt ? input.isy() : VersionNumber() < 2.6 ? true : input.isy8()
  32.  
  33. clp = sislumaonly ? input : ssispmt ? input.converttoy() : input.converttoy8()
  34.  
  35. LOS = string(lowsens)
  36. HIS = string(highsens/100.0)
  37. DRK = string(darkstr)
  38. BRT = string(brightstr)
  39. MPL = string(msk_pull)
  40. MPS = string(msk_push)
  41. ox  = clp.width()
  42. oy  = clp.height()
  43.  
  44. # Parameters that can only be adjusted from within the script
  45. mbl = 1.58
  46.  
  47. sm     = clp.bicubicresize(m4_md(ox/rx),m4_md(oy/ry))
  48. lg     = sm.bicubicresize(ox,oy,1,0)
  49. chl    = mt_lutxy(clp.srmdhae(sre,1),clp.srmdhai(sri,1),"x y -","x y -","x y -")
  50. lhl    = mt_lutxy(lg.srmdhae(sre,1),lg.srmdhai(sri,1),"x y -","x y -","x y -")
  51. mask_i = VersionNumber() < 2.6 ? mt_lutxy(lhl,chl,"y x - y 0.001 + / 255 * "+LOS+" - y 256 + 512 / "+HIS+" + *" )
  52.          \                     : mt_lutxy(lhl,chl,"clamp_f_i8 y x - y 0.001 + / range_max * "+LOS+" #F - y range_size + 512 #F / "+HIS+" + *" )
  53.  
  54. mask_f = VersionNumber() < 2.6 ? mt_lutxy(sm.mt_expand,sm.mt_inpand,"x y - 4 *") : mt_lutxy(sm.mt_expand,sm.mt_inpand,"clamp_f_i8 x y - 4 *")
  55. mask_f = VersionNumber() < 2.6 ? mask_f.blur(mbl).bicubicresize(ox,oy,1.0,0).mt_lut(yExpr="255 255 "+MPL+" - 255 "+MPS+" - - / x "+MPL+" - *")
  56.          \                     : mask_f.blur(mbl).bicubicresize(ox,oy,1.0,0).mt_lut(yExpr="clamp_f_i8 range_max range_max "+MPL+" #F - range_max "+MPS+" #F - - / x "+MPL+" #F - *")
  57.  
  58. mmg    = mt_merge(lg,clp,mask_i)
  59. ssc    = (ss==1.0) ? clp.repair(mmg,1,0)
  60.           \        : clp.spline64resize(m4_md(ox*ss),m4_md(oy*ss))
  61.           \             .mt_logic(mmg.mt_expand().bicubicresize(m4_md(ox*ss),m4_md(oy*ss)),"min",U=2,V=2)
  62.           \             .mt_logic(mmg.mt_inpand().bicubicresize(m4_md(ox*ss),m4_md(oy*ss)),"max",U=2,V=2)
  63.           \             .spline64resize(ox,oy)
  64. umfc   = VersionNumber() < 2.6 ? mt_lutxy(clp,ssc,"x y < x x y - "+DRK+" * - x x y - "+BRT+" * - ?",U=2,V=2)
  65.          \                     : mt_lutxy(clp,ssc,"clamp_f_i8 x y < x x y - "+DRK+" * - x x y - "+BRT+" * - ?",U=2,V=2)
  66. mfc    = mt_merge(clp,umfc,mask_f,U=2,V=2)
  67.  
  68. bb   = cs ? mfc.removegrain(11) : nop()
  69. xD   = cs ? mt_makediff(bb,bb.repair(bb.repair(bb.medianblur(2,-333,-333),1),1)) : nop()
  70. xD   = cs ? VersionNumber() < 2.6 ? xD.mt_lut("x 128 - 2.49 * 128 +") : xD.mt_lut("clamp_f_i8 x range_half - 2.49 * range_half +") : xD
  71. xDD  = cs ? VersionNumber() < 2.6 ? mt_lutxy(xD,mt_makediff(clp,mfc),"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?")
  72.             \                     : mt_lutxy(xD,mt_makediff(clp,mfc),"x range_half - y range_half - * 0 < range_half x range_half - abs y range_half - abs < x y ? ?") : nop()
  73. mfc  = cs ? mfc.mt_adddiff(xDD,U=2,V=2) : mfc
  74.  
  75. mfc    = sislumaonly ? mfc : ssispmt ? CombinePlanes(mfc,input,planes="YUV",sample_clip=input) : ytouv(input.utoy8(),input.vtoy8(),mfc)
  76.  
  77. return(show_msk ? mask_f : mfc)
  78. }
  79.  
  80. function m4_md(float x) {
  81. nx=VersionNumber() < 2.6 ? x<16?16:int(round(x/4.0)*4) : round(x)
  82. return(nx)
  83. }
  84.  
  85. Function srmdhae(clip i, int dr_rad, int count)
  86. {
  87. return count > dr_rad ? i : srmdhae(i.mt_expand(), dr_rad, count+1)
  88. }
  89.  
  90. Function srmdhai(clip i, int dr_rad, int count)
  91. {
  92. return count > dr_rad ? i : srmdhai(i.mt_inpand(), dr_rad, count+1)
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement