GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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), NULL,
46  PAGE_READWRITE, 0, size, NULL);
47  if (!png.handle)
48  return;
49  ptr = MapViewOfFile(png.handle, FILE_MAP_WRITE, 0, 0, size);
50  if (!ptr)
51  return;
52 #else
53  ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t)0);
54  if (ptr == MAP_FAILED)
55  return;
56 #endif
57 
58  if (png.grid)
59  G_free(png.grid);
60  png.grid = (unsigned int *)((char *)ptr + HEADER_SIZE);
61 
62  close(fd);
63 
64  png.mapped = 1;
65 }
66 
67 /*!
68  \brief Start up graphics processing
69 
70  Anything that needs to be assigned, set up,
71  started-up, or otherwise initialized happens here. This is called only at
72  the startup of the graphics driver.
73 
74  The external variables define the pixle limits of the graphics surface. The
75  coordinate system used by the applications programs has the (0,0) origin
76  in the upper left-hand corner. Hence,
77  screen_left < screen_right
78  screen_top < screen_bottom
79  */
80 
81 int PNG_Graph_set(void)
82 {
83  unsigned int red, grn, blu;
84  int do_read = 0;
85  int do_map = 0;
86  char *p;
87 
88  G_gisinit("PNG driver");
89 
90  p = getenv("GRASS_RENDER_FILE");
91  if (!p || strlen(p) == 0)
92  p = FILE_NAME;
93  G_debug(1, "png: GRASS_RENDER_FILE: %s", p);
94 
95  png.file_name = p;
96 
97  p = getenv("GRASS_RENDER_TRUECOLOR");
98  png.true_color = !p || strcmp(p, "FALSE") != 0;
99 
100  G_verbose_message(_("png: truecolor status %s"),
101  png.true_color ? _("enabled") : _("disabled"));
102 
103  p = getenv("GRASS_RENDER_FILE_MAPPED");
104  do_map = p && strcmp(p, "TRUE") == 0;
105 
106  if (do_map) {
107  char *ext = png.file_name + strlen(png.file_name) - 4;
108 
109  if (G_strcasecmp(ext, ".bmp") != 0)
110  do_map = 0;
111  }
112 
113  p = getenv("GRASS_RENDER_FILE_READ");
114  do_read = p && strcmp(p, "TRUE") == 0;
115 
116  if (do_read && access(png.file_name, 0) != 0)
117  do_read = 0;
118 
121 
122  png.clip_top = 0;
124  png.clip_left = 0;
126 
127  p = getenv("GRASS_RENDER_TRANSPARENT");
128  png.has_alpha = p && strcmp(p, "TRUE") == 0;
129 
131 
132  p = getenv("GRASS_RENDER_BACKGROUNDCOLOR");
133  if (p && *p &&
134  (sscanf(p, "%02x%02x%02x", &red, &grn, &blu) == 3 ||
135  G_str_to_color(p, (int *)&red, (int *)&grn, (int *)&blu) == 1)) {
136  png.background = png_get_color(red, grn, blu, png.has_alpha ? 255 : 0);
137  }
138  else {
139  /* 0xffffff = white, 0x000000 = black */
140  if (strcmp(DEFAULT_FG_COLOR, "white") == 0)
141  /* foreground: white, background: black */
142  png.background = png_get_color(0, 0, 0, png.has_alpha ? 255 : 0);
143  else
144  /* foreground: black, background: white */
145  png.background =
146  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"), png.width, png.height);
151 
152  if (do_read && do_map)
153  map_file();
154 
155  if (!png.mapped)
156  png.grid = G_malloc(png.width * png.height * sizeof(unsigned int));
157 
158  if (!do_read) {
159  PNG_Erase();
160  png.modified = 1;
161  }
162 
163  if (do_read && !png.mapped)
164  read_image();
165 
166  if (do_map && !png.mapped) {
167  write_image();
168  map_file();
169  }
170 
171  return 0;
172 }
173 
174 /*!
175  \brief Get render file
176 
177  \return file name
178  */
179 const char *PNG_Graph_get_file(void)
180 {
181  return png.file_name;
182 }
#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:101
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#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
#define DEFAULT_FG_COLOR
Definition: gis.h:399
#define G_gisinit(pgm)
Definition: gis.h:72
#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