You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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