GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
display/raster.c
Go to the documentation of this file.
1/*!
2 \file lib/display/raster.c
3
4 \brief Display Driver - draw raster data
5
6 SPDX-FileCopyrightText: 2006-2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Glynn Clements <glynn gclements.plus.com> (original contributor)
10 \author Huidae Cho <grass4u gmail.com>
11*/
12
13#include <stdio.h>
14#include <stdlib.h>
15
16#include <grass/gis.h>
17#include <grass/raster.h>
18#include <grass/display.h>
19#include "driver.h"
20
21extern int D__overlay_mode;
22
23static int src[2][2];
24static double dst[2][2];
25
26static int draw_cell(int, const void *, struct Colors *, RASTER_MAP_TYPE);
27
28/*!
29 \brief Draw raster row
30
31 - determine which pixel row gets the data
32 - resamples the data to create a pixel array
33 - determines best way to draw the array
34 a - for single cat array, a move and a draw
35 b - otherwise, a call to D_raster()
36
37 Presumes the map is drawn from north to south.
38
39 If overlay mode is desired, then call D_set_overlay_mode() first.
40
41 \param A_row row number (starts at 0)
42 \param array data buffer
43 \param colors pointer to Colors structure
44 \param data_type raster type (CELL, FCELL, DCELL)
45
46 \return row number needed for next pixel row
47 \return -1 nothing to draw (on error or end of raster)
48*/
49int D_draw_raster(int A_row, const void *array, struct Colors *colors,
50 RASTER_MAP_TYPE data_type)
51{
52 return draw_cell(A_row, array, colors, data_type);
53}
54
55/*!
56 \brief Draw raster row (DCELL)
57
58 \param A_row row number (starts at 0)
59 \param darray data buffer
60 \param colors pointer to Colors structure
61
62 \return
63*/
64int D_draw_d_raster(int A_row, const DCELL *darray, struct Colors *colors)
65{
66 return draw_cell(A_row, darray, colors, DCELL_TYPE);
67}
68
69/*!
70 \brief Draw raster row (FCELL)
71
72 \param A_row row number (starts at 0)
73 \param farray data buffer
74 \param colors pointer to Colors structure
75
76 \return row number needed for next pixel row
77 \return -1 nothing to draw (on error or end of raster)
78*/
79int D_draw_f_raster(int A_row, const FCELL *farray, struct Colors *colors)
80{
81 return draw_cell(A_row, farray, colors, FCELL_TYPE);
82}
83
84/*!
85 \brief Draw raster row (CELL)
86
87 The <b>row</b> gives the map array row. The <b>carray</b> array
88 provides the categories for each raster value in that row. This
89 routine is called consecutively with the information necessary to
90 draw a raster image from north to south. No rows can be skipped. All
91 screen pixel rows which represent the current map array row are
92 rendered. The routine returns the map array row which is needed to
93 draw the next screen pixel row.
94
95 \param A_row row number (starts at 0)
96 \param carray data buffer
97 \param colors pointer to Colors structure
98
99 \return row number needed for next pixel row
100 \return -1 nothing to draw (on error or end of raster)
101*/
102int D_draw_c_raster(int A_row, const CELL *carray, struct Colors *colors)
103{
104 return draw_cell(A_row, carray, colors, CELL_TYPE);
105}
106
107static int draw_cell(int A_row, const void *array, struct Colors *colors,
108 RASTER_MAP_TYPE data_type)
109{
110 static unsigned char *red, *grn, *blu, *set;
111 static int nalloc;
112
113 int ncols = src[0][1] - src[0][0];
114 int i;
115
116 if (nalloc < ncols) {
117 nalloc = ncols;
118 red = G_realloc(red, nalloc);
119 grn = G_realloc(grn, nalloc);
120 blu = G_realloc(blu, nalloc);
121 set = G_realloc(set, nalloc);
122 }
123
124 Rast_lookup_colors(array, red, grn, blu, set, ncols, colors, data_type);
125
126 if (D__overlay_mode)
127 for (i = 0; i < ncols; i++) {
128 set[i] = Rast_is_null_value(array, data_type);
129 array = G_incr_void_ptr(array, Rast_cell_size(data_type));
130 }
131
132 A_row =
133 COM_raster(ncols, A_row, red, grn, blu, D__overlay_mode ? set : NULL);
134
135 return (A_row < src[1][1]) ? A_row : -1;
136}
137
138/*!
139 \brief Prepare for raster graphic
140
141 The raster display subsystem establishes conversion parameters based
142 on the screen extent defined by <b>top, bottom, left</b>, and
143 <b>right</b>, all of which are obtainable from D_get_dst() for the
144 current frame.
145*/
147{
148 /* Set up the screen for drawing map */
149 D_get_a(src);
150 D_get_d(dst);
152}
153
154/*!
155 \brief Draw raster row in RGB mode
156
157 \param A_row row number (starts at 0)
158 \param r_raster red data buffer
159 \param g_raster green data buffer
160 \param b_raster blue data buffer
161 \param r_colors colors used for red channel
162 \param g_colors colors used for green channel
163 \param b_colors colors used for blue channel
164 \param r_type raster type used for red channel
165 \param g_type raster type used for red channel
166 \param b_type raster type used for red channel
167
168 \return row number needed for next pixel row
169 \return -1 nothing to draw (on error or end of raster)
170*/
171int D_draw_raster_RGB(int A_row, const void *r_raster, const void *g_raster,
172 const void *b_raster, struct Colors *r_colors,
173 struct Colors *g_colors, struct Colors *b_colors,
176{
177 static unsigned char *r_buf, *g_buf, *b_buf, *n_buf;
178 static int nalloc;
179
183 int ncols = src[0][1] - src[0][0];
184 int i;
185
186 /* reallocate color_buf if necessary */
187 if (nalloc < ncols) {
188 nalloc = ncols;
189 r_buf = G_realloc(r_buf, nalloc);
190 g_buf = G_realloc(g_buf, nalloc);
191 b_buf = G_realloc(b_buf, nalloc);
192 n_buf = G_realloc(n_buf, nalloc);
193 }
194
195 /* convert cell values to bytes */
197 r_type);
199 g_type);
201 b_type);
202
203 if (D__overlay_mode)
204 for (i = 0; i < ncols; i++) {
208
212 }
213
216
217 return (A_row < src[1][1]) ? A_row : -1;
218}
219
220/*!
221 \brief Finish raster rendering
222*/
224{
226}
#define NULL
Definition ccmath.h:32
void D_get_a(int[2][2])
Definition cnversions.c:361
void D_get_d(double[2][2])
Definition cnversions.c:369
#define G_realloc(p, n)
Definition defs/gis.h:138
#define G_incr_void_ptr(ptr, size)
Definition defs/gis.h:78
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
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
size_t Rast_cell_size(RASTER_MAP_TYPE)
Returns size of a raster cell in bytes.
Definition alloc_cell.c:35
void D_raster_draw_end(void)
Finish raster rendering.
void D_raster_draw_begin(void)
Prepare for raster graphic.
int D_draw_f_raster(int A_row, const FCELL *farray, struct Colors *colors)
Draw raster row (FCELL)
int D_draw_raster(int A_row, const void *array, struct Colors *colors, RASTER_MAP_TYPE data_type)
Draw raster row.
int D_draw_raster_RGB(int A_row, const void *r_raster, const void *g_raster, const void *b_raster, struct Colors *r_colors, struct Colors *g_colors, struct Colors *b_colors, RASTER_MAP_TYPE r_type, RASTER_MAP_TYPE g_type, RASTER_MAP_TYPE b_type)
Draw raster row in RGB mode.
int D_draw_d_raster(int A_row, const DCELL *darray, struct Colors *colors)
Draw raster row (DCELL)
int D_draw_c_raster(int A_row, const CELL *carray, struct Colors *colors)
Draw raster row (CELL)
int D__overlay_mode
Definition raster2.c:35
int COM_raster(int, int, const unsigned char *, const unsigned char *, const unsigned char *, const unsigned char *)
void COM_end_raster(void)
void COM_begin_raster(int, int[2][2], double[2][2])
float FCELL
Definition gis.h:633
double DCELL
Definition gis.h:632
int CELL
Definition gis.h:631
#define FCELL_TYPE
Definition raster.h:12
#define DCELL_TYPE
Definition raster.h:13
#define CELL_TYPE
Definition raster.h:11
int RASTER_MAP_TYPE
Definition raster.h:25
Definition gis.h:689