-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwall2pnm.c
88 lines (72 loc) · 1.87 KB
/
wall2pnm.c
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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
static void
die(const char *errstr, ...)
{
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(1);
}
static Pixmap
getrootpixmap(Display* display, Window *root)
{
Pixmap pixmap = 0;
Atom act_type;
int act_format;
unsigned long nitems, bytes_after;
unsigned char *data = NULL;
Atom _XROOTPMAP_ID = XInternAtom(display, "_XROOTPMAP_ID", False);
if (XGetWindowProperty(display, *root, _XROOTPMAP_ID, 0, 1, False, XA_PIXMAP, &act_type, &act_format, &nitems, &bytes_after, &data) == Success && data) {
pixmap = *((Pixmap *) data);
XFree(data);
}
return pixmap;
}
static void
writepnm(XImage *img, FILE *outfile)
{
int x, y;
long pixel;
fprintf(outfile, "P6\n%d %d\n255\n", img->width, img->height);
for (y = 0; y < img->height; y++) {
for (x = 0; x < img->width && (pixel = XGetPixel(img, x, y)); x++)
fprintf(outfile, "%c%c%c",
(char)(pixel>>16),
(char)((pixel&0x00ff00)>>8),
(char)(pixel&0x0000ff));
}
}
int
main(int argc, const char *argv[])
{
FILE *outfile;
Window root;
Display *display;
XWindowAttributes wa;
Pixmap bg;
XImage *img = NULL;
if (argc < 2)
die("Usage: %s [filename]\n", argv[0]);
if (!(display = XOpenDisplay(getenv("DISPLAY"))))
die("Cannot connect to X Server %s\n", getenv("DISPLAY") ? getenv("DISPLAY") : "(default)");
root = RootWindow(display, DefaultScreen(display));
XGetWindowAttributes(display, root, &wa);
if ((bg = getrootpixmap(display, &root))) {
img = XGetImage(display, bg, 0, 0, wa.width, wa.height, ~0, ZPixmap);
XFreePixmap(display, bg);
}
if (!img)
die("Cannot create XImage\n");
if (!(outfile = fopen(argv[1], "wb")))
die("Cannot open output file %s", argv[1]);
writepnm(img, outfile);
XDestroyImage(img);
fclose(outfile);
return 0;
}