GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
color_hist.c
Go to the documentation of this file.
1/*!
2 * \file lib/raster/color_hist.c
3 *
4 * \brief Raster Library - histogram grey scale colors
5 *
6 * SPDX-FileCopyrightText: 2007-2009 Glynn Clements
7 * SPDX-FileCopyrightText: GRASS Development Team
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 *
10 * \author Glynn Clements <glynn@gclements.plus.com>
11 */
12
13#include <math.h>
14
15#include <grass/gis.h>
16#include <grass/raster.h>
17#include <grass/colors.h>
18
19/*!
20 * \brief Make histogram-stretched grey colors
21 *
22 * Generates a histogram contrast-stretched grey scale color table
23 * that goes from the, histogram information in the Cell_stats
24 * structure (see \ref Raster_Histograms).
25 *
26 * Color range is 0-255.
27 *
28 * \param colors pointer to Colors structure which holds color info
29 * \param statf pointer to Cell_stats structure which holds cell stats info
30 */
32 struct Cell_stats *statf)
33{
34 long count, total;
35 CELL prev = 0, cat, val2;
36 double span, sum;
37 int first;
38 int x, grey;
39 int R, G, B;
40
41 Rast_init_colors(colors);
42
44 Rast_set_null_value_color(R, G, B, colors);
45
46 total = 0;
47
49 while (Rast_next_cell_stat(&cat, &count, statf))
50 if (count > 0)
51 total += count;
52 if (total <= 0)
53 return;
54
55 span = total / 256.0;
56 first = 1;
57 grey = 0;
58 sum = 0.0;
59
61 while (Rast_next_cell_stat(&cat, &count, statf)) {
62 if (count <= 0)
63 continue;
64 x = (sum + (count / 2.0)) / span;
65 if (x < 0)
66 x = 0;
67 else if (x > 255)
68 x = 255;
69 sum += count;
70 if (first) {
71 prev = cat;
72 grey = x;
73 first = 0;
74 }
75 else if (grey != x) {
76 val2 = cat - 1;
77 Rast_add_c_color_rule(&prev, grey, grey, grey, &val2, grey, grey,
78 grey, colors);
79 grey = x;
80 prev = cat;
81 }
82 }
83 if (!first) {
84 Rast_add_c_color_rule(&prev, grey, grey, grey, &cat, grey, grey, grey,
85 colors);
86 }
87}
88
89/*!
90 \brief Generates histogram with normalized log transformed grey scale.
91
92 Generates histogram with normalized log transformed grey scale from
93 cell stats structure info. Color range is 0-255.
94
95 \param colors pointer to Colors structure which holds color info
96 \param statf pointer to Cell_stats structure which holds cell stats info
97 \param min minimum value
98 \param max maximum value
99 */
101 struct Cell_stats *statf, int min, int max)
102{
103 long count, total;
104 double lmin, lmax;
105 CELL prev = 0, cat, val2;
106 int first;
107 int x, grey;
108 int R, G, B;
109
110 Rast_init_colors(colors);
111
113 Rast_set_null_value_color(R, G, B, colors);
114
115 total = 0;
116
118 while (Rast_next_cell_stat(&cat, &count, statf))
119 if (count > 0)
120 total += count;
121 if (total <= 0)
122 return;
123
124 first = 1;
125 grey = 0;
126
127 lmin = log(min);
128 lmax = log(max);
129
131 while (Rast_next_cell_stat(&cat, &count, statf)) {
132 if (count <= 0)
133 continue;
134
135 /* log transform normalized */
136 x = (int)(255 * (log(cat) - lmin) / (lmax - lmin));
137
138 if (x < 0)
139 x = 0;
140 else if (x > 255)
141 x = 255;
142 if (first) {
143 prev = cat;
144 grey = x;
145 first = 0;
146 }
147 else if (grey != x) {
148 val2 = cat - 1;
149 Rast_add_c_color_rule(&prev, grey, grey, grey, &val2, grey, grey,
150 grey, colors);
151 grey = x;
152 prev = cat;
153 }
154 }
155 if (!first) {
156 Rast_add_c_color_rule(&prev, grey, grey, grey, &cat, grey, grey, grey,
157 colors);
158 }
159}
void Rast_make_histogram_eq_colors(struct Colors *colors, struct Cell_stats *statf)
Make histogram-stretched grey colors.
Definition color_hist.c:31
void Rast_make_histogram_log_colors(struct Colors *colors, struct Cell_stats *statf, int min, int max)
Generates histogram with normalized log transformed grey scale.
Definition color_hist.c:100
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
Definition color_str.c:99
void Rast_set_null_value_color(int, int, int, struct Colors *)
Set color for NULL-value.
Definition color_set.c:77
void Rast_add_c_color_rule(const CELL *, int, int, int, const CELL *, int, int, int, struct Colors *)
Adds the integer color rule (CELL version)
Definition color_rule.c:74
int Rast_rewind_cell_stats(struct Cell_stats *)
Reset/rewind cell stats.
Definition cell_stats.c:246
void Rast_init_colors(struct Colors *)
Initialize color structure.
Definition color_init.c:23
int Rast_next_cell_stat(CELL *, long *, struct Cell_stats *)
Retrieve sorted cell stats.
Definition cell_stats.c:310
#define min(x, y)
Definition draw2.c:29
#define max(x, y)
Definition draw2.c:30
#define DEFAULT_BG_COLOR
Definition gis.h:403
int CELL
Definition gis.h:631
int count
Definition gis.h:689
#define x