LumaSharpen.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. _____________________
  3. LumaSharpen 1.4.1
  4. _____________________
  5. by Christian Cann Schuldt Jensen ~ CeeJay.dk
  6. It blurs the original pixel with the surrounding pixels and then subtracts this blur to sharpen the image.
  7. It does this in luma to avoid color artifacts and allows limiting the maximum sharpning to avoid or lessen halo artifacts.
  8. This is similar to using Unsharp Mask in Photoshop.
  9. Compiles with 3.0
  10. */
  11. /*-----------------------------------------------------------.
  12. / Developer settings /
  13. '-----------------------------------------------------------*/
  14. #define CoefLuma float3(0.2126, 0.7152, 0.0722) // BT.709 & sRBG luma coefficient (Monitors and HD Television)
  15. //#define CoefLuma float3(0.299, 0.587, 0.114) // BT.601 luma coefficient (SD Television)
  16. //#define CoefLuma float3(1.0/3.0, 1.0/3.0, 1.0/3.0) // Equal weight coefficient
  17. /*-----------------------------------------------------------.
  18. / Main code /
  19. '-----------------------------------------------------------*/
  20. float4 LumaSharpenPass(float4 inputcolor, float2 tex )
  21. {
  22. // -- Get the original pixel --
  23. float3 ori = myTex2D(s0, tex).rgb; // ori = original pixel
  24. // -- Combining the strength and luma multipliers --
  25. float3 sharp_strength_luma = (CoefLuma * sharp_strength); //I'll be combining even more multipliers with it later on
  26. /*-----------------------------------------------------------.
  27. / Sampling patterns /
  28. '-----------------------------------------------------------*/
  29. // [ NW, , NE ] Each texture lookup (except ori)
  30. // [ ,ori, ] samples 4 pixels
  31. // [ SW, , SE ]
  32. // -- Pattern 1 -- A (fast) 7 tap gaussian using only 2+1 texture fetches.
  33. #if pattern == 1
  34. // -- Gaussian filter --
  35. // [ 1/9, 2/9, ] [ 1 , 2 , ]
  36. // [ 2/9, 8/9, 2/9] = [ 2 , 8 , 2 ]
  37. // [ , 2/9, 1/9] [ , 2 , 1 ]
  38. float3 blur_ori = myTex2D(s0, tex + (float2(px,py) / 3.0) * offset_bias).rgb; // North West
  39. blur_ori += myTex2D(s0, tex + (float2(-px,-py) / 3.0) * offset_bias).rgb; // South East
  40. //blur_ori += myTex2D(s0, tex + float2(px,py) / 3.0 * offset_bias); // North East
  41. //blur_ori += myTex2D(s0, tex + float2(-px,-py) / 3.0 * offset_bias); // South West
  42. blur_ori /= 2; //Divide by the number of texture fetches
  43. sharp_strength_luma *= 1.5; // Adjust strength to aproximate the strength of pattern 2
  44. #endif
  45. // -- Pattern 2 -- A 9 tap gaussian using 4+1 texture fetches.
  46. #if pattern == 2
  47. // -- Gaussian filter --
  48. // [ .25, .50, .25] [ 1 , 2 , 1 ]
  49. // [ .50, 1, .50] = [ 2 , 4 , 2 ]
  50. // [ .25, .50, .25] [ 1 , 2 , 1 ]
  51. float3 blur_ori = myTex2D(s0, tex + float2(px,-py) * 0.5 * offset_bias).rgb; // South East
  52. blur_ori += myTex2D(s0, tex + float2(-px,-py) * 0.5 * offset_bias).rgb; // South West
  53. blur_ori += myTex2D(s0, tex + float2(px,py) * 0.5 * offset_bias).rgb; // North East
  54. blur_ori += myTex2D(s0, tex + float2(-px,py) * 0.5 * offset_bias).rgb; // North West
  55. blur_ori *= 0.25; // ( /= 4) Divide by the number of texture fetches
  56. #endif
  57. // -- Pattern 3 -- An experimental 17 tap gaussian using 4+1 texture fetches.
  58. #if pattern == 3
  59. // -- Gaussian filter --
  60. // [ , 4 , 6 , , ]
  61. // [ ,16 ,24 ,16 , 4 ]
  62. // [ 6 ,24 , ,24 , 6 ]
  63. // [ 4 ,16 ,24 ,16 , ]
  64. // [ , , 6 , 4 , ]
  65. float3 blur_ori = myTex2D(s0, tex + float2(0.4*px,-1.2*py)* offset_bias).rgb; // South South East
  66. blur_ori += myTex2D(s0, tex + float2(-1.2*px,-0.4*py) * offset_bias).rgb; // West South West
  67. blur_ori += myTex2D(s0, tex + float2(1.2*px,0.4*py) * offset_bias).rgb; // East North East
  68. blur_ori += myTex2D(s0, tex + float2(-0.4*px,1.2*py) * offset_bias).rgb; // North North West
  69. blur_ori *= 0.25; // ( /= 4) Divide by the number of texture fetches
  70. sharp_strength_luma *= 0.51;
  71. #endif
  72. // -- Pattern 4 -- A 9 tap high pass (pyramid filter) using 4+1 texture fetches.
  73. #if pattern == 4
  74. // -- Gaussian filter --
  75. // [ .50, .50, .50] [ 1 , 1 , 1 ]
  76. // [ .50, , .50] = [ 1 , , 1 ]
  77. // [ .50, .50, .50] [ 1 , 1 , 1 ]
  78. float3 blur_ori = myTex2D(s0, tex + float2(0.5 * px,-py * offset_bias)).rgb; // South South East
  79. blur_ori += myTex2D(s0, tex + float2(offset_bias * -px,0.5 * -py)).rgb; // West South West
  80. blur_ori += myTex2D(s0, tex + float2(offset_bias * px,0.5 * py)).rgb; // East North East
  81. blur_ori += myTex2D(s0, tex + float2(0.5 * -px,py * offset_bias)).rgb; // North North West
  82. //blur_ori += (2 * ori); // Probably not needed. Only serves to lessen the effect.
  83. blur_ori /= 4.0; //Divide by the number of texture fetches
  84. sharp_strength_luma *= 0.666; // Adjust strength to aproximate the strength of pattern 2
  85. #endif
  86. // -- Pattern 8 -- A (slower) 9 tap gaussian using 9 texture fetches.
  87. #if pattern == 8
  88. // -- Gaussian filter --
  89. // [ 1 , 2 , 1 ]
  90. // [ 2 , 4 , 2 ]
  91. // [ 1 , 2 , 1 ]
  92. half3 blur_ori = myTex2D(s0, tex + float2(-px,py) * offset_bias).rgb; // North West
  93. blur_ori += myTex2D(s0, tex + float2(px,-py) * offset_bias).rgb; // South East
  94. blur_ori += myTex2D(s0, tex + float2(-px,-py) * offset_bias).rgb; // South West
  95. blur_ori += myTex2D(s0, tex + float2(px,py) * offset_bias).rgb; // North East
  96. half3 blur_ori2 = myTex2D(s0, tex + float2(0,py) * offset_bias).rgb; // North
  97. blur_ori2 += myTex2D(s0, tex + float2(0,-py) * offset_bias).rgb; // South
  98. blur_ori2 += myTex2D(s0, tex + float2(-px,0) * offset_bias).rgb; // West
  99. blur_ori2 += myTex2D(s0, tex + float2(px,0) * offset_bias).rgb; // East
  100. blur_ori2 *= 2.0;
  101. blur_ori += blur_ori2;
  102. blur_ori += (ori * 4); // Probably not needed. Only serves to lessen the effect.
  103. // dot()s with gaussian strengths here?
  104. blur_ori /= 16.0; //Divide by the number of texture fetches
  105. //sharp_strength_luma *= 0.75; // Adjust strength to aproximate the strength of pattern 2
  106. #endif
  107. // -- Pattern 9 -- A (slower) 9 tap high pass using 9 texture fetches.
  108. #if pattern == 9
  109. // -- Gaussian filter --
  110. // [ 1 , 1 , 1 ]
  111. // [ 1 , 1 , 1 ]
  112. // [ 1 , 1 , 1 ]
  113. float3 blur_ori = myTex2D(s0, tex + float2(-px,py) * offset_bias).rgb; // North West
  114. blur_ori += myTex2D(s0, tex + float2(px,-py) * offset_bias).rgb; // South East
  115. blur_ori += myTex2D(s0, tex + float2(-px,-py) * offset_bias).rgb; // South West
  116. blur_ori += myTex2D(s0, tex + float2(px,py) * offset_bias).rgb; // North East
  117. blur_ori += ori.rgb; // Probably not needed. Only serves to lessen the effect.
  118. blur_ori += myTex2D(s0, tex + float2(0,py) * offset_bias).rgb; // North
  119. blur_ori += myTex2D(s0, tex + float2(0,-py) * offset_bias).rgb; // South
  120. blur_ori += myTex2D(s0, tex + float2(-px,0) * offset_bias).rgb; // West
  121. blur_ori += myTex2D(s0, tex + float2(px,0) * offset_bias).rgb; // East
  122. blur_ori /= 9; //Divide by the number of texture fetches
  123. //sharp_strength_luma *= (8.0/9.0); // Adjust strength to aproximate the strength of pattern 2
  124. #endif
  125. /*-----------------------------------------------------------.
  126. / Sharpen /
  127. '-----------------------------------------------------------*/
  128. // -- Calculate the sharpening --
  129. float3 sharp = ori - blur_ori; //Subtracting the blurred image from the original image
  130. #if 0 //New experimental limiter .. not yet finished
  131. float sharp_luma = dot(sharp, sharp_strength_luma); //Calculate the luma
  132. sharp_luma = (abs(sharp_luma)*8.0) * exp(1.0-(abs(sharp_luma)*8.0)) * sign(sharp_luma) / 16.0; //I should probably move the strength modifier here
  133. #elif 0 //SweetFX 1.4 code
  134. // -- Adjust strength of the sharpening --
  135. float sharp_luma = dot(sharp, sharp_strength_luma); //Calculate the luma and adjust the strength
  136. // -- Clamping the maximum amount of sharpening to prevent halo artifacts --
  137. sharp_luma = clamp(sharp_luma, -sharp_clamp, sharp_clamp); //TODO Try a curve function instead of a clamp
  138. #else //SweetFX 1.5.1 code
  139. // -- Adjust strength of the sharpening and clamp it--
  140. float4 sharp_strength_luma_clamp = float4(sharp_strength_luma * (0.5 / sharp_clamp),0.5); //Roll part of the clamp into the dot
  141. //sharp_luma = saturate((0.5 / sharp_clamp) * sharp_luma + 0.5); //scale up and clamp
  142. float sharp_luma = saturate(dot(float4(sharp,1.0), sharp_strength_luma_clamp)); //Calculate the luma, adjust the strength, scale up and clamp
  143. sharp_luma = (sharp_clamp * 2.0) * sharp_luma - sharp_clamp; //scale down
  144. #endif
  145. // -- Combining the values to get the final sharpened pixel --
  146. //float4 done = ori + sharp_luma; // Add the sharpening to the original.
  147. inputcolor.rgb = inputcolor.rgb + sharp_luma; // Add the sharpening to the input color.
  148. /*-----------------------------------------------------------.
  149. / Returning the output /
  150. '-----------------------------------------------------------*/
  151. #if show_sharpen == 1
  152. //inputcolor.rgb = abs(sharp * 4.0);
  153. inputcolor.rgb = saturate(0.5 + (sharp_luma * 4)).rrr;
  154. #endif
  155. return saturate(inputcolor);
  156. }