GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
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
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
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
135
png_init_color_table
();
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
}
G_free
void G_free(void *buf)
Free allocated memory.
Definition
alloc.c:147
HEADER_SIZE
#define HEADER_SIZE
Definition
cairodriver.h:46
NULL
#define NULL
Definition
ccmath.h:32
G_str_to_color
int G_str_to_color(const char *str, int *red, int *grn, int *blu)
Parse color string and set red,green,blue.
Definition
color_str.c:103
png_get_color
unsigned int png_get_color(int r, int g, int b, int a)
Definition
color_table.c:118
png_init_color_table
void png_init_color_table(void)
Definition
color_table.c:72
G_debug
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition
debug.c:66
screen_height
int screen_height
Definition
driver/init.c:30
screen_width
int screen_width
Definition
driver/init.c:29
G_verbose_message
void G_verbose_message(const char *msg,...)
Print a message to stderr but only if module is in verbose mode.
Definition
gis/error.c:108
FILE_NAME
#define FILE_NAME
Definition
htmlmap.h:8
PNG_Erase
void PNG_Erase(void)
Erase screen.
Definition
pngdriver/erase.c:20
png
struct png_state png
Definition
pngdriver/graph_set.c:32
PNG_Graph_set
int PNG_Graph_set(void)
Start up graphics processing.
Definition
pngdriver/graph_set.c:86
PNG_Graph_get_file
const char * PNG_Graph_get_file(void)
Get render file.
Definition
pngdriver/graph_set.c:184
pngdriver.h
GRASS png display driver - header file.
write_image
void write_image(void)
Definition
pngdriver/write.c:22
read_image
void read_image(void)
Definition
pngdriver/read.c:22
G_strcasecmp
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower).
Definition
strings.c:47
png_state
Definition
pngdriver.h:31
pngdriver
graph_set.c
Generated on
for GRASS 8 Programmer's Manual by
1.17.0