raster.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /*!
  21. \brief Draw raster row
  22. - determine which pixel row gets the data
  23. - resamples the data to create a pixel array
  24. - determines best way to draw the array
  25. a - for single cat array, a move and a draw
  26. b - otherwise, a call to D_raster()
  27. Presumes the map is drawn from north to south.
  28. If overlay mode is desired, then call D_set_overlay_mode() first.
  29. \param A_row row number (starts at 0)
  30. \param array data buffer
  31. \param colors pointer to Colors structure
  32. \param data_type raster type (CELL, FCELL, DCELL)
  33. \return row number needed for next pixel row
  34. \return -1 nothing to draw (on error or end of raster)
  35. */
  36. int D_draw_raster(int A_row,
  37. const void *array,
  38. struct Colors *colors, RASTER_MAP_TYPE data_type)
  39. {
  40. return draw_cell(A_row, array, colors, data_type);
  41. }
  42. /*!
  43. \brief Draw raster row (DCELL)
  44. \param A_row row number (starts at 0)
  45. \param darray data buffer
  46. \param colors pointer to Colors structure
  47. \return
  48. */
  49. int D_draw_d_raster(int A_row, const DCELL * darray, struct Colors *colors)
  50. {
  51. return draw_cell(A_row, darray, colors, DCELL_TYPE);
  52. }
  53. /*!
  54. \brief Draw raster row (FCELL)
  55. \param A_row row number (starts at 0)
  56. \param farray data buffer
  57. \param colors pointer to Colors structure
  58. \return row number needed for next pixel row
  59. \return -1 nothing to draw (on error or end of raster)
  60. */
  61. int D_draw_f_raster(int A_row, const FCELL * farray, struct Colors *colors)
  62. {
  63. return draw_cell(A_row, farray, colors, FCELL_TYPE);
  64. }
  65. /*!
  66. \brief Draw raster row (CELL)
  67. The <b>row</b> gives the map array row. The <b>carray</b> array
  68. provides the categories for each raster value in that row. This
  69. routine is called consecutively with the information necessary to
  70. draw a raster image from north to south. No rows can be skipped. All
  71. screen pixel rows which represent the current map array row are
  72. rendered. The routine returns the map array row which is needed to
  73. draw the next screen pixel row.
  74. \param A_row row number (starts at 0)
  75. \param carray data buffer
  76. \param colors pointer to Colors structure
  77. \return row number needed for next pixel row
  78. \return -1 nothing to draw (on error or end of raster)
  79. */
  80. int D_draw_c_raster(int A_row, const CELL * carray, struct Colors *colors)
  81. {
  82. return draw_cell(A_row, carray, colors, CELL_TYPE);
  83. }
  84. static int draw_cell(int A_row,
  85. const void *array,
  86. struct Colors *colors, RASTER_MAP_TYPE data_type)
  87. {
  88. static unsigned char *red, *grn, *blu, *set;
  89. static int nalloc;
  90. int ncols = src[0][1] - src[0][0];
  91. int i;
  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. Rast_lookup_colors(array, red, grn, blu, set, ncols, colors,
  100. data_type);
  101. if (D__overlay_mode)
  102. for (i = 0; i < ncols; i++) {
  103. set[i] = Rast_is_null_value(array, data_type);
  104. array = G_incr_void_ptr(array, Rast_cell_size(data_type));
  105. }
  106. A_row =
  107. COM_raster(ncols, A_row, red, grn, blu, D__overlay_mode ? set : NULL);
  108. return (A_row < src[1][1])
  109. ? A_row : -1;
  110. }
  111. /*!
  112. \brief Prepare for raster graphic
  113. The raster display subsystem establishes conversion parameters based
  114. on the screen extent defined by <b>top, bottom, left</b>, and
  115. <b>right</b>, all of which are obtainable from D_get_dst() for the
  116. current frame.
  117. */
  118. void D_raster_draw_begin(void)
  119. {
  120. /* Set up the screen for drawing map */
  121. D_get_a(src);
  122. D_get_d(dst);
  123. COM_begin_raster(D__overlay_mode, src, dst);
  124. }
  125. /*!
  126. \brief Draw raster row in RGB mode
  127. \param A_row row number (starts at 0)
  128. \param r_raster red data buffer
  129. \param g_raster green data buffer
  130. \param b_raster blue data buffer
  131. \param r_colors colors used for red channel
  132. \param g_colors colors used for green channel
  133. \param b_colors colors used for blue channel
  134. \param r_type raster type used for red channel
  135. \param g_type raster type used for red channel
  136. \param b_type raster type used for red channel
  137. \return row number needed for next pixel row
  138. \return -1 nothing to draw (on error or end of raster)
  139. */
  140. int D_draw_raster_RGB(int A_row,
  141. const void *r_raster, const void *g_raster,
  142. const void *b_raster, struct Colors *r_colors,
  143. struct Colors *g_colors, struct Colors *b_colors,
  144. RASTER_MAP_TYPE r_type, RASTER_MAP_TYPE g_type,
  145. RASTER_MAP_TYPE b_type)
  146. {
  147. static unsigned char *r_buf, *g_buf, *b_buf, *n_buf;
  148. static int nalloc;
  149. int r_size = Rast_cell_size(r_type);
  150. int g_size = Rast_cell_size(g_type);
  151. int b_size = Rast_cell_size(b_type);
  152. int ncols = src[0][1] - src[0][0];
  153. int i;
  154. /* reallocate color_buf if necessary */
  155. if (nalloc < ncols) {
  156. nalloc = ncols;
  157. r_buf = G_realloc(r_buf, nalloc);
  158. g_buf = G_realloc(g_buf, nalloc);
  159. b_buf = G_realloc(b_buf, nalloc);
  160. n_buf = G_realloc(n_buf, nalloc);
  161. }
  162. /* convert cell values to bytes */
  163. Rast_lookup_colors(r_raster, r_buf, n_buf, n_buf, n_buf, ncols,
  164. r_colors, r_type);
  165. Rast_lookup_colors(g_raster, n_buf, g_buf, n_buf, n_buf, ncols,
  166. g_colors, g_type);
  167. Rast_lookup_colors(b_raster, n_buf, n_buf, b_buf, n_buf, ncols,
  168. b_colors, b_type);
  169. if (D__overlay_mode)
  170. for (i = 0; i < ncols; i++) {
  171. n_buf[i] = (Rast_is_null_value(r_raster, r_type) ||
  172. Rast_is_null_value(g_raster, g_type) ||
  173. Rast_is_null_value(b_raster, b_type));
  174. r_raster = G_incr_void_ptr(r_raster, r_size);
  175. g_raster = G_incr_void_ptr(g_raster, g_size);
  176. b_raster = G_incr_void_ptr(b_raster, b_size);
  177. }
  178. A_row = COM_raster(ncols, A_row, r_buf, g_buf, b_buf,
  179. D__overlay_mode ? n_buf : NULL);
  180. return (A_row < src[1][1])
  181. ? A_row : -1;
  182. }
  183. /*!
  184. \brief Finish raster rendering
  185. */
  186. void D_raster_draw_end(void)
  187. {
  188. COM_end_raster();
  189. }