您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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