raster.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_dst(&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. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <grass/gis.h>
  28. #include <grass/raster.h>
  29. #include <grass/display.h>
  30. #include "driver.h"
  31. extern int D__overlay_mode;
  32. static int src[2][2];
  33. static double dst[2][2];
  34. static int draw_cell(int, const void *, struct Colors *, RASTER_MAP_TYPE);
  35. int D_draw_raster(int A_row,
  36. const void *array,
  37. struct Colors *colors, RASTER_MAP_TYPE data_type)
  38. {
  39. return draw_cell(A_row, array, colors, data_type);
  40. }
  41. int D_draw_d_raster(int A_row, const DCELL * darray, struct Colors *colors)
  42. {
  43. return draw_cell(A_row, darray, colors, DCELL_TYPE);
  44. }
  45. int D_draw_f_raster(int A_row, const FCELL * farray, struct Colors *colors)
  46. {
  47. return draw_cell(A_row, farray, colors, FCELL_TYPE);
  48. }
  49. int D_draw_c_raster(int A_row, const CELL * carray, struct Colors *colors)
  50. {
  51. return draw_cell(A_row, carray, colors, CELL_TYPE);
  52. }
  53. /*!
  54. * \brief render a raster row
  55. *
  56. * The <b>row</b> gives the map array row. The <b>raster</b>
  57. * array provides the categories for each raster value in that row.
  58. * This routine is called consecutively with the information necessary to draw a
  59. * raster image from north to south. No rows can be skipped. All screen pixel
  60. * rows which represent the current map array row are rendered. The routine
  61. * returns the map array row which is needed to draw the next screen pixel row.
  62. *
  63. * \param row
  64. * \param raster
  65. * \param colors
  66. * \return int
  67. */
  68. int D_draw_cell(int A_row, const CELL * carray, struct Colors *colors)
  69. {
  70. return draw_cell(A_row, carray, colors, CELL_TYPE);
  71. }
  72. static int draw_cell(int A_row,
  73. const void *array,
  74. struct Colors *colors, RASTER_MAP_TYPE data_type)
  75. {
  76. static unsigned char *red, *grn, *blu, *set;
  77. static int nalloc;
  78. int ncols = src[0][1] - src[0][0];
  79. int i;
  80. if (nalloc < ncols) {
  81. nalloc = ncols;
  82. red = G_realloc(red, nalloc);
  83. grn = G_realloc(grn, nalloc);
  84. blu = G_realloc(blu, nalloc);
  85. set = G_realloc(set, nalloc);
  86. }
  87. Rast_lookup_colors(array, red, grn, blu, set, ncols, colors,
  88. data_type);
  89. if (D__overlay_mode)
  90. for (i = 0; i < ncols; i++) {
  91. set[i] = Rast_is_null_value(array, data_type);
  92. array = G_incr_void_ptr(array, Rast_cell_size(data_type));
  93. }
  94. A_row =
  95. COM_raster(ncols, A_row, red, grn, blu, D__overlay_mode ? set : NULL);
  96. return (A_row < src[1][1])
  97. ? A_row : -1;
  98. }
  99. /*!
  100. * \brief prepare for raster graphic
  101. *
  102. * The raster display subsystem establishes
  103. * conversion parameters based on the screen extent defined by <b>top,
  104. * bottom, left</b>, and <b>right</b>, all of which are obtainable from
  105. * <i>D_get_dst for the current frame.</i>
  106. *
  107. * \param top
  108. * \param bottom
  109. * \param left
  110. * \param right
  111. * \return int
  112. */
  113. void D_cell_draw_begin(void)
  114. {
  115. /* Set up the screen for drawing map */
  116. D_get_a(src);
  117. D_get_d(dst);
  118. COM_begin_raster(D__overlay_mode, src, dst);
  119. }
  120. int D_draw_raster_RGB(int A_row,
  121. const void *r_raster, const void *g_raster,
  122. const void *b_raster, struct Colors *r_colors,
  123. struct Colors *g_colors, struct Colors *b_colors,
  124. RASTER_MAP_TYPE r_type, RASTER_MAP_TYPE g_type,
  125. RASTER_MAP_TYPE b_type)
  126. {
  127. static unsigned char *r_buf, *g_buf, *b_buf, *n_buf;
  128. static int nalloc;
  129. int r_size = Rast_cell_size(r_type);
  130. int g_size = Rast_cell_size(g_type);
  131. int b_size = Rast_cell_size(b_type);
  132. int ncols = src[0][1] - src[0][0];
  133. int i;
  134. /* reallocate color_buf if necessary */
  135. if (nalloc < ncols) {
  136. nalloc = ncols;
  137. r_buf = G_realloc(r_buf, nalloc);
  138. g_buf = G_realloc(g_buf, nalloc);
  139. b_buf = G_realloc(b_buf, nalloc);
  140. n_buf = G_realloc(n_buf, nalloc);
  141. }
  142. /* convert cell values to bytes */
  143. Rast_lookup_colors(r_raster, r_buf, n_buf, n_buf, n_buf, ncols,
  144. r_colors, r_type);
  145. Rast_lookup_colors(g_raster, n_buf, g_buf, n_buf, n_buf, ncols,
  146. g_colors, g_type);
  147. Rast_lookup_colors(b_raster, n_buf, n_buf, b_buf, n_buf, ncols,
  148. b_colors, b_type);
  149. if (D__overlay_mode)
  150. for (i = 0; i < ncols; i++) {
  151. n_buf[i] = (Rast_is_null_value(r_raster, r_type) ||
  152. Rast_is_null_value(g_raster, g_type) ||
  153. Rast_is_null_value(b_raster, b_type));
  154. r_raster = G_incr_void_ptr(r_raster, r_size);
  155. g_raster = G_incr_void_ptr(g_raster, g_size);
  156. b_raster = G_incr_void_ptr(b_raster, b_size);
  157. }
  158. A_row = COM_raster(ncols, A_row, r_buf, g_buf, b_buf,
  159. D__overlay_mode ? n_buf : NULL);
  160. return (A_row < src[1][1])
  161. ? A_row : -1;
  162. }
  163. void D_cell_draw_end(void)
  164. {
  165. COM_end_raster();
  166. }