draw_bitmap.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*!
  2. \file lib/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. /*!
  13. \brief Draw bitmap
  14. \param ncols,nrows number of columns and rows
  15. \param threshold threshold value
  16. \param buf data buffer
  17. */
  18. void Cairo_Bitmap(int ncols, int nrows, int threshold,
  19. const unsigned char *buf)
  20. {
  21. cairo_surface_t *surf;
  22. int stride;
  23. unsigned char *data;
  24. int i;
  25. G_debug(1, "Cairo_Bitmap: %d %d %d", ncols, nrows, threshold);
  26. #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,5,8)
  27. stride = cairo_format_stride_for_width(CAIRO_FORMAT_A8, ncols);
  28. #else
  29. #define MULTIPLE 4
  30. stride = (ncols + (MULTIPLE - 1)) / MULTIPLE * MULTIPLE;
  31. #endif
  32. data = malloc(stride * nrows);
  33. surf = cairo_image_surface_create_for_data(
  34. data, CAIRO_FORMAT_A8, ncols, nrows, stride);
  35. if (cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS)
  36. G_fatal_error(_("Cairo_Bitmap: Failed to create source"));
  37. for (i = 0; i < nrows; i++)
  38. memcpy(&data[i * stride], &buf[i * ncols], ncols);
  39. cairo_surface_mark_dirty(surf);
  40. cairo_mask_surface(cairo, surf, cur_x, cur_y);
  41. cairo_surface_destroy(surf);
  42. ca.modified = 1;
  43. }