Draw_bitmap.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*!
  2. \file cairodriver/Draw_bitmap.c
  3. \brief GRASS cairo display driver - draw bitmap
  4. (C) 2007-2008 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 <grass/glocale.h>
  11. #include "cairodriver.h"
  12. static cairo_surface_t *fix_surface(cairo_surface_t * src)
  13. {
  14. int width = cairo_image_surface_get_width(src);
  15. int height = cairo_image_surface_get_height(src);
  16. int stride = cairo_image_surface_get_stride(src);
  17. cairo_format_t format = cairo_image_surface_get_format(src);
  18. unsigned char *data = cairo_image_surface_get_data(src);
  19. cairo_surface_t *dst = cairo_image_surface_create(format, width, height);
  20. int stride2 = cairo_image_surface_get_stride(dst);
  21. unsigned char *data2 = cairo_image_surface_get_data(dst);
  22. int i;
  23. for (i = 0; i < height; i++) {
  24. void *p = data + i * stride;
  25. void *q = data2 + i * stride2;
  26. int n = stride < stride2 ? stride : stride2;
  27. memcpy(q, p, n);
  28. }
  29. cairo_surface_destroy(src);
  30. return dst;
  31. }
  32. /*!
  33. \brief Draw bitmap
  34. \param ncols,nrows number of columns and rows
  35. \param threshold threshold value
  36. \param buf data buffer
  37. */
  38. void Cairo_Bitmap(int ncols, int nrows, int threshold,
  39. const unsigned char *buf)
  40. {
  41. cairo_surface_t *surf;
  42. G_debug(1, "Cairo_Bitmap: %d %d %d", ncols, nrows, threshold);
  43. surf = cairo_image_surface_create_for_data((unsigned char *)buf,
  44. CAIRO_FORMAT_A8, ncols, nrows,
  45. ncols);
  46. if (cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS)
  47. G_fatal_error(_("Cairo_Bitmap: Failed to create source"));
  48. surf = fix_surface(surf);
  49. cairo_mask_surface(cairo, surf, cur_x, cur_y);
  50. cairo_surface_destroy(surf);
  51. ca.modified = 1;
  52. }