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