Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.1 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Font effects that operate on linear rgba data
  4. //
  5. //=====================================================================================//
  6. #include "tier0/platform.h"
  7. #include <tier0/dbg.h>
  8. #include <math.h>
  9. #include "FontEffects.h"
  10. // NOTE: This has to be the last file included!
  11. #include "tier0/memdbgon.h"
  12. //-----------------------------------------------------------------------------
  13. // Purpose: Adds center line to font
  14. //-----------------------------------------------------------------------------
  15. void ApplyRotaryEffectToTexture( int rgbaWide, int rgbaTall, unsigned char *rgba, bool bRotary )
  16. {
  17. if ( !bRotary )
  18. return;
  19. int y = rgbaTall * 0.5;
  20. unsigned char *line = &rgba[(y * rgbaWide) * 4];
  21. // Draw a line down middle
  22. for (int x = 0; x < rgbaWide; x++, line+=4)
  23. {
  24. line[0] = 127;
  25. line[1] = 127;
  26. line[2] = 127;
  27. line[3] = 255;
  28. }
  29. }
  30. //-----------------------------------------------------------------------------
  31. // Purpose: adds scanlines to the texture
  32. //-----------------------------------------------------------------------------
  33. void ApplyScanlineEffectToTexture( int rgbaWide, int rgbaTall, unsigned char *rgba, int iScanLines )
  34. {
  35. if ( iScanLines < 2 )
  36. return;
  37. float scale;
  38. scale = 0.7f;
  39. // darken all the areas except the scanlines
  40. for (int y = 0; y < rgbaTall; y++)
  41. {
  42. // skip the scan lines
  43. if (y % iScanLines == 0)
  44. continue;
  45. unsigned char *pBits = &rgba[(y * rgbaWide) * 4];
  46. // darken the other lines
  47. for (int x = 0; x < rgbaWide; x++, pBits += 4)
  48. {
  49. pBits[0] *= scale;
  50. pBits[1] *= scale;
  51. pBits[2] *= scale;
  52. }
  53. }
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Purpose: adds a dropshadow the the font texture
  57. //-----------------------------------------------------------------------------
  58. void ApplyDropShadowToTexture( int rgbaWide, int rgbaTall, unsigned char *rgba, int iDropShadowOffset )
  59. {
  60. if ( !iDropShadowOffset )
  61. return;
  62. // walk the original image from the bottom up
  63. // shifting it down and right, and turning it black (the dropshadow)
  64. for (int y = rgbaTall - 1; y >= iDropShadowOffset; y--)
  65. {
  66. for (int x = rgbaWide - 1; x >= iDropShadowOffset; x--)
  67. {
  68. unsigned char *dest = &rgba[(x + (y * rgbaWide)) * 4];
  69. if (dest[3] == 0)
  70. {
  71. // there is nothing in this spot, copy in the dropshadow
  72. unsigned char *src = &rgba[(x - iDropShadowOffset + ((y - iDropShadowOffset) * rgbaWide)) * 4];
  73. dest[0] = 0;
  74. dest[1] = 0;
  75. dest[2] = 0;
  76. dest[3] = src[3];
  77. }
  78. }
  79. }
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Purpose: adds an outline to the font texture
  83. //-----------------------------------------------------------------------------
  84. void ApplyOutlineToTexture( int rgbaWide, int rgbaTall, unsigned char *rgba, int iOutlineSize )
  85. {
  86. if ( !iOutlineSize )
  87. return;
  88. int x, y;
  89. for( y = 0; y < rgbaTall; y++ )
  90. {
  91. for( x = 0; x < rgbaWide; x++ )
  92. {
  93. unsigned char *src = &rgba[(x + (y * rgbaWide)) * 4];
  94. if( src[3] == 0 )
  95. {
  96. // We have a valid font texel. Make all the alpha == 0 neighbors black.
  97. int shadowX, shadowY;
  98. for( shadowX = -(int)iOutlineSize; shadowX <= (int)iOutlineSize; shadowX++ )
  99. {
  100. for( shadowY = -(int)iOutlineSize; shadowY <= (int)iOutlineSize; shadowY++ )
  101. {
  102. if( shadowX == 0 && shadowY == 0 )
  103. {
  104. continue;
  105. }
  106. int testX, testY;
  107. testX = shadowX + x;
  108. testY = shadowY + y;
  109. if( testX < 0 || testX >= rgbaWide ||
  110. testY < 0 || testY >= rgbaTall )
  111. {
  112. continue;
  113. }
  114. unsigned char *test = &rgba[(testX + (testY * rgbaWide)) * 4];
  115. if( test[0] != 0 && test[1] != 0 && test[2] != 0 && test[3] != 0 )
  116. {
  117. src[0] = 0;
  118. src[1] = 0;
  119. src[2] = 0;
  120. src[3] = 255;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Purpose: Gets the blur value for a single pixel
  130. //-----------------------------------------------------------------------------
  131. FORCEINLINE void GetBlurValueForPixel(unsigned char *src, int blur, float *gaussianDistribution, int srcX, int srcY, int rgbaWide, int rgbaTall, unsigned char *dest)
  132. {
  133. float accum = 0.0f;
  134. // scan the positive x direction
  135. int maxX = MIN(srcX + blur, rgbaWide - 1);
  136. int minX = MAX(srcX - blur, 0);
  137. for (int x = minX; x <= maxX; x++)
  138. {
  139. int maxY = MIN(srcY + blur, rgbaTall - 1);
  140. int minY = MAX(srcY - blur, 0);
  141. for (int y = minY; y <= maxY; y++)
  142. {
  143. unsigned char *srcPos = src + ((x + (y * rgbaWide)) * 4);
  144. // muliply by the value matrix
  145. float weight = gaussianDistribution[x - srcX + blur];
  146. float weight2 = gaussianDistribution[y - srcY + blur];
  147. accum += (srcPos[0] * (weight * weight2));
  148. }
  149. }
  150. dest[0] = dest[1] = dest[2] = 255; //leave ALL pixels white or we get black backgrounds mixed in
  151. dest[3] = MIN( (int)accum, 255); //blur occurs entirely in the alpha
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Purpose: blurs the texture
  155. //-----------------------------------------------------------------------------
  156. void ApplyGaussianBlurToTexture( int rgbaWide, int rgbaTall, unsigned char *rgba, int iBlur )
  157. {
  158. float *pGaussianDistribution;
  159. if ( !iBlur )
  160. return;
  161. // generate the gaussian field
  162. pGaussianDistribution = (float*) stackalloc( (iBlur*2+1) * sizeof(float) );
  163. double sigma = 0.683 * iBlur;
  164. for (int x = 0; x <= (iBlur * 2); x++)
  165. {
  166. int val = x - iBlur;
  167. pGaussianDistribution[x] = (float)( 1.0f / sqrt(2 * 3.14 * sigma * sigma)) * pow(2.7, -1 * (val * val) / (2 * sigma * sigma));
  168. }
  169. // alloc a new buffer
  170. unsigned char *src = (unsigned char *) stackalloc( rgbaWide * rgbaTall * 4);
  171. // copy in
  172. memcpy(src, rgba, rgbaWide * rgbaTall * 4);
  173. // incrementing destination pointer
  174. unsigned char *dest = rgba;
  175. for (int y = 0; y < rgbaTall; y++)
  176. {
  177. for (int x = 0; x < rgbaWide; x++)
  178. {
  179. // scan the source pixel
  180. GetBlurValueForPixel(src, iBlur, pGaussianDistribution, x, y, rgbaWide, rgbaTall, dest);
  181. // move to the next
  182. dest += 4;
  183. }
  184. }
  185. }