Skip to content

Commit

Permalink
rasterlib: Rast_print_json_colors() added (taken from r.colors.out) (#…
Browse files Browse the repository at this point in the history
…4665)

* rasterlib: Rast_print_json_colors() added (taken from r.colors.out)

Signed-off-by: Nishant Bansal <[email protected]>

* fixes CI build failure

Signed-off-by: Nishant Bansal <[email protected]>

* fixes failing pytest

Signed-off-by: Nishant Bansal <[email protected]>

* fixes parson issue

Signed-off-by: Nishant Bansal <[email protected]>

* fix json parsing issue using gjson api

Signed-off-by: Nishant Bansal <[email protected]>

* remove unused headers

Signed-off-by: Nishant Bansal <[email protected]>

* Update include/grass/raster.h

---------

Signed-off-by: Nishant Bansal <[email protected]>
Co-authored-by: Anna Petrasova <[email protected]>
  • Loading branch information
NishantBansal2003 and petrasovaa authored Jan 18, 2025
1 parent fc9418d commit 98001df
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 58 deletions.
2 changes: 1 addition & 1 deletion include/Make/Grass.make
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions include/grass/defs/raster.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions include/grass/raster.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ enum History_field {
HIST_NUM_FIELDS
};

/*!
\typedef ColorFormat
\brief Color format identifiers (enum)
Identifiers of all recognized color formats.
*/
typedef enum { RGB, HEX, HSV, TRIPLET } ColorFormat;

/*! \brief Raster history info (metadata) */
struct History {
/*! \brief Array of fields (see \ref History_field for details) */
Expand Down
80 changes: 46 additions & 34 deletions raster/r.colors.out/prt_json.c → lib/raster/json_color_out.c
Original file line number Diff line number Diff line change
@@ -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 <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <grass/gis.h>
#include <grass/gjson.h>
#include <grass/glocale.h>
#include <grass/parson.h>
#include <grass/raster.h>

#include "local_proto.h"

#define COLOR_STRING_LENGTH 30

/*!
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -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);
}

/*!
Expand All @@ -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 */
Expand Down Expand Up @@ -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);
}
8 changes: 4 additions & 4 deletions raster/r.colors.out/Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 0 additions & 7 deletions raster/r.colors.out/local_proto.h

This file was deleted.

9 changes: 3 additions & 6 deletions raster/r.colors.out/raster3d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
#include <grass/raster.h>
#include <grass/raster3d.h>
#include <grass/glocale.h>
#include <grass/parson.h>

#include "local_proto.h"

/* Run in raster3d mode */
int main(int argc, char **argv)
Expand All @@ -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]);

Expand Down Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions raster/r.colors.out/raster_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/glocale.h>
#include <grass/parson.h>

#include "local_proto.h"

/* Run in raster mode */
int main(int argc, char **argv)
Expand All @@ -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]);

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 98001df

Please sign in to comment.