2
0

render-sdf.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "render-sdf.h"
  2. #include "arithmetics.hpp"
  3. #include "DistanceMapping.h"
  4. #include "pixel-conversion.hpp"
  5. #include "bitmap-interpolation.hpp"
  6. namespace msdfgen {
  7. static float distVal(float dist, DistanceMapping mapping) {
  8. return (float) clamp(mapping(dist)+.5);
  9. }
  10. void renderSDF(const BitmapRef<float, 1> &output, const BitmapConstRef<float, 1> &sdf, Range sdfPxRange, float sdThreshold) {
  11. Vector2 scale((double) sdf.width/output.width, (double) sdf.height/output.height);
  12. if (sdfPxRange.lower == sdfPxRange.upper) {
  13. for (int y = 0; y < output.height; ++y) {
  14. for (int x = 0; x < output.width; ++x) {
  15. float sd;
  16. interpolate(&sd, sdf, scale*Point2(x+.5, y+.5));
  17. *output(x, y) = float(sd >= sdThreshold);
  18. }
  19. }
  20. } else {
  21. sdfPxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  22. DistanceMapping distanceMapping = DistanceMapping::inverse(sdfPxRange);
  23. float sdBias = .5f-sdThreshold;
  24. for (int y = 0; y < output.height; ++y) {
  25. for (int x = 0; x < output.width; ++x) {
  26. float sd;
  27. interpolate(&sd, sdf, scale*Point2(x+.5, y+.5));
  28. *output(x, y) = distVal(sd+sdBias, distanceMapping);
  29. }
  30. }
  31. }
  32. }
  33. void renderSDF(const BitmapRef<float, 3> &output, const BitmapConstRef<float, 1> &sdf, Range sdfPxRange, float sdThreshold) {
  34. Vector2 scale((double) sdf.width/output.width, (double) sdf.height/output.height);
  35. if (sdfPxRange.lower == sdfPxRange.upper) {
  36. for (int y = 0; y < output.height; ++y) {
  37. for (int x = 0; x < output.width; ++x) {
  38. float sd;
  39. interpolate(&sd, sdf, scale*Point2(x+.5, y+.5));
  40. float v = float(sd >= sdThreshold);
  41. output(x, y)[0] = v;
  42. output(x, y)[1] = v;
  43. output(x, y)[2] = v;
  44. }
  45. }
  46. } else {
  47. sdfPxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  48. DistanceMapping distanceMapping = DistanceMapping::inverse(sdfPxRange);
  49. float sdBias = .5f-sdThreshold;
  50. for (int y = 0; y < output.height; ++y) {
  51. for (int x = 0; x < output.width; ++x) {
  52. float sd;
  53. interpolate(&sd, sdf, scale*Point2(x+.5, y+.5));
  54. float v = distVal(sd+sdBias, distanceMapping);
  55. output(x, y)[0] = v;
  56. output(x, y)[1] = v;
  57. output(x, y)[2] = v;
  58. }
  59. }
  60. }
  61. }
  62. void renderSDF(const BitmapRef<float, 1> &output, const BitmapConstRef<float, 3> &sdf, Range sdfPxRange, float sdThreshold) {
  63. Vector2 scale((double) sdf.width/output.width, (double) sdf.height/output.height);
  64. if (sdfPxRange.lower == sdfPxRange.upper) {
  65. for (int y = 0; y < output.height; ++y) {
  66. for (int x = 0; x < output.width; ++x) {
  67. float sd[3];
  68. interpolate(sd, sdf, scale*Point2(x+.5, y+.5));
  69. *output(x, y) = float(median(sd[0], sd[1], sd[2]) >= sdThreshold);
  70. }
  71. }
  72. } else {
  73. sdfPxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  74. DistanceMapping distanceMapping = DistanceMapping::inverse(sdfPxRange);
  75. float sdBias = .5f-sdThreshold;
  76. for (int y = 0; y < output.height; ++y) {
  77. for (int x = 0; x < output.width; ++x) {
  78. float sd[3];
  79. interpolate(sd, sdf, scale*Point2(x+.5, y+.5));
  80. *output(x, y) = distVal(median(sd[0], sd[1], sd[2])+sdBias, distanceMapping);
  81. }
  82. }
  83. }
  84. }
  85. void renderSDF(const BitmapRef<float, 3> &output, const BitmapConstRef<float, 3> &sdf, Range sdfPxRange, float sdThreshold) {
  86. Vector2 scale((double) sdf.width/output.width, (double) sdf.height/output.height);
  87. if (sdfPxRange.lower == sdfPxRange.upper) {
  88. for (int y = 0; y < output.height; ++y) {
  89. for (int x = 0; x < output.width; ++x) {
  90. float sd[3];
  91. interpolate(sd, sdf, scale*Point2(x+.5, y+.5));
  92. output(x, y)[0] = float(sd[0] >= sdThreshold);
  93. output(x, y)[1] = float(sd[1] >= sdThreshold);
  94. output(x, y)[2] = float(sd[2] >= sdThreshold);
  95. }
  96. }
  97. } else {
  98. sdfPxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  99. DistanceMapping distanceMapping = DistanceMapping::inverse(sdfPxRange);
  100. float sdBias = .5f-sdThreshold;
  101. for (int y = 0; y < output.height; ++y) {
  102. for (int x = 0; x < output.width; ++x) {
  103. float sd[3];
  104. interpolate(sd, sdf, scale*Point2(x+.5, y+.5));
  105. output(x, y)[0] = distVal(sd[0]+sdBias, distanceMapping);
  106. output(x, y)[1] = distVal(sd[1]+sdBias, distanceMapping);
  107. output(x, y)[2] = distVal(sd[2]+sdBias, distanceMapping);
  108. }
  109. }
  110. }
  111. }
  112. void renderSDF(const BitmapRef<float, 1> &output, const BitmapConstRef<float, 4> &sdf, Range sdfPxRange, float sdThreshold) {
  113. Vector2 scale((double) sdf.width/output.width, (double) sdf.height/output.height);
  114. if (sdfPxRange.lower == sdfPxRange.upper) {
  115. for (int y = 0; y < output.height; ++y) {
  116. for (int x = 0; x < output.width; ++x) {
  117. float sd[4];
  118. interpolate(sd, sdf, scale*Point2(x+.5, y+.5));
  119. *output(x, y) = float(median(sd[0], sd[1], sd[2]) >= sdThreshold);
  120. }
  121. }
  122. } else {
  123. sdfPxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  124. DistanceMapping distanceMapping = DistanceMapping::inverse(sdfPxRange);
  125. float sdBias = .5f-sdThreshold;
  126. for (int y = 0; y < output.height; ++y) {
  127. for (int x = 0; x < output.width; ++x) {
  128. float sd[4];
  129. interpolate(sd, sdf, scale*Point2(x+.5, y+.5));
  130. *output(x, y) = distVal(median(sd[0], sd[1], sd[2])+sdBias, distanceMapping);
  131. }
  132. }
  133. }
  134. }
  135. void renderSDF(const BitmapRef<float, 4> &output, const BitmapConstRef<float, 4> &sdf, Range sdfPxRange, float sdThreshold) {
  136. Vector2 scale((double) sdf.width/output.width, (double) sdf.height/output.height);
  137. if (sdfPxRange.lower == sdfPxRange.upper) {
  138. for (int y = 0; y < output.height; ++y) {
  139. for (int x = 0; x < output.width; ++x) {
  140. float sd[4];
  141. interpolate(sd, sdf, scale*Point2(x+.5, y+.5));
  142. output(x, y)[0] = float(sd[0] >= sdThreshold);
  143. output(x, y)[1] = float(sd[1] >= sdThreshold);
  144. output(x, y)[2] = float(sd[2] >= sdThreshold);
  145. output(x, y)[3] = float(sd[3] >= sdThreshold);
  146. }
  147. }
  148. } else {
  149. sdfPxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  150. DistanceMapping distanceMapping = DistanceMapping::inverse(sdfPxRange);
  151. float sdBias = .5f-sdThreshold;
  152. for (int y = 0; y < output.height; ++y) {
  153. for (int x = 0; x < output.width; ++x) {
  154. float sd[4];
  155. interpolate(sd, sdf, scale*Point2(x+.5, y+.5));
  156. output(x, y)[0] = distVal(sd[0]+sdBias, distanceMapping);
  157. output(x, y)[1] = distVal(sd[1]+sdBias, distanceMapping);
  158. output(x, y)[2] = distVal(sd[2]+sdBias, distanceMapping);
  159. output(x, y)[3] = distVal(sd[3]+sdBias, distanceMapping);
  160. }
  161. }
  162. }
  163. }
  164. void simulate8bit(const BitmapRef<float, 1> &bitmap) {
  165. const float *end = bitmap.pixels+1*bitmap.width*bitmap.height;
  166. for (float *p = bitmap.pixels; p < end; ++p)
  167. *p = pixelByteToFloat(pixelFloatToByte(*p));
  168. }
  169. void simulate8bit(const BitmapRef<float, 3> &bitmap) {
  170. const float *end = bitmap.pixels+3*bitmap.width*bitmap.height;
  171. for (float *p = bitmap.pixels; p < end; ++p)
  172. *p = pixelByteToFloat(pixelFloatToByte(*p));
  173. }
  174. void simulate8bit(const BitmapRef<float, 4> &bitmap) {
  175. const float *end = bitmap.pixels+4*bitmap.width*bitmap.height;
  176. for (float *p = bitmap.pixels; p < end; ++p)
  177. *p = pixelByteToFloat(pixelFloatToByte(*p));
  178. }
  179. }