raster.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*!
  2. \file lib/cairodriver/raster.c
  3. \brief GRASS cairo display driver - draw raster
  4. (C) 2007-2014 by Lars Ahlzen and 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 Lars Ahlzen <lars ahlzen.com> (original contibutor)
  8. \author Glynn Clements
  9. */
  10. #include <math.h>
  11. #include "cairodriver.h"
  12. #include <grass/gis.h>
  13. #include <grass/glocale.h>
  14. #define MAX_IMAGE_SIZE 32767
  15. static int src_t, src_b, src_l, src_r, src_w, src_h;
  16. static int dst_t, dst_b, dst_l, dst_r, dst_w, dst_h;
  17. static int *trans;
  18. static cairo_surface_t *src_surf;
  19. static unsigned char *src_data;
  20. static int src_stride, ca_row;
  21. static int masked;
  22. static double scale(double k, int src_0, int src_1, int dst_0, int dst_1)
  23. {
  24. return dst_0 + (double) (k - src_0) * (dst_1 - dst_0) / (src_1 - src_0);
  25. }
  26. static int scale_fwd_y(int sy)
  27. {
  28. return (int)floor(scale(sy, src_t, src_b, dst_t, dst_b) + 0.5);
  29. }
  30. static int scale_rev_x(int dx)
  31. {
  32. return (int)floor(scale(dx + 0.5, dst_l, dst_r, src_l, src_r));
  33. }
  34. static int next_row(int sy, int dy)
  35. {
  36. sy++;
  37. for (;;) {
  38. int y = scale_fwd_y(sy);
  39. if (y > dy)
  40. return sy - 1;
  41. sy++;
  42. }
  43. }
  44. /*!
  45. \brief Start drawing raster
  46. \todo are top and left swapped?
  47. \param mask non-zero int for mask
  48. \param s source (map) extent (left, right, top, bottom)
  49. \param d destination (image) extent (left, right, top, bottom)
  50. */
  51. void Cairo_begin_raster(int mask, int s[2][2], double d[2][2])
  52. {
  53. int i;
  54. cairo_status_t status;
  55. masked = mask;
  56. src_l = s[0][0];
  57. src_r = s[0][1];
  58. src_t = s[1][0];
  59. src_b = s[1][1];
  60. src_w = src_r - src_l;
  61. src_h = src_b - src_t;
  62. dst_l = (int) floor(d[0][0] + 0.5);
  63. dst_r = (int) floor(d[0][1] + 0.5);
  64. dst_t = (int) floor(d[1][0] + 0.5);
  65. dst_b = (int) floor(d[1][1] + 0.5);
  66. dst_w = dst_r - dst_l;
  67. dst_h = dst_b - dst_t;
  68. G_debug(1, "Cairo_begin_raster(): masked=%d, src_lrtb=%d %d %d %d -> w/h=%d %d, "
  69. "dst_lrtb=%d %d %d %d -> w/h=%d %d",
  70. masked, src_l, src_r, src_t, src_b, src_w, src_h,
  71. dst_l, dst_r, dst_t, dst_b, dst_w, dst_h);
  72. /* create source surface */
  73. src_surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, ca.width, ca.height);
  74. status = cairo_surface_status(src_surf);
  75. if (status != CAIRO_STATUS_SUCCESS)
  76. G_fatal_error("%s - %s - size: %dx%d (cairo limit: %dx%d)",
  77. _("Failed to create cairo surface"),
  78. cairo_status_to_string (status), ca.width, ca.height,
  79. MAX_IMAGE_SIZE, MAX_IMAGE_SIZE);
  80. src_data = cairo_image_surface_get_data(src_surf);
  81. src_stride = cairo_image_surface_get_stride(src_surf);
  82. ca_row = 0;
  83. /* allocate buffer for down-sampling data */
  84. trans = G_malloc(dst_w * sizeof(int));
  85. for (i = 0; i < dst_w; i++)
  86. trans[i] = scale_rev_x(dst_l + i);
  87. }
  88. /*!
  89. \brief Draw raster row
  90. \param n number of cells
  91. \param row raster row (starting at 0)
  92. \param red,grn,blu,nul red,green,blue and null value
  93. \return next row
  94. */
  95. int Cairo_raster(int n, int row,
  96. const unsigned char *red, const unsigned char *grn,
  97. const unsigned char *blu, const unsigned char *nul)
  98. {
  99. int d_y0 = scale_fwd_y(row + 0);
  100. int d_y1 = scale_fwd_y(row + 1);
  101. int d_rows = d_y1 - d_y0;
  102. int x0 = MAX(0 - dst_l, 0);
  103. int x1 = MIN(ca.width - dst_l, dst_w);
  104. int y0 = MAX(0 - d_y0, 0);
  105. int y1 = MIN(ca.height - d_y0, d_rows);
  106. int x, y;
  107. if (y1 <= y0)
  108. return next_row(row, d_y1);
  109. G_debug(3, "Cairo_raster(): n=%d row=%d", n, row);
  110. for (x = x0; x < x1; x++) {
  111. int xx = dst_l + x;
  112. int j = trans[x];
  113. unsigned int c;
  114. if (masked && nul && nul[j])
  115. c = 0;
  116. else {
  117. unsigned int r = red[j];
  118. unsigned int g = grn[j];
  119. unsigned int b = blu[j];
  120. unsigned int a = 0xFF;
  121. c = (a << 24) + (r << 16) + (g << 8) + (b << 0);
  122. }
  123. for (y = y0; y < y1; y++) {
  124. int yy = d_y0 + y;
  125. *(unsigned int *)(src_data + yy * src_stride + xx * 4) = c;
  126. }
  127. }
  128. ca.modified = 1;
  129. ca_row++;
  130. return next_row(row, d_y1);
  131. }
  132. /*!
  133. \brief Finish drawing raster
  134. */
  135. void Cairo_end_raster(void)
  136. {
  137. G_debug(1, "Cairo_end_raster()");
  138. /* paint source surface onto destination (scaled) */
  139. cairo_save(cairo);
  140. /* cairo_translate(cairo, dst_l, dst_t); */
  141. /* cairo_scale(cairo, dst_w / src_w, dst_h / src_h); */
  142. cairo_surface_mark_dirty(src_surf);
  143. cairo_set_source_surface(cairo, src_surf, 0, 0);
  144. cairo_pattern_set_filter(cairo_get_source(cairo), CAIRO_FILTER_NEAREST);
  145. cairo_paint(cairo);
  146. cairo_restore(cairo);
  147. /* cleanup */
  148. G_free(trans);
  149. cairo_surface_destroy(src_surf);
  150. ca.modified = 1;
  151. }