Skip to content

Commit

Permalink
v.colors: Fix color inversion (#1478) (#1479)
Browse files Browse the repository at this point in the history
* v.colors: Fix color inversion

* 80 columns

* Consistent spacing after casting

* Use val.i for CELL

* Use Rast_invert_colors() instead of invert_cat_colors() for cat-based colors
  • Loading branch information
HuidaeCho committed Mar 23, 2021
1 parent c2335cd commit c6999b7
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 80 deletions.
22 changes: 11 additions & 11 deletions vector/v.colors/local_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
#define USE_CAT 2
#define USE_Z 3

/* make_colors.c */
void make_colors(struct Colors *, const char *, DCELL, DCELL, int);
void load_colors(struct Colors *, const char *, DCELL, DCELL, int);
/* scan_cats.c */
void scan_cats(const struct Map_info *, int, const char *, const char *,
const struct FPRange *, struct Colors *);

/* scan_attr.c */
int scan_attr(const struct Map_info *, int, const char *, const char *,
const char *, const struct FPRange *, struct Colors *,
struct Colors *);
void color_rules_to_cats(dbCatValArray *, int, struct Colors *,
struct Colors *);

/* scan_cats.c */
void scan_cats(const struct Map_info *, int, const char *, const char *,
const struct FPRange *, struct Colors *);
struct Colors *, int);

/* scan_z.c */
void scan_z(struct Map_info *, int, const char *, const char *,
const struct FPRange *, struct Colors *);
const struct FPRange *, struct Colors *, int);

/* make_colors.c */
void make_colors(struct Colors *, const char *, DCELL, DCELL, int);
void load_colors(struct Colors *, const char *, DCELL, DCELL, int);
void color_rules_to_cats(dbCatValArray *, int, struct Colors *,
struct Colors *, int, DCELL, DCELL);

/* write_rgb.c */
void write_rgb_values(const struct Map_info *, int, const char *,
Expand Down
33 changes: 18 additions & 15 deletions vector/v.colors/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ int main(int argc, char *argv[])
} flag;

struct {
struct Option *map, *field, *colr, *rast, *volume, *rules,
*attrcol, *rgbcol, *range, *use;
struct Option *map, *field, *colr, *rast, *volume, *rules, *attrcol,
*rgbcol, *range, *use;
} opt;

int layer;
int overwrite, remove, is_from_stdin, stat, have_colors, convert, use;
int overwrite, remove, is_from_stdin, stat, have_colors, convert, invert,
use;
const char *mapset, *cmapset;
const char *style, *rules, *cmap, *attrcolumn, *rgbcolumn;
char *name;
Expand Down Expand Up @@ -188,6 +189,7 @@ int main(int argc, char *argv[])
attrcolumn = opt.attrcol->answer;
rgbcolumn = opt.rgbcol->answer;
convert = flag.c->answer;
invert = flag.n->answer;
use = USE_CAT;
if (opt.use->answer) {
switch (opt.use->answer[0]) {
Expand All @@ -212,7 +214,8 @@ int main(int argc, char *argv[])
if (use == USE_ATTR && !attrcolumn)
G_fatal_error(_("Option <%s> required"), opt.attrcol->key);
if (use != USE_ATTR && attrcolumn) {
G_important_message(_("Option <%s> given, assuming <use=attr>..."), opt.attrcol->key);
G_important_message(_("Option <%s> given, assuming <use=attr>..."),
opt.attrcol->key);
use = USE_ATTR;
}

Expand Down Expand Up @@ -253,7 +256,8 @@ int main(int argc, char *argv[])
if (remove) {
stat = Vect_remove_colors(name, mapset);
if (stat < 0)
G_fatal_error(_("Unable to remove color table of vector map <%s>"), name);
G_fatal_error(_("Unable to remove color table of vector map <%s>"),
name);
if (stat == 0)
G_warning(_("Color table of vector map <%s> not found"), name);
return EXIT_SUCCESS;
Expand All @@ -262,9 +266,8 @@ int main(int argc, char *argv[])
G_suppress_warnings(TRUE);
have_colors = Vect_read_colors(name, mapset, NULL);

if (have_colors > 0 && !overwrite) {
if (have_colors > 0 && !overwrite)
G_fatal_error(_("Color table exists. Exiting."));
}

G_suppress_warnings(FALSE);

Expand Down Expand Up @@ -302,12 +305,12 @@ int main(int argc, char *argv[])
else if (use == USE_Z) {
scan_z(&Map, layer, style, rules,
opt.range->answer ? &range : NULL,
&colors);
&colors, invert);
}
else {
scan_attr(&Map, layer, attrcolumn, style, rules,
opt.range->answer ? &range : NULL,
&colors, NULL);
&colors, NULL, invert);
}
}
else {
Expand All @@ -332,13 +335,10 @@ int main(int argc, char *argv[])
colors_tmp = colors;
scan_attr(&Map, layer, attrcolumn, style, rules,
opt.range->answer ? &range : NULL,
&colors, &colors_tmp);
&colors, &colors_tmp, invert);
}
}

