Zastin

Resharpen.avsi - Contrasharpening with stricter limiting

Aug 31st, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Resharpen v0 "probably works" edition
  2.  
  3. Very safe, usually weak sharpening based on the idea of contrasharpening
  4.  
  5. Does a normal blind sharpening, and clamps it to the range of values between filtered and source clips
  6.  
  7. So if your filtered luma plane has a Y8 value of 100, source is 120, and sharpened is 140, the output will be 120
  8.  
  9. Or in math terms: hi = max(flt, src)
  10.                   lo = min(flt, src)
  11.                  
  12.                   sharp = min(hi, sharp)
  13.                   sharp = max(lo, sharp)
  14.                  
  15.                   return sharp
  16.                  
  17. PARAMETERS
  18.     flt   = Filtered clip, to sharpen
  19.     src   = Unfiltered clip, for limiting
  20.     rad   = Radius of sharpening. When mode="gauss" this is the amount of iterations of removegrain in the pattern 11, 20, 20...
  21.                                   When mode="box" this is the radius of the box filtering (average blur)
  22.             Default: 2 for HD, 1for SD (HD is anything with dimensions of 1100x600 or greater)
  23.     mode  = Type of blur used. "gauss" uses RemoveGrain to imitate a gaussian blur
  24.                                "box" uses RemoveGrain only if rad=1, otherwise it uses the rather outdated Dither_box_filter16 which requires stack16 (this is handled by the filter by checking the clip depth and lsb parameter)
  25.             Default: "gauss"
  26.     lsb   = Does the same thing it always does.
  27.             Default: False
  28.     y/u/v = How to process each channel. 1 = trash, 2 = copy from flt, 3 = process, 4 = copy from src
  29.             Default (y): 3
  30.             Default (u): 2
  31.             Default (v): Whatever you set "u" to
  32.  
  33. DEPENENCIES
  34.  - AVS+
  35.  - RGTools
  36.  - Dither (for mode="box" and lsb=True)
  37.  - CLExpr (for lsb=True)
  38. */
  39.  
  40. function resharpen(clip flt, clip src, int "rad", val "mode", bool "lsb", int "y", int "u", int "v"){
  41.     rad  = flt.Width < 1100 && flt.Height < 600 ? Default(rad, 1)
  42.     \                                           : Default(rad, 2)
  43.     mode = Default(mode, "gauss")
  44.     lsb  = Default(lsb, False)
  45.     y    = Default(y, 3)
  46.     u    = Default(u, 2)
  47.     v    = Default(v, u)
  48.    
  49.     blur = mode=="gauss" ? flt.resharpen_gauss(rad, lsb, y, u, v, True)
  50.     \                    : flt.resharpen_box(rad, lsb, y, u, v)
  51.    
  52.     yexp = y==3 ? "x x + z - x y min max x y max min" : y==2 ? "x" : y==4 ? "y" : "0"
  53.     uexp = u==3 ? "x x + z - x y min max x y max min" : u==2 ? "x" : u==4 ? "y" : "range_half"
  54.     vexp = v==3 ? "x x + z - x y min max x y max min" : v==2 ? "x" : v==4 ? "y" : "range_half"
  55.    
  56.     return lsb ? flt.cl_exprxyz(src, blur, yExpr=yexp, uExpr=uexp, vExpr=vexp, Y=y, U=u, V=v, lsb=True)
  57.     \          : Expr(flt, src, blur, yexp, uexp, vexp)
  58.     }
  59.    
  60. function resharpen_gauss(clip src, int rad, bool lsb, int y, int u, int v, bool first){
  61.     rgy = y==3 ? first ? 11 : 20 : -1
  62.     rgu = u==3 ? first ? 11 : 20 : -1
  63.     rgv = v==3 ? first ? 11 : 20 : -1
  64.    
  65.     return rad==0 ? src
  66.     \             : lsb ? src.Dither_removegrain16(rgy, rgu, rgv).resharpen_gauss(rad-1, lsb, y, u, v, False)
  67.     \                   : src.RemoveGrain         (rgy, rgu, rgv).resharpen_gauss(rad-1, lsb, y, u, v, False)
  68.     }
  69.  
  70. function resharpen_box(clip src, int rad, bool lsb, int y, int u, int v){
  71.     rgy = y==3 ? 20 : -1
  72.     rgu = u==3 ? 20 : -1
  73.     rgv = v==3 ? 20 : -1
  74.     bits = src.BitsPerComponent
  75.    
  76.     return rad == 1 ? lsb ? src.Dither_removegrain16(rgy, rgu, rgv)
  77.     \                     : src.RemoveGrain(rgy, rgu, rgv)
  78.     \               : lsb ? src.Dither_box_filter16(rad, y=y, u=u, v=v)
  79.     \                     : bits==8 ? src.Dither_convert_8_to_16().Dither_box_filter16(rad, y=y, u=u, v=v).DitherPost(mode=-1, y=y, u=u, v=v)
  80.     \                               : bits==16 ? src.ConvertToStacked().Dither_box_filter16(rad, y=y, u=u, v=v).ConvertFromStacked()
  81.     \                                          : src.ConvertBits(16).ConvertToStacked().Dither_box_filter16(rad, y=y, u=u, v=v).ConvertFromStacked().ConvertBits(bits)
  82.     }
Add Comment
Please, Sign In to add comment