GRASS Programmer's Manual  6.5.svn(2014)-r66266
 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;
31 int mapped;
32 
35 void *image;
36 unsigned int *grid;
37 unsigned char png_palette[256][4];
38 unsigned int background;
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 
108 
113 
114  p = getenv("GRASS_TRANSPARENT");
115  has_alpha = p && strcmp(p, "TRUE") == 0;
116 
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",
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 }
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:192
void * image
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
#define HEADER_SIZE
Definition: cairodriver.h:17
int has_alpha
int currentColor
FILE * fd
Definition: g3dcolor.c:368
int modified
tuple width
int screen_top
Definition: driver/init.c:35
int clip_rite
unsigned char * grid
int clip_bot
#define FILE_NAME
Definition: pngdriver.h:9
int true_color
int screen_left
Definition: driver/init.c:32
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
char * getenv()
void PNG_Erase(void)
int clip_top
void write_image(void)
int PNG_Graph_set(int argc, char **argv)
void init_color_table(void)
void G_message(const char *msg,...)
Print a message to stderr.
Definition: lib/gis/error.c:74
char * file_name
int screen_right
Definition: driver/init.c:33
unsigned int background
int
Definition: g3dcolor.c:48
int auto_write
Definition: cairodriver.h:44
unsigned char png_palette[256][4]
int clip_left
return NULL
Definition: dbfopen.c:1394
int mapped
unsigned int get_color(int r, int g, int b, int a)
int screen_bottom
Definition: driver/init.c:34
int height
void read_image(void)