GlyphBitmap.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Purpose:
  9. // - Hold a glyph bitmap and blit it to the main texture
  10. #include <AtomLyIntegration/AtomFont/GlyphBitmap.h>
  11. #include <math.h>
  12. #include <CryCommon/Cry_Math.h>
  13. //-------------------------------------------------------------------------------------------------
  14. AZ::GlyphBitmap::GlyphBitmap()
  15. : m_width(0)
  16. , m_height(0)
  17. , m_buffer(nullptr)
  18. {
  19. }
  20. //-------------------------------------------------------------------------------------------------
  21. AZ::GlyphBitmap::~GlyphBitmap()
  22. {
  23. }
  24. //-------------------------------------------------------------------------------------------------
  25. int AZ::GlyphBitmap::Create(int width, int height)
  26. {
  27. Release();
  28. m_buffer = AZStd::unique_ptr<uint8_t[]>(new uint8_t[width * height]);
  29. if (!m_buffer)
  30. {
  31. return 0;
  32. }
  33. m_width = width;
  34. m_height = height;
  35. return 1;
  36. }
  37. //-------------------------------------------------------------------------------------------------
  38. int AZ::GlyphBitmap::Release()
  39. {
  40. m_buffer = nullptr;
  41. m_width = m_height = 0;
  42. return 1;
  43. }
  44. //-------------------------------------------------------------------------------------------------
  45. int AZ::GlyphBitmap::Blur(AZ::FontSmoothAmount smoothAmount)
  46. {
  47. int iterationCount = 0;
  48. switch(smoothAmount)
  49. {
  50. case AZ::FontSmoothAmount::x2:
  51. iterationCount = 1;
  52. break;
  53. case AZ::FontSmoothAmount::x4:
  54. iterationCount = 2;
  55. break;
  56. }
  57. int colorSum;
  58. int yOffset;
  59. int yUpOffset;
  60. int yDownOffset;
  61. for (int i = 0; i < iterationCount; i++)
  62. {
  63. for (int y = 0; y < m_height; y++)
  64. {
  65. yOffset = y * m_width;
  66. if (y - 1 >= 0)
  67. {
  68. yUpOffset = (y - 1) * m_width;
  69. }
  70. else
  71. {
  72. yUpOffset = (y) * m_width;
  73. }
  74. if (y + 1 < m_height)
  75. {
  76. yDownOffset = (y + 1) * m_width;
  77. }
  78. else
  79. {
  80. yDownOffset = (y) * m_width;
  81. }
  82. for (int x = 0; x < m_width; x++)
  83. {
  84. colorSum = m_buffer[yUpOffset + x] + m_buffer[yDownOffset + x];
  85. if (x - 1 >= 0)
  86. {
  87. colorSum += m_buffer[yOffset + x - 1];
  88. }
  89. else
  90. {
  91. colorSum += m_buffer[yOffset + x];
  92. }
  93. if (x + 1 < m_width)
  94. {
  95. colorSum += m_buffer[yOffset + x + 1];
  96. }
  97. else
  98. {
  99. colorSum += m_buffer[yOffset + x];
  100. }
  101. m_buffer[yOffset + x] = static_cast<uint8_t>(colorSum >> 2);
  102. }
  103. }
  104. }
  105. return 1;
  106. }
  107. //-------------------------------------------------------------------------------------------------
  108. int AZ::GlyphBitmap::Clear()
  109. {
  110. memset(m_buffer.get(), 0, m_width * m_height);
  111. return 1;
  112. }
  113. //-------------------------------------------------------------------------------------------------
  114. int AZ::GlyphBitmap::BlitTo8(unsigned char* destBuffer, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth)
  115. {
  116. int ySrcOffset;
  117. int yDestOffset;
  118. for (int y = 0; y < srcHeight; y++)
  119. {
  120. ySrcOffset = (srcY + y) * m_width;
  121. yDestOffset = (destY + y) * destWidth;
  122. for (int x = 0; x < srcWidth; x++)
  123. {
  124. destBuffer[yDestOffset + destX + x] = m_buffer[ySrcOffset + srcX + x];
  125. }
  126. }
  127. return 1;
  128. }
  129. //-------------------------------------------------------------------------------------------------
  130. int AZ::GlyphBitmap::BlitScaledTo8(unsigned char* destBuffer, [[maybe_unused]] int srcReadXOffset, int srcReadYOffset, int srcWidth, int srcHeight, int destX, [[maybe_unused]] int destY, int destWidth, int destHeight, int destBufferWidth)
  131. {
  132. int newWidth = (int)destWidth;
  133. int newHeight = (int)destHeight;
  134. float destToSrcXScale = srcWidth / (float)newWidth;
  135. float destToSrcYScale = srcHeight / (float)newHeight;
  136. float srcReadX;
  137. float srcReadY;
  138. float srcReadXFraction;
  139. float srcReadYFraction;
  140. float oneMinusX;
  141. float oneMinusY;
  142. float fR0;
  143. float fR1;
  144. int srcReadXCeil;
  145. int srcReadYCeil;
  146. int srcReadXFloor;
  147. int srcReadYFloor;
  148. int destOffsetY;
  149. uint8_t color0;
  150. uint8_t color1;
  151. uint8_t color2;
  152. uint8_t color3;
  153. for (int y = 0; y < newHeight; ++y)
  154. {
  155. srcReadY = y * destToSrcYScale;
  156. srcReadYFloor = (int)floor_tpl(srcReadY);
  157. srcReadYCeil = srcReadYFloor + 1;
  158. srcReadYFraction = srcReadY - srcReadYFloor;
  159. oneMinusY = 1.0f - srcReadYFraction;
  160. destOffsetY = y * destBufferWidth;
  161. srcReadYFloor += srcReadYOffset;
  162. srcReadYCeil += srcReadYOffset;
  163. if (srcReadYCeil >= m_height)
  164. {
  165. srcReadYCeil = srcReadYFloor;
  166. }
  167. for (int x = 0; x < newWidth; ++x)
  168. {
  169. srcReadX = x * destToSrcXScale;
  170. srcReadXFloor = (int)floor_tpl(srcReadX);
  171. srcReadXCeil = srcReadXFloor + 1;
  172. srcReadXFraction = srcReadX - srcReadXFloor;
  173. oneMinusX = 1.0f - srcReadXFraction;
  174. // possible bug from Cry, using the y offset here
  175. srcReadXFloor += srcReadYOffset;
  176. srcReadXCeil += srcReadYOffset;
  177. if (srcReadXCeil >= m_width)
  178. {
  179. srcReadXCeil = srcReadXFloor;
  180. }
  181. color0 = m_buffer[srcReadYFloor * m_width + srcReadXFloor];
  182. color1 = m_buffer[srcReadYFloor * m_width + srcReadXCeil];
  183. color2 = m_buffer[srcReadYCeil * m_width + srcReadXFloor];
  184. color3 = m_buffer[srcReadYCeil * m_width + srcReadXCeil];
  185. fR0 = (oneMinusX * color0 + srcReadXFraction * color1);
  186. fR1 = (oneMinusX * color2 + srcReadXFraction * color3);
  187. destBuffer[destOffsetY + x + destX] = (unsigned char)((oneMinusY * fR0) + (srcReadYFraction * fR1));
  188. }
  189. }
  190. return 1;
  191. }
  192. //-------------------------------------------------------------------------------------------------