msdf-error-correction.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "msdf-error-correction.h"
  2. #include <vector>
  3. #include "arithmetics.hpp"
  4. namespace msdfgen {
  5. inline static bool detectClash(const float *a, const float *b, double threshold) {
  6. // Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference
  7. float a0 = a[0], a1 = a[1], a2 = a[2];
  8. float b0 = b[0], b1 = b[1], b2 = b[2];
  9. float tmp;
  10. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  11. tmp = a0, a0 = a1, a1 = tmp;
  12. tmp = b0, b0 = b1, b1 = tmp;
  13. }
  14. if (fabsf(b1-a1) < fabsf(b2-a2)) {
  15. tmp = a1, a1 = a2, a2 = tmp;
  16. tmp = b1, b1 = b2, b2 = tmp;
  17. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  18. tmp = a0, a0 = a1, a1 = tmp;
  19. tmp = b0, b0 = b1, b1 = tmp;
  20. }
  21. }
  22. return (fabsf(b1-a1) >= threshold) &&
  23. !(b0 == b1 && b0 == b2) && // Ignore if other pixel has been equalized
  24. fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge
  25. }
  26. template <int N>
  27. void msdfErrorCorrectionInner(const BitmapRef<float, N> &output, const Vector2 &threshold) {
  28. std::vector<std::pair<int, int> > clashes;
  29. int w = output.width, h = output.height;
  30. for (int y = 0; y < h; ++y)
  31. for (int x = 0; x < w; ++x) {
  32. if (
  33. (x > 0 && detectClash(output(x, y), output(x-1, y), threshold.x)) ||
  34. (x < w-1 && detectClash(output(x, y), output(x+1, y), threshold.x)) ||
  35. (y > 0 && detectClash(output(x, y), output(x, y-1), threshold.y)) ||
  36. (y < h-1 && detectClash(output(x, y), output(x, y+1), threshold.y))
  37. )
  38. clashes.push_back(std::make_pair(x, y));
  39. }
  40. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  41. float *pixel = output(clash->first, clash->second);
  42. float med = median(pixel[0], pixel[1], pixel[2]);
  43. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  44. }
  45. #ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION
  46. clashes.clear();
  47. for (int y = 0; y < h; ++y)
  48. for (int x = 0; x < w; ++x) {
  49. if (
  50. (x > 0 && y > 0 && detectClash(output(x, y), output(x-1, y-1), threshold.x+threshold.y)) ||
  51. (x < w-1 && y > 0 && detectClash(output(x, y), output(x+1, y-1), threshold.x+threshold.y)) ||
  52. (x > 0 && y < h-1 && detectClash(output(x, y), output(x-1, y+1), threshold.x+threshold.y)) ||
  53. (x < w-1 && y < h-1 && detectClash(output(x, y), output(x+1, y+1), threshold.x+threshold.y))
  54. )
  55. clashes.push_back(std::make_pair(x, y));
  56. }
  57. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  58. float *pixel = output(clash->first, clash->second);
  59. float med = median(pixel[0], pixel[1], pixel[2]);
  60. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  61. }
  62. #endif
  63. }
  64. void msdfErrorCorrection(const BitmapRef<float, 3> &output, const Vector2 &threshold) {
  65. msdfErrorCorrectionInner(output, threshold);
  66. }
  67. void msdfErrorCorrection(const BitmapRef<float, 4> &output, const Vector2 &threshold) {
  68. msdfErrorCorrectionInner(output, threshold);
  69. }
  70. void msdfErrorCorrection(const BitmapRef<float, 3> &output, double threshold, const Projection &projection, double range) {
  71. msdfErrorCorrectionInner(output, projection.unprojectVector(Vector2(threshold/range)));
  72. }
  73. void msdfErrorCorrection(const BitmapRef<float, 4> &output, double threshold, const Projection &projection, double range) {
  74. msdfErrorCorrectionInner(output, projection.unprojectVector(Vector2(threshold/range)));
  75. }
  76. }