GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 1999-2015 GRASS Development Team
14 * SPDX-License-Identifier: GPL-2.0-or-later
15 *
16 *****************************************************************************/
17
18#include <string.h>
19#include <stdlib.h>
20
21#include <grass/gis.h>
22#include <grass/raster.h>
23#include <grass/glocale.h>
24
25#define DEF_RED 255
26#define DEF_GRN 255
27#define DEF_BLU 255
28
29/* \brief Convert a raster map layer into a string with
30 * 32Bit ARGB, 32Bit RGB or 8Bit Gray little endian encoding.
31 *
32 * The raster color table is used for coloring the image. Null values are
33 * marked as transparent. Only little endian encoding is supported.
34 *
35 * This function uses Rast_window_rows() and Rast_window_cols() to
36 * get rows and cols, hence use Rast_set_window() to set the required
37 * region for raster access.
38 *
39 * \param name The name of the raster map layer to convert
40 * \param color_mode The color modes to use:
41 * Color mode 1 -> 32Bit ARGB (0xAARRGGBB)
42 * Color mode 2 -> 32Bit RGB (0xffRRGGBB)
43 * Color mode 3 -> grey scale formular: .33R+ .5G+ .17B
44 * Color mode 4 -> grey scale formular: .30R+ .59G+ .11B
45 *
46 * \param result: An unsigned char pointer to store the result.
47 * It must have size 4*cols*rows in case of
48 * ARGB and RGB,
49 * rows*cols in case of gray scale.
50 *
51 * \return: 0 in case map not found, -1 in case the color mode is incorrect, 1
52 * on success
53 *
54 */
55int Rast_map_to_img_str(char *name, int color_mode, unsigned char *result)
56{
57 unsigned char *set = NULL, *red = NULL, *green = NULL, *blue = NULL;
58 unsigned char alpha;
59 const char *mapset = NULL;
63 void *voidc = NULL;
64 int rtype, row, col;
65 size_t i;
66 int map = 0;
67
68 struct Colors colors;
69 int rows = Rast_window_rows();
70 int cols = Rast_window_cols();
71
72 if (color_mode > 3 || color_mode < 1)
73 return (-1);
74
75 mapset = G_find_raster2(name, "");
76
77 if (!mapset)
78 return (0);
79
80 map = Rast_open_old(name, "");
81
85
86 red = G_malloc(cols);
87 green = G_malloc(cols);
88 blue = G_malloc(cols);
89 set = G_malloc(cols);
90
91 Rast_read_colors(name, mapset, &colors);
92
94 if (rtype == CELL_TYPE)
95 voidc = (CELL *)cell_buf;
96 else if (rtype == FCELL_TYPE)
98 else if (rtype == DCELL_TYPE)
100
101 i = 0;
102
103 if (color_mode == 1 ||
104 color_mode == 2) { /* 32BIT ARGB COLOR IMAGE with transparency */
105 for (row = 0; row < rows; row++) {
106 Rast_get_row(map, (void *)voidc, row, rtype);
107 Rast_lookup_colors((void *)voidc, red, green, blue, set, cols,
108 &colors, rtype);
109
110 alpha = (unsigned char)255;
112 alpha = (unsigned char)0;
113 }
114 for (col = 0; col < cols; col++) {
115 /* Only little endian */
116 if (set[col]) {
117 result[i++] = blue[col];
118 result[i++] = green[col];
119 result[i++] = red[col];
120 result[i++] = alpha;
121 }
122 else {
123 result[i++] = DEF_BLU;
124 result[i++] = DEF_GRN;
125 result[i++] = DEF_RED;
126 result[i++] = alpha;
127 }
128 }
129 }
130 }
131 else { /* GREYSCALE IMAGE */
132 for (row = 0; row < rows; row++) {
133 Rast_get_row(map, (void *)voidc, row, rtype);
134 Rast_lookup_colors((void *)voidc, red, green, blue, set, cols,
135 &colors, rtype);
136
137 if (color_mode == 3) {
138 for (col = 0; col < cols; col++) {
139 /*.33R+ .5G+ .17B */
140 result[i++] = ((red[col]) * 11 + (green[col]) * 16 +
141 (blue[col]) * 5) >>
142 5;
143 }
144 }
145 else {
146 for (col = 0; col < cols; col++) {
147 /*NTSC Y equation: .30R+ .59G+ .11B */
148 result[i++] = ((red[col]) * 19 + (green[col]) * 38 +
149 (blue[col]) * 7) >>
150 6;
151 }
152 }
153 }
154 }
155
156 Rast_free_colors(&colors);
157
161 G_free(red);
162 G_free(green);
163 G_free(blue);
164 G_free(set);
165 Rast_close(map);
166
167 return (1);
168}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_malloc(n)
Definition defs/gis.h:136
const char * G_find_raster2(const char *, const char *)
Find a raster map (look but don't touch)
Definition find_rast.c:73
int Rast_is_null_value(const void *, RASTER_MAP_TYPE)
To check if a raster value is set to NULL.
Definition null_val.c:174
CELL * Rast_allocate_c_buf(void)
Allocate memory for a CELL type raster map.
Definition alloc_cell.c:78
DCELL * Rast_allocate_d_buf(void)
Allocates memory for a raster map of type DCELL.
Definition alloc_cell.c:104
int Rast_read_colors(const char *, const char *, struct Colors *)
Read color table of raster map.
void Rast_close(int)
Close a raster map.
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:76
int Rast_open_old(const char *, const char *)
Open an existing integer raster map (cell)
void Rast_free_colors(struct Colors *)
Free color structure memory.
Definition color_free.c:28
FCELL * Rast_allocate_f_buf(void)
Allocates memory for a raster map of type FCELL.
Definition alloc_cell.c:91
int Rast_window_cols(void)
Number of columns in active window.
int Rast_window_rows(void)
Number of rows in active window.
RASTER_MAP_TYPE Rast_get_map_type(int)
Determine raster type from descriptor.
void Rast_get_row(int, void *, int, RASTER_MAP_TYPE)
Get raster row.
float FCELL
Definition gis.h:633
double DCELL
Definition gis.h:632
int CELL
Definition gis.h:631
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:689