GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-112dd97adf
rast_to_img_string.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Function: Rast_map_to_img_str() based on r.to.ppm
4  * AUTHOR(S): Bill Brown, USA-CERL (original contributor)
5  * Markus Neteler <neteler itc.it>,
6  * Bernhard Reiter <bernhard intevation.de>,
7  * Glynn Clements <glynn gclements.plus.com>,
8  * Jachym Cepicky <jachym les-ejk.cz>,
9  * Jan-Oliver Wagner <jan intevation.de>
10  * Soeren Gebbert
11  * PURPOSE: converts a GRASS raster map into an ARGB or
12  * gray scale unsigned char string
13  * COPYRIGHT: (C) 1999-2015 by the GRASS Development Team
14  *
15  * This program is free software under the GNU General Public
16  * License (>=v2). Read the file COPYING that comes with GRASS
17  * for details.
18  *
19  *****************************************************************************/
20 
21 #include <string.h>
22 #include <stdlib.h>
23 
24 #include <grass/gis.h>
25 #include <grass/raster.h>
26 #include <grass/glocale.h>
27 
28 #define DEF_RED 255
29 #define DEF_GRN 255
30 #define DEF_BLU 255
31 
32 /* \brief Convert a raster map layer into a string with
33  * 32Bit ARGB, 32Bit RGB or 8Bit Gray little endian encoding.
34  *
35  * The raster color table is used for coloring the image. Null values are
36  * marked as transparent. Only little endian encoding is supported.
37  *
38  * This function uses Rast_window_rows() and Rast_window_cols() to
39  * get rows and cols, hence use Rast_set_window() to set the required
40  * region for raster access.
41  *
42  * \param name The name of the raster map layer to convert
43  * \param color_mode The color modes to use:
44  * Color mode 1 -> 32Bit ARGB (0xAARRGGBB)
45  * Color mode 2 -> 32Bit RGB (0xffRRGGBB)
46  * Color mode 3 -> grey scale formular: .33R+ .5G+ .17B
47  * Color mode 4 -> grey scale formular: .30R+ .59G+ .11B
48  *
49  * \param result: An unsigned char pointer to store the result.
50  * It must have size 4*cols*rows in case of
51  * ARGB and RGB,
52  * rows*cols in case of gray scale.
53  *
54  * \return: 0 in case map not found, -1 in case the color mode is incorrect, 1
55  * on success
56  *
57  */
58 int Rast_map_to_img_str(char *name, int color_mode, unsigned char *result)
59 {
60  unsigned char *set = NULL, *red = NULL, *green = NULL, *blue = NULL;
61  unsigned char alpha;
62  const char *mapset = NULL;
63  CELL *cell_buf = NULL;
64  FCELL *fcell_buf = NULL;
65  DCELL *dcell_buf = NULL;
66  void *voidc = NULL;
67  int rtype, row, col;
68  size_t i;
69  int map = 0;
70 
71  struct Colors colors;
72  int rows = Rast_window_rows();
73  int cols = Rast_window_cols();
74 
75  if (color_mode > 3 || color_mode < 1)
76  return (-1);
77 
78  mapset = G_find_raster2(name, "");
79 
80  if (!mapset)
81  return (0);
82 
83  map = Rast_open_old(name, "");
84 
85  cell_buf = Rast_allocate_c_buf();
86  fcell_buf = Rast_allocate_f_buf();
87  dcell_buf = Rast_allocate_d_buf();
88 
89  red = G_malloc(cols);
90  green = G_malloc(cols);
91  blue = G_malloc(cols);
92  set = G_malloc(cols);
93 
94  Rast_read_colors(name, mapset, &colors);
95 
96  rtype = Rast_get_map_type(map);
97  if (rtype == CELL_TYPE)
98  voidc = (CELL *)cell_buf;
99  else if (rtype == FCELL_TYPE)
100  voidc = (FCELL *)fcell_buf;
101  else if (rtype == DCELL_TYPE)
102  voidc = (DCELL *)dcell_buf;
103 
104  i = 0;
105 
106  if (color_mode == 1 ||
107  color_mode == 2) { /* 32BIT ARGB COLOR IMAGE with transparency */
108  for (row = 0; row < rows; row++) {
109  Rast_get_row(map, (void *)voidc, row, rtype);
110  Rast_lookup_colors((void *)voidc, red, green, blue, set, cols,
111  &colors, rtype);
112 
113  alpha = (unsigned char)255;
114  if (color_mode == 1 && Rast_is_null_value(voidc, rtype)) {
115  alpha = (unsigned char)0;
116  }
117  for (col = 0; col < cols; col++) {
118  /* Only little endian */
119  if (set[col]) {
120  result[i++] = blue[col];
121  result[i++] = green[col];
122  result[i++] = red[col];
123  result[i++] = alpha;
124  }
125  else {
126  result[i++] = DEF_BLU;
127  result[i++] = DEF_GRN;
128  result[i++] = DEF_RED;
129  result[i++] = alpha;
130  }
131  }
132  }
133  }
134  else { /* GREYSCALE IMAGE */
135  for (row = 0; row < rows; row++) {
136  Rast_get_row(map, (void *)voidc, row, rtype);
137  Rast_lookup_colors((void *)voidc, red, green, blue, set, cols,
138  &colors, rtype);
139 
140  if (color_mode == 3) {
141  for (col = 0; col < cols; col++) {
142  /*.33R+ .5G+ .17B */
143  result[i++] = ((red[col]) * 11 + (green[col]) * 16 +
144  (blue[col]) * 5) >>
145  5;
146  }
147  }
148  else {
149  for (col = 0; col < cols; col++) {
150  /*NTSC Y equation: .30R+ .59G+ .11B */
151  result[i++] = ((red[col]) * 19 + (green[col]) * 38 +
152  (blue[col]) * 7) >>
153  6;
154  }
155  }
156  }
157  }
158 
159  Rast_free_colors(&colors);
160 
161  G_free(cell_buf);
162  G_free(fcell_buf);
163  G_free(dcell_buf);
164  G_free(red);
165  G_free(green);
166  G_free(blue);
167  G_free(set);
168  Rast_close(map);
169 
170  return (1);
171 }
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_malloc(n)
Definition: defs/gis.h:94
const char * G_find_raster2(const char *, const char *)
Find a raster map (look but don't touch)
Definition: find_rast.c:76
int Rast_is_null_value(const void *, RASTER_MAP_TYPE)
To check if a raster value is set to NULL.
Definition: null_val.c:176
int Rast_read_colors(const char *, const char *, struct Colors *)
Read color table of raster map.
void Rast_close(int)
Close a raster map.
Definition: raster/close.c:99
void Rast_lookup_colors(const void *, unsigned char *, unsigned char *, unsigned char *, unsigned char *, int, struct Colors *, RASTER_MAP_TYPE)
Lookup an array of colors.
Definition: color_look.c:79
int Rast_open_old(const char *, const char *)
Open an existing integer raster map (cell)
Definition: raster/open.c:112
void Rast_free_colors(struct Colors *)
Free color structure memory.
Definition: color_free.c:30
FCELL * Rast_allocate_f_buf(void)
Allocates memory for a raster map of type FCELL.
Definition: alloc_cell.c:94
CELL * Rast_allocate_c_buf(void)
Allocate memory for a CELL type raster map.
Definition: alloc_cell.c:81
DCELL * Rast_allocate_d_buf(void)
Allocates memory for a raster map of type DCELL.
Definition: alloc_cell.c:107
int Rast_window_cols(void)
Number of columns in active window.
int Rast_window_rows(void)
Number of rows in active window.
Definition: raster/window.c:87
RASTER_MAP_TYPE Rast_get_map_type(int)
Determine raster type from descriptor.
Definition: raster/open.c:932
void Rast_get_row(int, void *, int, RASTER_MAP_TYPE)
Get raster row.
float FCELL
Definition: gis.h:627
double DCELL
Definition: gis.h:626
int CELL
Definition: gis.h:625
const char * name
Definition: named_colr.c:6
#define DEF_BLU
int Rast_map_to_img_str(char *name, int color_mode, unsigned char *result)
#define DEF_RED
#define DEF_GRN
#define FCELL_TYPE
Definition: raster.h:12
#define DCELL_TYPE
Definition: raster.h:13
#define CELL_TYPE
Definition: raster.h:11
Definition: gis.h:683