imageblur.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef IMAGEBLUR_H
  2. #define IMAGEBLUR_H
  3. #include <QImage>
  4. class ImageBlurUtility
  5. {
  6. public:
  7. //QImage存储数据的方式参考qrgb.h, 按AARRGGBB存
  8. //unsigned int4字节, 刚好存放rgba
  9. struct pixel_t
  10. {
  11. unsigned int rgba = 0;
  12. inline int r() const { return ((rgba >> 16) & 0xff); }
  13. inline int g() const { return ((rgba >> 8) & 0xff); }
  14. inline int b() const { return (rgba & 0xff); }
  15. inline int a() const { return rgba >> 24; }
  16. inline void Set(int r, int g, int b, int a)
  17. { rgba = ((a & 0xffu) << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu); }
  18. };
  19. //计算时用, 将整数的rgba值变为double类型来作计算, 最后再用qRound转回整数
  20. struct buff_t
  21. {
  22. double r, g, b, a;
  23. buff_t() : r(0.0), g(0.0), b(0.0), a(0.0) {}
  24. buff_t(const pixel_t &pixel):r(pixel.r()),g(pixel.g()),b(pixel.b()),a(pixel.a()) {}
  25. inline int Red() const { return qRound(r); }
  26. inline int Green() const { return qRound(g); }
  27. inline int Blue() const { return qRound(b); }
  28. inline int Alpha() const { return qRound(a); }
  29. };
  30. public:
  31. template <typename T>
  32. static T Edge(T i, T x, T w);
  33. public:
  34. static void Normalization(std::vector<double> &kernels);
  35. };
  36. class GaussBlur
  37. {
  38. public:
  39. static void Blur(QImage &image, int radius);
  40. private:
  41. static std::vector<double> GetKernels(int radius);
  42. };
  43. #endif // IMAGEBLUR_H