GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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 
33 void PNG_draw_bitmap(int ncols, int nrows, int threshold,
34  const unsigned char *buf)
35 {
36  int i0 = max(png.clip_left - cur_x, 0);
37  int i1 = min(png.clip_rite - cur_x, ncols);
38  int j0 = max(png.clip_top - cur_y, 0);
39  int j1 = min(png.clip_bot - cur_y, nrows);
40 
41  if (!png.true_color) {
42  int i, j;
43 
44  for (j = j0; j < j1; j++) {
45  int y = cur_y + j;
46 
47  for (i = i0; i < i1; i++) {
48  int x = cur_x + i;
49  unsigned int k = buf[j * ncols + i];
50  unsigned int *p = &png.grid[y * png.width + x];
51 
52  if (k > (unsigned int)threshold)
53  *p = png.current_color;
54  }
55  }
56  }
57  else {
58  int r1, g1, b1, a1;
59  int i, j;
60 
61  png_get_pixel(png.current_color, &r1, &g1, &b1, &a1);
62 
63  for (j = j0; j < j1; j++) {
64  int y = cur_y + j;
65 
66  for (i = i0; i < i1; i++) {
67  int x = cur_x + i;
68  unsigned int k = buf[j * ncols + i];
69  unsigned int *p = &png.grid[y * png.width + x];
70  int a0, r0, g0, b0;
71  unsigned int a, r, g, b;
72 
73  png_get_pixel(*p, &r0, &g0, &b0, &a0);
74 
75  a = (a0 * (255 - k) + a1 * k) / 255;
76  r = (r0 * (255 - k) + r1 * k) / 255;
77  g = (g0 * (255 - k) + g1 * k) / 255;
78  b = (b0 * (255 - k) + b1 * k) / 255;
79 
80  *p = png_get_color(r, g, b, a);
81  }
82  }
83  }
84 
85  png.modified = 1;
86 }
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