File: Graph.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (361 lines) | stat: -rw-r--r-- 7,776 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*!
  \file lib/cairodriver/Graph.c

  \brief GRASS cairo display driver - driver settings

  (C) 2007-2008, 2011 by Lars Ahlzen and the GRASS Development Team
  
  This program is free software under the GNU General Public License
  (>=v2). Read the file COPYING that comes with GRASS for details.
  
  \author Lars Ahlzen <lars ahlzen.com> (original contibutor)
  \author Glynn Clements  
*/

#include "cairodriver.h"

#if CAIRO_HAS_PS_SURFACE
#include <cairo-ps.h>
#endif
#if CAIRO_HAS_PDF_SURFACE
#include <cairo-pdf.h>
#endif
#if CAIRO_HAS_SVG_SURFACE
#include <cairo-svg.h>
#endif
#if CAIRO_HAS_XLIB_XRENDER_SURFACE
#include <cairo-xlib.h>
#include <cairo-xlib-xrender.h>
#endif

#include <unistd.h>
#ifndef __MINGW32__
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#endif

#if defined(USE_X11) && CAIRO_HAS_XLIB_SURFACE
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif

/* globals */
char *file_name;
int file_type;
int is_vector;
int width, height, stride;
unsigned char *grid;
int modified;
int auto_write;
int mapped;

/* background color */
double bgcolor_r, bgcolor_g, bgcolor_b, bgcolor_a;

/* cairo objects */
cairo_surface_t *surface;
cairo_t *cairo;

static void init_cairo(void);
static int ends_with(const char *string, const char *suffix);
static void map_file(void);

#if defined(USE_X11) && CAIRO_HAS_XLIB_SURFACE
static int init_xlib(void)
{
    Display *dpy;
    Drawable win;
    unsigned long xid;
    XVisualInfo templ;
    XVisualInfo *vinfo;
    int count;
    Window root;
    unsigned int depth;
    int si;
    unsigned int ui;
    Visual *visual;
    char *p;

    p = getenv("GRASS_CAIRO_DRAWABLE");
    if (!p || sscanf(p, "%li", &xid) != 1)
	G_fatal_error("invalid Drawable XID: %s", p);
    win = xid;

    dpy = XOpenDisplay(NULL);
    if (!dpy)
	G_fatal_error("Unable to open display");

    p = getenv("GRASS_CAIRO_VISUAL");
    if (!p || sscanf(p, "%li", &xid) != 1)
	G_fatal_error("invalid Visual XID: %s", p);
    templ.visualid = xid;

    vinfo = XGetVisualInfo(dpy, VisualIDMask, &templ, &count);
    if (!vinfo || !count)
	G_fatal_error("Unable to obtain visual");
    visual = vinfo[0].visual;

    if (!XGetGeometry
	(dpy, win, &root, &si, &si, &width, &height, &ui, &depth))
	G_fatal_error("Unable to query drawable");

    surface = cairo_xlib_surface_create(dpy, win, visual, width, height);

    if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS)
	G_fatal_error("Failed to initialize Xlib surface");

    cairo = cairo_create(surface);

    file_name = "<X11>";
    file_type = FTYPE_X11;

    screen_right = screen_left + width;
    screen_bottom = screen_top + height;

    return 0;
}
#endif

static int init_file(void)
{
    int do_read = 0;
    int do_map = 0;
    char *p;

    /* set image properties */
    width = screen_right - screen_left;
    height = screen_bottom - screen_top;
    stride = width * 4;

    /* get file name */
    p = getenv("GRASS_CAIROFILE");
    if (!p || strlen(p) == 0)
	p = DEFAULT_FILE_NAME;

    file_name = p;

    /* get file type (from extension) */
    if (file_type == FTYPE_X11) ;	/* skip */
    else if (ends_with(file_name, ".ppm"))
	file_type = FTYPE_PPM;
    else if (ends_with(file_name, ".bmp"))
	file_type = FTYPE_BMP;
#if CAIRO_HAS_PNG_FUNCTIONS
    else if (ends_with(file_name, ".png"))
	file_type = FTYPE_PNG;
#endif
#if CAIRO_HAS_PDF_SURFACE
    else if (ends_with(file_name, ".pdf"))
	file_type = FTYPE_PDF;
#endif
#if CAIRO_HAS_PS_SURFACE
    else if (ends_with(file_name, ".ps"))
	file_type = FTYPE_PS;
#endif
#if CAIRO_HAS_SVG_SURFACE
    else if (ends_with(file_name, ".svg"))
	file_type = FTYPE_SVG;
#endif
    else
	G_fatal_error("Unknown file extension: %s", p);
    G_debug(1, "File type: %s (%d)", file_name, file_type);

    switch (file_type) {
    case FTYPE_PDF:
    case FTYPE_PS:
    case FTYPE_SVG:
	is_vector = 1;
	break;
    }

    p = getenv("GRASS_CAIRO_MAPPED");
    do_map = p && strcmp(p, "TRUE") == 0 && ends_with(file_name, ".bmp");

    p = getenv("GRASS_CAIRO_READ");
    do_read = p && strcmp(p, "TRUE") == 0;

    if (is_vector) {
	do_read = do_map = 0;
	bgcolor_a = 1.0;
    }

    if (do_read && access(file_name, 0) != 0)
	do_read = 0;

    G_message
	("cairo: collecting to file: %s,\n     GRASS_WIDTH=%d, GRASS_HEIGHT=%d",
	 file_name, width, height);

    if (do_read && do_map)
	map_file();

    if (!mapped && !is_vector)
	grid = G_malloc(height * stride);

    init_cairo();

    if (!do_read && !is_vector) {
	Cairo_Erase();
	modified = 1;
    }

    if (do_read && !mapped)
	read_image();

    if (do_map && !mapped) {
	write_image();
	map_file();
	init_cairo();
    }

    return 0;
}

