render-sdf.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "render-sdf.h"
  2. #include "arithmetics.hpp"
  3. #include "pixel-conversion.hpp"
  4. namespace msdfgen {
  5. template <typename T, int N>
  6. static void sample(T *output, const BitmapConstRef<T, N> &bitmap, Point2 pos) {
  7. double x = pos.x*bitmap.width-.5;
  8. double y = pos.y*bitmap.height-.5;
  9. int l = (int) floor(x);
  10. int b = (int) floor(y);
  11. int r = l+1;
  12. int t = b+1;
  13. double lr = x-l;
  14. double bt = y-b;
  15. l = clamp(l, bitmap.width-1), r = clamp(r, bitmap.width-1);
  16. b = clamp(b, bitmap.height-1), t = clamp(t, bitmap.height-1);
  17. for (int i = 0; i < N; ++i)
  18. output[i] = mix(mix(bitmap(l, b)[i], bitmap(r, b)[i], lr), mix(bitmap(l, t)[i], bitmap(r, t)[i], lr), bt);
  19. }
  20. static float distVal(float dist, double pxRange) {
  21. if (!pxRange)
  22. return (float) (dist > .5f);
  23. return (float) clamp((dist-.5f)*pxRange+.5);
  24. }
  25. void renderSDF(const BitmapRef<float, 1> &output, const BitmapConstRef<float, 1> &sdf, double pxRange) {
  26. pxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  27. for (int y = 0; y < output.height; ++y)
  28. for (int x = 0; x < output.width; ++x) {
  29. float sd;
  30. sample(&sd, sdf, Point2((x+.5)/output.width, (y+.5)/output.height));
  31. *output(x, y) = distVal(sd, pxRange);
  32. }
  33. }
  34. void renderSDF(const BitmapRef<float, 3> &output, const BitmapConstRef<float, 1> &sdf, double pxRange) {
  35. pxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  36. for (int y = 0; y < output.height; ++y)
  37. for (int x = 0; x < output.width; ++x) {
  38. float sd;
  39. sample(&sd, sdf, Point2((x+.5)/output.width, (y+.5)/output.height));
  40. float v = distVal(sd, pxRange);
  41. output(x, y)[0] = v;
  42. output(x, y)[1] = v;
  43. output(x, y)[2] = v;
  44. }
  45. }
  46. void renderSDF(const BitmapRef<float, 1> &output, const BitmapConstRef<float, 3> &sdf, double pxRange) {
  47. pxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  48. for (int y = 0; y < output.height; ++y)
  49. for (int x = 0; x < output.width; ++x) {
  50. float sd[3];
  51. sample(sd, sdf, Point2((x+.5)/output.width, (y+.5)/output.height));
  52. *output(x, y) = distVal(median(sd[0], sd[1], sd[2]), pxRange);
  53. }
  54. }
  55. void renderSDF(const BitmapRef<float, 3> &output, const BitmapConstRef<float, 3> &sdf, double pxRange) {
  56. pxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  57. for (int y = 0; y < output.height; ++y)
  58. for (int x = 0; x < output.width; ++x) {
  59. float sd[3];
  60. sample(sd, sdf, Point2((x+.5)/output.width, (y+.5)/output.height));
  61. output(x, y)[0] = distVal(sd[0], pxRange);
  62. output(x, y)[1] = distVal(sd[1], pxRange);
  63. output(x, y)[2] = distVal(sd[2], pxRange);
  64. }
  65. }
  66. void renderSDF(const BitmapRef<float, 1> &output, const BitmapConstRef<float, 4> &sdf, double pxRange) {
  67. pxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  68. for (int y = 0; y < output.height; ++y)
  69. for (int x = 0; x < output.width; ++x) {
  70. float sd[4];
  71. sample(sd, sdf, Point2((x+.5)/output.width, (y+.5)/output.height));
  72. *output(x, y) = distVal(median(sd[0], sd[1], sd[2]), pxRange);
  73. }
  74. }
  75. void renderSDF(const BitmapRef<float, 4> &output, const BitmapConstRef<float, 4> &sdf, double pxRange) {
  76. pxRange *= (double) (output.width+output.height)/(sdf.width+sdf.height);
  77. for (int y = 0; y < output.height; ++y)
  78. for (int x = 0; x < output.width; ++x) {
  79. float sd[4];
  80. sample(sd, sdf, Point2((x+.5)/output.width, (y+.5)/output.height));
  81. output(x, y)[0] = distVal(sd[0], pxRange);
  82. output(x, y)[1] = distVal(sd[1], pxRange);
  83. output(x, y)[2] = distVal(sd[2], pxRange);
  84. output(x, y)[3] = distVal(sd[3], pxRange);
  85. }
  86. }
  87. void simulate8bit(const BitmapRef<float, 1> &bitmap) {
  88. const float *end = bitmap.pixels+1*bitmap.width*bitmap.height;
  89. for (float *p = bitmap.pixels; p < end; ++p)
  90. *p = pixelByteToFloat(pixelFloatToByte(*p));
  91. }
  92. void simulate8bit(const BitmapRef<float, 3> &bitmap) {
  93. const float *end = bitmap.pixels+3*bitmap.width*bitmap.height;
  94. for (float *p = bitmap.pixels; p < end; ++p)
  95. *p = pixelByteToFloat(pixelFloatToByte(*p));
  96. }
  97. void simulate8bit(const BitmapRef<float, 4> &bitmap) {
  98. const float *end = bitmap.pixels+4*bitmap.width*bitmap.height;
  99. for (float *p = bitmap.pixels; p < end; ++p)
  100. *p = pixelByteToFloat(pixelFloatToByte(*p));
  101. }
  102. }