GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
display/raster.c
Go to the documentation of this file.
1 /* routines used by programs such as Dcell, display, combine, and weight
2  * for generating raster images (for 1-byte, i.e. not super-cell, data)
3  *
4  * D_cell_draw_setup(t, b, l, r)
5  * int t, b, l, r (pixle extents of display window)
6  * (obtainable via D_get_screen_window(&t, &b, &l, &r)
7  * Sets up the environment for D_draw_cell
8  *
9  * D_draw_cell(A_row, xarray, colors)
10  * int A_row ;
11  * CELL *xarray ;
12  * - determinew which pixle row gets the data
13  * - resamples the data to create a pixle array
14  * - determines best way to draw the array
15  * a - for single cat array, a move and a draw
16  * b - otherwise, a call to D_raster()
17  * - returns -1 on error or end of picture
18  * or array row number needed for next pixle row.
19  *
20  * presumes the map is drawn from north to south
21  *
22  * ALSO: if overlay mode is desired, then call D_set_overlay_mode(1)
23  * first.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <grass/gis.h>
29 #include <grass/raster.h>
30 #include <grass/display.h>
31 
32 extern int D__overlay_mode;
33 
34 static int src[2][2], dst[2][2];
35 
36 static int draw_cell(int, const void *, struct Colors *, RASTER_MAP_TYPE);
37 
38 int D_draw_raster(int A_row,
39  const void *array,
40  struct Colors *colors, RASTER_MAP_TYPE data_type)
41 {
42  return draw_cell(A_row, array, colors, data_type);
43 }
44 
45 int D_draw_d_raster(int A_row, const DCELL * darray, struct Colors *colors)
46 {
47  return draw_cell(A_row, darray, colors, DCELL_TYPE);
48 }
49 
50 int D_draw_f_raster(int A_row, const FCELL * farray, struct Colors *colors)
51 {
52  return draw_cell(A_row, farray, colors, FCELL_TYPE);
53 }
54 
55 int D_draw_c_raster(int A_row, const CELL * carray, struct Colors *colors)
56 {
57  return draw_cell(A_row, carray, colors, CELL_TYPE);
58 }
59 
60 
77 int D_draw_cell(int A_row, const CELL * carray, struct Colors *colors)
78 {
79  return draw_cell(A_row, carray, colors, CELL_TYPE);
80 }
81 
82 static int draw_cell(int A_row,
83  const void *array,
84  struct Colors *colors, RASTER_MAP_TYPE data_type)
85 {
86  static unsigned char *red, *grn, *blu, *set;
87  static int nalloc;
88 
89  int ncols = src[0][1] - src[0][0];
90  int i;
91 
92  if (nalloc < ncols) {
93  nalloc = ncols;
94  red = G_realloc(red, nalloc);
95  grn = G_realloc(grn, nalloc);
96  blu = G_realloc(blu, nalloc);
97  set = G_realloc(set, nalloc);
98  }
99 
100  G_lookup_raster_colors(array, red, grn, blu, set, ncols, colors,
101  data_type);
102 
103  if (D__overlay_mode)
104  for (i = 0; i < ncols; i++) {
105  set[i] = G_is_null_value(array, data_type);
106  array = G_incr_void_ptr(array, G_raster_size(data_type));
107  }
108 
109  A_row =
110  R_scaled_raster(ncols, A_row, red, grn, blu,
111  D__overlay_mode ? set : NULL);
112 
113  return (A_row < src[1][1])
114  ? A_row : -1;
115 }
116 
132 int D_cell_draw_setup(int t, int b, int l, int r)
133 {
134  struct Cell_head window;
135 
136  if (G_get_set_window(&window) == -1)
137  G_fatal_error("Current window not available");
138  if (D_do_conversions(&window, t, b, l, r))
139  G_fatal_error("Error in calculating conversions");
140 
141  /* Set up the screen for drawing map */
142  D_get_a(src);
143  D_get_d(dst);
144 
146 
147  return 0;
148 }
149 
150 int D_draw_raster_RGB(int A_row,
151  const void *r_raster, const void *g_raster,
152  const void *b_raster, struct Colors *r_colors,
153  struct Colors *g_colors, struct Colors *b_colors,
154  RASTER_MAP_TYPE r_type, RASTER_MAP_TYPE g_type,
155  RASTER_MAP_TYPE b_type)
156 {
157  static unsigned char *r_buf, *g_buf, *b_buf, *n_buf;
158  static int nalloc;
159 
160  int r_size = G_raster_size(r_type);
161  int g_size = G_raster_size(g_type);
162  int b_size = G_raster_size(b_type);
163  int ncols = src[0][1] - src[0][0];
164  int i;
165 
166  /* reallocate color_buf if necessary */
167  if (nalloc < ncols) {
168  nalloc = ncols;
169  r_buf = G_realloc(r_buf, nalloc);
170  g_buf = G_realloc(g_buf, nalloc);
171  b_buf = G_realloc(b_buf, nalloc);
172  n_buf = G_realloc(n_buf, nalloc);
173  }
174 
175  /* convert cell values to bytes */
176  G_lookup_raster_colors(r_raster, r_buf, n_buf, n_buf, n_buf, ncols,
177  r_colors, r_type);
178  G_lookup_raster_colors(g_raster, n_buf, g_buf, n_buf, n_buf, ncols,
179  g_colors, g_type);
180  G_lookup_raster_colors(b_raster, n_buf, n_buf, b_buf, n_buf, ncols,
181  b_colors, b_type);
182 
183  if (D__overlay_mode)
184  for (i = 0; i < ncols; i++) {
185  n_buf[i] = (G_is_null_value(r_raster, r_type) ||
186  G_is_null_value(g_raster, g_type) ||
187  G_is_null_value(b_raster, b_type));
188 
189  r_raster = G_incr_void_ptr(r_raster, r_size);
190  g_raster = G_incr_void_ptr(g_raster, g_size);
191  b_raster = G_incr_void_ptr(b_raster, b_size);
192  }
193 
194  A_row =
195  R_scaled_raster(ncols, A_row, r_buf, g_buf, b_buf,
196  D__overlay_mode ? n_buf : NULL);
197 
198  return (A_row < src[1][1])
199  ? A_row : -1;
200 }
201 
202 void D_cell_draw_end(void)
203 {
205 }
int G_lookup_raster_colors(const void *raster, unsigned char *red, unsigned char *grn, unsigned char *blu, unsigned char *set, int n, struct Colors *colors, RASTER_MAP_TYPE map_type)
If the cell_type is CELL_TYPE, calls G_lookup_colors((CELL *)cell, r, g, b, set, n, colors); If the cell_type is FCELL_TYPE, calls G_lookup_f_raster_colors(FCELL *)cell, r, g, b, set, n, colors); If the cell_type is DCELL_TYPE, calls G_lookup_d_raster_colors(DCELL *)cell, r, g, b, set, n, colors);.
Definition: color_look.c:118
int l
Definition: dataquad.c:292
float b
Definition: named_colr.c:8
int G_get_set_window(struct Cell_head *window)
Get the current working window.
Definition: set_window.c:30
int D_cell_draw_setup(int t, int b, int l, int r)
prepare for raster graphic
float r
Definition: named_colr.c:8
int D_draw_raster(int A_row, const void *array, struct Colors *colors, RASTER_MAP_TYPE data_type)
int D_draw_cell(int A_row, const CELL *carray, struct Colors *colors)
render a raster row
void * G_incr_void_ptr(const void *ptr, const size_t size)
Advance void pointer.
Definition: gis/raster.c:33
void R_end_scaled_raster(void)
Definition: com_proto.c:527
int D_do_conversions(const struct Cell_head *window, int t, int b, int l, int r)
initialize conversions
Definition: cnversions.c:69
int R_scaled_raster(int n, int row, const unsigned char *red, const unsigned char *grn, const unsigned char *blu, const unsigned char *nul)
Definition: com_proto.c:520
int D_draw_d_raster(int A_row, const DCELL *darray, struct Colors *colors)
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)
size_t G_raster_size(RASTER_MAP_TYPE data_type)
Returns size of a raster CELL in bytes.
Definition: alloc_cell.c:38
void D_get_d(int x[2][2])
Definition: cnversions.c:252
void D_cell_draw_end(void)
return NULL
Definition: dbfopen.c:1394
void R_begin_scaled_raster(int mask, int src[2][2], int dst[2][2])
Definition: com_proto.c:515
int D__overlay_mode
Definition: raster2.c:35
int D_draw_f_raster(int A_row, const FCELL *farray, struct Colors *colors)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
void D_get_a(int x[2][2])
Definition: cnversions.c:244
string set
int G_is_null_value(const void *rast, RASTER_MAP_TYPE data_type)
If the data_type is CELL_TYPE, calls G_is_c_null_value ((CELL *) rast); If the data_type is FCELL_TYP...
Definition: null_val.c:207
int D_draw_c_raster(int A_row, const CELL *carray, struct Colors *colors)