GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
cellstats_eq.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 #include <grass/raster.h>
3 
5  CELL max1, /* input range to be rescaled */
6  CELL min2, CELL max2, /* output range */
7  int zero, /* include zero if min1 <= 0 <= min2 ? */
8  void (*func)(CELL, CELL, CELL))
9 {
10  long count, total;
11  CELL prev = 0;
12  CELL cat;
13  CELL x;
14  CELL newcat = 0;
15  int first;
16  double span, sum;
17  double range2;
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_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 Rast_rewind_cell_stats(struct Cell_stats *)
Reset/rewind cell stats.
Definition: cell_stats.c:248
int Rast_next_cell_stat(CELL *, long *, struct Cell_stats *)
Retrieve sorted cell stats.
Definition: cell_stats.c:312
int CELL
Definition: gis.h:628
int max1(int, int)
Definition: minmax.c:30
int min1(int, int)
Definition: minmax.c:17
int count
#define x