GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
color_rule.c
Go to the documentation of this file.
1 
17 #include <grass/gis.h>
18 
19 #define LIMIT(x) if (x < 0) x = 0; else if (x > 255) x = 255;
20 
21 static int add_color_rule(const void *, int, int, int,
22  const void *, int, int, int,
23  struct _Color_Info_ *, int,
24  DCELL *, DCELL *, RASTER_MAP_TYPE);
25 
26 
40 int
41 G_add_d_raster_color_rule(const DCELL * val1, int r1, int g1, int b1,
42  const DCELL * val2, int r2, int g2, int b2,
43  struct Colors *colors)
44 {
45  add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->fixed,
46  colors->version, &colors->cmin, &colors->cmax, DCELL_TYPE);
47  return 1;
48 }
49 
50 
64 int
65 G_add_f_raster_color_rule(const FCELL * cat1, int r1, int g1, int b1,
66  const FCELL * cat2, int r2, int g2, int b2,
67  struct Colors *colors)
68 {
69  add_color_rule(cat1, r1, g1, b1, cat2, r2, g2, b2, &colors->fixed,
70  colors->version, &colors->cmin, &colors->cmax, FCELL_TYPE);
71  return 1;
72 }
73 
74 
88 int
89 G_add_c_raster_color_rule(const CELL * cat1, int r1, int g1, int b1,
90  const CELL * cat2, int r2, int g2, int b2,
91  struct Colors *colors)
92 {
93  add_color_rule(cat1, r1, g1, b1, cat2, r2, g2, b2, &colors->fixed,
94  colors->version, &colors->cmin, &colors->cmax, CELL_TYPE);
95  return 1;
96 }
97 
98 
121 int
122 G_add_raster_color_rule(const void *val1, int r1, int g1, int b1,
123  const void *val2, int r2, int g2, int b2,
124  struct Colors *colors, RASTER_MAP_TYPE data_type)
125 {
126  add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->fixed,
127  colors->version, &colors->cmin, &colors->cmax, data_type);
128  return 1;
129 }
130 
131 
164 int
165 G_add_color_rule(CELL cat1, int r1, int g1, int b1, CELL cat2, int r2, int g2,
166  int b2, struct Colors *colors)
167 {
168  add_color_rule((void *)&cat1, r1, g1, b1, (void *)&cat2, r2, g2, b2,
169  &colors->fixed, colors->version, &colors->cmin,
170  &colors->cmax, CELL_TYPE);
171  return 1;
172 }
173 
186 int
187 G_add_modular_d_raster_color_rule(const DCELL * val1, int r1, int g1, int b1,
188  const DCELL * val2, int r2, int g2, int b2,
189  struct Colors *colors)
190 {
191  DCELL min, max;
192 
193  if (colors->version < 0)
194  return -1; /* can't use this on 3.0 colors */
195  min = colors->cmin;
196  max = colors->cmax;
197  add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->modular, 0,
198  &colors->cmin, &colors->cmax, DCELL_TYPE);
199  colors->cmin = min; /* don't reset these */
200  colors->cmax = max;
201 
202  return 1;
203 }
204 
217 int
218 G_add_modular_f_raster_color_rule(const FCELL * val1, int r1, int g1, int b1,
219  const FCELL * val2, int r2, int g2, int b2,
220  struct Colors *colors)
221 {
222  DCELL min, max;
223 
224  if (colors->version < 0)
225  return -1; /* can;t use this on 3.0 colors */
226  min = colors->cmin;
227  max = colors->cmax;
228  add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->modular, 0,
229  &colors->cmin, &colors->cmax, FCELL_TYPE);
230  colors->cmin = min; /* don't reset these */
231  colors->cmax = max;
232 
233  return 1;
234 }
235 
248 int
249 G_add_modular_c_raster_color_rule(const CELL * val1, int r1, int g1, int b1,
250  const CELL * val2, int r2, int g2, int b2,
251  struct Colors *colors)
252 {
253  return G_add_modular_color_rule(*val1, r1, g1, b1, *val2, r2, g2, b2,
254  colors);
255 }
256 
273 int
274 G_add_modular_raster_color_rule(const void *val1, int r1, int g1, int b1,
275  const void *val2, int r2, int g2, int b2,
276  struct Colors *colors,
277  RASTER_MAP_TYPE data_type)
278 {
279  CELL min, max;
280 
281  if (colors->version < 0)
282  return -1; /* can't use this on 3.0 colors */
283  min = colors->cmin;
284  max = colors->cmax;
285  add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->modular, 0,
286  &colors->cmin, &colors->cmax, data_type);
287  colors->cmin = min; /* don't reset these */
288  colors->cmax = max;
289 
290  return 1;
291 }
292 
309 int
310 G_add_modular_color_rule(CELL cat1, int r1, int g1, int b1, CELL cat2, int r2,
311  int g2, int b2, struct Colors *colors)
312 {
313  CELL min, max;
314 
315  if (colors->version < 0)
316  return -1; /* can;t use this on 3.0 colors */
317  min = colors->cmin;
318  max = colors->cmax;
319  add_color_rule((void *)&cat1, r1, g1, b1, (void *)&cat2, r2, g2, b2,
320  &colors->modular, 0, &colors->cmin, &colors->cmax,
321  CELL_TYPE);
322  colors->cmin = min; /* don't reset these */
323  colors->cmax = max;
324 
325  return 1;
326 }
327 
328 static int add_color_rule(const void *pt1, int r1, int g1, int b1,
329  const void *pt2, int r2, int g2, int b2,
330  struct _Color_Info_ *cp, int version, DCELL * cmin,
331  DCELL * cmax, RASTER_MAP_TYPE data_type)
332 {
333  struct _Color_Rule_ *rule, *next;
334  unsigned char red, grn, blu;
335  DCELL min, max, val1, val2;
336  CELL cat;
337 
338  val1 = G_get_raster_value_d(pt1, data_type);
339  val2 = G_get_raster_value_d(pt2, data_type);
340  /* allocate a low:high rule */
341  rule = (struct _Color_Rule_ *)G_malloc(sizeof(*rule));
342  rule->next = rule->prev = NULL;
343 
344  /* make sure colors are in the range [0,255] */
345  LIMIT(r1);
346  LIMIT(g1);
347  LIMIT(b1);
348  LIMIT(r2);
349  LIMIT(g2);
350  LIMIT(b2);
351 
352  /* val1==val2, use average color */
353  /* otherwise make sure low < high */
354  if (val1 == val2) {
355  rule->low.value = rule->high.value = val1;
356  rule->low.red = rule->high.red = (r1 + r2) / 2;
357  rule->low.grn = rule->high.grn = (g1 + g2) / 2;
358  rule->low.blu = rule->high.blu = (b1 + b2) / 2;
359  }
360  else if (val1 < val2) {
361  rule->low.value = val1;
362  rule->low.red = r1;
363  rule->low.grn = g1;
364  rule->low.blu = b1;
365 
366  rule->high.value = val2;
367  rule->high.red = r2;
368  rule->high.grn = g2;
369  rule->high.blu = b2;
370  }
371  else {
372  rule->low.value = val2;
373  rule->low.red = r2;
374  rule->low.grn = g2;
375  rule->low.blu = b2;
376 
377  rule->high.value = val1;
378  rule->high.red = r1;
379  rule->high.grn = g1;
380  rule->high.blu = b1;
381  }
382 
383  /* keep track of the overall min and max, excluding null */
384  if (G_is_d_null_value(&(rule->low.value)))
385  return 0;
386  if (G_is_d_null_value(&(rule->high.value)))
387  return 0;
388  min = rule->low.value;
389  max = rule->high.value;
390  if (min <= max) {
391  if (cp->min > cp->max) {
392  cp->min = min;
393  cp->max = max;
394  }
395  else {
396  if (cp->min > min)
397  cp->min = min;
398  if (cp->max < max)
399  cp->max = max;
400  }
401  }
402  if (*cmin > *cmax) {
403  *cmin = cp->min;
404  *cmax = cp->max;
405  }
406  else {
407  if (*cmin > cp->min)
408  *cmin = cp->min;
409  if (*cmax < cp->max)
410  *cmax = cp->max;
411  }
412 
413  /* If version is old style (i.e., pre 4.0),
414  * interpolate this rule from min to max
415  * and insert each cat into the lookup table.
416  * Then free the rule.
417  * Otherwise, free the lookup table, if active.
418  * G_organize_colors() will regenerate it
419  * Link this rule into the list of rules
420  */
421 
422  if (version < 0) {
423  for (cat = (CELL) min; cat <= (CELL) max; cat++) {
424  G__interpolate_color_rule((DCELL) cat, &red, &grn, &blu, rule);
425  G__insert_color_into_lookup(cat, (int)red, (int)grn, (int)blu,
426  cp);
427  }
428  G_free(rule);
429  }
430  else {
431  if (cp->rules)
432  cp->rules->prev = rule;
433  rule->next = cp->rules;
434  cp->rules = rule;
435 
436  /* prune the rules:
437  * remove all rules that are contained by this rule
438  */
439  min = rule->low.value; /* mod 4.1 */
440  max = rule->high.value; /* mod 4.1 */
441  cp->n_rules++;
442  for (rule = rule->next; rule; rule = next) {
443  next = rule->next; /* has to be done here, not in for stmt */
444  if (min <= rule->low.value && max >= rule->high.value) {
445  if ((rule->prev->next = next)) /* remove from the list */
446  next->prev = rule->prev;
447  G_free(rule);
448  cp->n_rules--;
449  }
450  }
451 
452  /* free lookup array, if allocated */
455  }
456 
457  return 0;
458 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int G_add_modular_color_rule(CELL cat1, int r1, int g1, int b1, CELL cat2, int r2, int g2, int b2, struct Colors *colors)
Add modular color rule.
Definition: color_rule.c:310
DCELL val1
Definition: g3dcats.c:91
int G_add_d_raster_color_rule(const DCELL *val1, int r1, int g1, int b1, const DCELL *val2, int r2, int g2, int b2, struct Colors *colors)
Adds the floating-point rule (DCELL version)
Definition: color_rule.c:41
int G__color_free_fp_lookup(struct _Color_Info_ *cp)
Definition: color_free.c:56
#define min(x, y)
Definition: draw2.c:68
int G_add_modular_f_raster_color_rule(const FCELL *val1, int r1, int g1, int b1, const FCELL *val2, int r2, int g2, int b2, struct Colors *colors)
Add modular color rule (FCELL version)
Definition: color_rule.c:218
def version
Get GRASS version as dictionary.
Definition: core.py:1199
int G_add_modular_raster_color_rule(const void *val1, int r1, int g1, int b1, const void *val2, int r2, int g2, int b2, struct Colors *colors, RASTER_MAP_TYPE data_type)
Add modular color rule.
Definition: color_rule.c:274
#define max(x, y)
Definition: draw2.c:69
DCELL val2
Definition: g3dcats.c:91
int G_add_f_raster_color_rule(const FCELL *cat1, int r1, int g1, int b1, const FCELL *cat2, int r2, int g2, int b2, struct Colors *colors)
Adds the floating-point rule (FCELL version)
Definition: color_rule.c:65
int G_is_d_null_value(const DCELL *dcellVal)
Returns 1 if dcell is NULL, 0 otherwise. This will test if the value dcell is a NaN. Same test as in G_is_f_null_value().
Definition: null_val.c:306
int G_add_modular_c_raster_color_rule(const CELL *val1, int r1, int g1, int b1, const CELL *val2, int r2, int g2, int b2, struct Colors *colors)
Add modular color rule (CCELL version)
Definition: color_rule.c:249
int G__insert_color_into_lookup(CELL cat, int red, int grn, int blu, struct _Color_Info_ *cp)
Definition: color_insrt.c:12
int G__interpolate_color_rule(DCELL val, unsigned char *red, unsigned char *grn, unsigned char *blu, const struct _Color_Rule_ *rule)
Definition: color_look.c:446
int G_add_raster_color_rule(const void *val1, int r1, int g1, int b1, const void *val2, int r2, int g2, int b2, struct Colors *colors, RASTER_MAP_TYPE data_type)
Adds the floating-point rule.
Definition: color_rule.c:122
return NULL
Definition: dbfopen.c:1394
int G__color_free_lookup(struct _Color_Info_ *cp)
Definition: color_free.c:43
int G_add_color_rule(CELL cat1, int r1, int g1, int b1, CELL cat2, int r2, int g2, int b2, struct Colors *colors)
Set colors rules.
Definition: color_rule.c:165
#define LIMIT(x)
Definition: color_rule.c:19
CELL cat
Definition: g3dcats.c:90
int G_add_modular_d_raster_color_rule(const DCELL *val1, int r1, int g1, int b1, const DCELL *val2, int r2, int g2, int b2, struct Colors *colors)
Add modular color rule (DCELL version)
Definition: color_rule.c:187
DCELL G_get_raster_value_d(const void *rast, RASTER_MAP_TYPE data_type)
Retrieves the value of type data_type from pointer p,.
Definition: gis/raster.c:313
int G_add_c_raster_color_rule(const CELL *cat1, int r1, int g1, int b1, const CELL *cat2, int r2, int g2, int b2, struct Colors *colors)
Adds the floating-point rule (CCELL version)
Definition: color_rule.c:89