GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
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 SPDX-FileCopyrightText: 2003-2014 Glynn Clements
7 SPDX-FileCopyrightText: GRASS Development Team
8 SPDX-License-Identifier: GPL-2.0-or-later
9
10 \author Per Henrik Johansen (original contributor)
11 \author Glynn Clements
12 */
13
14#include <string.h>
15#include <stdlib.h>
16#include <unistd.h>
17#include <fcntl.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#ifdef _WIN32
21#include <windows.h>
22#else
23#include <sys/mman.h>
24#endif
25
26#include <grass/gis.h>
27#include <grass/colors.h>
28#include <grass/glocale.h>
29#include "pngdriver.h"
30
32
33static void map_file(void)
34{
35 size_t size =
36 HEADER_SIZE + (size_t)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
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 */
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
124 png.width = screen_width;
125 png.height = screen_height;
126
127 png.clip_top = 0;
128 png.clip_bot = png.height;
129 png.clip_left = 0;
130 png.clip_rite = png.width;
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 =
162 G_malloc((size_t)png.width * png.height * sizeof(unsigned int));
163
164 if (!do_read) {
165 PNG_Erase();
166 png.modified = 1;
167 }
168
169 if (do_read && !png.mapped)
170 read_image();
171
172 if (do_map && !png.mapped) {
173 write_image();
174 map_file();
175 }
176
177 return 0;
178}
179
180/*!
181 \brief Get render file
182
183 \return file name
184 */
185const char *PNG_Graph_get_file(void)
186{
187 return png.file_name;
188}
#define HEADER_SIZE
Definition cairodriver.h:45
#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:99
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_malloc(n)
Definition defs/gis.h:136
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:45
int G_debug(int, const char *,...) __attribute__((format(printf
int screen_height
Definition driver/init.c:28
int screen_width
Definition driver/init.c:27
Header file for msvc/fcntl.c.
#define open
Definition fcntl.h:32
#define DEFAULT_FG_COLOR
Definition gis.h:402
#define G_gisinit(pgm)
Definition gis.h:68
#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