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