Advertisement
JPulowski

LumaSharpen 1.5.0

May 9th, 2015
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.50 KB | None | 0 0
  1. /*
  2.    _____________________
  3.  
  4.      LumaSharpen 1.5.0
  5.    _____________________
  6.  
  7.   by Christian Cann Schuldt Jensen ~ CeeJay.dk
  8.  
  9.   It blurs the original pixel with the surrounding pixels and then subtracts this blur to sharpen the image.
  10.   It does this in luma to avoid color artifacts and allows limiting the maximum sharpening to avoid or lessen halo artifacts.
  11.  
  12.   This is similar to using Unsharp Mask in Photoshop.
  13.  
  14.   Compiles with 3.0
  15. */
  16.  
  17. // -- Sharpening --
  18. #define sharp_strength 0.65   //[0.10 to 3.00] Strength of the sharpening
  19. #define sharp_clamp    0.035  //[0.000 to 1.000] Limits maximum amount of sharpening a pixel recieves - Default is 0.035
  20.  
  21. // -- Advanced sharpening settings --
  22. #define pattern 2        //[1|2|3|4] Choose a sample pattern. 1 = Fast, 2 = Normal, 3 = Wider, 4 = Pyramid shaped.
  23. #define offset_bias 1.0  //[0.0 to 6.0] Offset bias adjusts the radius of the sampling pattern.
  24.                          //I designed the pattern for offset_bias 1.0, but feel free to experiment.
  25.  
  26. // -- Debug sharpening settings --
  27. #define show_sharpen 0   //[0 or 1] Visualize the strength of the sharpen (multiplied by 4 to see it better)
  28.  
  29.    /*-----------------------------------------------------------.
  30.   /                      Developer settings                     /
  31.   '-----------------------------------------------------------*/
  32. #define CoefLuma float3(0.2126, 0.7152, 0.0722)      // BT.709 & sRBG luma coefficient (Monitors and HD Television)
  33. //#define CoefLuma float3(0.299, 0.587, 0.114)       // BT.601 luma coefficient (SD Television)
  34. //#define CoefLuma float3(1.0/3.0, 1.0/3.0, 1.0/3.0) // Equal weight coefficient
  35.  
  36.    /*-----------------------------------------------------------.
  37.   /                          Main code                          /
  38.   '-----------------------------------------------------------*/
  39.  
  40. sampler s0 : register(s0);
  41. float4 p1 : register(c1);
  42.  
  43. #define px p1.x
  44. #define py p1.y
  45.  
  46. float4 main(float2 tex : TEXCOORD0) : COLOR
  47. {
  48.   // -- Get the original pixel --
  49.   float4 ori = tex2D(s0, tex);       // ori = original pixel
  50.  
  51.   // -- Combining the strength and luma multipliers --
  52.   float3 sharp_strength_luma = (CoefLuma * sharp_strength); //I'll be combining even more multipliers with it later on
  53.  
  54.    /*-----------------------------------------------------------.
  55.   /                       Sampling patterns                     /
  56.   '-----------------------------------------------------------*/
  57.   //   [ NW,   , NE ] Each texture lookup (except ori)
  58.   //   [   ,ori,    ] samples 4 pixels
  59.   //   [ SW,   , SE ]
  60.  
  61.   // -- Pattern 1 -- A (fast) 7 tap gaussian using only 2+1 texture fetches.
  62.   #if pattern == 1
  63.  
  64.     // -- Gaussian filter --
  65.     //   [ 1/9, 2/9,    ]     [ 1 , 2 ,   ]
  66.     //   [ 2/9, 8/9, 2/9]  =  [ 2 , 8 , 2 ]
  67.     //   [    , 2/9, 1/9]     [   , 2 , 1 ]
  68.  
  69.     float3 blur_ori = tex2D(s0, tex + (float2(px,py) / 3.0) * offset_bias).rgb;  // North West
  70.     blur_ori += tex2D(s0, tex + (float2(-px,-py) / 3.0) * offset_bias).rgb; // South East
  71.  
  72.     //blur_ori += tex2D(s0, tex + float2(px,py) / 3.0 * offset_bias); // North East
  73.     //blur_ori += tex2D(s0, tex + float2(-px,-py) / 3.0 * offset_bias); // South West
  74.  
  75.     blur_ori /= 2;  //Divide by the number of texture fetches
  76.  
  77.     sharp_strength_luma *= 1.5; // Adjust strength to aproximate the strength of pattern 2
  78.  
  79.   #endif
  80.  
  81.   // -- Pattern 2 -- A 9 tap gaussian using 4+1 texture fetches.
  82.   #if pattern == 2
  83.  
  84.     // -- Gaussian filter --
  85.     //   [ .25, .50, .25]     [ 1 , 2 , 1 ]
  86.     //   [ .50,   1, .50]  =  [ 2 , 4 , 2 ]
  87.     //   [ .25, .50, .25]     [ 1 , 2 , 1 ]
  88.  
  89.  
  90.     float3 blur_ori = tex2D(s0, tex + float2(px,-py) * 0.5 * offset_bias).rgb; // South East
  91.     blur_ori += tex2D(s0, tex + float2(-px,-py) * 0.5 * offset_bias).rgb;  // South West
  92.     blur_ori += tex2D(s0, tex + float2(px,py) * 0.5 * offset_bias).rgb; // North East
  93.     blur_ori += tex2D(s0, tex + float2(-px,py) * 0.5 * offset_bias).rgb; // North West
  94.  
  95.     blur_ori *= 0.25;  // ( /= 4) Divide by the number of texture fetches
  96.  
  97.   #endif
  98.  
  99.   // -- Pattern 3 -- An experimental 17 tap gaussian using 4+1 texture fetches.
  100.   #if pattern == 3
  101.  
  102.     // -- Gaussian filter --
  103.     //   [   , 4 , 6 ,   ,   ]
  104.     //   [   ,16 ,24 ,16 , 4 ]
  105.     //   [ 6 ,24 ,   ,24 , 6 ]
  106.     //   [ 4 ,16 ,24 ,16 ,   ]
  107.     //   [   ,   , 6 , 4 ,   ]
  108.  
  109.     float3 blur_ori = tex2D(s0, tex + float2(0.4*px,-1.2*py)* offset_bias).rgb;  // South South East
  110.     blur_ori += tex2D(s0, tex + float2(-1.2*px,-0.4*py) * offset_bias).rgb; // West South West
  111.     blur_ori += tex2D(s0, tex + float2(1.2*px,0.4*py) * offset_bias).rgb; // East North East
  112.     blur_ori += tex2D(s0, tex + float2(-0.4*px,1.2*py) * offset_bias).rgb; // North North West
  113.  
  114.     blur_ori *= 0.25;  // ( /= 4) Divide by the number of texture fetches
  115.  
  116.     sharp_strength_luma *= 0.51;
  117.   #endif
  118.  
  119.   // -- Pattern 4 -- A 9 tap high pass (pyramid filter) using 4+1 texture fetches.
  120.   #if pattern == 4
  121.  
  122.     // -- Gaussian filter --
  123.     //   [ .50, .50, .50]     [ 1 , 1 , 1 ]
  124.     //   [ .50,    , .50]  =  [ 1 ,   , 1 ]
  125.     //   [ .50, .50, .50]     [ 1 , 1 , 1 ]
  126.  
  127.     float3 blur_ori = tex2D(s0, tex + float2(0.5 * px,-py * offset_bias)).rgb;  // South South East
  128.     blur_ori += tex2D(s0, tex + float2(offset_bias * -px,0.5 * -py)).rgb; // West South West
  129.     blur_ori += tex2D(s0, tex + float2(offset_bias * px,0.5 * py)).rgb; // East North East
  130.     blur_ori += tex2D(s0, tex + float2(0.5 * -px,py * offset_bias)).rgb; // North North West
  131.  
  132.     //blur_ori += (2 * ori); // Probably not needed. Only serves to lessen the effect.
  133.  
  134.     blur_ori /= 4.0;  //Divide by the number of texture fetches
  135.  
  136.     sharp_strength_luma *= 0.666; // Adjust strength to aproximate the strength of pattern 2
  137.   #endif
  138.  
  139.    /*-----------------------------------------------------------.
  140.   /                            Sharpen                          /
  141.   '-----------------------------------------------------------*/
  142.  
  143.   // -- Calculate the sharpening --
  144.   float3 sharp = ori.rgb - blur_ori;  //Subtracting the blurred image from the original image
  145.  
  146.   #if 0 //older SweetFX 1.4 code (included here because the new code while faster can be difficult to understand)
  147.     // -- Adjust strength of the sharpening --
  148.     float sharp_luma = dot(sharp, sharp_strength_luma); //Calculate the luma and adjust the strength
  149.  
  150.     // -- Clamping the maximum amount of sharpening to prevent halo artifacts --
  151.     sharp_luma = clamp(sharp_luma, -sharp_clamp, sharp_clamp);  //TODO Try a curve function instead of a clamp
  152.  
  153.   #else //new code
  154.     // -- Adjust strength of the sharpening and clamp it--
  155.     float4 sharp_strength_luma_clamp = float4(sharp_strength_luma * (0.5 / sharp_clamp),0.5); //Roll part of the clamp into the dot
  156.  
  157.     //sharp_luma = saturate((0.5 / sharp_clamp) * sharp_luma + 0.5); //scale up and clamp
  158.     float sharp_luma = saturate(dot(float4(sharp,1.0), sharp_strength_luma_clamp)); //Calculate the luma, adjust the strength, scale up and clamp
  159.     sharp_luma = (sharp_clamp * 2.0) * sharp_luma - sharp_clamp; //scale down
  160.   #endif
  161.  
  162.   // -- Combining the values to get the final sharpened pixel   --
  163.   float4 outputcolor = ori + sharp_luma;    // Add the sharpening to the the original.
  164.  
  165.    /*-----------------------------------------------------------.
  166.   /                     Returning the output                    /
  167.   '-----------------------------------------------------------*/
  168.   #if show_sharpen == 1
  169.     //outputcolor = abs(sharp * 4.0).rgbr;
  170.     outputcolor = saturate(0.5 + (sharp_luma * 4.0)).rrrr;
  171.   #endif
  172.  
  173.   return saturate(outputcolor);
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement