Debian Codegraph @

src/cacaview.c

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