bitmap-interpolation.hpp 669 B

1234567891011121314151617181920212223242526
  1. #pragma once
  2. #include "arithmetics.hpp"
  3. #include "Vector2.h"
  4. #include "BitmapRef.hpp"
  5. namespace msdfgen {
  6. template <typename T, int N>
  7. static void interpolate(T *output, const BitmapConstRef<T, N> &bitmap, Point2 pos) {
  8. pos -= .5;
  9. int l = (int) floor(pos.x);
  10. int b = (int) floor(pos.y);
  11. int r = l+1;
  12. int t = b+1;
  13. double lr = pos.x-l;
  14. double bt = pos.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. }