GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cellstats_eq.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 int G_cell_stats_histo_eq(struct Cell_stats *statf, CELL min1, CELL max1, /* input range to be rescaled */
3  CELL min2, CELL max2, /* output range */
4  int zero, /* include zero if min1 <= 0 <= min2 ? */
5  void (*func) (CELL, CELL, CELL))
6 {
7  long count, total;
8  CELL prev = 0;
9  CELL cat;
10  CELL x;
11  CELL newcat = 0;
12  int first;
13  double span, sum;
14  double range2;
15 
16 
17  if (min1 > max1 || min2 > max2)
18  return 0;
19 
20  total = 0;
21  G_rewind_cell_stats(statf);
22  while (G_next_cell_stat(&cat, &count, statf)) {
23  if (cat < min1)
24  continue;
25  if (cat > max1)
26  break;
27  if (cat == 0 && !zero)
28  continue;
29 
30  total += count;
31  }
32  if (total <= 0)
33  return 0;
34 
35  range2 = max2 - min2 + 1;
36  span = total / range2;
37 
38  first = 1;
39  sum = 0;
40 
41  G_rewind_cell_stats(statf);
42  while (G_next_cell_stat(&cat, &count, statf)) {
43  if (cat < min1)
44  continue;
45  if (cat > max1)
46  break;
47  if (cat == 0 && !zero)
48  continue;
49 
50  x = (sum + (count / 2.0)) / span;
51  if (x < 0)
52  x = 0;
53  x += min2;
54  sum += count;
55 
56  if (first) {
57  prev = cat;
58  newcat = x;
59  first = 0;
60  }
61  else if (newcat != x) {
62  func(prev, cat - 1, newcat);
63  newcat = x;
64  prev = cat;
65  }
66  }
67  if (!first) {
68  func(prev, cat, newcat);
69  if (!zero && min1 <= 0 && max1 >= 0)
70  func((CELL) 0, (CELL) 0, (CELL) 0);
71  }
72 
73  return first == 0;
74 }
int count
int G_next_cell_stat(CELL *cat, long *count, struct Cell_stats *s)
retrieve sorted cell stats
Definition: cell_stats.c:338
int first
Definition: form/open.c:25
int G_rewind_cell_stats(struct Cell_stats *s)
reset/rewind cell stats
Definition: cell_stats.c:265
int max1(int, int)
Definition: minmax.c:32
int G_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:2
CELL cat
Definition: g3dcats.c:90
int min1(int, int)
Definition: minmax.c:18