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.
 
 
 
 
 
 

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