GRASS 8 Programmer's Manual 8.6.0dev(2026)-813e1e5dca
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
97 \since version 8.5
98 */
100 FILE *fp, int perc, ColorFormat clr_frmt)
101{
103 if (root_value == NULL) {
104 close_file(fp);
105 G_fatal_error(_("Failed to initialize JSON object. Out of memory?"));
106 }
108
110 if (table_value == NULL) {
111 close_file(fp);
112 G_fatal_error(_("Failed to initialize JSON array. Out of memory?"));
113 }
115
117
118 if (colors->version < 0) {
119 /* 3.0 format */
120 CELL lo, hi;
121
122 // Retrieve the integer color range
123 Rast_get_c_color_range(&lo, &hi, colors);
124
125 for (int i = lo; i <= hi; i++) {
126 unsigned char r, g, b, set;
127 DCELL val = (DCELL)i;
128
129 // Look up the color for the current value and write JSON rule
130 Rast_lookup_c_colors(&i, &r, &g, &b, &set, 1, colors);
131 write_json_rule(&val, &min, &max, r, g, b, table_array, perc,
132 clr_frmt, fp, table_value);
133 }
134 }
135 else {
136 // Get the count of floating-point color rules
137 int count = Rast_colors_count(colors);
138
139 for (int i = 0; i < count; i++) {
140 DCELL val1, val2;
141 unsigned char r1, g1, b1, r2, g2, b2;
142
143 // Retrieve the color rule values and their respective RGB colors
144 Rast_get_fp_color_rule(&val1, &r1, &g1, &b1, &val2, &r2, &g2, &b2,
145 colors, count - 1 - i);
146
147 // write JSON rule
148 write_json_rule(&val1, &min, &max, r1, g1, b1, table_array, perc,
149 clr_frmt, fp, table_value);
150 write_json_rule(&val2, &min, &max, r2, g2, b2, table_array, perc,
151 clr_frmt, fp, table_value);
152 }
153 }
154
155 // Add the color table to the root object
157
158 // Add special color entries for "null" and "default" values
159 {
160 int r, g, b;
161
162 // Get RGB color for null values and create JSON entry
163 Rast_get_null_value_color(&r, &g, &b, colors);
166
167 // Get RGB color for default values and create JSON entry
168 Rast_get_default_color(&r, &g, &b, colors);
171 }
172
173 // Serialize JSON array to a string and print to the file
175 if (!json_string) {
177 close_file(fp);
178 G_fatal_error(_("Failed to serialize JSON to pretty format."));
179 }
180
181 fputs(json_string, fp);
182
185
186 close_file(fp);
187}
#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:214
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:136
G_JSON_Array * G_json_array(const G_JSON_Value *value)
Definition gjson.c:188
void G_json_value_free(G_JSON_Value *value)
Definition gjson.c:275
G_JSON_Status G_json_object_set_value(G_JSON_Object *object, const char *name, G_JSON_Value *value)
Definition gjson.c:121
void G_json_free_serialized_string(char *string)
Definition gjson.c:269
char * G_json_serialize_to_string_pretty(const G_JSON_Value *value)
Definition gjson.c:257
G_JSON_Status G_json_object_set_string(G_JSON_Object *object, const char *name, const char *string)
Definition gjson.c:129
G_JSON_Value * G_json_value_init_object(void)
Definition gjson.c:41
G_JSON_Value * G_json_value_init_array(void)
Definition gjson.c:47
G_JSON_Object * G_json_object(const G_JSON_Value *value)
Definition gjson.c:65
G_JSON_Status G_json_array_append_value(G_JSON_Array *array, G_JSON_Value *value)
Definition gjson.c:219
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