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