Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

536 righe
15 KiB

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