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.
 
 
 
 
 
 

736 lines
19 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 GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "config.h"
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #if defined(HAVE_IMLIB2_H)
  28. # include <Imlib2.h>
  29. #else
  30. # include <stdio.h>
  31. #endif
  32. #if defined(HAVE_SLEEP)
  33. # include <windows.h>
  34. #endif
  35. #include "caca.h"
  36. /* Local macros */
  37. #define MODE_IMAGE 1
  38. #define MODE_FILES 2
  39. #define STATUS_DITHERING 1
  40. #define STATUS_ANTIALIASING 2
  41. #define STATUS_BACKGROUND 3
  42. #define ZOOM_FACTOR 1.08f
  43. #define ZOOM_MAX 50
  44. #define PAD_STEP 0.15
  45. /* Local functions */
  46. static void print_status(void);
  47. static void print_help(int, int);
  48. static void set_zoom(int);
  49. static void load_image(char const *);
  50. static void unload_image(void);
  51. static void draw_checkers(int, int, int, int);
  52. #if !defined(HAVE_IMLIB2_H)
  53. static int freadint(FILE *);
  54. static int freadshort(FILE *);
  55. static int freadchar(FILE *);
  56. #endif
  57. /* Local variables */
  58. #if defined(HAVE_IMLIB2_H)
  59. Imlib_Image image = NULL;
  60. #endif
  61. char *pixels = NULL;
  62. struct caca_bitmap *bitmap = NULL;
  63. unsigned int w, h, depth, bpp, rmask, gmask, bmask, amask;
  64. #if !defined(HAVE_IMLIB2_H)
  65. unsigned int red[256], green[256], blue[256], alpha[256];
  66. #endif
  67. float zoomtab[ZOOM_MAX + 1];
  68. float xfactor = 1.0, yfactor = 1.0, dx = 0.5, dy = 0.5;
  69. int zoom = 0, fullscreen = 0, mode, ww, wh;
  70. int main(int argc, char **argv)
  71. {
  72. int quit = 0, update = 1, help = 0, status = 0;
  73. int reload = 0;
  74. char **list = NULL;
  75. int current = 0, items = 0, opts = 1;
  76. int i;
  77. /* Initialise libcaca */
  78. if(caca_init())
  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("cacaview");
  85. ww = caca_get_width();
  86. wh = caca_get_height();
  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(event_mask);
  119. else
  120. event = caca_wait_event(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 + caca_get_feature(CACA_BACKGROUND);
  156. if(i > CACA_BACKGROUND_MAX) i = CACA_BACKGROUND_MIN;
  157. caca_set_feature(i);
  158. new_status = STATUS_BACKGROUND;
  159. update = 1;
  160. break;
  161. case 'B':
  162. i = -1 + caca_get_feature(CACA_BACKGROUND);
  163. if(i < CACA_BACKGROUND_MIN) i = CACA_BACKGROUND_MAX;
  164. caca_set_feature(i);
  165. new_status = STATUS_BACKGROUND;
  166. update = 1;
  167. break;
  168. case 'a':
  169. i = 1 + caca_get_feature(CACA_ANTIALIASING);
  170. if(i > CACA_ANTIALIASING_MAX) i = CACA_ANTIALIASING_MIN;
  171. caca_set_feature(i);
  172. new_status = STATUS_ANTIALIASING;
  173. update = 1;
  174. break;
  175. case 'A':
  176. i = -1 + caca_get_feature(CACA_ANTIALIASING);
  177. if(i < CACA_ANTIALIASING_MIN) i = CACA_ANTIALIASING_MAX;
  178. caca_set_feature(i);
  179. new_status = STATUS_ANTIALIASING;
  180. update = 1;
  181. break;
  182. case 'd':
  183. i = 1 + caca_get_feature(CACA_DITHERING);
  184. if(i > CACA_DITHERING_MAX) i = CACA_DITHERING_MIN;
  185. caca_set_feature(i);
  186. new_status = STATUS_DITHERING;
  187. update = 1;
  188. break;
  189. case 'D':
  190. i = -1 + caca_get_feature(CACA_DITHERING);
  191. if(i < CACA_DITHERING_MIN) i = CACA_DITHERING_MAX;
  192. caca_set_feature(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();
  249. ww = caca_get_width();
  250. wh = caca_get_height();
  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(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. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  270. caca_putstr((ww - strlen(buffer)) / 2, wh / 2, buffer);
  271. caca_refresh();
  272. ww = caca_get_width();
  273. wh = caca_get_height();
  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. caca_clear();
  284. if(!items)
  285. {
  286. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  287. caca_printf(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. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  304. caca_putstr((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. caca_draw_bitmap(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. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  329. switch(status)
  330. {
  331. case STATUS_ANTIALIASING:
  332. caca_printf(0, wh - 1, "Antialiasing: %s",
  333. caca_get_feature_name(caca_get_feature(CACA_ANTIALIASING)));
  334. break;
  335. case STATUS_DITHERING:
  336. caca_printf(0, wh - 1, "Dithering: %s",
  337. caca_get_feature_name(caca_get_feature(CACA_DITHERING)));
  338. break;
  339. case STATUS_BACKGROUND:
  340. caca_printf(0, wh - 1, "Background: %s",
  341. caca_get_feature_name(caca_get_feature(CACA_BACKGROUND)));
  342. break;
  343. }
  344. }
  345. if(help)
  346. {
  347. print_help(ww - 25, 2);
  348. }
  349. caca_refresh();
  350. update = 0;
  351. }
  352. /* Clean up */
  353. unload_image();
  354. caca_end();
  355. return 0;
  356. }
  357. static void print_status(void)
  358. {
  359. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  360. caca_draw_line(0, 0, ww - 1, 0, ' ');
  361. caca_draw_line(0, wh - 2, ww - 1, wh - 2, '-');
  362. caca_putstr(0, 0, "q:Quit np:Next/Prev +-x:Zoom "
  363. "hjkl:Move d:Dithering a:Antialias");
  364. caca_putstr(ww - strlen("?:Help"), 0, "?:Help");
  365. caca_printf(3, wh - 2, "cacaview %s", VERSION);
  366. caca_printf(ww - 14, wh - 2, "(zoom: %s%i)", zoom > 0 ? "+" : "", zoom);
  367. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  368. caca_draw_line(0, wh - 1, ww - 1, wh - 1, ' ');
  369. }
  370. static void print_help(int x, int y)
  371. {
  372. static char const *help[] =
  373. {
  374. " +: zoom in ",
  375. " -: zoom out ",
  376. " x: reset zoom ",
  377. " ---------------------- ",
  378. " hjkl: move view ",
  379. " arrows: move view ",
  380. " ---------------------- ",
  381. " a: antialiasing method ",
  382. " d: dithering method ",
  383. " b: background mode ",
  384. " ---------------------- ",
  385. " ?: help ",
  386. " q: quit ",
  387. NULL
  388. };
  389. int i;
  390. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  391. for(i = 0; help[i]; i++)
  392. caca_putstr(x, y + i, help[i]);
  393. }
  394. static void set_zoom(int new_zoom)
  395. {
  396. int height;
  397. zoom = new_zoom;
  398. if(zoom > ZOOM_MAX) zoom = ZOOM_MAX;
  399. if(zoom < -ZOOM_MAX) zoom = -ZOOM_MAX;
  400. ww = caca_get_width();
  401. height = fullscreen ? wh : wh - 3;
  402. xfactor = (zoom < 0) ? 1.0 / zoomtab[-zoom] : zoomtab[zoom];
  403. yfactor = xfactor * ww / height * h / w
  404. * caca_get_height() / caca_get_width()
  405. * caca_get_window_width() / caca_get_window_height();
  406. if(yfactor > xfactor)
  407. {
  408. float tmp = xfactor;
  409. xfactor = tmp * tmp / yfactor;
  410. yfactor = tmp;
  411. }
  412. }
  413. static void unload_image(void)
  414. {
  415. #if defined(HAVE_IMLIB2_H)
  416. if(image)
  417. imlib_free_image();
  418. image = NULL;
  419. pixels = NULL;
  420. #else
  421. if(pixels)
  422. free(pixels);
  423. pixels = NULL;
  424. #endif
  425. if(bitmap)
  426. caca_free_bitmap(bitmap);
  427. bitmap = NULL;
  428. }
  429. static void load_image(char const *name)
  430. {
  431. #if defined(HAVE_IMLIB2_H)
  432. /* Load the new image */
  433. image = imlib_load_image(name);
  434. if(!image)
  435. return;
  436. imlib_context_set_image(image);
  437. pixels = (char *)imlib_image_get_data_for_reading_only();
  438. w = imlib_image_get_width();
  439. h = imlib_image_get_height();
  440. rmask = 0x00ff0000;
  441. gmask = 0x0000ff00;
  442. bmask = 0x000000ff;
  443. amask = 0xff000000;
  444. bpp = 32;
  445. depth = 4;
  446. /* Create the libcaca bitmap */
  447. bitmap = caca_create_bitmap(bpp, w, h, depth * w,
  448. rmask, gmask, bmask, amask);
  449. if(!bitmap)
  450. {
  451. imlib_free_image();
  452. image = NULL;
  453. }
  454. #else
  455. /* Try to load a BMP file */
  456. FILE *fp;
  457. unsigned int i, colors, offset, tmp, planes;
  458. fp = fopen(name, "rb");
  459. if(!fp)
  460. return;
  461. if(freadshort(fp) != 0x4d42)
  462. {
  463. fclose(fp);
  464. return;
  465. }
  466. freadint(fp); /* size */
  467. freadshort(fp); /* reserved 1 */
  468. freadshort(fp); /* reserved 2 */
  469. offset = freadint(fp);
  470. tmp = freadint(fp); /* header size */
  471. if(tmp == 40)
  472. {
  473. w = freadint(fp);
  474. h = freadint(fp);
  475. planes = freadshort(fp);
  476. bpp = freadshort(fp);
  477. tmp = freadint(fp); /* compression */
  478. if(tmp != 0)
  479. {
  480. fclose(fp);
  481. return;
  482. }
  483. freadint(fp); /* sizeimage */
  484. freadint(fp); /* xpelspermeter */
  485. freadint(fp); /* ypelspermeter */
  486. freadint(fp); /* biclrused */
  487. freadint(fp); /* biclrimportantn */
  488. colors = (offset - 54) / 4;
  489. for(i = 0; i < colors && i < 256; i++)
  490. {
  491. blue[i] = freadchar(fp) * 16;
  492. green[i] = freadchar(fp) * 16;
  493. red[i] = freadchar(fp) * 16;
  494. alpha[i] = 0;
  495. freadchar(fp);
  496. }
  497. }
  498. else if(tmp == 12)
  499. {
  500. w = freadint(fp);
  501. h = freadint(fp);
  502. planes = freadshort(fp);
  503. bpp = freadshort(fp);
  504. colors = (offset - 26) / 3;
  505. for(i = 0; i < colors && i < 256; i++)
  506. {
  507. blue[i] = freadchar(fp);
  508. green[i] = freadchar(fp);
  509. red[i] = freadchar(fp);
  510. alpha[i] = 0;
  511. }
  512. }
  513. else
  514. {
  515. fclose(fp);
  516. return;
  517. }
  518. /* Fill the rest of the palette */
  519. for(i = colors; i < 256; i++)
  520. blue[i] = green[i] = red[i] = alpha[i] = 0;
  521. depth = (bpp + 7) / 8;
  522. /* Sanity check */
  523. if(!w || w > 0x10000 || !h || h > 0x10000 || planes != 1 /*|| bpp != 24*/)
  524. {
  525. fclose(fp);
  526. return;
  527. }
  528. /* Allocate the pixel buffer */
  529. pixels = malloc(w * h * depth);
  530. if(!pixels)
  531. {
  532. fclose(fp);
  533. return;
  534. }
  535. memset(pixels, 0, w * h * depth);
  536. /* Read the bitmap data */
  537. for(i = h; i--; )
  538. {
  539. unsigned int j, k, bits = 0;
  540. switch(bpp)
  541. {
  542. case 1:
  543. for(j = 0; j < w; j++)
  544. {
  545. k = j % 32;
  546. if(k == 0)
  547. bits = freadint(fp);
  548. pixels[w * i * depth + j] =
  549. (bits >> ((k & ~0xf) + 0xf - (k & 0xf))) & 0x1;
  550. }
  551. break;
  552. case 4:
  553. for(j = 0; j < w; j++)
  554. {
  555. k = j % 8;
  556. if(k == 0)
  557. bits = freadint(fp);
  558. pixels[w * i * depth + j] =
  559. (bits >> (4 * ((k & ~0x1) + 0x1 - (k & 0x1)))) & 0xf;
  560. }
  561. break;
  562. default:
  563. /* Works for 8bpp, but also for 16, 24 etc. */
  564. fread(pixels + w * i * depth, w * depth, 1, fp);
  565. /* Pad reads to 4 bytes */
  566. tmp = (w * depth) % 4;
  567. tmp = (4 - tmp) % 4;
  568. while(tmp--)
  569. freadchar(fp);
  570. break;
  571. }
  572. }
  573. switch(depth)
  574. {
  575. case 3:
  576. rmask = 0xff0000;
  577. gmask = 0x00ff00;
  578. bmask = 0x0000ff;
  579. amask = 0x000000;
  580. break;
  581. case 2: /* XXX: those are the 16 bits values */
  582. rmask = 0x7c00;
  583. gmask = 0x03e0;
  584. bmask = 0x001f;
  585. amask = 0x0000;
  586. break;
  587. case 1:
  588. default:
  589. bpp = 8;
  590. rmask = gmask = bmask = amask = 0;
  591. break;
  592. }
  593. fclose(fp);
  594. /* Create the libcaca bitmap */
  595. bitmap = caca_create_bitmap(bpp, w, h, depth * w,
  596. rmask, gmask, bmask, amask);
  597. if(!bitmap)
  598. {
  599. free(pixels);
  600. pixels = NULL;
  601. return;
  602. }
  603. if(bpp == 8)
  604. caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
  605. #endif
  606. }
  607. static void draw_checkers(int x1, int y1, int x2, int y2)
  608. {
  609. int xn, yn;
  610. if(x2 + 1 > (int)caca_get_width()) x2 = caca_get_width() - 1;
  611. if(y2 + 1 > (int)caca_get_height()) y2 = caca_get_height() - 1;
  612. for(yn = y1 > 0 ? y1 : 0; yn <= y2; yn++)
  613. for(xn = x1 > 0 ? x1 : 0; xn <= x2; xn++)
  614. {
  615. if((((xn - x1) / 5) ^ ((yn - y1) / 3)) & 1)
  616. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_DARKGRAY);
  617. else
  618. caca_set_color(CACA_COLOR_DARKGRAY, CACA_COLOR_LIGHTGRAY);
  619. caca_putchar(xn, yn, ' ');
  620. }
  621. }
  622. #if !defined(HAVE_IMLIB2_H)
  623. static int freadint(FILE *fp)
  624. {
  625. unsigned char buffer[4];
  626. fread(buffer, 4, 1, fp);
  627. return ((int)buffer[3] << 24) | ((int)buffer[2] << 16)
  628. | ((int)buffer[1] << 8) | ((int)buffer[0]);
  629. }
  630. static int freadshort(FILE *fp)
  631. {
  632. unsigned char buffer[2];
  633. fread(buffer, 2, 1, fp);
  634. return ((int)buffer[1] << 8) | ((int)buffer[0]);
  635. }
  636. static int freadchar(FILE *fp)
  637. {
  638. unsigned char buffer;
  639. fread(&buffer, 1, 1, fp);
  640. return (int)buffer;
  641. }
  642. #endif