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
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Nov 7, 2024
1 parent aa07454 commit e49b097
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 25 deletions.
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)
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) */
Expand Down
1 change: 1 addition & 0 deletions lib/raster/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MODULE_TOPDIR = ../..

LIB = RASTER
LIBES = $(PARSONLIB)
EXTRA_INC =

include $(MODULE_TOPDIR)/include/Make/Vars.make
Expand Down
23 changes: 17 additions & 6 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,3 +1,16 @@
/*!
\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.
*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -8,8 +21,6 @@
#include <grass/parson.h>
#include <grass/raster.h>

#include "local_proto.h"

#define COLOR_STRING_LENGTH 30

/*!
Expand Down Expand Up @@ -77,7 +88,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 Down Expand Up @@ -126,7 +137,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 Down Expand Up @@ -168,8 +179,8 @@ 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();
if (root_value == NULL) {
Expand Down
4 changes: 2 additions & 2 deletions raster/r.colors.out/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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.

8 changes: 3 additions & 5 deletions raster/r.colors.out/raster3d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#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 +39,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 +100,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
8 changes: 3 additions & 5 deletions raster/r.colors.out/raster_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#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 +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 @@ -101,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

0 comments on commit e49b097

Please sign in to comment.