1
0

convolve.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <precomp.h>
  2. #include "convolve.h"
  3. #define RED(a) (((a)>>16)&0xff)
  4. #define GRN(a) (((a)>>8)&0xff)
  5. #define BLU(a) (((a)&0xff))
  6. #define ALP(a) (((a)>>24))
  7. Convolve3x3::Convolve3x3(ARGB32 *_bits, int _w, int _h) : bits(_bits), w(_w), h(_h) {
  8. ZERO(vals);
  9. multiplier = 0;
  10. }
  11. void Convolve3x3::setPos(int x, int y, float v) {
  12. ASSERT(x >= -1 && x <= 1 && y >= -1 && y <= 1);
  13. vals[y+1][x+1] = v;
  14. }
  15. void Convolve3x3::setMultiplier(int m) {
  16. multiplier = m;
  17. }
  18. void Convolve3x3::convolve() {
  19. if (bits == NULL || w <= 0 || h <= 0) return; // nothin'
  20. MemMatrix<ARGB32> temp(w, h, bits); // make a copy
  21. for (int y = 0; y < h; y++) {
  22. for (int x = 0; x < w; x++) {
  23. if (ALP(temp(x, y))<=1) continue;
  24. float ra=0, rg=0, rb=0;
  25. for (int a = -1; a <= 1; a++) {
  26. for (int b = -1; b <= 1; b++) {
  27. int px = x + a, py = y + b;
  28. if (px < 0 || px >= w || py < 0 || py >= h) continue;
  29. ARGB32 c = temp(px, py);
  30. if (ALP(c) <= 1) continue;
  31. ra += (float)RED(c) * vals[b][a] * multiplier;
  32. rg += (float)GRN(c) * vals[b][a] * multiplier;
  33. rb += (float)BLU(c) * vals[b][a] * multiplier;
  34. }
  35. }
  36. unsigned int r = MINMAX((int)ra, 0, 255);
  37. unsigned int g = MINMAX((int)rg, 0, 255);
  38. unsigned int b = MINMAX((int)rb, 0, 255);
  39. unsigned int lum = MAX(MAX(r, g), b);
  40. if (lum < 64) lum = 0;
  41. else if (lum > 192) lum = 255;
  42. //bits[x+y*w] = (ALP(*temp.m(x, y))<<24)|(r<<16)|(g<<8)|(b);
  43. bits[x+y*w] &= 0xffffff;
  44. bits[x+y*w] |= (255-lum) << 24;
  45. //if (lum < 64) {
  46. // bits[x+y*w] &= 0xffff00ff;
  47. // bits[x+y*w] |= lum << 8;
  48. //} else {
  49. // bits[x+y*w] &= 0xff00ffff;
  50. // bits[x+y*w] |= lum << 16;
  51. //}
  52. }
  53. }
  54. }