cellstats_eq.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. int Rast_cell_stats_histo_eq(struct Cell_stats *statf, CELL min1, CELL max1, /* input range to be rescaled */
  4. CELL min2, CELL max2, /* output range */
  5. int zero, /* include zero if min1 <= 0 <= min2 ? */
  6. void (*func) (CELL, CELL, CELL))
  7. {
  8. long count, total;
  9. CELL prev = 0;
  10. CELL cat;
  11. CELL x;
  12. CELL newcat = 0;
  13. int first;
  14. double span, sum;
  15. double range2;
  16. if (min1 > max1 || min2 > max2)
  17. return 0;
  18. total = 0;
  19. Rast_rewind_cell_stats(statf);
  20. while (Rast_next_cell_stat(&cat, &count, statf)) {
  21. if (cat < min1)
  22. continue;
  23. if (cat > max1)
  24. break;
  25. if (cat == 0 && !zero)
  26. continue;
  27. total += count;
  28. }
  29. if (total <= 0)
  30. return 0;
  31. range2 = max2 - min2 + 1;
  32. span = total / range2;
  33. first = 1;
  34. sum = 0;
  35. Rast_rewind_cell_stats(statf);
  36. while (Rast_next_cell_stat(&cat, &count, statf)) {
  37. if (cat < min1)
  38. continue;
  39. if (cat > max1)
  40. break;
  41. if (cat == 0 && !zero)
  42. continue;
  43. x = (sum + (count / 2.0)) / span;
  44. if (x < 0)
  45. x = 0;
  46. x += min2;
  47. sum += count;
  48. if (first) {
  49. prev = cat;
  50. newcat = x;
  51. first = 0;
  52. }
  53. else if (newcat != x) {
  54. func(prev, cat - 1, newcat);
  55. newcat = x;
  56. prev = cat;
  57. }
  58. }
  59. if (!first) {
  60. func(prev, cat, newcat);
  61. if (!zero && min1 <= 0 && max1 >= 0)
  62. func((CELL) 0, (CELL) 0, (CELL) 0);
  63. }
  64. return first == 0;
  65. }