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