選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

632 行
18 KiB

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