GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
json_color_out.c
Go to the documentation of this file.
1/*!
2 * \file lib/raster/json_color_out.c
3 *
4 * \brief Raster Library - Print color table in json format
5 *
6 * (C) 2010-2024 by the GRASS Development Team
7 *
8 * This program is free software under the GNU General Public
9 * License (>=v2). Read the file COPYING that comes with GRASS
10 * for details.
11 *
12 * \author Nishant Bansal
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <grass/gis.h>
20#include <grass/colors.h>
21#include <grass/gjson.h>
22#include <grass/glocale.h>
23#include <grass/raster.h>
24
25/*!
26 \brief Closes the file if it is not stdout.
27
28 \param fp file where to print color table rules
29 */
30static void close_file(FILE *fp)
31{
32 if (fp != stdout)
33 fclose(fp);
34}
35
36/*!
37 \brief Writes a JSON rule for a specific color entry.
38
39 \param val pointer to the DCELL value
40 \param min,max minimum and maximum value for percentage output (used only
41 when \p perc is non-zero)
42 \param r red component of RGB color
43 \param g green component of RGB color
44 \param b blue component of RGB color
45 \param root_array pointer to the JSON array
46 \param perc TRUE for percentage output
47 \param clr_frmt color format to be used (RBG, HEX, HSV, TRIPLET).
48 \param fp file where to print color table rules
49 \param root_value pointer to json value
50 */
51static void write_json_rule(DCELL *val, DCELL *min, DCELL *max, int r, int g,
52 int b, G_JSON_Array *root_array, int perc,
55{
56 static DCELL v0;
57 static int r0 = -1, g0 = -1, b0 = -1;
58
59 // Skip writing if the current color is the same as the last one
60 if (v0 == *val && r0 == r && g0 == g && b0 == b)
61 return;
62 // Update last processed values
63 v0 = *val, r0 = r, g0 = g, b0 = b;
64
66 if (color_value == NULL) {
68 close_file(fp);
69 G_fatal_error(_("Failed to initialize JSON object. Out of memory?"));
70 }
72
73 // Set the value as a percentage if requested, otherwise set it as-is
74 if (perc)
76 100 * (*val - *min) / (*max - *min));
77 else
79
83
85}
86
87/*!
88 \brief Print color table in JSON format
89
90 \param colors pointer to Colors structure
91 \param min,max minimum and maximum value for percentage output (used only
92 when \p perc is non-zero)
93 \param fp file where to print color table rules
94 \param perc TRUE for percentage output
95 \param clr_frmt color format to be used (RBG, HEX, HSV, TRIPLET).
96 */
98 FILE *fp, int perc, ColorFormat clr_frmt)
99{
101 if (root_value == NULL) {
102 close_file(fp);
103 G_fatal_error(_("Failed to initialize JSON object. Out of memory?"));
104 }
106
108 if (table_value == NULL) {
109 close_file(fp);
110 G_fatal_error(_("Failed to initialize JSON array. Out of memory?"));
111 }
113
115
116 if (colors->version < 0) {
117 /* 3.0 format */
118 CELL lo, hi;
119
120 // Retrieve the integer color range
121 Rast_get_c_color_range(&lo, &hi, colors);
122
123 for (int i = lo; i <= hi; i++) {
124 unsigned char r, g, b, set;
125 DCELL val = (DCELL)i;
126
127 // Look up the color for the current value and write JSON rule
128 Rast_lookup_c_colors(&i, &r, &g, &b, &set, 1, colors);
129 write_json_rule(&val, &min, &max, r, g, b, table_array, perc,
130 clr_frmt, fp, table_value);
131 }
132 }
133 else {
134 // Get the count of floating-point color rules
135 int count = Rast_colors_count(colors);
136
137 for (int i = 0; i < count; i++) {
138 DCELL val1, val2;
139 unsigned char r1, g1, b1, r2, g2, b2;
140
141 // Retrieve the color rule values and their respective RGB colors
142 Rast_get_fp_color_rule(&val1, &r1, &g1, &b1, &val2, &r2, &g2, &b2,
143 colors, count - 1 - i);
144
145 // write JSON rule
146 write_json_rule(&val1, &min, &max, r1, g1, b1, table_array, perc,
147 clr_frmt, fp, table_value);
148 write_json_rule(&val2, &min, &max, r2, g2, b2, table_array, perc,
149 clr_frmt, fp, table_value);
150 }
151 }
152
153 // Add the color table to the root object
155
156 // Add special color entries for "null" and "default" values
157 {
158 int r, g, b;
159
160 // Get RGB color for null values and create JSON entry
161 Rast_get_null_value_color(&r, &g, &b, colors);
164
165 // Get RGB color for default values and create JSON entry
166 Rast_get_default_color(&r, &g, &b, colors);
169 }
170
171 // Serialize JSON array to a string and print to the file
173 if (!json_string) {
175 close_file(fp);
176 G_fatal_error(_("Failed to serialize JSON to pretty format."));
177 }
178
179 fputs(json_string, fp);
180
183
184 close_file(fp);
185}
#define NULL
Definition ccmath.h:32
ColorFormat
Color format identifiers (enum)
Definition colors.h:55
#define COLOR_STRING_LENGTH
Definition colors.h:28
void G_color_to_str(int, int, int, ColorFormat, char *)
Parse red,green,blue and set color string.
Definition color_str.c:210
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int Rast_colors_count(const struct Colors *)
Get both modular and fixed rules count.
void Rast_get_c_color_range(CELL *, CELL *, const struct Colors *)
Get color range values (CELL)
Definition color_range.c:64
int Rast_get_fp_color_rule(DCELL *, unsigned char *, unsigned char *, unsigned char *, DCELL *, unsigned char *, unsigned char *, unsigned char *, const struct Colors *, int)
Get color rule from both modular and fixed rules.
void Rast_get_null_value_color(int *, int *, int *, const struct Colors *)
Gets color for null value.
Definition color_get.c:126
void Rast_lookup_c_colors(const CELL *, unsigned char *, unsigned char *, unsigned char *, unsigned char *, int, struct Colors *)
Lookup an array of colors.
Definition color_look.c:45
void Rast_get_default_color(int *, int *, int *, const struct Colors *)
Gets default color.
Definition color_get.c:154
#define min(x, y)
Definition draw2.c:29
#define max(x, y)
Definition draw2.c:30
double DCELL
Definition gis.h:635
int CELL
Definition gis.h:634
G_JSON_Status G_json_object_set_number(G_JSON_Object *object, const char *name, double number)
Definition gjson.c:107
G_JSON_Array * G_json_array(const G_JSON_Value *value)
Definition gjson.c:150
void G_json_value_free(G_JSON_Value *value)
Definition gjson.c:223
G_JSON_Status G_json_object_set_value(G_JSON_Object *object, const char *name, G_JSON_Value *value)
Definition gjson.c:96
void G_json_free_serialized_string(char *string)
Definition gjson.c:218
char * G_json_serialize_to_string_pretty(const G_JSON_Value *value)
Definition gjson.c:208
G_JSON_Status G_json_object_set_string(G_JSON_Object *object, const char *name, const char *string)
Definition gjson.c:102
G_JSON_Value * G_json_value_init_object(void)
Definition gjson.c:29
G_JSON_Value * G_json_value_init_array(void)
Definition gjson.c:34
G_JSON_Object * G_json_object(const G_JSON_Value *value)
Definition gjson.c:49
G_JSON_Status G_json_array_append_value(G_JSON_Array *array, G_JSON_Value *value)
Definition gjson.c:176
struct G_json_array_t G_JSON_Array
Definition gjson.h:11
struct G_json_object_t G_JSON_Object
Definition gjson.h:10
struct G_json_value_t G_JSON_Value
Definition gjson.h:12
#define _(str)
Definition glocale.h:10
int count
void Rast_print_json_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp, int perc, ColorFormat clr_frmt)
Print color table in JSON format.
float g
Definition named_colr.c:7
const char * json_string(const JSON_Value *value)
Definition parson.c:2809
double b
Definition r_raster.c:39
double r
Definition r_raster.c:39
Definition gis.h:692
int version
Definition gis.h:693