GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
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 * SPDX-FileCopyrightText: 2010-2024 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Nishant Bansal
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
16#include <grass/gis.h>
17#include <grass/colors.h>
18#include <grass/gjson.h>
19#include <grass/glocale.h>
20#include <grass/raster.h>
21
22/*!
23 \brief Closes the file if it is not stdout.
24
25 \param fp file where to print color table rules
26 */
27static void close_file(FILE *fp)
28{
29 if (fp != stdout)
30 fclose(fp);
31}
32
33/*!
34 \brief Writes a JSON rule for a specific color entry.
35
36 \param val pointer to the DCELL value
37 \param min,max minimum and maximum value for percentage output (used only
38 when \p perc is non-zero)
39 \param r red component of RGB color
40 \param g green component of RGB color
41 \param b blue component of RGB color
42 \param root_array pointer to the JSON array
43 \param perc TRUE for percentage output
44 \param clr_frmt color format to be used (RBG, HEX, HSV, TRIPLET).
45 \param fp file where to print color table rules
46 \param root_value pointer to json value
47 */
48static void write_json_rule(DCELL *val, DCELL *min, DCELL *max, int r, int g,
49 int b, G_JSON_Array *root_array, int perc,
52{
53 static DCELL v0;
54 static int r0 = -1, g0 = -1, b0 = -1;
55
56 // Skip writing if the current color is the same as the last one
57 if (v0 == *val && r0 == r && g0 == g && b0 == b)
58 return;
59 // Update last processed values
60 v0 = *val, r0 = r, g0 = g, b0 = b;
61
63 if (color_value == NULL) {
65 close_file(fp);
66 G_fatal_error(_("Failed to initialize JSON object. Out of memory?"));
67 }
69
70 // Set the value as a percentage if requested, otherwise set it as-is
71 if (perc)
73 100 * (*val - *min) / (*max - *min));
74 else
76
80
82}
83
84/*!
85 \brief Print color table in JSON format
86
87 \param colors pointer to Colors structure
88 \param min,max minimum and maximum value for percentage output (used only
89 when \p perc is non-zero)
90 \param fp file where to print color table rules
91 \param perc TRUE for percentage output
92 \param clr_frmt color format to be used (RBG, HEX, HSV, TRIPLET).
93
94 \since version 8.5
95 */
97 FILE *fp, int perc, ColorFormat clr_frmt)
98{
100 if (root_value == NULL) {
101 close_file(fp);
102 G_fatal_error(_("Failed to initialize JSON object. Out of memory?"));
103 }
105
107 if (table_value == NULL) {
108 close_file(fp);
109 G_fatal_error(_("Failed to initialize JSON array. Out of memory?"));
110 }
112
114
115 if (colors->version < 0) {
116 /* 3.0 format */
117 CELL lo, hi;
118
119 // Retrieve the integer color range
120 Rast_get_c_color_range(&lo, &hi, colors);
121
122 for (int i = lo; i <= hi; i++) {
123 unsigned char r, g, b, set;
124 DCELL val = (DCELL)i;
125
126 // Look up the color for the current value and write JSON rule
127 Rast_lookup_c_colors(&i, &r, &g, &b, &set, 1, colors);
128 write_json_rule(&val, &min, &max, r, g, b, table_array, perc,
129 clr_frmt, fp, table_value);
130 }
131 }
132 else {
133 // Get the count of floating-point color rules
134 int count = Rast_colors_count(colors);
135
136 for (int i = 0; i < count; i++) {
137 DCELL val1, val2;
138 unsigned char r1, g1, b1, r2, g2, b2;
139
140 // Retrieve the color rule values and their respective RGB colors
141 Rast_get_fp_color_rule(&val1, &r1, &g1, &b1, &val2, &r2, &g2, &b2,
142 colors, count - 1 - i);
143
144 // write JSON rule
145 write_json_rule(&val1, &min, &max, r1, g1, b1, table_array, perc,
146 clr_frmt, fp, table_value);
147 write_json_rule(&val2, &min, &max, r2, g2, b2, table_array, perc,
148 clr_frmt, fp, table_value);
149 }
150 }
151
152 // Add the color table to the root object
154
155 // Add special color entries for "null" and "default" values
156 {
157 int r, g, b;
158
159 // Get RGB color for null values and create JSON entry
160 Rast_get_null_value_color(&r, &g, &b, colors);
163
164 // Get RGB color for default values and create JSON entry
165 Rast_get_default_color(&r, &g, &b, colors);
168 }
169
170 // Serialize JSON array to a string and print to the file
172 if (!json_string) {
174 close_file(fp);
175 G_fatal_error(_("Failed to serialize JSON to pretty format."));
176 }
177
178 fputs(json_string, fp);
179
182
183 close_file(fp);
184}
#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:62
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:124
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:42
void Rast_get_default_color(int *, int *, int *, const struct Colors *)
Gets default color.
Definition color_get.c:152
#define min(x, y)
Definition draw2.c:29
#define max(x, y)
Definition draw2.c:30
double DCELL
Definition gis.h:632
int CELL
Definition gis.h:631
G_JSON_Status G_json_object_set_number(G_JSON_Object *object, const char *name, double number)
Definition gjson.c:133
G_JSON_Array * G_json_array(const G_JSON_Value *value)
Definition gjson.c:185
void G_json_value_free(G_JSON_Value *value)
Definition gjson.c:272
G_JSON_Status G_json_object_set_value(G_JSON_Object *object, const char *name, G_JSON_Value *value)
Definition gjson.c:118
void G_json_free_serialized_string(char *string)
Definition gjson.c:266
char * G_json_serialize_to_string_pretty(const G_JSON_Value *value)
Definition gjson.c:254
G_JSON_Status G_json_object_set_string(G_JSON_Object *object, const char *name, const char *string)
Definition gjson.c:126
G_JSON_Value * G_json_value_init_object(void)
Definition gjson.c:38
G_JSON_Value * G_json_value_init_array(void)
Definition gjson.c:44
G_JSON_Object * G_json_object(const G_JSON_Value *value)
Definition gjson.c:62
G_JSON_Status G_json_array_append_value(G_JSON_Array *array, G_JSON_Value *value)
Definition gjson.c:216
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:37
double r
Definition r_raster.c:37
Definition gis.h:689
int version
Definition gis.h:690