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.
 
 
 
 
 
 

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