GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
cellstats_eq.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 #include <grass/raster.h>
3 
4 int Rast_cell_stats_histo_eq(struct Cell_stats *statf, CELL min1, CELL max1, /* input range to be rescaled */
5  CELL min2, CELL max2, /* output range */
6  int zero, /* include zero if min1 <= 0 <= min2 ? */
7  void (*func) (CELL, CELL, CELL))
8 {
9  long count, total;
10  CELL prev = 0;
11  CELL cat;
12  CELL x;
13  CELL newcat = 0;
14  int first;
15  double span, sum;
16  double range2;
17 
18 
19  if (min1 > max1 || min2 > max2)
20  return 0;
21 
22  total = 0;
24  while (Rast_next_cell_stat(&cat, &count, statf)) {
25  if (cat < min1)
26  continue;
27  if (cat > max1)
28  break;
29  if (cat == 0 && !zero)
30  continue;
31 
32  total += count;
33  }
34  if (total <= 0)
35  return 0;
36 
37  range2 = max2 - min2 + 1;
38  span = total / range2;
39 
40  first = 1;
41  sum = 0;
42 
44  while (Rast_next_cell_stat(&cat, &count, statf)) {
45  if (cat < min1)
46  continue;
47  if (cat > max1)
48  break;
49  if (cat == 0 && !zero)
50  continue;
51 
52  x = (sum + (count / 2.0)) / span;
53  if (x < 0)
54  x = 0;
55  x += min2;
56  sum += count;
57 
58  if (first) {
59  prev = cat;
60  newcat = x;
61  first = 0;
62  }
63  else if (newcat != x) {
64  func(prev, cat - 1, newcat);
65  newcat = x;
66  prev = cat;
67  }
68  }
69  if (!first) {
70  func(prev, cat, newcat);
71  if (!zero && min1 <= 0 && max1 >= 0)
72  func((CELL) 0, (CELL) 0, (CELL) 0);
73  }
74 
75  return first == 0;
76 }
int Rast_next_cell_stat(CELL *, long *, struct Cell_stats *)
Retrieve sorted cell stats.
Definition: cell_stats.c:314
int Rast_rewind_cell_stats(struct Cell_stats *)
Reset/rewind cell stats.
Definition: cell_stats.c:250
int count
#define x
int max1(int, int)
Definition: minmax.c:32
int CELL
Definition: gis.h:602
int Rast_cell_stats_histo_eq(struct Cell_stats *statf, CELL min1, CELL max1, CELL min2, CELL max2, int zero, void(*func)(CELL, CELL, CELL))
Definition: cellstats_eq.c:4
int min1(int, int)
Definition: minmax.c:18