664 lines
18 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. #include <unistd.h>
  28. #if defined(HAVE_IMLIB2_H)
  29. # include <Imlib2.h>
  30. #else
  31. # include <stdio.h>
  32. #endif
  33. #if defined(HAVE_SLEEP)
  34. # include <windows.h>
  35. #endif
  36. #include "caca.h"
  37. /* Local macros */
  38. #define STATUS_DITHERING 1
  39. #define STATUS_ANTIALIASING 2
  40. #define STATUS_BACKGROUND 3
  41. /* Local functions */
  42. static void load_image(char const *);
  43. static void unload_image(void);
  44. static void draw_checkers(unsigned int, unsigned int,
  45. unsigned int, unsigned int);
  46. #if !defined(HAVE_IMLIB2_H)
  47. static int freadint(FILE *);
  48. static int freadshort(FILE *);
  49. static int freadchar(FILE *);
  50. #endif
  51. /* Local variables */
  52. #if defined(HAVE_IMLIB2_H)
  53. Imlib_Image image = NULL;
  54. #endif
  55. char *pixels = NULL;
  56. struct caca_bitmap *bitmap = NULL;
  57. int x, y;
  58. unsigned int w, h, depth, bpp, rmask, gmask, bmask, amask;
  59. #if !defined(HAVE_IMLIB2_H)
  60. unsigned int red[256], green[256], blue[256], alpha[256];
  61. #endif
  62. int main(int argc, char **argv)
  63. {
  64. int quit = 0, update = 1, help = 0, fullscreen = 0, status = 0;
  65. int reload = 0, zoom = 0;
  66. char **list = NULL;
  67. int current = 0, items = 0, opts = 1;
  68. int i;
  69. /* Initialise libcaca */
  70. if(caca_init())
  71. {
  72. fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
  73. return 1;
  74. }
  75. /* Set the window title */
  76. caca_set_title("cacaview");
  77. /* Load items into playlist */
  78. for(i = 1; i < argc; i++)
  79. {
  80. /* Skip options except after `--' */
  81. if(opts && argv[i][0] == '-')
  82. {
  83. if(argv[i][1] == '-' && argv[i][2] == '\0')
  84. opts = 0;
  85. continue;
  86. }
  87. /* Add argv[i] to the list */
  88. if(items)
  89. list = realloc(list, (items + 1) * sizeof(char *));
  90. else
  91. list = malloc(sizeof(char *));
  92. list[items] = argv[i];
  93. items++;
  94. reload = 1;
  95. }
  96. /* Go ! */
  97. while(!quit)
  98. {
  99. int ww = caca_get_width();
  100. int wh = caca_get_height();
  101. unsigned int event, new_status = 0, new_help = 0;
  102. if(update)
  103. event = caca_get_event(CACA_EVENT_KEY_PRESS);
  104. else
  105. event = caca_wait_event(CACA_EVENT_KEY_PRESS);
  106. while(event)
  107. {
  108. unsigned int key = event & 0x00ffffff;
  109. switch(key)
  110. {
  111. case 'n':
  112. case 'N':
  113. if(items) current = (current + 1) % items;
  114. reload = 1;
  115. break;
  116. case 'p':
  117. case 'P':
  118. if(items) current = (items + current - 1) % items;
  119. reload = 1;
  120. break;
  121. case 'f':
  122. case 'F':
  123. fullscreen = ~fullscreen;
  124. update = 1;
  125. break;
  126. case 'b':
  127. i = 1 + caca_get_feature(CACA_BACKGROUND);
  128. if(i > CACA_BACKGROUND_MAX) i = CACA_BACKGROUND_MIN;
  129. caca_set_feature(i);
  130. new_status = STATUS_BACKGROUND;
  131. update = 1;
  132. break;
  133. case 'B':
  134. i = -1 + caca_get_feature(CACA_BACKGROUND);
  135. if(i < CACA_BACKGROUND_MIN) i = CACA_BACKGROUND_MAX;
  136. caca_set_feature(i);
  137. new_status = STATUS_BACKGROUND;
  138. update = 1;
  139. break;
  140. case 'a':
  141. i = 1 + caca_get_feature(CACA_ANTIALIASING);
  142. if(i > CACA_ANTIALIASING_MAX) i = CACA_ANTIALIASING_MIN;
  143. caca_set_feature(i);
  144. new_status = STATUS_ANTIALIASING;
  145. update = 1;
  146. break;
  147. case 'A':
  148. i = -1 + caca_get_feature(CACA_ANTIALIASING);
  149. if(i < CACA_ANTIALIASING_MIN) i = CACA_ANTIALIASING_MAX;
  150. caca_set_feature(i);
  151. new_status = STATUS_ANTIALIASING;
  152. update = 1;
  153. break;
  154. case 'd':
  155. i = 1 + caca_get_feature(CACA_DITHERING);
  156. if(i > CACA_DITHERING_MAX) i = CACA_DITHERING_MIN;
  157. caca_set_feature(i);
  158. new_status = STATUS_DITHERING;
  159. update = 1;
  160. break;
  161. case 'D':
  162. i = -1 + caca_get_feature(CACA_DITHERING);
  163. if(i < CACA_DITHERING_MIN) i = CACA_DITHERING_MAX;
  164. caca_set_feature(i);
  165. new_status = STATUS_DITHERING;
  166. update = 1;
  167. break;
  168. case '+':
  169. zoom++;
  170. if(zoom > 48) zoom = 48; else update = 1;
  171. break;
  172. case '-':
  173. zoom--;
  174. if(zoom < -48) zoom = -48; else update = 1;
  175. break;
  176. case 'x':
  177. case 'X':
  178. zoom = 0;
  179. update = 1;
  180. break;
  181. case 'k':
  182. case 'K':
  183. case CACA_KEY_UP:
  184. if(zoom > 0) y -= 1 + h / (2 + zoom) / 8;
  185. update = 1;
  186. break;
  187. case 'j':
  188. case 'J':
  189. case CACA_KEY_DOWN:
  190. if(zoom > 0) y += 1 + h / (2 + zoom) / 8;
  191. update = 1;
  192. break;
  193. case 'h':
  194. case 'H':
  195. case CACA_KEY_LEFT:
  196. if(zoom > 0) x -= 1 + w / (2 + zoom) / 8;
  197. update = 1;
  198. break;
  199. case 'l':
  200. case 'L':
  201. case CACA_KEY_RIGHT:
  202. if(zoom > 0) x += 1 + w / (2 + zoom) / 8;
  203. update = 1;
  204. break;
  205. case '?':
  206. new_help = !help;
  207. update = 1;
  208. break;
  209. case 'q':
  210. case 'Q':
  211. quit = 1;
  212. break;
  213. }
  214. if(status || new_status)
  215. status = new_status;
  216. if(help || new_help)
  217. help = new_help;
  218. event = caca_get_event(CACA_EVENT_KEY_PRESS);
  219. }
  220. if(items && reload)
  221. {
  222. char *buffer;
  223. int len = strlen(" Loading `%s'... ") + strlen(list[current]);
  224. if(len < ww + 1)
  225. len = ww + 1;
  226. buffer = malloc(len);
  227. /* Reset image-specific runtime variables */
  228. zoom = 0;
  229. sprintf(buffer, " Loading `%s'... ", list[current]);
  230. buffer[ww] = '\0';
  231. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  232. caca_putstr((ww - strlen(buffer)) / 2, wh / 2, buffer);
  233. caca_refresh();
  234. unload_image();
  235. load_image(list[current]);
  236. reload = 0;
  237. update = 1;
  238. free(buffer);
  239. }
  240. caca_clear();
  241. if(!items)
  242. {
  243. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  244. caca_printf(ww / 2 - 5, wh / 2, " No image. ");
  245. }
  246. else if(!pixels)
  247. {
  248. #if defined(HAVE_IMLIB2_H)
  249. # define ERROR_STRING " Error loading `%s'. "
  250. #else
  251. # define ERROR_STRING " Error loading `%s'. Only BMP is supported. "
  252. #endif
  253. char *buffer;
  254. int len = strlen(ERROR_STRING) + strlen(list[current]);
  255. if(len < ww + 1)
  256. len = ww + 1;
  257. buffer = malloc(len);
  258. sprintf(buffer, ERROR_STRING, list[current]);
  259. buffer[ww] = '\0';
  260. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  261. caca_putstr((ww - strlen(buffer)) / 2, wh / 2, buffer);
  262. free(buffer);
  263. }
  264. else if(zoom < 0)
  265. {
  266. int xo = (ww - 1) / 2;
  267. int yo = (wh - 1) / 2;
  268. int xn = (ww - 1) / (2 - zoom);
  269. int yn = (wh - 1) / (2 - zoom);
  270. draw_checkers(xo - xn, yo - yn, xo + xn, yo + yn);
  271. caca_draw_bitmap(xo - xn, yo - yn, xo + xn, yo + yn,
  272. bitmap, pixels);
  273. }
  274. else if(zoom > 0)
  275. {
  276. struct caca_bitmap *newbitmap;
  277. int xn = w / (2 + zoom);
  278. int yn = h / (2 + zoom);
  279. if(x < xn) x = xn;
  280. if(y < yn) y = yn;
  281. if(xn + x > (int)w) x = w - xn;
  282. if(yn + y > (int)h) y = h - yn;
  283. newbitmap = caca_create_bitmap(bpp, 2 * xn, 2 * yn, depth * w,
  284. rmask, gmask, bmask, amask);
  285. #if !defined(HAVE_IMLIB2_H)
  286. if(bpp == 8)
  287. caca_set_bitmap_palette(newbitmap, red, green, blue, alpha);
  288. #endif
  289. draw_checkers(0, fullscreen ? 0 : 1,
  290. ww - 1, fullscreen ? wh - 1 : wh - 3);
  291. caca_draw_bitmap(0, fullscreen ? 0 : 1,
  292. ww - 1, fullscreen ? wh - 1 : wh - 3,
  293. newbitmap,
  294. pixels + depth * (x - xn) + depth * w * (y - yn));
  295. caca_free_bitmap(newbitmap);
  296. }
  297. else
  298. {
  299. draw_checkers(0, fullscreen ? 0 : 1,
  300. ww - 1, fullscreen ? wh - 1 : wh - 3);
  301. caca_draw_bitmap(0, fullscreen ? 0 : 1,
  302. ww - 1, fullscreen ? wh - 1 : wh - 3,
  303. bitmap, pixels);
  304. }
  305. if(!fullscreen)
  306. {
  307. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  308. caca_draw_line(0, 0, ww - 1, 0, ' ');
  309. caca_draw_line(0, wh - 2, ww - 1, wh - 2, '-');
  310. caca_putstr(0, 0, "q:Quit np:Next/Prev +-x:Zoom "
  311. "hjkl:Move d:Dithering a:Antialias");
  312. caca_putstr(ww - strlen("?:Help"), 0, "?:Help");
  313. caca_printf(3, wh - 2, "cacaview %s", VERSION);
  314. caca_printf(ww - 14, wh - 2,
  315. "(zoom: %s%i)", zoom > 0 ? "+" : "", zoom);
  316. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  317. caca_draw_line(0, wh - 1, ww - 1, wh - 1, ' ');
  318. switch(status)
  319. {
  320. case STATUS_ANTIALIASING:
  321. caca_printf(0, wh - 1, "Antialiasing: %s",
  322. caca_get_feature_name(caca_get_feature(CACA_ANTIALIASING)));
  323. break;
  324. case STATUS_DITHERING:
  325. caca_printf(0, wh - 1, "Dithering: %s",
  326. caca_get_feature_name(caca_get_feature(CACA_DITHERING)));
  327. break;
  328. case STATUS_BACKGROUND:
  329. caca_printf(0, wh - 1, "Background: %s",
  330. caca_get_feature_name(caca_get_feature(CACA_BACKGROUND)));
  331. break;
  332. }
  333. }
  334. if(help)
  335. {
  336. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  337. caca_putstr(ww - 25, 2, " +: zoom in ");
  338. caca_putstr(ww - 25, 3, " -: zoom out ");
  339. caca_putstr(ww - 25, 4, " x: reset zoom ");
  340. caca_putstr(ww - 25, 5, " ---------------------- ");
  341. caca_putstr(ww - 25, 6, " hjkl: move view ");
  342. caca_putstr(ww - 25, 7, " arrows: move view ");
  343. caca_putstr(ww - 25, 8, " ---------------------- ");
  344. caca_putstr(ww - 25, 9, " a: antialiasing method ");
  345. caca_putstr(ww - 25, 10, " d: dithering method ");
  346. caca_putstr(ww - 25, 11, " b: background mode ");
  347. caca_putstr(ww - 25, 12, " ---------------------- ");
  348. caca_putstr(ww - 25, 13, " ?: help ");
  349. caca_putstr(ww - 25, 14, " q: quit ");
  350. }
  351. caca_refresh();
  352. update = 0;
  353. }
  354. /* Clean up */
  355. unload_image();
  356. caca_end();
  357. return 0;
  358. }
  359. static void unload_image(void)
  360. {
  361. #if defined(HAVE_IMLIB2_H)
  362. if(image)
  363. imlib_free_image();
  364. image = NULL;
  365. pixels = NULL;
  366. #else
  367. if(pixels)
  368. free(pixels);
  369. pixels = NULL;
  370. #endif
  371. if(bitmap)
  372. caca_free_bitmap(bitmap);
  373. bitmap = NULL;
  374. }
  375. static void load_image(char const *name)
  376. {
  377. #if defined(HAVE_IMLIB2_H)
  378. /* Load the new image */
  379. image = imlib_load_image(name);
  380. if(!image)
  381. return;
  382. imlib_context_set_image(image);
  383. pixels = (char *)imlib_image_get_data_for_reading_only();
  384. w = imlib_image_get_width();
  385. h = imlib_image_get_height();
  386. rmask = 0x00ff0000;
  387. gmask = 0x0000ff00;
  388. bmask = 0x000000ff;
  389. amask = 0xff000000;
  390. bpp = 32;
  391. depth = 4;
  392. /* Create the libcaca bitmap */
  393. bitmap = caca_create_bitmap(bpp, w, h, depth * w,
  394. rmask, gmask, bmask, amask);
  395. if(!bitmap)
  396. {
  397. imlib_free_image();
  398. image = NULL;
  399. }
  400. #else
  401. /* Try to load a BMP file */
  402. FILE *fp;
  403. unsigned int i, colors, offset, tmp, planes;
  404. fp = fopen(name, "rb");
  405. if(!fp)
  406. return;
  407. if(freadshort(fp) != 0x4d42)
  408. {
  409. fclose(fp);
  410. return;
  411. }
  412. freadint(fp); /* size */
  413. freadshort(fp); /* reserved 1 */
  414. freadshort(fp); /* reserved 2 */
  415. offset = freadint(fp);
  416. tmp = freadint(fp); /* header size */
  417. if(tmp == 40)
  418. {
  419. w = freadint(fp);
  420. h = freadint(fp);
  421. planes = freadshort(fp);
  422. bpp = freadshort(fp);
  423. tmp = freadint(fp); /* compression */
  424. if(tmp != 0)
  425. {
  426. fclose(fp);
  427. return;
  428. }
  429. freadint(fp); /* sizeimage */
  430. freadint(fp); /* xpelspermeter */
  431. freadint(fp); /* ypelspermeter */
  432. freadint(fp); /* biclrused */
  433. freadint(fp); /* biclrimportantn */
  434. colors = (offset - 54) / 4;
  435. for(i = 0; i < colors && i < 256; i++)
  436. {
  437. blue[i] = freadchar(fp) * 16;
  438. green[i] = freadchar(fp) * 16;
  439. red[i] = freadchar(fp) * 16;
  440. alpha[i] = 0;
  441. freadchar(fp);
  442. }
  443. }
  444. else if(tmp == 12)
  445. {
  446. w = freadint(fp);
  447. h = freadint(fp);
  448. planes = freadshort(fp);
  449. bpp = freadshort(fp);
  450. colors = (offset - 26) / 3;
  451. for(i = 0; i < colors && i < 256; i++)
  452. {
  453. blue[i] = freadchar(fp);
  454. green[i] = freadchar(fp);
  455. red[i] = freadchar(fp);
  456. alpha[i] = 0;
  457. }
  458. }
  459. else
  460. {
  461. fclose(fp);
  462. return;
  463. }
  464. /* Fill the rest of the palette */
  465. for(i = colors; i < 256; i++)
  466. blue[i] = green[i] = red[i] = alpha[i] = 0;
  467. depth = (bpp + 7) / 8;
  468. /* Sanity check */
  469. if(!w || w > 0x10000 || !h || h > 0x10000 || planes != 1 /*|| bpp != 24*/)
  470. {
  471. fclose(fp);
  472. return;
  473. }
  474. /* Allocate the pixel buffer */
  475. pixels = malloc(w * h * depth);
  476. if(!pixels)
  477. {
  478. fclose(fp);
  479. return;
  480. }
  481. memset(pixels, 0, w * h * depth);
  482. /* Read the bitmap data */
  483. for(i = h; i--; )
  484. {
  485. unsigned int j, k, bits = 0;
  486. switch(bpp)
  487. {
  488. case 1:
  489. for(j = 0; j < w; j++)
  490. {
  491. k = j % 32;
  492. if(k == 0)
  493. bits = freadint(fp);
  494. pixels[w * i * depth + j] =
  495. (bits >> ((k & ~0xf) + 0xf - (k & 0xf))) & 0x1;
  496. }
  497. break;
  498. case 4:
  499. for(j = 0; j < w; j++)
  500. {
  501. k = j % 8;
  502. if(k == 0)
  503. bits = freadint(fp);
  504. pixels[w * i * depth + j] =
  505. (bits >> (4 * ((k & ~0x1) + 0x1 - (k & 0x1)))) & 0xf;
  506. }
  507. break;
  508. default:
  509. /* Works for 8bpp, but also for 16, 24 etc. */
  510. fread(pixels + w * i * depth, w * depth, 1, fp);
  511. /* Pad reads to 4 bytes */
  512. tmp = (w * depth) % 4;
  513. tmp = (4 - tmp) % 4;
  514. while(tmp--)
  515. freadchar(fp);
  516. break;
  517. }
  518. }
  519. switch(depth)
  520. {
  521. case 3:
  522. rmask = 0xff0000;
  523. gmask = 0x00ff00;
  524. bmask = 0x0000ff;
  525. amask = 0x000000;
  526. break;
  527. case 2: /* XXX: those are the 16 bits values */
  528. rmask = 0x7c00;
  529. gmask = 0x03e0;
  530. bmask = 0x001f;
  531. amask = 0x0000;
  532. break;
  533. case 1:
  534. default:
  535. bpp = 8;
  536. rmask = gmask = bmask = amask = 0;
  537. break;
  538. }
  539. fclose(fp);
  540. /* Create the libcaca bitmap */
  541. bitmap = caca_create_bitmap(bpp, w, h, depth * w,
  542. rmask, gmask, bmask, amask);
  543. if(!bitmap)
  544. {
  545. free(pixels);
  546. pixels = NULL;
  547. return;
  548. }
  549. if(bpp == 8)
  550. caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
  551. #endif
  552. x = w / 2;
  553. y = h / 2;
  554. }
  555. static void draw_checkers(unsigned int x1, unsigned int y1,
  556. unsigned int x2, unsigned int y2)
  557. {
  558. unsigned int xn, yn;
  559. for(yn = y1; yn <= y2; yn++)
  560. for(xn = x1; xn <= x2; xn++)
  561. {
  562. if((((xn - x1) / 5) ^ ((yn - y1) / 3)) & 1)
  563. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_DARKGRAY);
  564. else
  565. caca_set_color(CACA_COLOR_DARKGRAY, CACA_COLOR_LIGHTGRAY);
  566. caca_putchar(xn, yn, ' ');
  567. }
  568. }
  569. #if !defined(HAVE_IMLIB2_H)
  570. static int freadint(FILE *fp)
  571. {
  572. unsigned char buffer[4];
  573. fread(buffer, 4, 1, fp);
  574. return ((int)buffer[3] << 24) | ((int)buffer[2] << 16)
  575. | ((int)buffer[1] << 8) | ((int)buffer[0]);
  576. }
  577. static int freadshort(FILE *fp)
  578. {
  579. unsigned char buffer[2];
  580. fread(buffer, 2, 1, fp);
  581. return ((int)buffer[1] << 8) | ((int)buffer[0]);
  582. }
  583. static int freadchar(FILE *fp)
  584. {
  585. unsigned char buffer;
  586. fread(&buffer, 1, 1, fp);
  587. return (int)buffer;
  588. }
  589. #endif