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ů.

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