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