File: Graph_set.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 (156 lines) | stat: -rw-r--r-- 3,490 bytes parent folder | download | duplicates (3)
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
/*
 * Start up graphics processing.  Anything that needs to be assigned, set up,
 * started-up, or otherwise initialized happens here.  This is called only at
 * the startup of the graphics driver.
 *
 * The external variables define the pixle limits of the graphics surface.  The
 * coordinate system used by the applications programs has the (0,0) origin
 * in the upper left-hand corner.  Hence,
 *    screen_left < screen_right
 *    screen_top  < screen_bottom 
 */

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

#include <grass/gis.h>
#include "pngdriver.h"

char *file_name;
int currentColor;
int true_color;
int auto_write;
int has_alpha;
int mapped;

int clip_top, clip_bot, clip_left, clip_rite;
int width, height;
void *image;
unsigned int *grid;
unsigned char png_palette[256][4];
unsigned int background;
int modified;

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)
	G_free(grid);
    grid = (int *)((char *)ptr + HEADER_SIZE);

    close(fd);

    mapped = 1;
#endif
}

int PNG_Graph_set(int argc, char **argv)
{
    unsigned int red, grn, blu;
    int do_read = 0;
    int do_map = 0;
    char *p;

    G_gisinit("PNG driver");

    p = getenv("GRASS_PNGFILE");
    if (!p || strlen(p) == 0)
	p = FILE_NAME;

    file_name = p;

    p = getenv("GRASS_TRUECOLOR");
    true_color = p && strcmp(p, "TRUE") == 0;

    G_message("PNG: GRASS_TRUECOLOR status: %s",
	      true_color ? "TRUE" : "FALSE");

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

    p = getenv("GRASS_PNG_MAPPED");
    do_map = p && strcmp(p, "TRUE") == 0;

    if (do_map) {
	char *ext = file_name + strlen(file_name) - 4;

	if (G_strcasecmp(ext, ".bmp") != 0)
	    do_map = 0;
    }

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

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

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

    clip_top = screen_top;
    clip_bot = screen_bottom;
    clip_left = screen_left;
    clip_rite = screen_right;

    p = getenv("GRASS_TRANSPARENT");
    has_alpha = p && strcmp(p, "TRUE") == 0;

    init_color_table();

    p = getenv("GRASS_BACKGROUNDCOLOR");
    if (p && *p && sscanf(p, "%02x%02x%02x", &red, &grn, &blu) == 3)
	background = get_color(red, grn, blu, has_alpha ? 255 : 0);
    else {
	/* 0xffffff = white, 0x000000 = black */
	if (strcmp(DEFAULT_FG_COLOR, "white") == 0)
	    /* foreground: white, background: black */
	    background = get_color(0, 0, 0, has_alpha ? 255 : 0);
	else
	    /* foreground: black, background: white */
	    background = get_color(255, 255, 255, has_alpha ? 255 : 0);
    }

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

    if (do_read && do_map)
	map_file();

    if (!mapped)
	grid = G_malloc(width * height * sizeof(unsigned int));

    if (!do_read) {
	PNG_Erase();
	modified = 1;
    }

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

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

    return 0;
}