GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
pngdriver/graph_set.c
Go to the documentation of this file.
1 /*!
2  \file lib/pngdriver/graph_set.c
3 
4  \brief GRASS png display driver - set graphics processing
5 
6  (C) 2003-2014 by Glynn Clements 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 <stdlib.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #ifdef __MINGW32__
22 #include <windows.h>
23 #else
24 #include <sys/mman.h>
25 #endif
26 
27 #include <grass/gis.h>
28 #include <grass/colors.h>
29 #include <grass/glocale.h>
30 #include "pngdriver.h"
31 
32 struct png_state png;
33 
34 static void map_file(void)
35 {
36  size_t size = HEADER_SIZE + png.width * png.height * sizeof(unsigned int);
37  void *ptr;
38  int fd;
39 
40  fd = open(png.file_name, O_RDWR);
41  if (fd < 0)
42  return;
43 
44 #ifdef __MINGW32__
45  png.handle = CreateFileMapping((HANDLE) _get_osfhandle(fd),
46  NULL, PAGE_READWRITE,
47  0, size, NULL);
48  if (!png.handle)
49  return;
50  ptr = MapViewOfFile(png.handle, FILE_MAP_WRITE, 0, 0, size);
51  if (!ptr)
52  return;
53 #else
54  ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) 0);
55  if (ptr == MAP_FAILED)
56  return;
57 #endif
58 
59  if (png.grid)
60  G_free(png.grid);
61  png.grid = (unsigned int *)((char *) ptr + HEADER_SIZE);
62 
63  close(fd);
64 
65  png.mapped = 1;
66 }
67 
68 /*!
69  \brief Start up graphics processing
70 
71  Anything that needs to be assigned, set up,
72  started-up, or otherwise initialized happens here. This is called only at
73  the startup of the graphics driver.
74 
75  The external variables define the pixle limits of the graphics surface. The
76  coordinate system used by the applications programs has the (0,0) origin
77  in the upper left-hand corner. Hence,
78  screen_left < screen_right
79  screen_top < screen_bottom
80 */
81 
82 int PNG_Graph_set(void)
83 {
84  unsigned int red, grn, blu;
85  int do_read = 0;
86  int do_map = 0;
87  char *p;
88 
89  G_gisinit("PNG driver");
90 
91  p = getenv("GRASS_RENDER_FILE");
92  if (!p || strlen(p) == 0)
93  p = FILE_NAME;
94  G_debug(1, "png: GRASS_RENDER_FILE: %s", p);
95 
96  png.file_name = p;
97 
98  p = getenv("GRASS_RENDER_TRUECOLOR");
99  png.true_color = !p || strcmp(p, "FALSE") != 0;
100 
101  G_verbose_message(_("png: truecolor status %s"),
102  png.true_color ? _("enabled") : _("disabled"));
103 
104  p = getenv("GRASS_RENDER_FILE_MAPPED");
105  do_map = p && strcmp(p, "TRUE") == 0;
106 
107  if (do_map) {
108  char *ext = png.file_name + strlen(png.file_name) - 4;
109 
110  if (G_strcasecmp(ext, ".bmp") != 0)
111  do_map = 0;
112  }
113 
114  p = getenv("GRASS_RENDER_FILE_READ");
115  do_read = p && strcmp(p, "TRUE") == 0;
116 
117  if (do_read && access(png.file_name, 0) != 0)
118  do_read = 0;
119 
122 
123  png.clip_top = 0;
125  png.clip_left = 0;
127 
128  p = getenv("GRASS_RENDER_TRANSPARENT");
129  png.has_alpha = p && strcmp(p, "TRUE") == 0;
130 
132 
133  p = getenv("GRASS_RENDER_BACKGROUNDCOLOR");
134  if (p && *p &&
135  (sscanf(p, "%02x%02x%02x", &red, &grn, &blu) == 3 ||
136  G_str_to_color(p, (int *)&red, (int *)&grn, (int *)&blu) == 1)) {
137  png.background = png_get_color(red, grn, blu, png.has_alpha ? 255 : 0);
138  }
139  else {
140  /* 0xffffff = white, 0x000000 = black */
141  if (strcmp(DEFAULT_FG_COLOR, "white") == 0)
142  /* foreground: white, background: black */
143  png.background = png_get_color(0, 0, 0, png.has_alpha ? 255 : 0);
144  else
145  /* foreground: black, background: white */
146  png.background = png_get_color(255, 255, 255, png.has_alpha ? 255 : 0);
147  }
148 
149  G_verbose_message(_("png: collecting to file '%s'"), png.file_name);
150  G_verbose_message(_("png: image size %dx%d"),
151  png.width, png.height);
152 
153  if (do_read && do_map)
154  map_file();
155 
156  if (!png.mapped)
157  png.grid = G_malloc(png.width * png.height * sizeof(unsigned int));
158 
159  if (!do_read) {
160  PNG_Erase();
161  png.modified = 1;
162  }
163 
164  if (do_read && !png.mapped)
165  read_image();
166 
167  if (do_map && !png.mapped) {
168  write_image();
169  map_file();
170  }
171 
172  return 0;
173 }
174 
175 /*!
176  \brief Get render file
177 
178  \return file name
179 */
180 const char *PNG_Graph_get_file(void)
181 {
182  return png.file_name;
183 }
#define G_malloc(n)
Definition: defs/gis.h:112
#define HEADER_SIZE
Definition: cairodriver.h:45
int width
Definition: pngdriver.h:43
double clip_left
Definition: pngdriver.h:42
GRASS png display driver - header file.
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
Definition: color_str.c:112
int screen_width
Definition: driver/init.c:29
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
int screen_height
Definition: driver/init.c:30
#define DEFAULT_FG_COLOR
Definition: gis.h:370
int height
Definition: pngdriver.h:43
double clip_top
Definition: pngdriver.h:42
double clip_rite
Definition: pngdriver.h:42
double clip_bot
Definition: pngdriver.h:42
#define NULL
Definition: ccmath.h:32
const char * PNG_Graph_get_file(void)
Get render file.
struct png_state png
void read_image(void)
int mapped
Definition: pngdriver.h:37
unsigned int png_get_color(int r, int g, int b, int a)
Definition: color_table.c:120
void PNG_Erase(void)
Erase screen.
int modified
Definition: pngdriver.h:47
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition: strings.c:47
int PNG_Graph_set(void)
Start up graphics processing.
void write_image(void)
char * file_name
Definition: pngdriver.h:33
unsigned int * grid
Definition: pngdriver.h:44
#define G_gisinit(pgm)
Definition: gis.h:53
int has_alpha
Definition: pngdriver.h:36
#define FILE_NAME
Definition: htmlmap.h:9
#define _(str)
Definition: glocale.h:10
int true_color
Definition: pngdriver.h:35
unsigned int background
Definition: pngdriver.h:46
char * getenv()
void void G_verbose_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
void png_init_color_table(void)
Definition: color_table.c:73