2
0

render-sdf.cpp 4.4 KB

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