int Cairo_Graph_set(int argc, char **argv)
{
    char *p;

    G_gisinit("Cairo driver");
    G_debug(1, "Cairo_Graph_set");

    /* get background color */
    p = getenv("GRASS_BACKGROUNDCOLOR");
    if (p && *p) {
	unsigned int red, green, blue;

	if (sscanf(p, "%02x%02x%02x", &red, &green, &blue) == 3) {
	    bgcolor_r = CAIROCOLOR(red);
	    bgcolor_g = CAIROCOLOR(green);
	    bgcolor_b = CAIROCOLOR(blue);
	}
	else
	    G_fatal_error("Unknown background color: %s", p);
    }
    else
	bgcolor_r = bgcolor_g = bgcolor_b = 1.0;

    /* get background transparency setting */
    p = getenv("GRASS_TRANSPARENT");
    if (p && strcmp(p, "TRUE") == 0)
	bgcolor_a = 0.0;
    else
	bgcolor_a = 1.0;

    p = getenv("GRASS_AUTO_WRITE");
    auto_write = p && strcmp(p, "TRUE") == 0;

#if defined(USE_X11) && CAIRO_HAS_XLIB_SURFACE
    p = getenv("GRASS_CAIRO_DRAWABLE");
    if (p)
	return init_xlib();
#endif
    return init_file();
}

void Cairo_Graph_close(void)
{
    G_debug(1, "Cairo_Graph_close");

    write_image();

    if (cairo) {
	cairo_destroy(cairo);
	cairo = NULL;
    }
    if (surface) {
	cairo_surface_destroy(surface);
	surface = NULL;
    }
}

static void init_cairo(void)
{
    G_debug(1, "init_cairo");

    /* create cairo surface */
    switch (file_type) {
    case FTYPE_PPM:
    case FTYPE_BMP:
    case FTYPE_PNG:
	surface =
	    (cairo_surface_t *) cairo_image_surface_create_for_data(grid,
								    CAIRO_FORMAT_ARGB32,
								    width,
								    height,
								    stride);
	break;
#if CAIRO_HAS_PDF_SURFACE
    case FTYPE_PDF:
	surface =
	    (cairo_surface_t *) cairo_pdf_surface_create(file_name,
							 (double)width,
							 (double)height);
	break;
#endif
#if CAIRO_HAS_PS_SURFACE
    case FTYPE_PS:
	surface =
	    (cairo_surface_t *) cairo_ps_surface_create(file_name,
							(double)width,
							(double)height);
	break;
#endif
#if CAIRO_HAS_SVG_SURFACE
    case FTYPE_SVG:
	surface =
	    (cairo_surface_t *) cairo_svg_surface_create(file_name,
							 (double)width,
							 (double)height);
	break;
#endif
    default:
	G_fatal_error("Unknown Cairo surface type");
	break;
    }

    if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS)
	G_fatal_error("Failed to initialize Cairo surface");

    cairo = cairo_create(surface);
}

/* Returns TRUE if string ends with suffix (case insensitive) */
static int ends_with(const char *string, const char *suffix)
{
    if (strlen(string) < strlen(suffix))
	return FALSE;

    return G_strcasecmp(suffix,
			string + strlen(string) - strlen(suffix)) == 0;
}

static void map_file(void)
{
#ifndef __MINGW32__
    size_t size = HEADER_SIZE + width * height * sizeof(unsigned int);
    void *ptr;
    int fd;

    fd = open(file_name, O_RDWR);
    if (fd < 0)
	return;

    ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) 0);
    if (ptr == MAP_FAILED)
	return;

    if (grid) {
	cairo_destroy(cairo);
	cairo_surface_destroy(surface);
	G_free(grid);
    }
    grid = (unsigned char *)ptr + HEADER_SIZE;

    close(fd);

    mapped = 1;
#endif
}