Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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