1 | /* |
---|
2 | * cacaview image viewer for libcaca |
---|
3 | * Copyright (c) 2003-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: cacaview.c 811 2006-04-18 15:11:25Z sam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | #include "config.h" |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <string.h> |
---|
18 | #include <stdlib.h> |
---|
19 | |
---|
20 | #if defined(HAVE_SLEEP) |
---|
21 | # include <windows.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include "cucul.h" |
---|
25 | #include "caca.h" |
---|
26 | #include "common-image.h" |
---|
27 | |
---|
28 | /* Local macros */ |
---|
29 | #define MODE_IMAGE 1 |
---|
30 | #define MODE_FILES 2 |
---|
31 | |
---|
32 | #define STATUS_DITHERING 1 |
---|
33 | #define STATUS_ANTIALIASING 2 |
---|
34 | #define STATUS_BACKGROUND 3 |
---|
35 | |
---|
36 | #define ZOOM_FACTOR 1.08f |
---|
37 | #define ZOOM_MAX 50 |
---|
38 | #define GAMMA_FACTOR 1.04f |
---|
39 | #define GAMMA_MAX 100 |
---|
40 | #define GAMMA(g) (((g) < 0) ? 1.0 / gammatab[-(g)] : gammatab[(g)]) |
---|
41 | #define PAD_STEP 0.15 |
---|
42 | |
---|
43 | /* libcucul/libcaca contexts */ |
---|
44 | cucul_canvas_t *cv; caca_display_t *dp; |
---|
45 | |
---|
46 | /* Local functions */ |
---|
47 | static void print_status(void); |
---|
48 | static void print_help(int, int); |
---|
49 | static void set_zoom(int); |
---|
50 | static void set_gamma(int); |
---|
51 | static void draw_checkers(int, int, int, int); |
---|
52 | |
---|
53 | /* Local variables */ |
---|
54 | struct image *im = NULL; |
---|
55 | |
---|
56 | float zoomtab[ZOOM_MAX + 1]; |
---|
57 | float gammatab[GAMMA_MAX + 1]; |
---|
58 | float xfactor = 1.0, yfactor = 1.0, dx = 0.5, dy = 0.5; |
---|
59 | int zoom = 0, g = 0, fullscreen = 0, mode, ww, wh; |
---|
60 | |
---|
61 | int main(int argc, char **argv) |
---|
62 | { |
---|
63 | int quit = 0, update = 1, help = 0, status = 0; |
---|
64 | int reload = 0; |
---|
65 | |
---|
66 | char **list = NULL; |
---|
67 | int current = 0, items = 0, opts = 1; |
---|
68 | int i; |
---|
69 | |
---|
70 | /* Initialise libcucul */ |
---|
71 | cv = cucul_create(0, 0); |
---|
72 | if(!cv) |
---|
73 | { |
---|
74 | fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]); |
---|
75 | return 1; |
---|
76 | } |
---|
77 | |
---|
78 | dp = caca_attach(cv); |
---|
79 | if(!dp) |
---|
80 | { |
---|
81 | fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]); |
---|
82 | return 1; |
---|
83 | } |
---|
84 | |
---|
85 | /* Set the window title */ |
---|
86 | caca_set_window_title(dp, "cacaview"); |
---|
87 | |
---|
88 | ww = cucul_get_width(cv); |
---|
89 | wh = cucul_get_height(cv); |
---|
90 | |
---|
91 | /* Fill the zoom table */ |
---|
92 | zoomtab[0] = 1.0; |
---|
93 | for(i = 0; i < ZOOM_MAX; i++) |
---|
94 | zoomtab[i + 1] = zoomtab[i] * ZOOM_FACTOR; |
---|
95 | |
---|
96 | /* Fill the gamma table */ |
---|
97 | gammatab[0] = 1.0; |
---|
98 | for(i = 0; i < GAMMA_MAX; i++) |
---|
99 | gammatab[i + 1] = gammatab[i] * GAMMA_FACTOR; |
---|
100 | |
---|
101 | /* Load items into playlist */ |
---|
102 | for(i = 1; i < argc; i++) |
---|
103 | { |
---|
104 | /* Skip options except after `--' */ |
---|
105 | if(opts && argv[i][0] == '-') |
---|
106 | { |
---|
107 | if(argv[i][1] == '-' && argv[i][2] == '\0') |
---|
108 | opts = 0; |
---|
109 | continue; |
---|
110 | } |
---|
111 | |
---|
112 | /* Add argv[i] to the list */ |
---|
113 | if(items) |
---|
114 | list = realloc(list, (items + 1) * sizeof(char *)); |
---|
115 | else |
---|
116 | list = malloc(sizeof(char *)); |
---|
117 | list[items] = argv[i]; |
---|
118 | items++; |
---|
119 | |
---|
120 | reload = 1; |
---|
121 | } |
---|
122 | |
---|
123 | /* Go ! */ |
---|
124 | while(!quit) |
---|
125 | { |
---|
126 | caca_event_t ev; |
---|
127 | unsigned int const event_mask = CACA_EVENT_KEY_PRESS |
---|
128 | | CACA_EVENT_RESIZE |
---|
129 | | CACA_EVENT_MOUSE_PRESS; |
---|
130 | unsigned int new_status = 0, new_help = 0; |
---|
131 | int event; |
---|
132 | |
---|
133 | if(update) |
---|
134 | event = caca_get_event(dp, event_mask, &ev, 0); |
---|
135 | else |
---|
136 | event = caca_get_event(dp, event_mask, &ev, -1); |
---|
137 | |
---|
138 | while(event) |
---|
139 | { |
---|
140 | if(ev.type & CACA_EVENT_MOUSE_PRESS) |
---|
141 | { |
---|
142 | if(ev.data.mouse.button == 1) |
---|
143 | { |
---|
144 | if(items) current = (current + 1) % items; |
---|
145 | reload = 1; |
---|
146 | } |
---|
147 | else if(ev.data.mouse.button == 2) |
---|
148 | { |
---|
149 | if(items) current = (items + current - 1) % items; |
---|
150 | reload = 1; |
---|
151 | } |
---|
152 | } |
---|
153 | else if(ev.type & CACA_EVENT_KEY_PRESS) switch(ev.data.key.ch) |
---|
154 | { |
---|
155 | case 'n': |
---|
156 | case 'N': |
---|
157 | if(items) current = (current + 1) % items; |
---|
158 | reload = 1; |
---|
159 | break; |
---|
160 | case 'p': |
---|
161 | case 'P': |
---|
162 | if(items) current = (items + current - 1) % items; |
---|
163 | reload = 1; |
---|
164 | break; |
---|
165 | case 'f': |
---|
166 | case 'F': |
---|
167 | fullscreen = ~fullscreen; |
---|
168 | update = 1; |
---|
169 | set_zoom(zoom); |
---|
170 | break; |
---|
171 | #if 0 /* FIXME */ |
---|
172 | case 'b': |
---|
173 | i = 1 + cucul_get_feature(cv, CUCUL_BACKGROUND); |
---|
174 | if(i > CUCUL_BACKGROUND_MAX) i = CUCUL_BACKGROUND_MIN; |
---|
175 | cucul_set_feature(cv, i); |
---|
176 | new_status = STATUS_BACKGROUND; |
---|
177 | update = 1; |
---|
178 | break; |
---|
179 | case 'B': |
---|
180 | i = -1 + cucul_get_feature(cv, CUCUL_BACKGROUND); |
---|
181 | if(i < CUCUL_BACKGROUND_MIN) i = CUCUL_BACKGROUND_MAX; |
---|
182 | cucul_set_feature(cv, i); |
---|
183 | new_status = STATUS_BACKGROUND; |
---|
184 | update = 1; |
---|
185 | break; |
---|
186 | case 'a': |
---|
187 | i = 1 + cucul_get_feature(cv, CUCUL_ANTIALIASING); |
---|
188 | if(i > CUCUL_ANTIALIASING_MAX) i = CUCUL_ANTIALIASING_MIN; |
---|
189 | cucul_set_feature(cv, i); |
---|
190 | new_status = STATUS_ANTIALIASING; |
---|
191 | update = 1; |
---|
192 | break; |
---|
193 | case 'A': |
---|
194 | i = -1 + cucul_get_feature(cv, CUCUL_ANTIALIASING); |
---|
195 | if(i < CUCUL_ANTIALIASING_MIN) i = CUCUL_ANTIALIASING_MAX; |
---|
196 | cucul_set_feature(cv, i); |
---|
197 | new_status = STATUS_ANTIALIASING; |
---|
198 | update = 1; |
---|
199 | break; |
---|
200 | case 'd': |
---|
201 | i = 1 + cucul_get_feature(cv, CUCUL_DITHERING); |
---|
202 | if(i > CUCUL_DITHERING_MAX) i = CUCUL_DITHERING_MIN; |
---|
203 | cucul_set_feature(cv, i); |
---|
204 | new_status = STATUS_DITHERING; |
---|
205 | update = 1; |
---|
206 | break; |
---|
207 | case 'D': |
---|
208 | i = -1 + cucul_get_feature(cv, CUCUL_DITHERING); |
---|
209 | if(i < CUCUL_DITHERING_MIN) i = CUCUL_DITHERING_MAX; |
---|
210 | cucul_set_feature(cv, i); |
---|
211 | new_status = STATUS_DITHERING; |
---|
212 | update = 1; |
---|
213 | break; |
---|
214 | #endif |
---|
215 | case '+': |
---|
216 | update = 1; |
---|
217 | set_zoom(zoom + 1); |
---|
218 | break; |
---|
219 | case '-': |
---|
220 | update = 1; |
---|
221 | set_zoom(zoom - 1); |
---|
222 | break; |
---|
223 | case 'G': |
---|
224 | update = 1; |
---|
225 | set_gamma(g + 1); |
---|
226 | break; |
---|
227 | case 'g': |
---|
228 | update = 1; |
---|
229 | set_gamma(g - 1); |
---|
230 | break; |
---|
231 | case 'x': |
---|
232 | case 'X': |
---|
233 | update = 1; |
---|
234 | set_zoom(0); |
---|
235 | set_gamma(0); |
---|
236 | break; |
---|
237 | case 'k': |
---|
238 | case 'K': |
---|
239 | case CACA_KEY_UP: |
---|
240 | if(yfactor > 1.0) dy -= PAD_STEP / yfactor; |
---|
241 | if(dy < 0.0) dy = 0.0; |
---|
242 | update = 1; |
---|
243 | break; |
---|
244 | case 'j': |
---|
245 | case 'J': |
---|
246 | case CACA_KEY_DOWN: |
---|
247 | if(yfactor > 1.0) dy += PAD_STEP / yfactor; |
---|
248 | if(dy > 1.0) dy = 1.0; |
---|
249 | update = 1; |
---|
250 | break; |
---|
251 | case 'h': |
---|
252 | case 'H': |
---|
253 | case CACA_KEY_LEFT: |
---|
254 | if(xfactor > 1.0) dx -= PAD_STEP / xfactor; |
---|
255 | if(dx < 0.0) dx = 0.0; |
---|
256 | update = 1; |
---|
257 | break; |
---|
258 | case 'l': |
---|
259 | case 'L': |
---|
260 | case CACA_KEY_RIGHT: |
---|
261 | if(xfactor > 1.0) dx += PAD_STEP / xfactor; |
---|
262 | if(dx > 1.0) dx = 1.0; |
---|
263 | update = 1; |
---|
264 | break; |
---|
265 | case '?': |
---|
266 | new_help = !help; |
---|
267 | update = 1; |
---|
268 | break; |
---|
269 | case 'q': |
---|
270 | case 'Q': |
---|
271 | quit = 1; |
---|
272 | break; |
---|
273 | } |
---|
274 | else if(ev.type == CACA_EVENT_RESIZE) |
---|
275 | { |
---|
276 | caca_display(dp); |
---|
277 | ww = ev.data.resize.w; |
---|
278 | wh = ev.data.resize.h; |
---|
279 | update = 1; |
---|
280 | set_zoom(zoom); |
---|
281 | } |
---|
282 | |
---|
283 | if(status || new_status) |
---|
284 | status = new_status; |
---|
285 | |
---|
286 | if(help || new_help) |
---|
287 | help = new_help; |
---|
288 | |
---|
289 | event = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0); |
---|
290 | } |
---|
291 | |
---|
292 | if(items && reload) |
---|
293 | { |
---|
294 | char *buffer; |
---|
295 | int len = strlen(" Loading `%s'... ") + strlen(list[current]); |
---|
296 | |
---|
297 | if(len < ww + 1) |
---|
298 | len = ww + 1; |
---|
299 | |
---|
300 | buffer = malloc(len); |
---|
301 | |
---|
302 | sprintf(buffer, " Loading `%s'... ", list[current]); |
---|
303 | buffer[ww] = '\0'; |
---|
304 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
305 | cucul_putstr(cv, (ww - strlen(buffer)) / 2, wh / 2, buffer); |
---|
306 | caca_display(dp); |
---|
307 | ww = cucul_get_width(cv); |
---|
308 | wh = cucul_get_height(cv); |
---|
309 | |
---|
310 | if(im) |
---|
311 | unload_image(im); |
---|
312 | im = load_image(list[current]); |
---|
313 | reload = 0; |
---|
314 | |
---|
315 | /* Reset image-specific runtime variables */ |
---|
316 | dx = dy = 0.5; |
---|
317 | update = 1; |
---|
318 | set_zoom(0); |
---|
319 | set_gamma(0); |
---|
320 | |
---|
321 | free(buffer); |
---|
322 | } |
---|
323 | |
---|
324 | cucul_clear(cv); |
---|
325 | |
---|
326 | if(!items) |
---|
327 | { |
---|
328 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
329 | cucul_printf(cv, ww / 2 - 5, wh / 2, " No image. "); |
---|
330 | } |
---|
331 | else if(!im) |
---|
332 | { |
---|
333 | #if defined(HAVE_IMLIB2_H) |
---|
334 | # define ERROR_STRING " Error loading `%s'. " |
---|
335 | #else |
---|
336 | # define ERROR_STRING " Error loading `%s'. Only BMP is supported. " |
---|
337 | #endif |
---|
338 | char *buffer; |
---|
339 | int len = strlen(ERROR_STRING) + strlen(list[current]); |
---|
340 | |
---|
341 | if(len < ww + 1) |
---|
342 | len = ww + 1; |
---|
343 | |
---|
344 | buffer = malloc(len); |
---|
345 | |
---|
346 | sprintf(buffer, ERROR_STRING, list[current]); |
---|
347 | buffer[ww] = '\0'; |
---|
348 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
349 | cucul_putstr(cv, (ww - strlen(buffer)) / 2, wh / 2, buffer); |
---|
350 | free(buffer); |
---|
351 | } |
---|
352 | else |
---|
353 | { |
---|
354 | float xdelta, ydelta; |
---|
355 | int y, height; |
---|
356 | |
---|
357 | y = fullscreen ? 0 : 1; |
---|
358 | height = fullscreen ? wh : wh - 3; |
---|
359 | |
---|
360 | xdelta = (xfactor > 1.0) ? dx : 0.5; |
---|
361 | ydelta = (yfactor > 1.0) ? dy : 0.5; |
---|
362 | |
---|
363 | draw_checkers(ww * (1.0 - xfactor) / 2, |
---|
364 | y + height * (1.0 - yfactor) / 2, |
---|
365 | ww * (1.0 + xfactor) / 2, |
---|
366 | y + height * (1.0 + yfactor) / 2); |
---|
367 | |
---|
368 | cucul_dither_bitmap(cv, ww * (1.0 - xfactor) * xdelta, |
---|
369 | y + height * (1.0 - yfactor) * ydelta, |
---|
370 | ww * (xdelta + (1.0 - xdelta) * xfactor), |
---|
371 | y + height * (ydelta + (1.0 - ydelta) * yfactor), |
---|
372 | im->dither, im->pixels); |
---|
373 | } |
---|
374 | |
---|
375 | if(!fullscreen) |
---|
376 | { |
---|
377 | print_status(); |
---|
378 | |
---|
379 | #if 0 /* FIXME */ |
---|
380 | cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); |
---|
381 | switch(status) |
---|
382 | { |
---|
383 | case STATUS_ANTIALIASING: |
---|
384 | cucul_printf(cv, 0, wh - 1, "Antialiasing: %s", |
---|
385 | cucul_get_feature_name(cucul_get_feature(cv, CUCUL_ANTIALIASING))); |
---|
386 | break; |
---|
387 | case STATUS_DITHERING: |
---|
388 | cucul_printf(cv, 0, wh - 1, "Dithering: %s", |
---|
389 | cucul_get_feature_name(cucul_get_feature(cv, CUCUL_DITHERING))); |
---|
390 | break; |
---|
391 | case STATUS_BACKGROUND: |
---|
392 | cucul_printf(cv, 0, wh - 1, "Background: %s", |
---|
393 | cucul_get_feature_name(cucul_get_feature(cv, CUCUL_BACKGROUND))); |
---|
394 | break; |
---|
395 | } |
---|
396 | #endif |
---|
397 | } |
---|
398 | |
---|
399 | if(help) |
---|
400 | { |
---|
401 | print_help(ww - 26, 2); |
---|
402 | } |
---|
403 | |
---|
404 | caca_display(dp); |
---|
405 | update = 0; |
---|
406 | } |
---|
407 | |
---|
408 | /* Clean up */ |
---|
409 | if(im) |
---|
410 | unload_image(im); |
---|
411 | caca_detach(dp); |
---|
412 | cucul_free(cv); |
---|
413 | |
---|
414 | return 0; |
---|
415 | } |
---|
416 | |
---|
417 | static void print_status(void) |
---|
418 | { |
---|
419 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
420 | cucul_draw_line(cv, 0, 0, ww - 1, 0, " "); |
---|
421 | cucul_draw_line(cv, 0, wh - 2, ww - 1, wh - 2, "-"); |
---|
422 | cucul_putstr(cv, 0, 0, "q:Quit np:Next/Prev +-x:Zoom gG:Gamma " |
---|
423 | "hjkl:Move d:Dither a:Antialias"); |
---|
424 | cucul_putstr(cv, ww - strlen("?:Help"), 0, "?:Help"); |
---|
425 | cucul_printf(cv, 3, wh - 2, "cacaview %s", VERSION); |
---|
426 | cucul_printf(cv, ww - 30, wh - 2, "(gamma: %#.3g)", GAMMA(g)); |
---|
427 | cucul_printf(cv, ww - 14, wh - 2, "(zoom: %s%i)", zoom > 0 ? "+" : "", zoom); |
---|
428 | |
---|
429 | cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); |
---|
430 | cucul_draw_line(cv, 0, wh - 1, ww - 1, wh - 1, " "); |
---|
431 | } |
---|
432 | |
---|
433 | static void print_help(int x, int y) |
---|
434 | { |
---|
435 | static char const *help[] = |
---|
436 | { |
---|
437 | " +: zoom in ", |
---|
438 | " -: zoom out ", |
---|
439 | " g: decrease gamma ", |
---|
440 | " G: increase gamma ", |
---|
441 | " x: reset zoom and gamma ", |
---|
442 | " ----------------------- ", |
---|
443 | " hjkl: move view ", |
---|
444 | " arrows: move view ", |
---|
445 | " ----------------------- ", |
---|
446 | " a: antialiasing method ", |
---|
447 | " d: dithering method ", |
---|
448 | " b: background mode ", |
---|
449 | " ----------------------- ", |
---|
450 | " ?: help ", |
---|
451 | " q: quit ", |
---|
452 | NULL |
---|
453 | }; |
---|
454 | |
---|
455 | int i; |
---|
456 | |
---|
457 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
458 | |
---|
459 | for(i = 0; help[i]; i++) |
---|
460 | cucul_putstr(cv, x, y + i, help[i]); |
---|
461 | } |
---|
462 | |
---|
463 | static void set_zoom(int new_zoom) |
---|
464 | { |
---|
465 | int height; |
---|
466 | |
---|
467 | if(!im) |
---|
468 | return; |
---|
469 | |
---|
470 | zoom = new_zoom; |
---|
471 | |
---|
472 | if(zoom > ZOOM_MAX) zoom = ZOOM_MAX; |
---|
473 | if(zoom < -ZOOM_MAX) zoom = -ZOOM_MAX; |
---|
474 | |
---|
475 | ww = cucul_get_width(cv); |
---|
476 | height = fullscreen ? wh : wh - 3; |
---|
477 | |
---|
478 | xfactor = (zoom < 0) ? 1.0 / zoomtab[-zoom] : zoomtab[zoom]; |
---|
479 | yfactor = xfactor * ww / height * im->h / im->w |
---|
480 | * cucul_get_height(cv) / cucul_get_width(cv) |
---|
481 | * caca_get_window_width(dp) / caca_get_window_height(dp); |
---|
482 | |
---|
483 | if(yfactor > xfactor) |
---|
484 | { |
---|
485 | float tmp = xfactor; |
---|
486 | xfactor = tmp * tmp / yfactor; |
---|
487 | yfactor = tmp; |
---|
488 | } |
---|
489 | } |
---|
490 | |
---|
491 | static void set_gamma(int new_gamma) |
---|
492 | { |
---|
493 | if(!im) |
---|
494 | return; |
---|
495 | |
---|
496 | g = new_gamma; |
---|
497 | |
---|
498 | if(g > GAMMA_MAX) g = GAMMA_MAX; |
---|
499 | if(g < -GAMMA_MAX) g = -GAMMA_MAX; |
---|
500 | |
---|
501 | cucul_set_dither_gamma(im->dither, |
---|
502 | (g < 0) ? 1.0 / gammatab[-g] : gammatab[g]); |
---|
503 | } |
---|
504 | |
---|
505 | static void draw_checkers(int x1, int y1, int x2, int y2) |
---|
506 | { |
---|
507 | int xn, yn; |
---|
508 | |
---|
509 | if(x2 + 1 > (int)cucul_get_width(cv)) x2 = cucul_get_width(cv) - 1; |
---|
510 | if(y2 + 1 > (int)cucul_get_height(cv)) y2 = cucul_get_height(cv) - 1; |
---|
511 | |
---|
512 | for(yn = y1 > 0 ? y1 : 0; yn <= y2; yn++) |
---|
513 | for(xn = x1 > 0 ? x1 : 0; xn <= x2; xn++) |
---|
514 | { |
---|
515 | if((((xn - x1) / 5) ^ ((yn - y1) / 3)) & 1) |
---|
516 | cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_DARKGRAY); |
---|
517 | else |
---|
518 | cucul_set_color(cv, CUCUL_COLOR_DARKGRAY, CUCUL_COLOR_LIGHTGRAY); |
---|
519 | cucul_putchar(cv, xn, yn, ' '); |
---|
520 | } |
---|
521 | } |
---|
522 | |
---|