GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
pngdriver/raster.c
Go to the documentation of this file.
1 /*!
2  \file lib/pngdriver/raster.c
3 
4  \brief GRASS png display driver - draw raster
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 <string.h>
16 #include <math.h>
17 #include <grass/gis.h>
18 #include "driver.h"
19 #include "pngdriver.h"
20 
21 #ifndef min
22 #define min(a,b) ((a)<(b)?(a):(b))
23 #endif
24 #ifndef max
25 #define max(a,b) ((a)>(b)?(a):(b))
26 #endif
27 
28 static int *trans;
29 static int ncols;
30 static int nalloc;
31 static int masked;
32 static int src[2][2];
33 static int dst[2][2];
34 
35 static double scale(double k, const int src[2], const int dst[2])
36 {
37  return dst[0] + (double)(k - src[0]) * (dst[1] - dst[0]) / (src[1] -
38  src[0]);
39 }
40 
41 static int scale_fwd_y(int sy)
42 {
43  return (int)floor(scale(sy, src[1], dst[1]) + 0.5);
44 }
45 
46 static int scale_rev_x(int dx)
47 {
48  return (int)floor(scale(dx + 0.5, dst[0], src[0]));
49 }
50 
51 static int next_row(int sy, int dy)
52 {
53  sy++;
54 
55  for (;;) {
56  int y = scale_fwd_y(sy);
57 
58  if (y > dy)
59  return sy - 1;
60  sy++;
61  }
62 }
63 
64 static void alloc_buffers(void)
65 {
66  if (nalloc >= ncols)
67  return;
68 
69  nalloc = ncols;
70  trans = G_realloc(trans, nalloc * sizeof(int));
71 }
72 
73 /*!
74  \brief Start drawing raster
75 
76  \param mask non-zero int for mask
77  \param s source (map) extent (left, right, top, bottom)
78  \param fd destination (image) extent (left, right, top, bottom)
79 */
80 void PNG_begin_raster(int mask, int s[2][2], double fd[2][2])
81 {
82  int d[2][2];
83  int i;
84 
85  d[0][0] = (int) floor(fd[0][0] + 0.5);
86  d[0][1] = (int) floor(fd[0][1] + 0.5);
87  d[1][0] = (int) floor(fd[1][0] + 0.5);
88  d[1][1] = (int) floor(fd[1][1] + 0.5);
89 
90  ncols = d[0][1] - d[0][0];
91 
92  memcpy(src, s, sizeof(src));
93  memcpy(dst, d, sizeof(dst));
94  masked = mask;
95 
96  alloc_buffers();
97 
98  for (i = 0; i < ncols; i++)
99  trans[i] = scale_rev_x(d[0][0] + i);
100 }
101 
102 /*!
103  \brief Draw raster row
104 
105  \param n number of cells
106  \param row raster row (starts at 0)
107  \param red,grn,blu,nul red,green,blue and null value
108 
109  \return next row
110 */
111 int PNG_raster(int n, int row,
112  const unsigned char *red, const unsigned char *grn,
113  const unsigned char *blu, const unsigned char *nul)
114 {
115  int d_y0 = scale_fwd_y(row + 0);
116  int d_y1 = scale_fwd_y(row + 1);
117  int d_rows = d_y1 - d_y0;
118  int x0 = max(png.clip_left - dst[0][0], 0);
119  int x1 = min(png.clip_rite - dst[0][0], ncols);
120  int y0 = max(png.clip_top - d_y0, 0);
121  int y1 = min(png.clip_bot - d_y0, d_rows);
122  int x, y;
123 
124  if (y1 <= y0)
125  return next_row(row, d_y1);
126 
127  for (x = x0; x < x1; x++) {
128  int xx = dst[0][0] + x;
129  int j = trans[x];
130  int c;
131 
132  if (masked && nul && nul[j])
133  continue;
134 
135  c = png_get_color(red[j], grn[j], blu[j], 0);
136 
137  for (y = y0; y < y1; y++) {
138  int yy = d_y0 + y;
139 
140  png.grid[yy * png.width + xx] = c;
141  }
142  }
143 
144  png.modified = 1;
145 
146  return next_row(row, d_y1);
147 }
#define max(a, b)
int width
Definition: pngdriver.h:43
double clip_left
Definition: pngdriver.h:42
GRASS png display driver - header file.
void PNG_begin_raster(int mask, int s[2][2], double fd[2][2])
Start drawing raster.
#define min(a, b)
char * dst
Definition: lz4.h:599
double clip_top
Definition: pngdriver.h:42
double clip_rite
Definition: pngdriver.h:42
double clip_bot
Definition: pngdriver.h:42
#define x
int PNG_raster(int n, int row, const unsigned char *red, const unsigned char *grn, const unsigned char *blu, const unsigned char *nul)
Draw raster row.
struct png_state png
unsigned int png_get_color(int r, int g, int b, int a)
Definition: color_table.c:120
int modified
Definition: pngdriver.h:47
unsigned int * grid
Definition: pngdriver.h:44
double * y
Array of Y coordinates.
Definition: dig_structs.h:1684
#define G_realloc(p, n)
Definition: defs/gis.h:114