+ gitignore
[hateecw.git] / hateecw.c
blobc9e3861faee0bdd18aad066c47878bbcd948137b
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <assert.h>
5 #include <stdbool.h>
7 #include <X11/Xlib.h>
8 #include <X11/Xutil.h>
10 #include <NCSEcw.h>
11 #include <NCSECWClient.h>
12 #include <NCSErrors.h>
14 uint_fast8_t const bands = 3;
15 Display *D;
16 int S;
17 Window W;
18 GC Gc;
19 uint_fast16_t WX = 200, WY = 200;
21 #define NCS(wut) \
22 e = wut; \
23 if (e != NCS_SUCCESS) { \
24 printf("Error = %s\n", NCSGetErrorText(e)); \
27 NCSEcwReadStatus showcb(NCSFileView *fw) {
28 NCSError e;
29 NCSFileViewSetInfo *vi;
31 NCS(NCScbmGetViewInfo(fw, &vi));
33 //printf("X %i Y %i avblks %i avblk %i mblk %i vblk %i\n", vi->nSizeX, vi->nSizeY, vi->nBlocksAvailableAtSetView, vi->nBlocksAvailable, vi->nMissedBlocksDuringRead, vi->nBlocksInView);
34 uint_fast32_t bpl = vi->nSizeX * 4;
35 uint8_t *img = malloc(bands * vi->nSizeY * bpl);
36 assert(img);
38 uint8_t *img_ = img, *img__ = malloc(bands * bpl);
39 for (uint_fast32_t y = vi->nSizeY; y != 0; y--) {
40 NCSEcwReadStatus s = NCScbmReadViewLineBGR(fw, img__);
41 assert(s == 0);
42 for (uint_fast32_t x = 0; x < vi->nSizeX; x++) {
43 img_[x * 4] = img__[x * 3];
44 img_[x * 4 + 1] = img__[x * 3 + 1];
45 img_[x * 4 + 2] = img__[x * 3 + 2];
47 //printf("status %i\n", s);
49 img_ += bpl;
51 free(img__);
53 //printf("%i %i %i\n", DefaultDepth(D, S), WX, WY);
54 XLockDisplay(D);
55 XImage *xi = XCreateImage(D, DefaultVisual(D, S), DefaultDepth(D, S), ZPixmap, 0, (void *)img, vi->nSizeX, vi->nSizeY, 32, bpl);
56 assert(xi);
57 XPutImage(D, W, Gc, xi, 0, 0, 0, 0, WX, WY);
58 XFlush(D);
59 XDestroyImage(xi);
60 XUnlockDisplay(D);
61 return NCSECW_READ_OK;
64 int main(int argc, char *argv[]) {
65 if (argc < 2) return 1;
67 XInitThreads();
68 D = XOpenDisplay(NULL);
69 assert(D);
70 S = DefaultScreen(D);
71 W = XCreateSimpleWindow(D, RootWindow(D, S), 10, 10, WX, WY, 1,
72 BlackPixel(D, S), WhitePixel(D, S));
73 Gc = DefaultGC(D, S); //XCreateGC(D, W, 0, 0);
74 assert(Gc);
75 XSelectInput(D, W, ExposureMask | KeyPressMask | StructureNotifyMask);
76 XMapWindow(D, W);
78 NCSecwInit();
79 NCSError e;
80 NCSFileView *fw;
81 NCSFileViewFileInfo *fi;
82 NCSFileViewSetInfo *si;
83 NCS(NCScbmOpenFileView(argv[1], &fw, showcb));
84 NCS(NCScbmGetViewFileInfo(fw, &fi));
85 printf("%i bands\n", fi->nBands);
86 NCS(NCScbmGetViewInfo(fw, &si));
87 uint32_t *bl = malloc(4 * bands);
88 for (uint_fast8_t i = 0; i < bands; i++)
89 bl[i] = i;
91 uint_fast32_t x = 0, y = 0, w = fi->nSizeX - 1, h = fi->nSizeY - 1;
92 // NCS(NCScbmSetFileView(fw, 3, bl, x, y, w, h, WX, WY));
94 bool run = true;
95 while (run) {
96 XEvent ev;
97 XNextEvent(D, &ev);
98 switch (ev.type) {
99 case Expose:
100 // XFillRectangle(D, W, DefaultGC(D, S), 20, 20, 10, 10);
101 break;
102 case ConfigureNotify:
103 WX = ev.xconfigure.width;
104 WY = ev.xconfigure.height;
105 //NCS(NCScbmSetFileView(fw, bands, bl, x, y, w, h, WX, WY));
106 break;
107 case KeyPress: {
108 XLockDisplay(D);
109 KeySym ks = XKeycodeToKeysym(D, (KeyCode)ev.xkey.keycode, 0);
110 XUnlockDisplay(D);
111 int_fast32_t t;
112 switch (ks) {
113 case XK_Up:
114 t = y - h / 4;
115 y = t;
116 break;
117 case XK_Down:
118 t = y + h / 4;
119 y = t;
120 break;
121 case XK_Left:
122 t = x - w / 4;
123 x = t;
124 break;
125 case XK_Right:
126 t = x + w / 4;
127 x = t;
128 break;
129 case XK_plus:
130 case XK_equal:
131 w = w * 2 / 3;
132 h = h * 2 / 3;
133 x += w / 3;
134 y += h / 3;
135 printf("zoom %lf%%\n", 100.0 * w / fi->nSizeX);
136 break;
137 case XK_minus:
138 x -= w / 3;
139 y -= h / 3;
140 w = w * 3 / 2;
141 h = h * 3 / 2;
142 printf("zoom %lf%%\n", 100.0 * (w + 1) / fi->nSizeX);
143 break;
144 case 'i':
145 printf("zoom %lf%%\n", 100.0 * (w + 1) / fi->nSizeX);
146 break;
147 case 'q':
148 run = false;
149 break;
150 default:
151 printf("keysym %lx\n", ks);
153 NCS(NCScbmSetFileView(fw, bands, bl, x, y, w + x, h + y, WX, WY));
155 break;
156 default:
157 break;
161 XLockDisplay(D);
162 XFreeGC(D, Gc);
163 XCloseDisplay(D);
165 return 0;