diff --git a/include/Make/Grass.make b/include/Make/Grass.make index 17ce25fcb71..dfcbd974b6a 100644 --- a/include/Make/Grass.make +++ b/include/Make/Grass.make @@ -213,7 +213,7 @@ NVIZDEPS = $(OGSFLIB) $(GISLIB) $(OPENGLLIB) OGSFDEPS = $(BITMAPLIB) $(RASTER3DLIB) $(VECTORLIB) $(DBMILIB) $(RASTERLIB) $(GISLIB) $(TIFFLIBPATH) $(TIFFLIB) $(OPENGLLIB) $(OPENGLULIB) $(MATHLIB) PNGDRIVERDEPS = $(DRIVERLIB) $(GISLIB) $(PNGLIB) $(MATHLIB) PSDRIVERDEPS = $(DRIVERLIB) $(GISLIB) $(MATHLIB) -RASTERDEPS = $(GISLIB) $(GPROJLIB) $(MATHLIB) +RASTERDEPS = $(GISLIB) $(GPROJLIB) $(MATHLIB) $(PARSONLIB) RLIDEPS = $(RASTERLIB) $(GISLIB) $(MATHLIB) ROWIODEPS = $(GISLIB) RTREEDEPS = $(GISLIB) $(MATHLIB) diff --git a/include/grass/defs/raster.h b/include/grass/defs/raster.h index bbdc8bfeaa1..f81445a374c 100644 --- a/include/grass/defs/raster.h +++ b/include/grass/defs/raster.h @@ -178,6 +178,10 @@ void Rast__organize_colors(struct Colors *); /* color_out.c */ void Rast_print_colors(struct Colors *, DCELL, DCELL, FILE *, int); +/* json_color_out.c */ +void Rast_print_json_colors(struct Colors *, DCELL, DCELL, FILE *, int, + ColorFormat); + /* color_rand.c */ void Rast_make_random_colors(struct Colors *, CELL, CELL); diff --git a/include/grass/raster.h b/include/grass/raster.h index 138fb21c3d1..7c04b64944d 100644 --- a/include/grass/raster.h +++ b/include/grass/raster.h @@ -168,6 +168,15 @@ enum History_field { HIST_NUM_FIELDS }; +/*! + \typedef ColorFormat + \brief Color format identifiers (enum) + + Identifies of all recognized color format. + + */ +typedef enum { RGB, HEX, HSV, TRIPLET } ColorFormat; + /*! \brief Raster history info (metadata) */ struct History { /*! \brief Array of fields (see \ref History_field for details) */ diff --git a/raster/r.colors.out/prt_json.c b/lib/raster/json_color_out.c similarity index 75% rename from raster/r.colors.out/prt_json.c rename to lib/raster/json_color_out.c index 816a63e1b0e..f5073f9f13f 100644 --- a/raster/r.colors.out/prt_json.c +++ b/lib/raster/json_color_out.c @@ -1,15 +1,27 @@ +/*! + * \file lib/raster/json_color_out.c + * + * \brief Raster Library - Print color table in json format + * + * (C) 2010-2024 by the GRASS Development Team + * + * This program is free software under the GNU General Public + * License (>=v2). Read the file COPYING that comes with GRASS + * for details. + * + * \author Nishant Bansal + */ + #include #include #include #include #include +#include #include -#include #include -#include "local_proto.h" - #define COLOR_STRING_LENGTH 30 /*! @@ -77,7 +89,7 @@ static void rgb_to_hsv(int r, int g, int b, float *h, float *s, float *v) \param clr_frmt color format to be used (RGB, HEX, HSV, TRIPLET). \param color_object pointer to the JSON object */ -static void set_color(int r, int g, int b, enum ColorFormat clr_frmt, +static void set_color(int r, int g, int b, ColorFormat clr_frmt, JSON_Object *color_object) { char color_string[COLOR_STRING_LENGTH]; @@ -87,24 +99,24 @@ static void set_color(int r, int g, int b, enum ColorFormat clr_frmt, case RGB: snprintf(color_string, sizeof(color_string), "rgb(%d, %d, %d)", r, g, b); - json_object_set_string(color_object, "rgb", color_string); + G_json_object_set_string(color_object, "rgb", color_string); break; case HEX: snprintf(color_string, sizeof(color_string), "#%02X%02X%02X", r, g, b); - json_object_set_string(color_object, "hex", color_string); + G_json_object_set_string(color_object, "hex", color_string); break; case HSV: rgb_to_hsv(r, g, b, &h, &s, &v); snprintf(color_string, sizeof(color_string), "hsv(%d, %d, %d)", (int)h, (int)s, (int)v); - json_object_set_string(color_object, "hsv", color_string); + G_json_object_set_string(color_object, "hsv", color_string); break; case TRIPLET: snprintf(color_string, sizeof(color_string), "%d:%d:%d", r, g, b); - json_object_set_string(color_object, "triplet", color_string); + G_json_object_set_string(color_object, "triplet", color_string); break; } } @@ -126,7 +138,7 @@ static void set_color(int r, int g, int b, enum ColorFormat clr_frmt, */ static void write_json_rule(DCELL *val, DCELL *min, DCELL *max, int r, int g, int b, JSON_Array *root_array, int perc, - enum ColorFormat clr_frmt, FILE *fp, + ColorFormat clr_frmt, FILE *fp, JSON_Value *root_value) { static DCELL v0; @@ -138,24 +150,24 @@ static void write_json_rule(DCELL *val, DCELL *min, DCELL *max, int r, int g, // Update last processed values v0 = *val, r0 = r, g0 = g, b0 = b; - JSON_Value *color_value = json_value_init_object(); + JSON_Value *color_value = G_json_value_init_object(); if (color_value == NULL) { - json_value_free(root_value); + G_json_value_free(root_value); close_file(fp); G_fatal_error(_("Failed to initialize JSON object. Out of memory?")); } - JSON_Object *color_object = json_object(color_value); + JSON_Object *color_object = G_json_object(color_value); // Set the value as a percentage if requested, otherwise set it as-is if (perc) - json_object_set_number(color_object, "value", - 100 * (*val - *min) / (*max - *min)); + G_json_object_set_number(color_object, "value", + 100 * (*val - *min) / (*max - *min)); else - json_object_set_number(color_object, "value", *val); + G_json_object_set_number(color_object, "value", *val); set_color(r, g, b, clr_frmt, color_object); - json_array_append_value(root_array, color_value); + G_json_array_append_value(root_array, color_value); } /*! @@ -168,15 +180,15 @@ static void write_json_rule(DCELL *val, DCELL *min, DCELL *max, int r, int g, \param perc TRUE for percentage output \param clr_frmt color format to be used (RBG, HEX, HSV, TRIPLET). */ -void print_json_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp, - int perc, enum ColorFormat clr_frmt) +void Rast_print_json_colors(struct Colors *colors, DCELL min, DCELL max, + FILE *fp, int perc, ColorFormat clr_frmt) { - JSON_Value *root_value = json_value_init_array(); + JSON_Value *root_value = G_json_value_init_array(); if (root_value == NULL) { close_file(fp); G_fatal_error(_("Failed to initialize JSON array. Out of memory?")); } - JSON_Array *root_array = json_array(root_value); + JSON_Array *root_array = G_json_array(root_value); if (colors->version < 0) { /* 3.0 format */ @@ -221,45 +233,45 @@ void print_json_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp, // Get RGB color for null values and create JSON entry Rast_get_null_value_color(&r, &g, &b, colors); - JSON_Value *nv_value = json_value_init_object(); + JSON_Value *nv_value = G_json_value_init_object(); if (nv_value == NULL) { - json_value_free(root_value); + G_json_value_free(root_value); close_file(fp); G_fatal_error( _("Failed to initialize JSON object. Out of memory?")); } - JSON_Object *nv_object = json_object(nv_value); - json_object_set_string(nv_object, "value", "nv"); + JSON_Object *nv_object = G_json_object(nv_value); + G_json_object_set_string(nv_object, "value", "nv"); set_color(r, g, b, clr_frmt, nv_object); - json_array_append_value(root_array, nv_value); + G_json_array_append_value(root_array, nv_value); // Get RGB color for default values and create JSON entry Rast_get_default_color(&r, &g, &b, colors); - JSON_Value *default_value = json_value_init_object(); + JSON_Value *default_value = G_json_value_init_object(); if (default_value == NULL) { - json_value_free(root_value); + G_json_value_free(root_value); close_file(fp); G_fatal_error( _("Failed to initialize JSON object. Out of memory?")); } - JSON_Object *default_object = json_object(default_value); - json_object_set_string(default_object, "value", "default"); + JSON_Object *default_object = G_json_object(default_value); + G_json_object_set_string(default_object, "value", "default"); set_color(r, g, b, clr_frmt, default_object); - json_array_append_value(root_array, default_value); + G_json_array_append_value(root_array, default_value); } // Serialize JSON array to a string and print to the file - char *json_string = json_serialize_to_string_pretty(root_value); + char *json_string = G_json_serialize_to_string_pretty(root_value); if (!json_string) { - json_value_free(root_value); + G_json_value_free(root_value); close_file(fp); G_fatal_error(_("Failed to serialize JSON to pretty format.")); } fputs(json_string, fp); - json_free_serialized_string(json_string); - json_value_free(root_value); + G_json_free_serialized_string(json_string); + G_json_value_free(root_value); close_file(fp); } diff --git a/raster/r.colors.out/Makefile b/raster/r.colors.out/Makefile index e2acf03ca41..b6717d12937 100644 --- a/raster/r.colors.out/Makefile +++ b/raster/r.colors.out/Makefile @@ -1,13 +1,13 @@ MODULE_TOPDIR = ../.. -LIBES2 = $(RASTERLIB) $(GISLIB) $(PARSONLIB) -LIBES3 = $(RASTER3DLIB) $(RASTERLIB) $(GISLIB) $(PARSONLIB) +LIBES2 = $(RASTERLIB) $(GISLIB) +LIBES3 = $(RASTER3DLIB) $(RASTERLIB) $(GISLIB) DEPENDENCIES = $(RASTER3DDEP) $(GISDEP) $(RASTERDEP) PROGRAMS = r.colors.out r3.colors.out -r_colors_out_OBJS = raster_main.o prt_json.o -r3_colors_out_OBJS = raster3d_main.o prt_json.o +r_colors_out_OBJS = raster_main.o +r3_colors_out_OBJS = raster3d_main.o include $(MODULE_TOPDIR)/include/Make/Multi.make diff --git a/raster/r.colors.out/local_proto.h b/raster/r.colors.out/local_proto.h deleted file mode 100644 index 5ed09a06e69..00000000000 --- a/raster/r.colors.out/local_proto.h +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -enum ColorFormat { RGB, HEX, HSV, TRIPLET }; - -void print_json_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp, - int perc, enum ColorFormat clr_frmt); diff --git a/raster/r.colors.out/raster3d_main.c b/raster/r.colors.out/raster3d_main.c index 904db24a91f..e988663d665 100644 --- a/raster/r.colors.out/raster3d_main.c +++ b/raster/r.colors.out/raster3d_main.c @@ -21,9 +21,6 @@ #include #include #include -#include - -#include "local_proto.h" /* Run in raster3d mode */ int main(int argc, char **argv) @@ -41,7 +38,7 @@ int main(int argc, char **argv) struct Colors colors; struct FPRange range; - enum ColorFormat clr_frmt; + ColorFormat clr_frmt; G_gisinit(argv[0]); @@ -102,8 +99,8 @@ int main(int argc, char **argv) else { clr_frmt = HEX; } - print_json_colors(&colors, range.min, range.max, fp, - flag.p->answer ? 1 : 0, clr_frmt); + Rast_print_json_colors(&colors, range.min, range.max, fp, + flag.p->answer ? 1 : 0, clr_frmt); } else { Rast_print_colors(&colors, range.min, range.max, fp, diff --git a/raster/r.colors.out/raster_main.c b/raster/r.colors.out/raster_main.c index 6b4a8ed36ee..60807c06383 100644 --- a/raster/r.colors.out/raster_main.c +++ b/raster/r.colors.out/raster_main.c @@ -20,9 +20,6 @@ #include #include #include -#include - -#include "local_proto.h" /* Run in raster mode */ int main(int argc, char **argv) @@ -40,7 +37,7 @@ int main(int argc, char **argv) struct Colors colors; struct FPRange range; - enum ColorFormat clr_frmt; + ColorFormat clr_frmt; G_gisinit(argv[0]); @@ -101,8 +98,8 @@ int main(int argc, char **argv) else { clr_frmt = HEX; } - print_json_colors(&colors, range.min, range.max, fp, - flag.p->answer ? 1 : 0, clr_frmt); + Rast_print_json_colors(&colors, range.min, range.max, fp, + flag.p->answer ? 1 : 0, clr_frmt); } else { Rast_print_colors(&colors, range.min, range.max, fp,