GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
pngdriver/draw_bitmap.c
Go to the documentation of this file.
1 /*!
2  \file lib/pngdriver/draw_bitmap.c
3 
4  \brief GRASS png display driver - draw bitmap
5 
6  (C) 2003-2014 by Per Henrik Johansen and the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Per Henrik Johansen (original contributor)
12  \author Glynn Clements
13  */
14 
15 #include <math.h>
16 #include "pngdriver.h"
17 
18 #ifndef min
19 #define min(a, b) ((a) < (b) ? (a) : (b))
20 #endif
21 #ifndef max
22 #define max(a, b) ((a) > (b) ? (a) : (b))
23 #endif
24 
25 /*!
26  \brief Draw bitmap
27 
28  \param ncols,nrows number of columns and rows
29  \param threshold threshold value
30  \param buf data buffer
31  */
32 void PNG_draw_bitmap(int ncols, int nrows, int threshold,
33  const unsigned char *buf)
34 {
35  int i0 = max(png.clip_left - cur_x, 0);
36  int i1 = min(png.clip_rite - cur_x, ncols);
37  int j0 = max(png.clip_top - cur_y, 0);
38  int j1 = min(png.clip_bot - cur_y, nrows);
39 
40  if (!png.true_color) {
41  int i, j;
42 
43  for (j = j0; j < j1; j++) {
44  int y = cur_y + j;
45 
46  for (i = i0; i < i1; i++) {
47  int x = cur_x + i;
48  unsigned int k = buf[j * ncols + i];
49  unsigned int *p = &png.grid[y * png.width + x];
50 
51  if (k > (unsigned int)threshold)
52  *p = png.current_color;
53  }
54  }
55  }
56  else {
57  int r1, g1, b1, a1;
58  int i, j;
59 
60  png_get_pixel(png.current_color, &r1, &g1, &b1, &a1);
61 
62  for (j = j0; j < j1; j++) {
63  int y = cur_y + j;
64 
65  for (i = i0; i < i1; i++) {
66  int x = cur_x + i;
67  unsigned int k = buf[j * ncols + i];
68  unsigned int *p = &png.grid[y * png.width + x];
69  int a0, r0, g0, b0;
70  unsigned int a, r, g, b;
71 
72  png_get_pixel(*p, &r0, &g0, &b0, &a0);
73 
74  a = (a0 * (255 - k) + a1 * k) / 255;
75  r = (r0 * (255 - k) + r1 * k) / 255;
76  g = (g0 * (255 - k) + g1 * k) / 255;
77  b = (b0 * (255 - k) + b1 * k) / 255;
78 
79  *p = png_get_color(r, g, b, a);
80  }
81  }
82  }
83 
84  png.modified = 1;
85 }
unsigned int png_get_color(int r, int g, int b, int a)
Definition: color_table.c:118
void png_get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
Definition: color_table.c:110
double cur_x
Definition: driver/init.c:32
double cur_y
Definition: driver/init.c:33
float g
Definition: named_colr.c:7
void PNG_draw_bitmap(int ncols, int nrows, int threshold, const unsigned char *buf)
Draw bitmap.
#define min(a, b)
#define max(a, b)
struct png_state png
GRASS png display driver - header file.
double b
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
double clip_left
Definition: pngdriver.h:41
double clip_bot
Definition: pngdriver.h:41
double clip_top
Definition: pngdriver.h:41
int current_color
Definition: pngdriver.h:33
int true_color
Definition: pngdriver.h:34
unsigned int * grid
Definition: pngdriver.h:43
int width
Definition: pngdriver.h:42
double clip_rite
Definition: pngdriver.h:41
int modified
Definition: pngdriver.h:46
#define x