Advertisement
Guest User

Sharpen Complex 2

a guest
Nov 12th, 2011
1,305
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # [insert rant about how I can't register on Doom9]
  2. # port of MPC-HC's Sharpen Complex 2 to Avisynth
  3. # http://forum.doom9.org/showthread.php?t=158385
  4. # (I'm assuming it works correctly because MPC-HC refuses to run in Wine)
  5. # previous version: http://pastebin.com/gamRgbgD
  6. # only processes luma
  7. # str0 is the non-edge unsharp amount, default 2.0
  8. # str1 is the edge unsharp amount, default 1.125
  9. # k0 is the non-edge blur kernel, default "3 14 3"
  10. # k1 is the edge blur kernel, default "1 1 1"
  11. # the kernels must have odd lengths (as required by mt_convolution)
  12. # edgethr is the threshold for an edge on an arbitrary scale, default 0.2
  13. # if debug is true, then the edge mask is returned instead
  14.  
  15. # for the stacked 16-bit version, there's no debug parameter
  16. # lsb_in if true treats the input as a stacked 16-bit clip, default true
  17. # lsb if true leaves the output as stacked 16-bit, otherwise it's dithered
  18. # (output is the same format as the input by default)
  19. # mode is for the DitherPost mode, default 0 (see Dither docs)
  20.  
  21. # recommendation: just use a regular unsharp filter, don't bother with this
  22. # unless you're going to set the unsharp amount/kernels (in which case this
  23. # script actually becomes useful; the default edge/non-edge filters are
  24. # sufficiently similar to be pointless). the 16-bit version is an extra serving
  25. # of pointlessness because it's almost visually identical to the 8-bit version
  26. # with the default settings and it's significantly slower; it might be useful
  27. # though, if the source is banding-prone or with non-default unsharp
  28. # amounts/kernels.
  29.  
  30. # requirements: MaskTools v2 (for both the regular and 16-bit functions)
  31. #               Dither package (16-bit only)
  32.  
  33. function SharpenComplex2(clip src,float "str0",float "str1",string "k0",string "k1",float "edgethr",bool "debug")
  34. {
  35.  
  36. str0=default(str0,2.0)   # corresponds to CoefFlou
  37. str1=default(str1,1.125) # corresponds to Sharpen_val1 * 9
  38.  
  39. k0=default(k0,"3 14 3") # ~ Gaussian blur with sigma^2=0.3
  40. k1=default(k1,"1 1 1")  # ~ Gaussian blur with sigma^2=1/3=0.333...
  41. #the 3 14 3 kernel is derived from assuming linear interpolation in the original
  42. #code; you can set it to other kernels based on other interpolators, but bear in
  43. #mind that blurrier interpolators lead to more sharpening
  44.  
  45. edgethr=default(edgethr,0.2) # corresponds to SharpenEdge
  46.  
  47. src
  48.  
  49. unsharp0=mt_lutxy(src,mt_convolution(k0,k0),"x x y - "+string(str0)+" * +",chroma="copy")
  50. unsharp1=mt_lutxy(src,mt_convolution(k1,k1),"x x y - "+string(str1)+" * +")
  51.  
  52. sobelh=mt_convolution("-1 0 1","1 2 1",saturate=false,total=1.)
  53. sobelv=mt_convolution("1 2 1","-1 0 1",saturate=false,total=1.)
  54. edgemask=mt_lutxy(sobelh,sobelv,"x x * y y * + 0.5 ^ 255 / "+string(edgethr)+" > 255 0 ?")
  55. default(debug,false) ? edgemask.grayscale : mt_merge(unsharp0,unsharp1,edgemask)
  56.  
  57. }
  58.  
  59. function SharpenComplex2_16(clip src,float "str0",float "str1",string "k0",string "k1",float "edgethr",bool "lsb_in",bool "lsb",int "mode")
  60. {
  61.  
  62. str0=default(str0,2.0)
  63. str1=default(str1,1.125)
  64.  
  65. k0="impulse "+default(k0,"3 14 3")
  66. k1="impulse "+default(k1,"1 1 1")
  67.  
  68. edgethr=default(edgethr,0.2)
  69.  
  70. lsb_in=default(lsb_in,true)
  71. lsb=default(lsb,lsb_in)
  72.  
  73. mode=default(mode,0)
  74.  
  75. lsb_in ? src : src.Dither_convert_8_to_16
  76.  
  77. o=last
  78.  
  79. w=width()
  80. h=height()/2
  81.  
  82. blur0=Dither_resize16(w,h,kernel=k0,fh=-1,fv=-1,center=false,u=1,v=1)
  83. blur1=Dither_resize16(w,h,kernel=k1,fh=-1,fv=-1,center=false,u=1,v=1)
  84. unsharp0=Dither_add16(last,Dither_lut16(Dither_add16(last,Dither_lut16(blur0,"65535 x -"),dif=true),string(str0)+" x 32767 - * 32768 +"),dif=true)
  85. unsharp1=Dither_add16(last,Dither_lut16(Dither_add16(last,Dither_lut16(blur1,"65535 x -"),dif=true),string(str1)+" x 32767 - * 32768 +"),dif=true)
  86. #this is why Dither needs subtraction and multiplication utility functions
  87.  
  88. #edge mask computation doesn't require higher precision
  89. #(more like there's no lutxy for 16-bit)
  90. lsb_in?src.DitherPost(mode=-1):src
  91. sobelh=mt_convolution("-1 0 1","1 2 1",saturate=false,total=1.)
  92. sobelv=mt_convolution("1 2 1","-1 0 1",saturate=false,total=1.)
  93. edgemask=mt_lutxy(sobelh,sobelv,"x x * y y * + 0.5 ^ 255 / "+string(edgethr)+" > 255 0 ?")
  94.  
  95. Dither_merge16_8(unsharp0,unsharp1,edgemask)
  96. MergeLuma(o,last)
  97.  
  98. lsb ? last : DitherPost(mode=mode)
  99.  
  100. }
  101.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement