Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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