GRASS GIS 7 Programmer's Manual  7.0.2(2015)-r00000
pngdriver/graph_set.c
Go to the documentation of this file.
1 
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),
46  NULL, PAGE_READWRITE,
47  0, size, NULL);
48  if (!png.handle)
49  return;
50  ptr = MapViewOfFile(png.handle, FILE_MAP_WRITE, 0, 0, size);
51  if (!ptr)
52  return;
53 #else
54  ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) 0);
55  if (ptr == MAP_FAILED)
56  return;
57 #endif
58 
59  if (png.grid)
60  G_free(png.grid);
61  png.grid = (unsigned int *)((char *) ptr + HEADER_SIZE);
62 
63  close(fd);
64 
65  png.mapped = 1;
66 }
67 
82 int PNG_Graph_set(void)
83 {
84  unsigned int red, grn, blu;
85  int do_read = 0;
86  int do_map = 0;
87  char *p;
88 
89  G_gisinit("PNG driver");
90 
91  p = getenv("GRASS_RENDER_FILE");
92  if (!p || strlen(p) == 0)
93  p = FILE_NAME;
94  G_debug(1, "png: GRASS_RENDER_FILE: %s", p);
95 
96  png.file_name = p;
97 
98  p = getenv("GRASS_RENDER_TRUECOLOR");
99  png.true_color = !p || strcmp(p, "FALSE") != 0;
100 
101  G_verbose_message(_("png: truecolor status %s"),
102  png.true_color ? _("enabled") : _("disabled"));
103 
104  p = getenv("GRASS_RENDER_FILE_MAPPED");
105  do_map = p && strcmp(p, "TRUE") == 0;
106 
107  if (do_map) {
108  char *ext = png.file_name + strlen(png.file_name) - 4;
109 
110  if (G_strcasecmp(ext, ".bmp") != 0)
111  do_map = 0;
112  }
113 
114  p = getenv("GRASS_RENDER_FILE_READ");
115  do_read = p && strcmp(p, "TRUE") == 0;
116 
117  if (do_read && access(png.file_name, 0) != 0)
118  do_read = 0;
119 
122 
123  png.clip_top = 0;
125  png.clip_left = 0;
127 
128  p = getenv("GRASS_RENDER_TRANSPARENT");
129  png.has_alpha = p && strcmp(p, "TRUE") == 0;
130 
132 
133  p = getenv("GRASS_RENDER_BACKGROUNDCOLOR");
134  if (p && *p &&
135  (sscanf(p, "%02x%02x%02x", &red, &grn, &blu) == 3 ||
136  G_str_to_color(p, (int *)&red, (int *)&grn, (int *)&blu) == 1)) {
137  png.background = png_get_color(red, grn, blu, png.has_alpha ? 255 : 0);
138  }
139  else {
140  /* 0xffffff = white, 0x000000 = black */
141  if (strcmp(DEFAULT_FG_COLOR, "white") == 0)
142  /* foreground: white, background: black */
143  png.background = png_get_color(0, 0, 0, png.has_alpha ? 255 : 0);
144  else
145  /* foreground: black, background: white */
146  png.background = 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"),
151  png.width, png.height);
152 
153  if (do_read && do_map)
154  map_file();
155 
156  if (!png.mapped)
157  png.grid = G_malloc(png.width * png.height * sizeof(unsigned int));
158 
159  if (!do_read) {
160  PNG_Erase();
161  png.modified = 1;
162  }
163 
164  if (do_read && !png.mapped)
165  read_image();
166 
167  if (do_map && !png.mapped) {
168  write_image();
169  map_file();
170  }
171 
172  return 0;
173 }
174 
180 const char *PNG_Graph_get_file(void)
181 {
182  return png.file_name;
183 }
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:46
#define HEADER_SIZE
Definition: cairodriver.h:45
int width
Definition: pngdriver.h:43
double clip_left
Definition: pngdriver.h:42
GRASS png display driver - header file.
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
int screen_width
Definition: driver/init.c:29
int screen_height
Definition: driver/init.c:30
int height
Definition: pngdriver.h:43
double clip_top
Definition: pngdriver.h:42
double clip_rite
Definition: pngdriver.h:42
double clip_bot
Definition: pngdriver.h:42
#define NULL
Definition: ccmath.h:32
const char * PNG_Graph_get_file(void)
Get render file.
struct png_state png
void read_image(void)
int mapped
Definition: pngdriver.h:37
unsigned int png_get_color(int r, int g, int b, int a)
Definition: color_table.c:120
void PNG_Erase(void)
Erase screen.
int modified
Definition: pngdriver.h:47
int PNG_Graph_set(void)
Start up graphics processing.
void write_image(void)
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
char * file_name
Definition: pngdriver.h:33
unsigned int * grid
Definition: pngdriver.h:44
int has_alpha
Definition: pngdriver.h:36
#define FILE_NAME
Definition: htmlmap.h:9
int true_color
Definition: pngdriver.h:35
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:112
unsigned int background
Definition: pngdriver.h:46
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149
void png_init_color_table(void)
Definition: color_table.c:73