-
Notifications
You must be signed in to change notification settings - Fork 0
/
dqoi.c
136 lines (105 loc) · 2.94 KB
/
dqoi.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
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
/*
* Copyright (c) 2022 Brian Callahan <bcallah@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "qoi.h"
int
main(int argc, char *argv[])
{
Display *disp;
XVisualInfo vinfo;
Window win;
XSetWindowAttributes attr;
GC gc;
XImage *ximg;
XEvent e;
qoi_desc desc;
uint64_t i;
uint32_t *pixels;
int ac = 1;
if (argc < 2) {
(void) fputs("usage: dqoi file.qoi [...]\n", stderr);
return 1;
}
again:
if ((pixels = qoi_read(argv[ac], &desc, 4)) == NULL) {
(void) fprintf(stderr, "dqoi: error: read failed: %s\n",
argv[ac]);
return 1;
}
if (desc.width == 0 || desc.height == 0) {
(void) fprintf(stderr, "dqoi: error: invalid image: %s\n",
argv[ac]);
return 1;
}
for (i = 0; i < desc.width * desc.height; i++) {
pixels[i] = (pixels[i] >> 24) << 24 |
(pixels[i] & 0xff) << 16 |
((pixels[i] >> 8) & 0xff) << 8 |
((pixels[i] >> 16) & 0xff);
}
if ((disp = XOpenDisplay(NULL)) == NULL) {
(void) fputs("dqoi: error: could not open display\n", stderr);
return 1;
}
XMatchVisualInfo(disp, DefaultScreen(disp), 32, TrueColor, &vinfo);
attr.colormap = XCreateColormap(disp, DefaultRootWindow(disp),
vinfo.visual, AllocNone);
attr.background_pixel = None;
attr.border_pixel = None;
win = XCreateWindow(disp, DefaultRootWindow(disp), 0, 0, desc.width,
desc.height, 0, vinfo.depth, InputOutput, vinfo.visual,
CWColormap | CWBackPixel | CWBorderPixel, &attr);
XStoreName(disp, win, argv[ac]);
gc = XCreateGC(disp, win, 0, 0);
ximg = XCreateImage(disp, vinfo.visual, vinfo.depth, ZPixmap, 0,
(char *) pixels, desc.width, desc.height, 32, 0);
XSelectInput(disp, win, ExposureMask | KeyPressMask);
XMapWindow(disp, win);
for (;;) {
XNextEvent(disp, &e);
if (e.type == Expose) {
XPutImage(disp, win, gc, ximg, 0, 0, 0, 0, desc.width,
desc.height);
}
if (e.type == KeyPress) {
switch (e.xkey.keycode) {
case 0x64:
if (ac == 1)
continue;
break;
case 0x66:
if (ac == argc - 1)
continue;
}
break;
}
}
XCloseDisplay(disp);
free(pixels);
switch (e.xkey.keycode) {
case 0x64:
--ac;
goto again;
case 0x66:
++ac;
goto again;
}
return 0;
}