GRASS Programmer's Manual
6.4.2(2012)
Main Page
Related Pages
Namespaces
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Pages
pngdriver/Graph_set.c
Go to the documentation of this file.
1
/*
2
* Start up graphics processing. Anything that needs to be assigned, set up,
3
* started-up, or otherwise initialized happens here. This is called only at
4
* the startup of the graphics driver.
5
*
6
* The external variables define the pixle limits of the graphics surface. The
7
* coordinate system used by the applications programs has the (0,0) origin
8
* in the upper left-hand corner. Hence,
9
* screen_left < screen_right
10
* screen_top < screen_bottom
11
*/
12
13
#include <string.h>
14
#include <stdlib.h>
15
#include <unistd.h>
16
#ifndef __MINGW32__
17
#include <fcntl.h>
18
#include <sys/types.h>
19
#include <sys/stat.h>
20
#include <sys/mman.h>
21
#endif
22
23
#include <grass/gis.h>
24
#include "
pngdriver.h
"
25
26
char
*
file_name
;
27
int
currentColor
;
28
int
true_color
;
29
int
auto_write
;
30
int
has_alpha
;
31
int
mapped
;
32
33
int
clip_top
,
clip_bot
,
clip_left
,
clip_rite
;
34
int
width
,
height
;
35
void
*
image
;
36
unsigned
int
*
grid
;
37
unsigned
char
png_palette
[256][4];
38
unsigned
int
background
;
39
int
modified
;
40
41
static
void
map_file(
void
)
42
{
43
#ifndef __MINGW32__
44
size_t
size =
HEADER_SIZE
+
width
*
height
*
sizeof
(
unsigned
int);
45
void
*ptr;
46
int
fd;
47
48
fd = open(
file_name
, O_RDWR);
49
if
(fd < 0)
50
return
;
51
52
ptr = mmap(
NULL
, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) 0);
53
if
(ptr == MAP_FAILED)
54
return
;
55
56
if
(
grid
)
57
G_free
(
grid
);
58
grid
= (
int
*)((
char
*)ptr +
HEADER_SIZE
);
59
60
close(fd);
61
62
mapped
= 1;
63
#endif
64
}
65
66
int
PNG_Graph_set
(
int
argc,
char
**argv)
67
{
68
unsigned
int
red, grn, blu;
69
int
do_read = 0;
70
int
do_map = 0;
71
char
*p;
72
73
G_gisinit(
"PNG driver"
);
74
75
p =
getenv
(
"GRASS_PNGFILE"
);
76
if
(!p || strlen(p) == 0)
77
p =
FILE_NAME
;
78
79
file_name
= p;
80
81
p =
getenv
(
"GRASS_TRUECOLOR"
);
82
true_color
= p && strcmp(p,
"TRUE"
) == 0;
83
84
G_message
(
"PNG: GRASS_TRUECOLOR status: %s"
,
85
true_color
?
"TRUE"
:
"FALSE"
);
86
87
p =
getenv
(
"GRASS_PNG_AUTO_WRITE"
);
88
auto_write
= p && strcmp(p,
"TRUE"
) == 0;
89
90
p =
getenv
(
"GRASS_PNG_MAPPED"
);
91
do_map = p && strcmp(p,
"TRUE"
) == 0;
92
93
if
(do_map) {
94
char
*ext =
file_name
+ strlen(
file_name
) - 4;
95
96
if
(
G_strcasecmp
(ext,
".bmp"
) != 0)
97
do_map = 0;
98
}
99
100
p =
getenv
(
"GRASS_PNG_READ"
);
101
do_read = p && strcmp(p,
"TRUE"
) == 0;
102
103
if
(do_read && access(
file_name
, 0) != 0)
104
do_read = 0;
105
106
width
=
screen_right
-
screen_left
;
107
height
=
screen_bottom
-
screen_top
;
108
109
clip_top
=
screen_top
;
110
clip_bot
=
screen_bottom
;
111
clip_left
=
screen_left
;
112
clip_rite
=
screen_right
;
113
114
p =
getenv
(
"GRASS_TRANSPARENT"
);
115
has_alpha
= p && strcmp(p,
"TRUE"
) == 0;
116
117
init_color_table
();
118
119
p =
getenv
(
"GRASS_BACKGROUNDCOLOR"
);
120
if
(p && *p && sscanf(p,
"%02x%02x%02x"
, &red, &grn, &blu) == 3)
121
background
=
get_color
(red, grn, blu,
has_alpha
? 255 : 0);
122
else
{
123
/* 0xffffff = white, 0x000000 = black */
124
if
(strcmp(DEFAULT_FG_COLOR,
"white"
) == 0)
125
/* foreground: white, background: black */
126
background
=
get_color
(0, 0, 0,
has_alpha
? 255 : 0);
127
else
128
/* foreground: black, background: white */
129
background
=
get_color
(255, 255, 255,
has_alpha
? 255 : 0);
130
}
131
132
G_message
133
(
"PNG: collecting to file: %s,\n GRASS_WIDTH=%d, GRASS_HEIGHT=%d"
,
134
file_name
,
width
,
height
);
135
136
if
(do_read && do_map)
137
map_file();
138
139
if
(!
mapped
)
140
grid
= G_malloc(
width
*
height
*
sizeof
(
unsigned
int
));
141
142
if
(!do_read) {
143
PNG_Erase
();
144
modified
= 1;
145
}
146
147
if
(do_read && !
mapped
)
148
read_image
();
149
150
if
(do_map && !
mapped
) {
151
write_image
();
152
map_file();
153
}
154
155
return
0;
156
}
lib
pngdriver
Graph_set.c
Generated on Sun Mar 16 2014 05:07:45 for GRASS Programmer's Manual by
1.8.1.2