distanceField.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "math/mPoint2.h"
  23. #include "gfx/bitmap/gBitmap.h"
  24. #include "gfx/util/distanceField.h"
  25. //-----------------------------------------------------------------------------
  26. struct DistanceFieldSearchSpaceStruct
  27. {
  28. S32 xOffset;
  29. S32 yOffset;
  30. F32 distance;
  31. };
  32. S32 QSORT_CALLBACK cmpSortDistanceFieldSearchSpaceStruct(const void* p1, const void* p2)
  33. {
  34. const DistanceFieldSearchSpaceStruct* sp1 = (const DistanceFieldSearchSpaceStruct*)p1;
  35. const DistanceFieldSearchSpaceStruct* sp2 = (const DistanceFieldSearchSpaceStruct*)p2;
  36. if (sp2->distance > sp1->distance)
  37. return -1;
  38. else if (sp2->distance == sp1->distance)
  39. return 0;
  40. else
  41. return 1;
  42. }
  43. //GBitmap * GFXUtil::DistanceField::makeDistanceField(GBitmap * sourceBmp, S32 targetSizeX, S32 targetSizeY, F32 rangePct)
  44. //{
  45. // AssertFatal(sourceBmp->getFormat() == GFXFormatA8,"Use an alpha-only texture to create distance fields");
  46. //
  47. // static Vector<DistanceFieldSearchSpaceStruct> searchSpace;
  48. //
  49. // S32 sourceSizeX = sourceBmp->getWidth();
  50. // S32 sourceSizeY = sourceBmp->getHeight();
  51. //
  52. // S32 targetToSourceScalarX = sourceSizeX / targetSizeX;
  53. // S32 targetToSourceScalarY = sourceSizeY / targetSizeY;
  54. // S32 targetToSourcePixOffsetX = targetToSourceScalarX / 2;
  55. // S32 targetToSourcePixOffsetY = targetToSourceScalarY / 2;
  56. //
  57. // F32 range = getMin(sourceSizeX,sourceSizeY) * rangePct;
  58. // F32 range2 = range * 2.f;
  59. //
  60. // {
  61. // S32 intRange = mCeil(range);
  62. // for(S32 spaceY = -intRange; spaceY < intRange; spaceY++)
  63. // {
  64. // for(S32 spaceX = -intRange; spaceX < intRange; spaceX++)
  65. // {
  66. // if(spaceX == 0 && spaceY == 0)
  67. // continue;
  68. //
  69. // F32 distance = Point2F(spaceX,spaceY).len();
  70. // if(distance <= range)
  71. // {
  72. // searchSpace.increment();
  73. // searchSpace.last().distance = distance;
  74. // searchSpace.last().xOffset = spaceX;
  75. // searchSpace.last().yOffset = spaceY;
  76. // }
  77. // }
  78. // }
  79. // }
  80. // dQsort(searchSpace.address(), searchSpace.size(), sizeof(DistanceFieldSearchSpaceStruct), cmpSortDistanceFieldSearchSpaceStruct);
  81. //
  82. // GBitmap * targetBmp = new GBitmap(targetSizeX,targetSizeY,false,GFXFormatA8);
  83. //
  84. // U8 * targetPixel = targetBmp->getWritableBits();
  85. // for(S32 y = 0; y < targetSizeY; y++)
  86. // {
  87. // for(S32 x = 0; x < targetSizeX; x++)
  88. // {
  89. // S32 sourceX = x * targetToSourceScalarX + targetToSourcePixOffsetX;
  90. // S32 sourceY = y * targetToSourceScalarY + targetToSourcePixOffsetY;
  91. //
  92. // const U8 * thisPixel = sourceBmp->getAddress(sourceX,sourceY);
  93. //
  94. // bool thisPixelEmpty = *thisPixel == 0;
  95. //
  96. // F32 closestDist = F32_MAX;
  97. //
  98. // for(DistanceFieldSearchSpaceStruct * seachSpaceStructPtr = searchSpace.begin(); seachSpaceStructPtr <= searchSpace.end(); seachSpaceStructPtr++)
  99. // {
  100. // DistanceFieldSearchSpaceStruct & searchSpaceStruct = *seachSpaceStructPtr;
  101. // S32 cx = sourceX + searchSpaceStruct.xOffset;
  102. // if(cx < 0 || cx >= sourceSizeX)
  103. // continue;
  104. //
  105. // S32 cy = sourceY + searchSpaceStruct.yOffset;
  106. // if(cy < 0 || cy >= sourceSizeY)
  107. // continue;
  108. //
  109. // const U8 * checkPixel = sourceBmp->getAddress(cx,cy);
  110. // if((*checkPixel == 0) != thisPixelEmpty)
  111. // {
  112. // closestDist = searchSpaceStruct.distance;
  113. // break;
  114. // }
  115. // }
  116. //
  117. // F32 diff = thisPixelEmpty ? getMax(-0.5f,-(closestDist / range2)) : getMin(0.5f,closestDist / range2);
  118. // F32 targetValue = 0.5f + diff;
  119. //
  120. // *targetPixel = targetValue * 255;
  121. // targetPixel++;
  122. // }
  123. // }
  124. //
  125. // searchSpace.clear();
  126. //
  127. // return targetBmp;
  128. //}
  129. void GFXUtil::DistanceField::makeDistanceField( const U8 * sourceData, S32 sourceSizeX, S32 sourceSizeY, U8 * targetData, S32 targetSizeX, S32 targetSizeY, F32 radius )
  130. {
  131. static Vector<DistanceFieldSearchSpaceStruct> searchSpace;
  132. S32 targetToSourceScalarX = sourceSizeX / targetSizeX;
  133. S32 targetToSourceScalarY = sourceSizeY / targetSizeY;
  134. S32 targetToSourcePixOffsetX = targetToSourceScalarX / 2;
  135. S32 targetToSourcePixOffsetY = targetToSourceScalarY / 2;
  136. F32 radius2 = radius * 2.f;
  137. {
  138. S32 intRange = mCeil(radius);
  139. for(S32 spaceY = -intRange; spaceY < intRange; spaceY++)
  140. {
  141. for(S32 spaceX = -intRange; spaceX < intRange; spaceX++)
  142. {
  143. if(spaceX == 0 && spaceY == 0)
  144. continue;
  145. F32 distance = Point2F(spaceX,spaceY).len();
  146. if(distance <= radius)
  147. {
  148. searchSpace.increment();
  149. searchSpace.last().distance = distance;
  150. searchSpace.last().xOffset = spaceX;
  151. searchSpace.last().yOffset = spaceY;
  152. }
  153. }
  154. }
  155. }
  156. dQsort(searchSpace.address(), searchSpace.size(), sizeof(DistanceFieldSearchSpaceStruct), cmpSortDistanceFieldSearchSpaceStruct);
  157. for(S32 y = 0; y < targetSizeY; y++)
  158. {
  159. for(S32 x = 0; x < targetSizeX; x++)
  160. {
  161. S32 sourceX = x * targetToSourceScalarX + targetToSourcePixOffsetX;
  162. S32 sourceY = y * targetToSourceScalarY + targetToSourcePixOffsetY;
  163. bool thisPixelEmpty = sourceData[sourceY * sourceSizeX + sourceX] < 127;
  164. F32 closestDist = F32_MAX;
  165. for(DistanceFieldSearchSpaceStruct * seachSpaceStructPtr = searchSpace.begin(); seachSpaceStructPtr <= searchSpace.end(); seachSpaceStructPtr++)
  166. {
  167. DistanceFieldSearchSpaceStruct & searchSpaceStruct = *seachSpaceStructPtr;
  168. S32 cx = sourceX + searchSpaceStruct.xOffset;
  169. if(cx < 0 || cx >= sourceSizeX)
  170. continue;
  171. S32 cy = sourceY + searchSpaceStruct.yOffset;
  172. if(cy < 0 || cy >= sourceSizeY)
  173. continue;
  174. if((sourceData[cy * sourceSizeX + cx] < 127) != thisPixelEmpty)
  175. {
  176. closestDist = searchSpaceStruct.distance;
  177. break;
  178. }
  179. }
  180. F32 diff = thisPixelEmpty ? getMax(-0.5f,-(closestDist / radius2)) : getMin(0.5f,closestDist / radius2);
  181. F32 targetValue = 0.5f + diff;
  182. *targetData = targetValue * 255;
  183. targetData++;
  184. }
  185. }
  186. searchSpace.clear();
  187. }