if (flag.n->answer)
Rast_invert_colors(&colors);

/* TODO ?
if (flag.e->answer) {
if (!have_stats)
Expand All @@ -357,6 +357,9 @@ int main(int argc, char *argv[])
colors = colors_tmp;
}

if (use == USE_CAT && invert)
Rast_invert_colors(&colors);

G_important_message(_("Writing color rules..."));

if (style || rules || opt.rast->answer || opt.volume->answer) {
Expand All @@ -375,8 +378,8 @@ int main(int argc, char *argv[])

G_message(_("Color table for vector map <%s> set to '%s'"),
G_fully_qualified_name(name, mapset),
is_from_stdin || convert ? "rules" : style ? style : rules ? rules :
cmap);
is_from_stdin || convert ? "rules" :
(style ? style : (rules ? rules : cmap)));

exit(EXIT_SUCCESS);
}
52 changes: 49 additions & 3 deletions vector/v.colors/make_colors.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>

void make_colors(struct Colors *colors, const char *style, DCELL min, DCELL max, int is_fp)
void make_colors(struct Colors *colors, const char *style, DCELL min, DCELL max,
int is_fp)
{

G_debug(3, "make_colors(): range=%f,%f is_fp=%d", min, max, is_fp);
Expand Down Expand Up @@ -38,12 +40,14 @@ void make_colors(struct Colors *colors, const char *style, DCELL min, DCELL max,
}
}

void load_colors(struct Colors *colors, const char *rules, DCELL min, DCELL max, int is_fp)
void load_colors(struct Colors *colors, const char *rules, DCELL min, DCELL max,
int is_fp)
{
int ret;

if (rules[0] == '-' && rules[1] == 0)
ret = Rast_read_color_rules(colors, min, max, Rast_read_color_rule, stdin);
ret = Rast_read_color_rules(colors, min, max, Rast_read_color_rule,
stdin);
else if (is_fp)
ret = Rast_load_fp_colors(colors, rules, (DCELL) min, (DCELL) max);
else
Expand All @@ -52,3 +56,45 @@ void load_colors(struct Colors *colors, const char *rules, DCELL min, DCELL max,
if (ret == 0)
G_fatal_error(_("Unable to load rules file <%s>"), rules);
}

void color_rules_to_cats(dbCatValArray *cvarr, int is_fp,
struct Colors *vcolors, struct Colors *colors,
int invert, DCELL min, DCELL max)
{
int i, cat;
dbCatVal *cv;
int red, grn, blu;

/* color table for categories */
G_message(_("Converting color rules into categories..."));
for (i = 0; i < cvarr->n_values; i++) {
G_percent(i, cvarr->n_values, 2);
cv = &(cvarr->value[i]);
cat = cv->cat;
if (is_fp) {
DCELL v = invert ? min + max - cv->val.d : cv->val.d;
if (Rast_get_d_color((const DCELL *) &v, &red, &grn, &blu,
vcolors) == 0) {
/* G_warning(_("No color rule defined for value %f"), v); */
G_debug(3, "scan_attr(): cat=%d, val=%f -> no color rule",
cat, v);
continue;
}
}
else {
CELL v = invert ? (CELL) min + (CELL) max - cv->val.i : cv->val.i;
if (Rast_get_c_color((const CELL *) &v, &red, &grn, &blu,
vcolors) == 0) {
/* G_warning(_("No color rule defined for value %d"), v); */
G_debug(3, "scan_attr(): cat=%d, val=%d -> no color rule",
cat, v);
continue;
}
}
G_debug(3, "scan_attr(): cat=%d, val=%f, r=%d, g=%d, b=%d",
cat, is_fp ? cv->val.d : cv->val.i, red, grn, blu);
Rast_add_c_color_rule((const CELL*) &cat, red, grn, blu,
(const CELL*) &cat, red, grn, blu, colors);
}
G_percent(2, 2, 2);
}
47 changes: 6 additions & 41 deletions vector/v.colors/scan_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "local_proto.h"

