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