raster.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "cairodriver.h"
  11. #include <grass/glocale.h>
  12. #define MAX_IMAGE_SIZE 32767
  13. static int src_t, src_b, src_l, src_r, src_w, src_h;
  14. static double dst_t, dst_b, dst_l, dst_r, dst_w, dst_h;
  15. static cairo_surface_t *src_surf;
  16. static unsigned char *src_data;
  17. static int src_stride;
  18. static int masked;
  19. /*!
  20. \brief Start drawing raster
  21. \todo are top and left swapped?
  22. \param mask non-zero int for mask
  23. \param s source (map) extent (left, right, top, bottom)
  24. \param d destination (image) extent (left, right, top, bottom)
  25. */
  26. void Cairo_begin_raster(int mask, int s[2][2], double d[2][2])
  27. {
  28. cairo_status_t status;
  29. masked = mask;
  30. src_l = s[0][0];
  31. src_r = s[0][1];
  32. src_t = s[1][0];
  33. src_b = s[1][1];
  34. src_w = src_r - src_l;
  35. src_h = src_b - src_t;
  36. dst_l = d[0][0];
  37. dst_r = d[0][1];
  38. dst_t = d[1][0];
  39. dst_b = d[1][1];
  40. dst_w = dst_r - dst_l;
  41. dst_h = dst_b - dst_t;
  42. G_debug(1, "Cairo_begin_raster(): masked=%d, src_lrtb=%d %d %d %d -> w/h=%d/%d, "
  43. "dst_lrtb=%f %f %f %f -> w/h=%f %f",
  44. masked, src_l, src_r, src_t, src_b, src_w, src_h,
  45. dst_l, dst_r, dst_t, dst_b, dst_w, dst_h);
  46. /* create source surface */
  47. src_surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, src_w, src_h);
  48. status = cairo_surface_status(src_surf);
  49. if (status != CAIRO_STATUS_SUCCESS)
  50. G_fatal_error("%s - %s - size: %dx%d (cairo limit: %dx%d)",
  51. _("Failed to create cairo surface"),
  52. cairo_status_to_string (status), src_w, src_h,
  53. MAX_IMAGE_SIZE, MAX_IMAGE_SIZE);
  54. src_data = cairo_image_surface_get_data(src_surf);
  55. src_stride = cairo_image_surface_get_stride(src_surf);
  56. }
  57. /*!
  58. \brief Draw raster row
  59. \param n number of cell
  60. \param row raster row (starting at 0)
  61. \param red,grn,blu,nul red,green,blue and null value
  62. \return next row
  63. */
  64. int Cairo_raster(int n, int row,
  65. const unsigned char *red, const unsigned char *grn,
  66. const unsigned char *blu, const unsigned char *nul)
  67. {
  68. int i;
  69. unsigned int *dst;
  70. dst = (unsigned int *)(src_data + (row - src_t) * src_stride);
  71. G_debug(3, "Cairo_raster(): n=%d row=%d", n, row);
  72. for (i = 0; i < n; i++) {
  73. if (masked && nul && nul[i])
  74. *dst++ = 0;
  75. else {
  76. unsigned int r = red[i];
  77. unsigned int g = grn[i];
  78. unsigned int b = blu[i];
  79. unsigned int a = 0xFF;
  80. *dst++ = (a << 24) + (r << 16) + (g << 8) + (b << 0);
  81. }
  82. }
  83. return row + 1;
  84. }
  85. /*!
  86. \brief Finish drawing raster
  87. */
  88. void Cairo_end_raster(void)
  89. {
  90. G_debug(1, "Cairo_end_raster()");
  91. /* paint source surface onto destination (scaled) */
  92. cairo_save(cairo);
  93. cairo_translate(cairo, dst_l, dst_t);
  94. cairo_scale(cairo, dst_w / src_w, dst_h / src_h);
  95. cairo_surface_mark_dirty(src_surf);
  96. cairo_set_source_surface(cairo, src_surf, 0, 0);
  97. cairo_pattern_set_filter(cairo_get_source(cairo), CAIRO_FILTER_NEAREST);
  98. cairo_paint(cairo);
  99. cairo_restore(cairo);
  100. /* cleanup */
  101. cairo_surface_destroy(src_surf);
  102. ca.modified = 1;
  103. }