Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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