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