GRASS Programmer's Manual
6.4.2(2012)
|
00001 /* routines used by programs such as Dcell, display, combine, and weight 00002 * for generating raster images (for 1-byte, i.e. not super-cell, data) 00003 * 00004 * D_cell_draw_setup(t, b, l, r) 00005 * int t, b, l, r (pixle extents of display window) 00006 * (obtainable via D_get_screen_window(&t, &b, &l, &r) 00007 * Sets up the environment for D_draw_cell 00008 * 00009 * D_draw_cell(A_row, xarray, colors) 00010 * int A_row ; 00011 * CELL *xarray ; 00012 * - determinew which pixle row gets the data 00013 * - resamples the data to create a pixle array 00014 * - determines best way to draw the array 00015 * a - for single cat array, a move and a draw 00016 * b - otherwise, a call to D_raster() 00017 * - returns -1 on error or end of picture 00018 * or array row number needed for next pixle row. 00019 * 00020 * presumes the map is drawn from north to south 00021 * 00022 * ALSO: if overlay mode is desired, then call D_set_overlay_mode(1) 00023 * first. 00024 */ 00025 00026 #include <stdio.h> 00027 #include <stdlib.h> 00028 #include <grass/gis.h> 00029 #include <grass/raster.h> 00030 #include <grass/display.h> 00031 00032 extern int D__overlay_mode; 00033 00034 static int src[2][2], dst[2][2]; 00035 00036 static int draw_cell(int, const void *, struct Colors *, RASTER_MAP_TYPE); 00037 00038 int D_draw_raster(int A_row, 00039 const void *array, 00040 struct Colors *colors, RASTER_MAP_TYPE data_type) 00041 { 00042 return draw_cell(A_row, array, colors, data_type); 00043 } 00044 00045 int D_draw_d_raster(int A_row, const DCELL * darray, struct Colors *colors) 00046 { 00047 return draw_cell(A_row, darray, colors, DCELL_TYPE); 00048 } 00049 00050 int D_draw_f_raster(int A_row, const FCELL * farray, struct Colors *colors) 00051 { 00052 return draw_cell(A_row, farray, colors, FCELL_TYPE); 00053 } 00054 00055 int D_draw_c_raster(int A_row, const CELL * carray, struct Colors *colors) 00056 { 00057 return draw_cell(A_row, carray, colors, CELL_TYPE); 00058 } 00059 00060 00077 int D_draw_cell(int A_row, const CELL * carray, struct Colors *colors) 00078 { 00079 return draw_cell(A_row, carray, colors, CELL_TYPE); 00080 } 00081 00082 static int draw_cell(int A_row, 00083 const void *array, 00084 struct Colors *colors, RASTER_MAP_TYPE data_type) 00085 { 00086 static unsigned char *red, *grn, *blu, *set; 00087 static int nalloc; 00088 00089 int ncols = src[0][1] - src[0][0]; 00090 int i; 00091 00092 if (nalloc < ncols) { 00093 nalloc = ncols; 00094 red = G_realloc(red, nalloc); 00095 grn = G_realloc(grn, nalloc); 00096 blu = G_realloc(blu, nalloc); 00097 set = G_realloc(set, nalloc); 00098 } 00099 00100 G_lookup_raster_colors(array, red, grn, blu, set, ncols, colors, 00101 data_type); 00102 00103 if (D__overlay_mode) 00104 for (i = 0; i < ncols; i++) { 00105 set[i] = G_is_null_value(array, data_type); 00106 array = G_incr_void_ptr(array, G_raster_size(data_type)); 00107 } 00108 00109 A_row = 00110 R_scaled_raster(ncols, A_row, red, grn, blu, 00111 D__overlay_mode ? set : NULL); 00112 00113 return (A_row < src[1][1]) 00114 ? A_row : -1; 00115 } 00116 00132 int D_cell_draw_setup(int t, int b, int l, int r) 00133 { 00134 struct Cell_head window; 00135 00136 if (G_get_set_window(&window) == -1) 00137 G_fatal_error("Current window not available"); 00138 if (D_do_conversions(&window, t, b, l, r)) 00139 G_fatal_error("Error in calculating conversions"); 00140 00141 /* Set up the screen for drawing map */ 00142 D_get_a(src); 00143 D_get_d(dst); 00144 00145 R_begin_scaled_raster(D__overlay_mode, src, dst); 00146 00147 return 0; 00148 } 00149 00150 int D_draw_raster_RGB(int A_row, 00151 const void *r_raster, const void *g_raster, 00152 const void *b_raster, struct Colors *r_colors, 00153 struct Colors *g_colors, struct Colors *b_colors, 00154 RASTER_MAP_TYPE r_type, RASTER_MAP_TYPE g_type, 00155 RASTER_MAP_TYPE b_type) 00156 { 00157 static unsigned char *r_buf, *g_buf, *b_buf, *n_buf; 00158 static int nalloc; 00159 00160 int r_size = G_raster_size(r_type); 00161 int g_size = G_raster_size(g_type); 00162 int b_size = G_raster_size(b_type); 00163 int ncols = src[0][1] - src[0][0]; 00164 int i; 00165 00166 /* reallocate color_buf if necessary */ 00167 if (nalloc < ncols) { 00168 nalloc = ncols; 00169 r_buf = G_realloc(r_buf, nalloc); 00170 g_buf = G_realloc(g_buf, nalloc); 00171 b_buf = G_realloc(b_buf, nalloc); 00172 n_buf = G_realloc(n_buf, nalloc); 00173 } 00174 00175 /* convert cell values to bytes */ 00176 G_lookup_raster_colors(r_raster, r_buf, n_buf, n_buf, n_buf, ncols, 00177 r_colors, r_type); 00178 G_lookup_raster_colors(g_raster, n_buf, g_buf, n_buf, n_buf, ncols, 00179 g_colors, g_type); 00180 G_lookup_raster_colors(b_raster, n_buf, n_buf, b_buf, n_buf, ncols, 00181 b_colors, b_type); 00182 00183 if (D__overlay_mode) 00184 for (i = 0; i < ncols; i++) { 00185 n_buf[i] = (G_is_null_value(r_raster, r_type) || 00186 G_is_null_value(g_raster, g_type) || 00187 G_is_null_value(b_raster, b_type)); 00188 00189 r_raster = G_incr_void_ptr(r_raster, r_size); 00190 g_raster = G_incr_void_ptr(g_raster, g_size); 00191 b_raster = G_incr_void_ptr(b_raster, b_size); 00192 } 00193 00194 A_row = 00195 R_scaled_raster(ncols, A_row, r_buf, g_buf, b_buf, 00196 D__overlay_mode ? n_buf : NULL); 00197 00198 return (A_row < src[1][1]) 00199 ? A_row : -1; 00200 } 00201 00202 void D_cell_draw_end(void) 00203 { 00204 R_end_scaled_raster(); 00205 }