Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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