File: main.c

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (325 lines) | stat: -rw-r--r-- 6,928 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
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

/****************************************************************************
 *
 * MODULE:       ximgview
 * AUTHOR(S):    Glynn Clements
 * PURPOSE:      View BMP images from the PNG driver
 * COPYRIGHT:    (C) 2007 Glynn Clements
 *
 *               This program is free software under the GNU General Public
 *               License (>=v2). Read the file COPYING that comes with GRASS
 *               for details.
 *
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>

#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <grass/gis.h>
#include <grass/glocale.h>

#define HEADER_SIZE 64

Display *dpy;
int scrn;
Window grwin;
XWindowAttributes xwa;

static int evmask = ExposureMask | StructureNotifyMask;
static int w_width, w_height;
static int i_width, i_height;
static unsigned long last;
static double fraction;

static void *imgbuf;
static void *xbuf;
static XImage *ximg;
static GC gc;

extern Colormap InitColorTableFixed(Colormap cmap);
extern unsigned long find_color(unsigned int r, unsigned int g,
				unsigned int b);

static void create_window(void)
{
    XSetWindowAttributes xswa;
    Colormap fixedcmap;

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

    scrn = DefaultScreen(dpy);

    xswa.event_mask = evmask;
    xswa.backing_store = NotUseful;
    xswa.background_pixel = BlackPixel(dpy, scrn);

    grwin = XCreateWindow(dpy, RootWindow(dpy, scrn),
			  0, 0,
			  800, 600,
			  0,
			  DefaultDepth(dpy, scrn),
			  InputOutput,
			  DefaultVisual(dpy, scrn),
			  CWEventMask | CWBackingStore | CWBackPixel, &xswa);

    XMapWindow(dpy, grwin);

    if (!XGetWindowAttributes(dpy, grwin, &xwa))
	G_fatal_error(_("Unable to get window attributes"));

    fixedcmap = InitColorTableFixed(DefaultColormap(dpy, scrn));

    XSetWindowColormap(dpy, grwin, fixedcmap);

    gc = XCreateGC(dpy, grwin, 0UL, NULL);

    xbuf = G_malloc(i_width * i_height * 4);
    ximg = XCreateImage(dpy, xwa.visual, xwa.depth, ZPixmap,
			0, xbuf, i_width, i_height, 32, 0);

    w_width = xwa.width;
    w_height = xwa.height;

    XFlush(dpy);
}

static void draw(void)
{
    int x0 = (w_width - i_width) / 2;
    int y0 = (w_height - i_height) / 2;
    const unsigned char *p = imgbuf;
    int row, col;

    for (row = 0; row < i_height; row++) {
	for (col = 0; col < i_width; col++) {
	    unsigned char b = *p++;
	    unsigned char g = *p++;
	    unsigned char r = *p++;
	    unsigned char a = *p++;
	    unsigned long c = find_color(r, g, b);

	    XPutPixel(ximg, col, row, c);
	}
    }

    XPutImage(dpy, grwin, gc, ximg, 0, 0, x0, y0, i_width, i_height);
    XSync(dpy, False);
}

static void redraw(void)
{
    struct timeval tv0, tv1;

    gettimeofday(&tv0, NULL);

    draw();

    gettimeofday(&tv1, NULL);
    last = (tv1.tv_sec - tv0.tv_sec) * 1000000L + (tv1.tv_usec - tv0.tv_usec);
}

static void dummy_handler(int sig)
{
}

static void main_loop(void)
{
    int xfd = ConnectionNumber(dpy);
    struct sigaction act;

    act.sa_handler = &dummy_handler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    sigaction(SIGUSR1, &act, NULL);

    for (;;) {
	fd_set waitset;
	struct timeval tv;
	unsigned long delay;

	while (XPending(dpy) > 0) {
	    XEvent event;

	    XNextEvent(dpy, &event);

	    switch (event.type) {
	    case Expose:
		draw();
		break;
	    case ConfigureNotify:
		w_width = event.xconfigure.width;
		w_height = event.xconfigure.height;
		break;
	    }
	}

	if (fraction > 0.001)
	    delay = (unsigned long)(last / fraction);

	tv.tv_sec = delay / 1000000;
	tv.tv_usec = delay % 1000000;

	FD_ZERO(&waitset);
	FD_SET(xfd, &waitset);
	errno = 0;
	if (select(FD_SETSIZE, &waitset, NULL, NULL, &tv) < 0 && errno != EINTR)
	    continue;

	if (!FD_ISSET(xfd, &waitset) || errno == EINTR)
	    redraw();
    }
}

static unsigned int get_2(const unsigned char **q)
{
    const unsigned char *p = *q;
    unsigned int n = (p[0] << 0) | (p[1] << 8);

    *q += 2;
    return n;
}

static unsigned int get_4(const unsigned char **q)
{
    const unsigned char *p = *q;
    unsigned int n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);

    *q += 4;
    return n;
}

static int read_bmp_header(const unsigned char *p)
{
    int size;

    if (*p++ != 'B')
	return 0;
    if (*p++ != 'M')
	return 0;

    size = get_4(&p);

    get_4(&p);

    if (get_4(&p) != HEADER_SIZE)
	return 0;

    if (get_4(&p) != 40)
	return 0;

    i_width = get_4(&p);
    i_height = -get_4(&p);

    get_2(&p);
    if (get_2(&p) != 32)
	return 0;

    if (get_4(&p) != 0)
	return 0;
    if (get_4(&p) != i_width * i_height * 4)
	return 0;

    if (size != HEADER_SIZE + i_width * i_height * 4)
	return 0;

    get_4(&p);
    get_4(&p);
    get_4(&p);
    get_4(&p);

    return 1;
}

static void map_file(const char *filename)
{
    char header[HEADER_SIZE];
    size_t size;
    void *ptr;
    int fd;

    fd = open(filename, O_RDONLY);
    if (fd < 0)
	G_fatal_error(_("Unable to open image file"));

    if (read(fd, header, sizeof(header)) != sizeof(header))
	G_fatal_error(_("Unable to read BMP header"));

    if (!read_bmp_header(header))
	G_fatal_error(_("Invalid BMP header"));

    size = HEADER_SIZE + i_width * i_height * 4;

    ptr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, (off_t) 0);
    if (ptr == MAP_FAILED)
	G_fatal_error(_("Unable to map image file"));

    imgbuf = (char *)ptr + HEADER_SIZE;

    close(fd);
}

int main(int argc, char **argv)
{
    struct GModule *module;
    struct
    {
	struct Option *image, *percent;
    } opt;
    const char *filename;
    int percent;

    G_gisinit(argv[0]);

    module = G_define_module();
    G_add_keyword(_("display"));
    G_add_keyword(_("graphics"));
    G_add_keyword(_("raster"));
    G_add_keyword(_("vector"));
    G_add_keyword(_("visualization"));
    module->description = _("View BMP images from the PNG driver.");

    opt.image = G_define_standard_option(G_OPT_F_INPUT);
    opt.image->key = "image";
    opt.image->required = YES;
    opt.image->gisprompt = "old_file,file,file";
    opt.image->description = _("Image file");

    opt.percent = G_define_option();
    opt.percent->key = "percent";
    opt.percent->type = TYPE_INTEGER;
    opt.percent->required = NO;
    opt.percent->multiple = NO;
    opt.percent->description = _("Percentage of CPU time to use");
    opt.percent->answer = "10";

    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    filename = opt.image->answer;
    percent = atoi(opt.percent->answer);
    fraction = percent / 100.0;

    map_file(filename);
    create_window();
    main_loop();

    XCloseDisplay(dpy);

    return EXIT_SUCCESS;
}