int scan_attr(const struct Map_info *Map, int layer, const char *column_name,
const char *style, const char *rules,
const struct FPRange *range, struct Colors *colors, struct Colors *rcolors)
const char *style, const char *rules, const struct FPRange *range,
struct Colors *colors, struct Colors *rcolors, int invert)
{
int ctype, is_fp, nrec;
double fmin, fmax;
Expand Down Expand Up @@ -87,55 +87,20 @@ int scan_attr(const struct Map_info *Map, int layer, const char *column_name,

if (rcolors)
/* color table for categories */
color_rules_to_cats(&cvarr, is_fp, rcolors, colors);
color_rules_to_cats(&cvarr, is_fp, rcolors, colors,
invert, (DCELL) fmin, (DCELL) fmax);
else {
if (style)
make_colors(&vcolors, style, (DCELL) fmin, (DCELL) fmax, is_fp);
else if (rules)
load_colors(&vcolors, rules, (DCELL) fmin, (DCELL) fmax, is_fp);

/* color table for categories */
color_rules_to_cats(&cvarr, is_fp, &vcolors, colors);
color_rules_to_cats(&cvarr, is_fp, &vcolors, colors,
invert, (DCELL) fmin, (DCELL) fmax);
}

db_close_database(driver);

return is_fp;
}

void color_rules_to_cats(dbCatValArray *cvarr, int is_fp,
struct Colors *vcolors, struct Colors *colors)
{
int i, cat;
dbCatVal *cv;
int red, grn, blu;

/* color table for categories */
G_message(_("Converting color rules into categories..."));
for (i = 0; i < cvarr->n_values; i++) {
G_percent(i, cvarr->n_values, 2);
cv = &(cvarr->value[i]);
cat = cv->cat;
if (is_fp) {
if (Rast_get_d_color((const DCELL *) &(cv->val.d), &red, &grn, &blu,
vcolors) == 0) {
/* G_warning(_("No color rule defined for value %f"), cv->val.d); */
G_debug(3, "scan_attr(): cat=%d, val=%f -> no color rule", cat, cv->val.d);
continue;
}
}
else {
if (Rast_get_c_color((const CELL *) &(cv->val.i), &red, &grn, &blu,
vcolors) == 0) {
/* G_warning(_("No color rule defined for value %d"), cv->val.i); */
G_debug(3, "scan_attr(): cat=%d, val=%d -> no color rule", cat, cv->val.i);
continue;
}
}
G_debug(3, "scan_attr(): cat=%d, val=%f, r=%d, g=%d, b=%d",
cat, is_fp ? cv->val.d : cv->val.i, red, grn, blu);
Rast_add_c_color_rule((const CELL*) &cat, red, grn, blu,
(const CELL*) &cat, red, grn, blu, colors);
}
G_percent(2, 2, 2);
}
3 changes: 1 addition & 2 deletions vector/v.colors/scan_cats.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ void scan_cats(const struct Map_info *Map, int field,

if (style)
make_colors(colors, style, (DCELL) cmin, (DCELL) cmax, FALSE);
else if (rules) {
else if (rules)
load_colors(colors, rules, (DCELL) cmin, (DCELL) cmax, FALSE);
}

Vect_destroy_cats_struct(Cats);
}
Expand Down
12 changes: 6 additions & 6 deletions vector/v.colors/scan_z.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

void scan_z(struct Map_info *Map, int field,
const char *style, const char *rules,
const struct FPRange *range, struct Colors *colors)
const struct FPRange *range, struct Colors *colors, int invert)
{
int ltype, line, cat, i, found;
int items_alloc;
Expand All @@ -26,7 +26,9 @@ void scan_z(struct Map_info *Map, int field,
cvarr.ctype = DB_C_TYPE_DOUBLE;

Vect_set_constraint_field(Map, field);
Vect_set_constraint_type(Map, GV_POINTS); /* points, centroids or kernels only) */

/* points, centroids or kernels only) */
Vect_set_constraint_type(Map, GV_POINTS);

G_message(_("Reading features..."));
line = i = found = 0;
Expand Down Expand Up @@ -78,15 +80,13 @@ void scan_z(struct Map_info *Map, int field,

if (style)
make_colors(&vcolors, style, (DCELL) zmin, (DCELL) zmax, TRUE);
else if (rules) {
else if (rules)
load_colors(&vcolors, rules, (DCELL) zmin, (DCELL) zmax, TRUE);
}

/* color table for categories */
color_rules_to_cats(&cvarr, TRUE, &vcolors, colors);
color_rules_to_cats(&cvarr, TRUE, &vcolors, colors, invert, zmin, zmax);

Vect_destroy_line_struct(Points);
Vect_destroy_cats_struct(Cats);
db_CatValArray_free(&cvarr);

}
4 changes: 2 additions & 2 deletions vector/v.colors/write_rgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <grass/raster.h>
#include <grass/glocale.h>

void write_rgb_values(const struct Map_info *Map, int layer, const char *column_name,
struct Colors *colors)
void write_rgb_values(const struct Map_info *Map, int layer,
const char *column_name, struct Colors *colors)
{
int ctype, nrec, i;
int red, grn, blu;
Expand Down

0 comments on commit c6999b7

Please sign in to comment.