fill.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. namespace nall {
  3. inline auto image::fill(uint64_t color) -> void {
  4. for(uint y = 0; y < _height; y++) {
  5. uint8_t* dp = _data + pitch() * y;
  6. for(uint x = 0; x < _width; x++) {
  7. write(dp, color);
  8. dp += stride();
  9. }
  10. }
  11. }
  12. inline auto image::gradient(uint64_t a, uint64_t b, uint64_t c, uint64_t d) -> void {
  13. for(uint y = 0; y < _height; y++) {
  14. uint8_t* dp = _data + pitch() * y;
  15. double muY = (double)y / (double)_height;
  16. for(uint x = 0; x < _width; x++) {
  17. double muX = (double)x / (double)_width;
  18. write(dp, interpolate4f(a, b, c, d, muX, muY));
  19. dp += stride();
  20. }
  21. }
  22. }
  23. inline auto image::gradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY, function<double (double, double)> callback) -> void {
  24. for(int y = 0; y < _height; y++) {
  25. uint8_t* dp = _data + pitch() * y;
  26. double py = max(-radiusY, min(+radiusY, y - centerY)) * 1.0 / radiusY;
  27. for(int x = 0; x < _width; x++) {
  28. double px = max(-radiusX, min(+radiusX, x - centerX)) * 1.0 / radiusX;
  29. double mu = max(0.0, min(1.0, callback(px, py)));
  30. if(mu != mu) mu = 1.0; //NaN
  31. write(dp, interpolate4f(a, b, mu));
  32. dp += stride();
  33. }
  34. }
  35. }
  36. inline auto image::crossGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void {
  37. return gradient(a, b, radiusX, radiusY, centerX, centerY, [](double x, double y) -> double {
  38. x = fabs(x), y = fabs(y);
  39. return min(x, y) * min(x, y);
  40. });
  41. }
  42. inline auto image::diamondGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void {
  43. return gradient(a, b, radiusX, radiusY, centerX, centerY, [](double x, double y) -> double {
  44. return fabs(x) + fabs(y);
  45. });
  46. }
  47. inline auto image::horizontalGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void {
  48. return gradient(a, b, radiusX, radiusY, centerX, centerY, [](double x, double y) -> double {
  49. return fabs(x);
  50. });
  51. }
  52. inline auto image::radialGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void {
  53. return gradient(a, b, radiusX, radiusY, centerX, centerY, [](double x, double y) -> double {
  54. return sqrt(x * x + y * y);
  55. });
  56. }
  57. inline auto image::sphericalGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void {
  58. return gradient(a, b, radiusX, radiusY, centerX, centerY, [](double x, double y) -> double {
  59. return x * x + y * y;
  60. });
  61. }
  62. inline auto image::squareGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void {
  63. return gradient(a, b, radiusX, radiusY, centerX, centerY, [](double x, double y) -> double {
  64. return max(fabs(x), fabs(y));
  65. });
  66. }
  67. inline auto image::verticalGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void {
  68. return gradient(a, b, radiusX, radiusY, centerX, centerY, [](double x, double y) -> double {
  69. return fabs(y);
  70. });
  71. }
  72. }