You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

739 line
20 KiB

  1. /*
  2. * cacaview image viewer for libcaca
  3. * Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #if defined(HAVE_IMLIB2_H)
  18. # include <Imlib2.h>
  19. #else
  20. # include <stdio.h>
  21. #endif
  22. #if defined(HAVE_SLEEP)
  23. # include <windows.h>
  24. #endif
  25. #include "cucul.h"
  26. #include "caca.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 PAD_STEP 0.15
  36. /* libcucul/libcaca contexts */
  37. cucul_t *qq; caca_t *kk;
  38. /* Local functions */
  39. static void print_status(void);
  40. static void print_help(int, int);
  41. static void set_zoom(int);
  42. static void load_image(char const *);
  43. static void unload_image(void);
  44. static void draw_checkers(int, int, int, int);
  45. #if !defined(HAVE_IMLIB2_H)
  46. static int freadint(FILE *);
  47. static int freadshort(FILE *);
  48. static int freadchar(FILE *);
  49. #endif
  50. /* Local variables */
  51. #if defined(HAVE_IMLIB2_H)
  52. Imlib_Image image = NULL;
  53. #endif
  54. char *pixels = NULL;
  55. struct cucul_bitmap *bitmap = NULL;
  56. unsigned int w, h, depth, bpp, rmask, gmask, bmask, amask;
  57. #if !defined(HAVE_IMLIB2_H)
  58. unsigned int red[256], green[256], blue[256], alpha[256];
  59. #endif
  60. float zoomtab[ZOOM_MAX + 1];
  61. float xfactor = 1.0, yfactor = 1.0, dx = 0.5, dy = 0.5;
  62. int zoom = 0, fullscreen = 0, mode, ww, wh;
  63. int main(int argc, char **argv)
  64. {
  65. int quit = 0, update = 1, help = 0, status = 0;
  66. int reload = 0;
  67. char **list = NULL;
  68. int current = 0, items = 0, opts = 1;
  69. int i;
  70. /* Initialise libcucul */
  71. qq = cucul_init();
  72. if(!qq)
  73. {
  74. fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]);
  75. return 1;
  76. }
  77. kk = caca_attach(qq);
  78. if(!kk)
  79. {
  80. fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
  81. return 1;
  82. }
  83. /* Set the window title */
  84. caca_set_window_title(kk, "cacaview");
  85. ww = cucul_get_width(qq);
  86. wh = cucul_get_height(qq);
  87. /* Fill the zoom table */
  88. zoomtab[0] = 1.0;
  89. for(i = 0; i < ZOOM_MAX; i++)
  90. zoomtab[i + 1] = zoomtab[i] * 1.08;
  91. /* Load items into playlist */
  92. for(i = 1; i < argc; i++)
  93. {
  94. /* Skip options except after `--' */
  95. if(opts && argv[i][0] == '-')
  96. {
  97. if(argv[i][1] == '-' && argv[i][2] == '\0')
  98. opts = 0;
  99. continue;
  100. }
  101. /* Add argv[i] to the list */
  102. if(items)
  103. list = realloc(list, (items + 1) * sizeof(char *));
  104. else
  105. list = malloc(sizeof(char *));
  106. list[items] = argv[i];
  107. items++;
  108. reload = 1;
  109. }
  110. /* Go ! */
  111. while(!quit)
  112. {
  113. unsigned int const event_mask = CACA_EVENT_KEY_PRESS
  114. | CACA_EVENT_RESIZE
  115. | CACA_EVENT_MOUSE_PRESS;
  116. unsigned int event, new_status = 0, new_help = 0;
  117. if(update)
  118. event = caca_get_event(kk, event_mask);
  119. else
  120. event = caca_wait_event(kk, event_mask);
  121. while(event)
  122. {
  123. if(event & CACA_EVENT_MOUSE_PRESS)
  124. {
  125. if((event & 0x00ffffff) == 1)
  126. {
  127. if(items) current = (current + 1) % items;
  128. reload = 1;
  129. }
  130. else if((event & 0x00ffffff) == 2)
  131. {
  132. if(items) current = (items + current - 1) % items;
  133. reload = 1;
  134. }
  135. }
  136. else if(event & CACA_EVENT_KEY_PRESS) switch(event & 0x00ffffff)
  137. {
  138. case 'n':
  139. case 'N':
  140. if(items) current = (current + 1) % items;
  141. reload = 1;
  142. break;
  143. case 'p':
  144. case 'P':
  145. if(items) current = (items + current - 1) % items;
  146. reload = 1;
  147. break;
  148. case 'f':
  149. case 'F':
  150. fullscreen = ~fullscreen;
  151. update = 1;
  152. set_zoom(zoom);
  153. break;
  154. case 'b':
  155. i = 1 + cucul_get_feature(qq, CUCUL_BACKGROUND);
  156. if(i > CUCUL_BACKGROUND_MAX) i = CUCUL_BACKGROUND_MIN;
  157. cucul_set_feature(qq, i);
  158. new_status = STATUS_BACKGROUND;
  159. update = 1;
  160. break;
  161. case 'B':
  162. i = -1 + cucul_get_feature(qq, CUCUL_BACKGROUND);
  163. if(i < CUCUL_BACKGROUND_MIN) i = CUCUL_BACKGROUND_MAX;
  164. cucul_set_feature(qq, i);
  165. new_status = STATUS_BACKGROUND;
  166. update = 1;
  167. break;
  168. case 'a':
  169. i = 1 + cucul_get_feature(qq, CUCUL_ANTIALIASING);
  170. if(i > CUCUL_ANTIALIASING_MAX) i = CUCUL_ANTIALIASING_MIN;
  171. cucul_set_feature(qq, i);
  172. new_status = STATUS_ANTIALIASING;
  173. update = 1;
  174. break;
  175. case 'A':
  176. i = -1 + cucul_get_feature(qq, CUCUL_ANTIALIASING);
  177. if(i < CUCUL_ANTIALIASING_MIN) i = CUCUL_ANTIALIASING_MAX;
  178. cucul_set_feature(qq, i);
  179. new_status = STATUS_ANTIALIASING;
  180. update = 1;
  181. break;
  182. case 'd':
  183. i = 1 + cucul_get_feature(qq, CUCUL_DITHERING);
  184. if(i > CUCUL_DITHERING_MAX) i = CUCUL_DITHERING_MIN;
  185. cucul_set_feature(qq, i);
  186. new_status = STATUS_DITHERING;
  187. update = 1;
  188. break;
  189. case 'D':
  190. i = -1 + cucul_get_feature(qq, CUCUL_DITHERING);
  191. if(i < CUCUL_DITHERING_MIN) i = CUCUL_DITHERING_MAX;
  192. cucul_set_feature(qq, i);
  193. new_status = STATUS_DITHERING;
  194. update = 1;
  195. break;
  196. case '+':
  197. update = 1;
  198. set_zoom(zoom + 1);
  199. break;
  200. case '-':
  201. update = 1;
  202. set_zoom(zoom - 1);
  203. break;
  204. case 'x':
  205. case 'X':
  206. update = 1;
  207. set_zoom(0);
  208. break;
  209. case 'k':
  210. case 'K':
  211. case CACA_KEY_UP:
  212. if(yfactor > 1.0) dy -= PAD_STEP / yfactor;
  213. if(dy < 0.0) dy = 0.0;
  214. update = 1;
  215. break;
  216. case 'j':
  217. case 'J':
  218. case CACA_KEY_DOWN:
  219. if(yfactor > 1.0) dy += PAD_STEP / yfactor;
  220. if(dy > 1.0) dy = 1.0;
  221. update = 1;
  222. break;
  223. case 'h':
  224. case 'H':
  225. case CACA_KEY_LEFT:
  226. if(xfactor > 1.0) dx -= PAD_STEP / xfactor;
  227. if(dx < 0.0) dx = 0.0;
  228. update = 1;
  229. break;
  230. case 'l':
  231. case 'L':
  232. case CACA_KEY_RIGHT:
  233. if(xfactor > 1.0) dx += PAD_STEP / xfactor;
  234. if(dx > 1.0) dx = 1.0;
  235. update = 1;
  236. break;
  237. case '?':
  238. new_help = !help;
  239. update = 1;
  240. break;
  241. case 'q':
  242. case 'Q':
  243. quit = 1;
  244. break;
  245. }
  246. else if(event == CACA_EVENT_RESIZE)
  247. {
  248. caca_refresh(kk);
  249. ww = cucul_get_width(qq);
  250. wh = cucul_get_height(qq);
  251. update = 1;
  252. set_zoom(zoom);
  253. }
  254. if(status || new_status)
  255. status = new_status;
  256. if(help || new_help)
  257. help = new_help;
  258. event = caca_get_event(kk, CACA_EVENT_KEY_PRESS);
  259. }
  260. if(items && reload)
  261. {
  262. char *buffer;
  263. int len = strlen(" Loading `%s'... ") + strlen(list[current]);
  264. if(len < ww + 1)
  265. len = ww + 1;
  266. buffer = malloc(len);
  267. sprintf(buffer, " Loading `%s'... ", list[current]);
  268. buffer[ww] = '\0';
  269. cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  270. cucul_putstr(qq, (ww - strlen(buffer)) / 2, wh / 2, buffer);
  271. caca_refresh(kk);
  272. ww = cucul_get_width(qq);
  273. wh = cucul_get_height(qq);
  274. unload_image();
  275. load_image(list[current]);
  276. reload = 0;
  277. /* Reset image-specific runtime variables */
  278. dx = dy = 0.5;
  279. update = 1;
  280. set_zoom(0);
  281. free(buffer);
  282. }
  283. cucul_clear(qq);
  284. if(!items)
  285. {
  286. cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  287. cucul_printf(qq, ww / 2 - 5, wh / 2, " No image. ");
  288. }
  289. else if(!pixels)
  290. {
  291. #if defined(HAVE_IMLIB2_H)
  292. # define ERROR_STRING " Error loading `%s'. "
  293. #else
  294. # define ERROR_STRING " Error loading `%s'. Only BMP is supported. "
  295. #endif
  296. char *buffer;
  297. int len = strlen(ERROR_STRING) + strlen(list[current]);
  298. if(len < ww + 1)
  299. len = ww + 1;
  300. buffer = malloc(len);
  301. sprintf(buffer, ERROR_STRING, list[current]);
  302. buffer[ww] = '\0';
  303. cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  304. cucul_putstr(qq, (ww - strlen(buffer)) / 2, wh / 2, buffer);
  305. free(buffer);
  306. }
  307. else
  308. {
  309. float xdelta, ydelta;
  310. int y, height;
  311. y = fullscreen ? 0 : 1;
  312. height = fullscreen ? wh : wh - 3;
  313. xdelta = (xfactor > 1.0) ? dx : 0.5;
  314. ydelta = (yfactor > 1.0) ? dy : 0.5;
  315. draw_checkers(ww * (1.0 - xfactor) / 2,
  316. y + height * (1.0 - yfactor) / 2,
  317. ww * (1.0 + xfactor) / 2,
  318. y + height * (1.0 + yfactor) / 2);
  319. cucul_draw_bitmap(qq, ww * (1.0 - xfactor) * xdelta,
  320. y + height * (1.0 - yfactor) * ydelta,
  321. ww * (xdelta + (1.0 - xdelta) * xfactor),
  322. y + height * (ydelta + (1.0 - ydelta) * yfactor),
  323. bitmap, pixels);
  324. }
  325. if(!fullscreen)
  326. {
  327. print_status();
  328. cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  329. switch(status)
  330. {
  331. case STATUS_ANTIALIASING:
  332. cucul_printf(qq, 0, wh - 1, "Antialiasing: %s",
  333. cucul_get_feature_name(cucul_get_feature(qq, CUCUL_ANTIALIASING)));
  334. break;
  335. case STATUS_DITHERING:
  336. cucul_printf(qq, 0, wh - 1, "Dithering: %s",
  337. cucul_get_feature_name(cucul_get_feature(qq, CUCUL_DITHERING)));
  338. break;
  339. case STATUS_BACKGROUND:
  340. cucul_printf(qq, 0, wh - 1, "Background: %s",
  341. cucul_get_feature_name(cucul_get_feature(qq, CUCUL_BACKGROUND)));
  342. break;
  343. }
  344. }
  345. if(help)
  346. {
  347. print_help(ww - 25, 2);
  348. }
  349. caca_refresh(kk);
  350. update = 0;
  351. }
  352. /* Clean up */
  353. unload_image();
  354. caca_detach(kk);
  355. cucul_end(qq);
  356. return 0;
  357. }
  358. static void print_status(void)
  359. {
  360. cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  361. cucul_draw_line(qq, 0, 0, ww - 1, 0, ' ');
  362. cucul_draw_line(qq, 0, wh - 2, ww - 1, wh - 2, '-');
  363. cucul_putstr(qq, 0, 0, "q:Quit np:Next/Prev +-x:Zoom "
  364. "hjkl:Move d:Dithering a:Antialias");
  365. cucul_putstr(qq, ww - strlen("?:Help"), 0, "?:Help");
  366. cucul_printf(qq, 3, wh - 2, "cacaview %s", VERSION);
  367. cucul_printf(qq, ww - 14, wh - 2, "(zoom: %s%i)", zoom > 0 ? "+" : "", zoom);
  368. cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  369. cucul_draw_line(qq, 0, wh - 1, ww - 1, wh - 1, ' ');
  370. }
  371. static void print_help(int x, int y)
  372. {
  373. static char const *help[] =
  374. {
  375. " +: zoom in ",
  376. " -: zoom out ",
  377. " x: reset zoom ",
  378. " ---------------------- ",
  379. " hjkl: move view ",
  380. " arrows: move view ",
  381. " ---------------------- ",
  382. " a: antialiasing method ",
  383. " d: dithering method ",
  384. " b: background mode ",
  385. " ---------------------- ",
  386. " ?: help ",
  387. " q: quit ",
  388. NULL
  389. };
  390. int i;
  391. cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  392. for(i = 0; help[i]; i++)
  393. cucul_putstr(qq, x, y + i, help[i]);
  394. }
  395. static void set_zoom(int new_zoom)
  396. {
  397. int height;
  398. zoom = new_zoom;
  399. if(zoom > ZOOM_MAX) zoom = ZOOM_MAX;
  400. if(zoom < -ZOOM_MAX) zoom = -ZOOM_MAX;
  401. ww = cucul_get_width(qq);
  402. height = fullscreen ? wh : wh - 3;
  403. xfactor = (zoom < 0) ? 1.0 / zoomtab[-zoom] : zoomtab[zoom];
  404. yfactor = xfactor * ww / height * h / w
  405. * cucul_get_height(qq) / cucul_get_width(qq)
  406. * caca_get_window_width(kk) / caca_get_window_height(kk);
  407. if(yfactor > xfactor)
  408. {
  409. float tmp = xfactor;
  410. xfactor = tmp * tmp / yfactor;
  411. yfactor = tmp;
  412. }
  413. }
  414. static void unload_image(void)
  415. {
  416. #if defined(HAVE_IMLIB2_H)
  417. if(image)
  418. imlib_free_image();
  419. image = NULL;
  420. pixels = NULL;
  421. #else
  422. if(pixels)
  423. free(pixels);
  424. pixels = NULL;
  425. #endif
  426. if(bitmap)
  427. cucul_free_bitmap(qq, bitmap);
  428. bitmap = NULL;
  429. }
  430. static void load_image(char const *name)
  431. {
  432. #if defined(HAVE_IMLIB2_H)
  433. /* Load the new image */
  434. image = imlib_load_image(name);
  435. if(!image)
  436. return;
  437. imlib_context_set_image(image);
  438. pixels = (char *)imlib_image_get_data_for_reading_only();
  439. w = imlib_image_get_width();
  440. h = imlib_image_get_height();
  441. rmask = 0x00ff0000;
  442. gmask = 0x0000ff00;
  443. bmask = 0x000000ff;
  444. amask = 0xff000000;
  445. bpp = 32;
  446. depth = 4;
  447. /* Create the libcucul bitmap */
  448. bitmap = cucul_create_bitmap(qq, bpp, w, h, depth * w,
  449. rmask, gmask, bmask, amask);
  450. if(!bitmap)
  451. {
  452. imlib_free_image();
  453. image = NULL;
  454. }
  455. #else
  456. /* Try to load a BMP file */
  457. FILE *fp;
  458. unsigned int i, colors, offset, tmp, planes;
  459. fp = fopen(name, "rb");
  460. if(!fp)
  461. return;
  462. if(freadshort(fp) != 0x4d42)
  463. {
  464. fclose(fp);
  465. return;
  466. }
  467. freadint(fp); /* size */
  468. freadshort(fp); /* reserved 1 */
  469. freadshort(fp); /* reserved 2 */
  470. offset = freadint(fp);
  471. tmp = freadint(fp); /* header size */
  472. if(tmp == 40)
  473. {
  474. w = freadint(fp);
  475. h = freadint(fp);
  476. planes = freadshort(fp);
  477. bpp = freadshort(fp);
  478. tmp = freadint(fp); /* compression */
  479. if(tmp != 0)
  480. {
  481. fclose(fp);
  482. return;
  483. }
  484. freadint(fp); /* sizeimage */
  485. freadint(fp); /* xpelspermeter */
  486. freadint(fp); /* ypelspermeter */
  487. freadint(fp); /* biclrused */
  488. freadint(fp); /* biclrimportantn */
  489. colors = (offset - 54) / 4;
  490. for(i = 0; i < colors && i < 256; i++)
  491. {
  492. blue[i] = freadchar(fp) * 16;
  493. green[i] = freadchar(fp) * 16;
  494. red[i] = freadchar(fp) * 16;
  495. alpha[i] = 0;
  496. freadchar(fp);
  497. }
  498. }
  499. else if(tmp == 12)
  500. {
  501. w = freadint(fp);
  502. h = freadint(fp);
  503. planes = freadshort(fp);
  504. bpp = freadshort(fp);
  505. colors = (offset - 26) / 3;
  506. for(i = 0; i < colors && i < 256; i++)
  507. {
  508. blue[i] = freadchar(fp);
  509. green[i] = freadchar(fp);
  510. red[i] = freadchar(fp);
  511. alpha[i] = 0;
  512. }
  513. }
  514. else
  515. {
  516. fclose(fp);
  517. return;
  518. }
  519. /* Fill the rest of the palette */
  520. for(i = colors; i < 256; i++)
  521. blue[i] = green[i] = red[i] = alpha[i] = 0;
  522. depth = (bpp + 7) / 8;
  523. /* Sanity check */
  524. if(!w || w > 0x10000 || !h || h > 0x10000 || planes != 1 /*|| bpp != 24*/)
  525. {
  526. fclose(fp);
  527. return;
  528. }
  529. /* Allocate the pixel buffer */
  530. pixels = malloc(w * h * depth);
  531. if(!pixels)
  532. {
  533. fclose(fp);
  534. return;
  535. }
  536. memset(pixels, 0, w * h * depth);
  537. /* Read the bitmap data */
  538. for(i = h; i--; )
  539. {
  540. unsigned int j, k, bits = 0;
  541. switch(bpp)
  542. {
  543. case 1:
  544. for(j = 0; j < w; j++)
  545. {
  546. k = j % 32;
  547. if(k == 0)
  548. bits = freadint(fp);
  549. pixels[w * i * depth + j] =
  550. (bits >> ((k & ~0xf) + 0xf - (k & 0xf))) & 0x1;
  551. }
  552. break;
  553. case 4:
  554. for(j = 0; j < w; j++)
  555. {
  556. k = j % 8;
  557. if(k == 0)
  558. bits = freadint(fp);
  559. pixels[w * i * depth + j] =
  560. (bits >> (4 * ((k & ~0x1) + 0x1 - (k & 0x1)))) & 0xf;
  561. }
  562. break;
  563. default:
  564. /* Works for 8bpp, but also for 16, 24 etc. */
  565. fread(pixels + w * i * depth, w * depth, 1, fp);
  566. /* Pad reads to 4 bytes */
  567. tmp = (w * depth) % 4;
  568. tmp = (4 - tmp) % 4;
  569. while(tmp--)
  570. freadchar(fp);
  571. break;
  572. }
  573. }
  574. switch(depth)
  575. {
  576. case 3:
  577. rmask = 0xff0000;
  578. gmask = 0x00ff00;
  579. bmask = 0x0000ff;
  580. amask = 0x000000;
  581. break;
  582. case 2: /* XXX: those are the 16 bits values */
  583. rmask = 0x7c00;
  584. gmask = 0x03e0;
  585. bmask = 0x001f;
  586. amask = 0x0000;
  587. break;
  588. case 1:
  589. default:
  590. bpp = 8;
  591. rmask = gmask = bmask = amask = 0;
  592. break;
  593. }
  594. fclose(fp);
  595. /* Create the libcaca bitmap */
  596. bitmap = caca_create_bitmap(bpp, w, h, depth * w,
  597. rmask, gmask, bmask, amask);
  598. if(!bitmap)
  599. {
  600. free(pixels);
  601. pixels = NULL;
  602. return;
  603. }
  604. if(bpp == 8)
  605. caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
  606. #endif
  607. }
  608. static void draw_checkers(int x1, int y1, int x2, int y2)
  609. {
  610. int xn, yn;
  611. if(x2 + 1 > (int)cucul_get_width(qq)) x2 = cucul_get_width(qq) - 1;
  612. if(y2 + 1 > (int)cucul_get_height(qq)) y2 = cucul_get_height(qq) - 1;
  613. for(yn = y1 > 0 ? y1 : 0; yn <= y2; yn++)
  614. for(xn = x1 > 0 ? x1 : 0; xn <= x2; xn++)
  615. {
  616. if((((xn - x1) / 5) ^ ((yn - y1) / 3)) & 1)
  617. cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_DARKGRAY);
  618. else
  619. cucul_set_color(qq, CUCUL_COLOR_DARKGRAY, CUCUL_COLOR_LIGHTGRAY);
  620. cucul_putchar(qq, xn, yn, ' ');
  621. }
  622. }
  623. #if !defined(HAVE_IMLIB2_H)
  624. static int freadint(FILE *fp)
  625. {
  626. unsigned char buffer[4];
  627. fread(buffer, 4, 1, fp);
  628. return ((int)buffer[3] << 24) | ((int)buffer[2] << 16)
  629. | ((int)buffer[1] << 8) | ((int)buffer[0]);
  630. }
  631. static int freadshort(FILE *fp)
  632. {
  633. unsigned char buffer[2];
  634. fread(buffer, 2, 1, fp);
  635. return ((int)buffer[1] << 8) | ((int)buffer[0]);
  636. }
  637. static int freadchar(FILE *fp)
  638. {
  639. unsigned char buffer;
  640. fread(&buffer, 1, 1, fp);
  641. return (int)buffer;
  642. }
  643. #endif