raster.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*!
  2. \file lib/display/raster.c
  3. \brief Display Driver - draw raster data
  4. (C) 2006-2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Glynn Clements <glynn gclements.plus.com> (original contributor)
  8. \author Huidae Cho <grass4u gmail.com>
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <grass/gis.h>
  13. #include <grass/raster.h>
  14. #include <grass/display.h>
  15. #include "driver.h"
  16. extern int D__overlay_mode;
  17. static int src[2][2];
  18. static double dst[2][2];
  19. static int draw_cell(int, const void *, struct Colors *, RASTER_MAP_TYPE);
  20. /* routines used by programs such as Dcell, display, combine, and weight
  21. * for generating raster images (for 1-byte, i.e. not super-cell, data)
  22. *
  23. * D_cell_draw_setup(t, b, l, r)
  24. * int t, b, l, r (pixle extents of display window)
  25. * (obtainable via D_get_dst(&t, &b, &l, &r)
  26. * Sets up the environment for D_draw_cell
  27. *
  28. * D_draw_cell(A_row, xarray, colors)
  29. * int A_row ;
  30. * CELL *xarray ;
  31. * - determinew which pixle row gets the data
  32. * - resamples the data to create a pixle 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. * - returns -1 on error or end of picture
  37. * or array row number needed for next pixle row.
  38. *
  39. * presumes the map is drawn from north to south
  40. *
  41. * ALSO: if overlay mode is desired, then call D_set_overlay_mode(1)
  42. * first.
  43. */
  44. /*!
  45. \brief Draw raster row
  46. \param A_row row number
  47. \param array
  48. \param colors pointer to Colors structure
  49. \param data_type raster type (CELL, FCELL, DCELL)
  50. \return
  51. */
  52. int D_draw_raster(int A_row,
  53. const void *array,
  54. struct Colors *colors, RASTER_MAP_TYPE data_type)
  55. {
  56. return draw_cell(A_row, array, colors, data_type);
  57. }
  58. /*!
  59. \brief Draw raster row (DCELL)
  60. \param A_row row number
  61. \param darray
  62. \param colors pointer to Colors structure
  63. \return
  64. */
  65. int D_draw_d_raster(int A_row, const DCELL * darray, struct Colors *colors)
  66. {
  67. return draw_cell(A_row, darray, colors, DCELL_TYPE);
  68. }
  69. /*!
  70. \brief Draw raster row (FCELL)
  71. \param A_row row number
  72. \param farray
  73. \param colors pointer to Colors structure
  74. \return
  75. */
  76. int D_draw_f_raster(int A_row, const FCELL * farray, struct Colors *colors)
  77. {
  78. return draw_cell(A_row, farray, colors, FCELL_TYPE);
  79. }
  80. /*!
  81. \brief Draw raster row (CELL)
  82. \param A_row row number
  83. \param carray
  84. \param colors pointer to Colors structure
  85. \return
  86. */
  87. int D_draw_c_raster(int A_row, const CELL * carray, struct Colors *colors)
  88. {
  89. return draw_cell(A_row, carray, colors, CELL_TYPE);
  90. }
  91. /*!
  92. \brief Render a raster row
  93. \todo Replace by D_draw_c_raster()
  94. The <b>row</b> gives the map array row. The <b>raster</b> array
  95. provides the categories for each raster value in that row. This
  96. routine is called consecutively with the information necessary to
  97. draw a raster image from north to south. No rows can be skipped. All
  98. screen pixel rows which represent the current map array row are
  99. rendered. The routine returns the map array row which is needed to
  100. draw the next screen pixel row.
  101. \param A_row row number
  102. \param carray
  103. \param colors pointer to Colors structure
  104. \return
  105. */
  106. int D_draw_cell(int A_row, const CELL * carray, struct Colors *colors)
  107. {
  108. return draw_cell(A_row, carray, colors, CELL_TYPE);
  109. }
  110. static int draw_cell(int A_row,
  111. const void *array,
  112. struct Colors *colors, RASTER_MAP_TYPE data_type)
  113. {
  114. static unsigned char *red, *grn, *blu, *set;
  115. static int nalloc;
  116. int ncols = src[0][1] - src[0][0];
  117. int i;
  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. Rast_lookup_colors(array, red, grn, blu, set, ncols, colors,
  126. data_type);
  127. if (D__overlay_mode)
  128. for (i = 0; i < ncols; i++) {
  129. set[i] = Rast_is_null_value(array, data_type);
  130. array = G_incr_void_ptr(array, Rast_cell_size(data_type));
  131. }
  132. A_row =
  133. COM_raster(ncols, A_row, red, grn, blu, D__overlay_mode ? set : NULL);
  134. return (A_row < src[1][1])
  135. ? A_row : -1;
  136. }
  137. /*!
  138. \brief Prepare for raster graphic
  139. The raster display subsystem establishes conversion parameters based
  140. on the screen extent defined by <b>top, bottom, left</b>, and
  141. <b>right</b>, all of which are obtainable from <i>D_get_dst for the
  142. current frame.</i>
  143. */
  144. void D_cell_draw_begin(void)
  145. {
  146. /* Set up the screen for drawing map */
  147. D_get_a(src);
  148. D_get_d(dst);
  149. COM_begin_raster(D__overlay_mode, src, dst);
  150. }
  151. /*!
  152. \brief Draw raster row in RGB mode
  153. \param A_row row number
  154. \param r_raster red data buffer
  155. \param g_raster green data buffer
  156. \param b_raster blue data buffer
  157. \param r_colors colors used for red channel
  158. \param g_colors colors used for green channel
  159. \param b_colors colors used for blue channel
  160. \param r_type raster type used for red channel
  161. \param g_type raster type used for red channel
  162. \param b_type raster type used for red channel
  163. \return
  164. */
  165. int D_draw_raster_RGB(int A_row,
  166. const void *r_raster, const void *g_raster,
  167. const void *b_raster, struct Colors *r_colors,
  168. struct Colors *g_colors, struct Colors *b_colors,
  169. RASTER_MAP_TYPE r_type, RASTER_MAP_TYPE g_type,
  170. RASTER_MAP_TYPE b_type)
  171. {
  172. static unsigned char *r_buf, *g_buf, *b_buf, *n_buf;
  173. static int nalloc;
  174. int r_size = Rast_cell_size(r_type);
  175. int g_size = Rast_cell_size(g_type);
  176. int b_size = Rast_cell_size(b_type);
  177. int ncols = src[0][1] - src[0][0];
  178. int i;
  179. /* reallocate color_buf if necessary */
  180. if (nalloc < ncols) {
  181. nalloc = ncols;
  182. r_buf = G_realloc(r_buf, nalloc);
  183. g_buf = G_realloc(g_buf, nalloc);
  184. b_buf = G_realloc(b_buf, nalloc);
  185. n_buf = G_realloc(n_buf, nalloc);
  186. }
  187. /* convert cell values to bytes */
  188. Rast_lookup_colors(r_raster, r_buf, n_buf, n_buf, n_buf, ncols,
  189. r_colors, r_type);
  190. Rast_lookup_colors(g_raster, n_buf, g_buf, n_buf, n_buf, ncols,
  191. g_colors, g_type);
  192. Rast_lookup_colors(b_raster, n_buf, n_buf, b_buf, n_buf, ncols,
  193. b_colors, b_type);
  194. if (D__overlay_mode)
  195. for (i = 0; i < ncols; i++) {
  196. n_buf[i] = (Rast_is_null_value(r_raster, r_type) ||
  197. Rast_is_null_value(g_raster, g_type) ||
  198. Rast_is_null_value(b_raster, b_type));
  199. r_raster = G_incr_void_ptr(r_raster, r_size);
  200. g_raster = G_incr_void_ptr(g_raster, g_size);
  201. b_raster = G_incr_void_ptr(b_raster, b_size);
  202. }
  203. A_row = COM_raster(ncols, A_row, r_buf, g_buf, b_buf,
  204. D__overlay_mode ? n_buf : NULL);
  205. return (A_row < src[1][1])
  206. ? A_row : -1;
  207. }
  208. /*!
  209. \brief Finish rendering
  210. */
  211. void D_cell_draw_end(void)
  212. {
  213. COM_end_raster();
  214. }