00001 #include <grass/gis.h>
00002 int G_cell_stats_histo_eq (
00003 struct Cell_stats *statf,
00004 CELL min1,CELL max1,
00005 CELL min2,CELL max2,
00006 int zero,
00007 void (*func)())
00008 {
00009 long count, total;
00010 CELL prev = 0;
00011 CELL cat;
00012 CELL x;
00013 CELL newcat = 0;
00014 int first;
00015 double span, sum;
00016 double range2;
00017
00018
00019 if (min1 > max1 || min2 > max2) return 0;
00020
00021 total = 0;
00022 G_rewind_cell_stats (statf);
00023 while (G_next_cell_stat (&cat, &count, statf))
00024 {
00025 if (cat < min1) continue;
00026 if (cat > max1) break;
00027 if (cat == 0 && !zero) continue;
00028
00029 total += count;
00030 }
00031 if (total <= 0) return 0;
00032
00033 range2 = max2 - min2 + 1;
00034 span = total/range2;
00035
00036 first = 1;
00037 sum = 0;
00038
00039 G_rewind_cell_stats (statf);
00040 while (G_next_cell_stat (&cat, &count, statf))
00041 {
00042 if (cat < min1) continue;
00043 if (cat > max1) break;
00044 if (cat == 0 && !zero) continue;
00045
00046 x = (sum + (count/2.0))/span;
00047 if (x < 0) x = 0;
00048 x += min2;
00049 sum += count;
00050
00051 if (first)
00052 {
00053 prev = cat;
00054 newcat = x;
00055 first = 0;
00056 }
00057 else if (newcat != x)
00058 {
00059 func (prev, cat-1, newcat);
00060 newcat = x;
00061 prev = cat;
00062 }
00063 }
00064 if (!first)
00065 {
00066 func (prev, cat, newcat);
00067 if (!zero && min1 <= 0 && max1 >= 0)
00068 func ((CELL)0, (CELL)0, (CELL)0);
00069 }
00070
00071 return first == 0;
00072 }