draw_bitmap.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*!
  2. \file lib/pngdriver/draw_bitmap.c
  3. \brief GRASS png display driver - draw bitmap
  4. (C) 2003-2014 by Per Henrik Johansen 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 Per Henrik Johansen (original contributor)
  8. \author Glynn Clements
  9. */
  10. #include <math.h>
  11. #include "pngdriver.h"
  12. #ifndef min
  13. #define min(a,b) ((a)<(b)?(a):(b))
  14. #endif
  15. #ifndef max
  16. #define max(a,b) ((a)>(b)?(a):(b))
  17. #endif
  18. /*!
  19. \brief Draw bitmap
  20. \param ncols,nrows number of columns and rows
  21. \param threshold threshold value
  22. \param buf data buffer
  23. */
  24. void PNG_draw_bitmap(int ncols, int nrows, int threshold,
  25. const unsigned char *buf)
  26. {
  27. int i0 = max(png.clip_left - cur_x, 0);
  28. int i1 = min(png.clip_rite - cur_x, ncols);
  29. int j0 = max(png.clip_top - cur_y, 0);
  30. int j1 = min(png.clip_bot - cur_y, nrows);
  31. if (!png.true_color) {
  32. int i, j;
  33. for (j = j0; j < j1; j++) {
  34. int y = cur_y + j;
  35. for (i = i0; i < i1; i++) {
  36. int x = cur_x + i;
  37. unsigned int k = buf[j * ncols + i];
  38. unsigned int *p = &png.grid[y * png.width + x];
  39. if (k > threshold)
  40. *p = png.current_color;
  41. }
  42. }
  43. }
  44. else {
  45. int r1, g1, b1, a1;
  46. int i, j;
  47. png_get_pixel(png.current_color, &r1, &g1, &b1, &a1);
  48. for (j = j0; j < j1; j++) {
  49. int y = cur_y + j;
  50. for (i = i0; i < i1; i++) {
  51. int x = cur_x + i;
  52. unsigned int k = buf[j * ncols + i];
  53. unsigned int *p = &png.grid[y * png.width + x];
  54. unsigned int a0, r0, g0, b0;
  55. unsigned int a, r, g, b;
  56. png_get_pixel(*p, &r0, &g0, &b0, &a0);
  57. a = (a0 * (255 - k) + a1 * k) / 255;
  58. r = (r0 * (255 - k) + r1 * k) / 255;
  59. g = (g0 * (255 - k) + g1 * k) / 255;
  60. b = (b0 * (255 - k) + b1 * k) / 255;
  61. *p = png_get_color(r, g, b, a);
  62. }
  63. }
  64. }
  65. png.modified = 1;
  66. }