A Simple X Image Viewer
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.
 
 
 
 
 
 

767 lines
15 KiB

  1. /* Copyright 2011, 2012 Bert Muennich
  2. *
  3. * This file is part of sxiv.
  4. *
  5. * sxiv is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * sxiv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  19. #define _IMAGE_CONFIG
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <unistd.h>
  24. #if HAVE_GIFLIB
  25. #include <gif_lib.h>
  26. enum { MIN_GIF_DELAY = 25 };
  27. #endif
  28. #include "exif.h"
  29. #include "image.h"
  30. #include "options.h"
  31. #include "util.h"
  32. #include "config.h"
  33. float zoom_min;
  34. float zoom_max;
  35. int zoomdiff(float z1, float z2)
  36. {
  37. return (int) (z1 * 1000.0 - z2 * 1000.0);
  38. }
  39. void img_init(img_t *img, win_t *win)
  40. {
  41. zoom_min = zoom_levels[0] / 100.0;
  42. zoom_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0;
  43. if (img == NULL || win == NULL)
  44. return;
  45. imlib_context_set_display(win->env.dpy);
  46. imlib_context_set_visual(win->env.vis);
  47. imlib_context_set_colormap(win->env.cmap);
  48. img->im = NULL;
  49. img->win = win;
  50. img->zoom = options->zoom;
  51. img->zoom = MAX(img->zoom, zoom_min);
  52. img->zoom = MIN(img->zoom, zoom_max);
  53. img->checkpan = false;
  54. img->dirty = false;
  55. img->aa = options->aa;
  56. img->alpha = true;
  57. img->multi.cap = img->multi.cnt = 0;
  58. img->multi.animate = false;
  59. }
  60. void exif_auto_orientate(const fileinfo_t *file)
  61. {
  62. switch (exif_orientation(file)) {
  63. case 5:
  64. imlib_image_orientate(1);
  65. case 2:
  66. imlib_image_flip_vertical();
  67. break;
  68. case 3:
  69. imlib_image_orientate(2);
  70. break;
  71. case 7:
  72. imlib_image_orientate(1);
  73. case 4:
  74. imlib_image_flip_horizontal();
  75. break;
  76. case 6:
  77. imlib_image_orientate(1);
  78. break;
  79. case 8:
  80. imlib_image_orientate(3);
  81. break;
  82. }
  83. }
  84. #if HAVE_GIFLIB
  85. bool img_load_gif(img_t *img, const fileinfo_t *file)
  86. {
  87. GifFileType *gif;
  88. GifRowType *rows = NULL;
  89. GifRecordType rec;
  90. ColorMapObject *cmap;
  91. DATA32 bgpixel, *data, *ptr;
  92. DATA32 *prev_frame = NULL;
  93. Imlib_Image *im;
  94. int i, j, bg, r, g, b;
  95. int x, y, w, h, sw, sh;
  96. int px, py, pw, ph;
  97. int intoffset[] = { 0, 4, 2, 1 };
  98. int intjump[] = { 8, 8, 4, 2 };
  99. int transp = -1;
  100. unsigned int disposal = 0, prev_disposal = 0;
  101. unsigned int delay = 0;
  102. bool err = false;
  103. if (img->multi.cap == 0) {
  104. img->multi.cap = 8;
  105. img->multi.frames = (img_frame_t*)
  106. s_malloc(sizeof(img_frame_t) * img->multi.cap);
  107. }
  108. img->multi.cnt = 0;
  109. img->multi.sel = 0;
  110. gif = DGifOpenFileName(file->path);
  111. if (gif == NULL) {
  112. warn("could not open gif file: %s", file->name);
  113. return false;
  114. }
  115. bg = gif->SBackGroundColor;
  116. sw = gif->SWidth;
  117. sh = gif->SHeight;
  118. px = py = pw = ph = 0;
  119. do {
  120. if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
  121. err = true;
  122. break;
  123. }
  124. if (rec == EXTENSION_RECORD_TYPE) {
  125. int ext_code;
  126. GifByteType *ext = NULL;
  127. DGifGetExtension(gif, &ext_code, &ext);
  128. while (ext) {
  129. if (ext_code == 0xf9) {
  130. if (ext[1] & 1)
  131. transp = (int) ext[4];
  132. else
  133. transp = -1;
  134. delay = 10 * ((unsigned int) ext[3] << 8 | (unsigned int) ext[2]);
  135. if (delay)
  136. delay = MAX(delay, MIN_GIF_DELAY);
  137. disposal = (unsigned int) ext[1] >> 2 & 0x7;
  138. }
  139. ext = NULL;
  140. DGifGetExtensionNext(gif, &ext);
  141. }
  142. } else if (rec == IMAGE_DESC_RECORD_TYPE) {
  143. if (DGifGetImageDesc(gif) == GIF_ERROR) {
  144. err = true;
  145. break;
  146. }
  147. x = gif->Image.Left;
  148. y = gif->Image.Top;
  149. w = gif->Image.Width;
  150. h = gif->Image.Height;
  151. rows = (GifRowType*) s_malloc(h * sizeof(GifRowType));
  152. for (i = 0; i < h; i++)
  153. rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType));
  154. if (gif->Image.Interlace) {
  155. for (i = 0; i < 4; i++) {
  156. for (j = intoffset[i]; j < h; j += intjump[i])
  157. DGifGetLine(gif, rows[j], w);
  158. }
  159. } else {
  160. for (i = 0; i < h; i++)
  161. DGifGetLine(gif, rows[i], w);
  162. }
  163. ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh);
  164. cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
  165. r = cmap->Colors[bg].Red;
  166. g = cmap->Colors[bg].Green;
  167. b = cmap->Colors[bg].Blue;
  168. bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
  169. for (i = 0; i < sh; i++) {
  170. for (j = 0; j < sw; j++) {
  171. if (i < y || i >= y + h || j < x || j >= x + w ||
  172. rows[i-y][j-x] == transp)
  173. {
  174. if (prev_frame != NULL && (prev_disposal != 2 ||
  175. i < py || i >= py + ph || j < px || j >= px + pw))
  176. {
  177. *ptr = prev_frame[i * sw + j];
  178. } else {
  179. *ptr = bgpixel;
  180. }
  181. } else {
  182. r = cmap->Colors[rows[i-y][j-x]].Red;
  183. g = cmap->Colors[rows[i-y][j-x]].Green;
  184. b = cmap->Colors[rows[i-y][j-x]].Blue;
  185. *ptr = 0xff << 24 | r << 16 | g << 8 | b;
  186. }
  187. ptr++;
  188. }
  189. }
  190. im = imlib_create_image_using_copied_data(sw, sh, data);
  191. for (i = 0; i < h; i++)
  192. free(rows[i]);
  193. free(rows);
  194. free(data);
  195. if (im == NULL) {
  196. err = true;
  197. break;
  198. }
  199. imlib_context_set_image(im);
  200. imlib_image_set_format("gif");
  201. if (transp >= 0)
  202. imlib_image_set_has_alpha(1);
  203. if (disposal != 3)
  204. prev_frame = imlib_image_get_data_for_reading_only();
  205. prev_disposal = disposal;
  206. px = x, py = y, pw = w, ph = h;
  207. if (img->multi.cnt == img->multi.cap) {
  208. img->multi.cap *= 2;
  209. img->multi.frames = (img_frame_t*)
  210. s_realloc(img->multi.frames,
  211. img->multi.cap * sizeof(img_frame_t));
  212. }
  213. img->multi.frames[img->multi.cnt].im = im;
  214. img->multi.frames[img->multi.cnt].delay = delay ? delay : GIF_DELAY;
  215. img->multi.cnt++;
  216. }
  217. } while (rec != TERMINATE_RECORD_TYPE);
  218. DGifCloseFile(gif);
  219. if (err && !file->loaded)
  220. warn("corrupted gif file: %s", file->name);
  221. if (img->multi.cnt > 1) {
  222. imlib_context_set_image(img->im);
  223. imlib_free_image();
  224. img->im = img->multi.frames[0].im;
  225. img->multi.animate = GIF_AUTOPLAY;
  226. } else if (img->multi.cnt == 1) {
  227. imlib_context_set_image(img->multi.frames[0].im);
  228. imlib_free_image();
  229. img->multi.cnt = 0;
  230. img->multi.animate = false;
  231. }
  232. imlib_context_set_image(img->im);
  233. return !err;
  234. }
  235. #endif /* HAVE_GIFLIB */
  236. bool img_load(img_t *img, const fileinfo_t *file)
  237. {
  238. const char *fmt;
  239. if (img == NULL || file == NULL || file->name == NULL || file->path == NULL)
  240. return false;
  241. if (access(file->path, R_OK) < 0 ||
  242. (img->im = imlib_load_image(file->path)) == NULL)
  243. {
  244. warn("could not open image: %s", file->name);
  245. return false;
  246. }
  247. imlib_context_set_image(img->im);
  248. imlib_image_set_changes_on_disk();
  249. if ((fmt = imlib_image_format()) == NULL) {
  250. warn("could not open image: %s", file->name);
  251. return false;
  252. }
  253. if (STREQ(fmt, "jpeg"))
  254. exif_auto_orientate(file);
  255. #if HAVE_GIFLIB
  256. if (STREQ(fmt, "gif"))
  257. img_load_gif(img, file);
  258. #endif
  259. img->w = imlib_image_get_width();
  260. img->h = imlib_image_get_height();
  261. img->scalemode = options->scalemode;
  262. img->re = false;
  263. img->checkpan = false;
  264. img->dirty = true;
  265. return true;
  266. }
  267. void img_close(img_t *img, bool decache)
  268. {
  269. int i;
  270. if (img == NULL)
  271. return;
  272. if (img->multi.cnt > 0) {
  273. for (i = 0; i < img->multi.cnt; i++) {
  274. imlib_context_set_image(img->multi.frames[i].im);
  275. imlib_free_image();
  276. }
  277. img->multi.cnt = 0;
  278. img->im = NULL;
  279. } else if (img->im != NULL) {
  280. imlib_context_set_image(img->im);
  281. if (decache)
  282. imlib_free_image_and_decache();
  283. else
  284. imlib_free_image();
  285. img->im = NULL;
  286. }
  287. }
  288. void img_check_pan(img_t *img, bool moved)
  289. {
  290. win_t *win;
  291. int ox, oy;
  292. if (img == NULL || img->im == NULL || img->win == NULL)
  293. return;
  294. win = img->win;
  295. ox = img->x;
  296. oy = img->y;
  297. if (img->w * img->zoom > win->w) {
  298. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  299. img->x = 0;
  300. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  301. img->x = win->w - img->w * img->zoom;
  302. } else {
  303. img->x = (win->w - img->w * img->zoom) / 2;
  304. }
  305. if (img->h * img->zoom > win->h) {
  306. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  307. img->y = 0;
  308. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  309. img->y = win->h - img->h * img->zoom;
  310. } else {
  311. img->y = (win->h - img->h * img->zoom) / 2;
  312. }
  313. if (!moved && (ox != img->x || oy != img->y))
  314. img->dirty = true;
  315. }
  316. bool img_fit(img_t *img)
  317. {
  318. float z, zmax, zw, zh;
  319. if (img == NULL || img->im == NULL || img->win == NULL)
  320. return false;
  321. if (img->scalemode == SCALE_ZOOM)
  322. return false;
  323. zmax = img->scalemode == SCALE_DOWN ? 1.0 : zoom_max;
  324. zw = (float) img->win->w / (float) img->w;
  325. zh = (float) img->win->h / (float) img->h;
  326. switch (img->scalemode) {
  327. case SCALE_WIDTH:
  328. z = zw;
  329. break;
  330. case SCALE_HEIGHT:
  331. z = zh;
  332. break;
  333. default:
  334. z = MIN(zw, zh);
  335. break;
  336. }
  337. z = MAX(z, zoom_min);
  338. z = MIN(z, zmax);
  339. if (zoomdiff(z, img->zoom) != 0) {
  340. img->zoom = z;
  341. img->dirty = true;
  342. return true;
  343. } else {
  344. return false;
  345. }
  346. }
  347. void img_render(img_t *img)
  348. {
  349. win_t *win;
  350. int sx, sy, sw, sh;
  351. int dx, dy, dw, dh;
  352. if (img == NULL || img->im == NULL || img->win == NULL)
  353. return;
  354. win = img->win;
  355. img_fit(img);
  356. if (!img->re) {
  357. /* rendered for the first time */
  358. img->re = true;
  359. if (img->zoom * img->w <= win->w)
  360. img->x = (win->w - img->w * img->zoom) / 2;
  361. else
  362. img->x = 0;
  363. if (img->zoom * img->h <= win->h)
  364. img->y = (win->h - img->h * img->zoom) / 2;
  365. else
  366. img->y = 0;
  367. }
  368. if (img->checkpan) {
  369. img_check_pan(img, false);
  370. img->checkpan = false;
  371. }
  372. if (!img->dirty)
  373. return;
  374. /* calculate source and destination offsets */
  375. if (img->x < 0) {
  376. sx = -img->x / img->zoom;
  377. sw = win->w / img->zoom;
  378. dx = 0;
  379. dw = win->w;
  380. } else {
  381. sx = 0;
  382. sw = img->w;
  383. dx = img->x;
  384. dw = img->w * img->zoom;
  385. }
  386. if (img->y < 0) {
  387. sy = -img->y / img->zoom;
  388. sh = win->h / img->zoom;
  389. dy = 0;
  390. dh = win->h;
  391. } else {
  392. sy = 0;
  393. sh = img->h;
  394. dy = img->y;
  395. dh = img->h * img->zoom;
  396. }
  397. win_clear(win);
  398. imlib_context_set_image(img->im);
  399. imlib_context_set_anti_alias(img->aa);
  400. if (!img->alpha && imlib_image_has_alpha())
  401. win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white);
  402. imlib_context_set_drawable(win->pm);
  403. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  404. img->dirty = false;
  405. }
  406. bool img_fit_win(img_t *img, scalemode_t sm)
  407. {
  408. if (img == NULL || img->im == NULL)
  409. return false;
  410. img->scalemode = sm;
  411. return img_fit(img);
  412. }
  413. bool img_center(img_t *img)
  414. {
  415. int ox, oy;
  416. if (img == NULL || img->im == NULL || img->win == NULL)
  417. return false;
  418. ox = img->x;
  419. oy = img->y;
  420. img->x = (img->win->w - img->w * img->zoom) / 2;
  421. img->y = (img->win->h - img->h * img->zoom) / 2;
  422. if (ox != img->x || oy != img->y) {
  423. img->dirty = true;
  424. return true;
  425. } else {
  426. return false;
  427. }
  428. }
  429. bool img_zoom(img_t *img, float z)
  430. {
  431. if (img == NULL || img->im == NULL || img->win == NULL)
  432. return false;
  433. z = MAX(z, zoom_min);
  434. z = MIN(z, zoom_max);
  435. img->scalemode = SCALE_ZOOM;
  436. if (zoomdiff(z, img->zoom) != 0) {
  437. img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * z / img->zoom;
  438. img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * z / img->zoom;
  439. img->zoom = z;
  440. img->checkpan = true;
  441. img->dirty = true;
  442. return true;
  443. } else {
  444. return false;
  445. }
  446. }
  447. bool img_zoom_in(img_t *img)
  448. {
  449. int i;
  450. float z;
  451. if (img == NULL || img->im == NULL)
  452. return false;
  453. for (i = 1; i < ARRLEN(zoom_levels); i++) {
  454. z = zoom_levels[i] / 100.0;
  455. if (zoomdiff(z, img->zoom) > 0)
  456. return img_zoom(img, z);
  457. }
  458. return false;
  459. }
  460. bool img_zoom_out(img_t *img)
  461. {
  462. int i;
  463. float z;
  464. if (img == NULL || img->im == NULL)
  465. return false;
  466. for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
  467. z = zoom_levels[i] / 100.0;
  468. if (zoomdiff(z, img->zoom) < 0)
  469. return img_zoom(img, z);
  470. }
  471. return false;
  472. }
  473. bool img_move(img_t *img, float dx, float dy)
  474. {
  475. float ox, oy;
  476. if (img == NULL || img->im == NULL)
  477. return false;
  478. ox = img->x;
  479. oy = img->y;
  480. img->x += dx;
  481. img->y += dy;
  482. img_check_pan(img, true);
  483. if (ox != img->x || oy != img->y) {
  484. img->dirty = true;
  485. return true;
  486. } else {
  487. return false;
  488. }
  489. }
  490. bool img_pan(img_t *img, direction_t dir, int d)
  491. {
  492. /* d < 0: screen-wise
  493. * d = 0: 1/5 of screen
  494. * d > 0: num of pixels
  495. */
  496. float x, y;
  497. if (img == NULL || img->im == NULL || img->win == NULL)
  498. return false;
  499. if (d > 0) {
  500. x = y = MAX(1, (float) d * img->zoom);
  501. } else {
  502. x = img->win->w / (d < 0 ? 1 : 5);
  503. y = img->win->h / (d < 0 ? 1 : 5);
  504. }
  505. switch (dir) {
  506. case DIR_LEFT:
  507. return img_move(img, x, 0.0);
  508. case DIR_RIGHT:
  509. return img_move(img, -x, 0.0);
  510. case DIR_UP:
  511. return img_move(img, 0.0, y);
  512. case DIR_DOWN:
  513. return img_move(img, 0.0, -y);
  514. }
  515. return false;
  516. }
  517. bool img_pan_edge(img_t *img, direction_t dir)
  518. {
  519. int ox, oy;
  520. if (img == NULL || img->im == NULL || img->win == NULL)
  521. return false;
  522. ox = img->x;
  523. oy = img->y;
  524. switch (dir) {
  525. case DIR_LEFT:
  526. img->x = 0;
  527. break;
  528. case DIR_RIGHT:
  529. img->x = img->win->w - img->w * img->zoom;
  530. break;
  531. case DIR_UP:
  532. img->y = 0;
  533. break;
  534. case DIR_DOWN:
  535. img->y = img->win->h - img->h * img->zoom;
  536. break;
  537. }
  538. img_check_pan(img, true);
  539. if (ox != img->x || oy != img->y) {
  540. img->dirty = true;
  541. return true;
  542. } else {
  543. return false;
  544. }
  545. }
  546. void img_rotate(img_t *img, int d)
  547. {
  548. win_t *win;
  549. int ox, oy, tmp;
  550. if (img == NULL || img->im == NULL || img->win == NULL)
  551. return;
  552. win = img->win;
  553. ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
  554. oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
  555. imlib_context_set_image(img->im);
  556. imlib_image_orientate(d);
  557. img->x = oy + (win->w - win->h) / 2;
  558. img->y = ox + (win->h - win->w) / 2;
  559. tmp = img->w;
  560. img->w = img->h;
  561. img->h = tmp;
  562. img->checkpan = true;
  563. img->dirty = true;
  564. }
  565. void img_rotate_left(img_t *img)
  566. {
  567. img_rotate(img, 3);
  568. }
  569. void img_rotate_right(img_t *img)
  570. {
  571. img_rotate(img, 1);
  572. }
  573. void img_flip(img_t *img, flipdir_t d)
  574. {
  575. if (img == NULL || img->im == NULL)
  576. return;
  577. imlib_context_set_image(img->im);
  578. switch (d) {
  579. case FLIP_HORIZONTAL:
  580. imlib_image_flip_horizontal();
  581. break;
  582. case FLIP_VERTICAL:
  583. imlib_image_flip_vertical();
  584. break;
  585. }
  586. img->dirty = true;
  587. }
  588. void img_toggle_antialias(img_t *img)
  589. {
  590. if (img == NULL || img->im == NULL)
  591. return;
  592. img->aa = !img->aa;
  593. imlib_context_set_image(img->im);
  594. imlib_context_set_anti_alias(img->aa);
  595. img->dirty = true;
  596. }
  597. bool img_frame_goto(img_t *img, int n)
  598. {
  599. if (img == NULL || img->im == NULL)
  600. return false;
  601. if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
  602. return false;
  603. img->multi.sel = n;
  604. img->im = img->multi.frames[n].im;
  605. imlib_context_set_image(img->im);
  606. img->w = imlib_image_get_width();
  607. img->h = imlib_image_get_height();
  608. img->checkpan = true;
  609. img->dirty = true;
  610. return true;
  611. }
  612. bool img_frame_navigate(img_t *img, int d)
  613. {
  614. if (img == NULL|| img->im == NULL || img->multi.cnt == 0 || d == 0)
  615. return false;
  616. d += img->multi.sel;
  617. if (d < 0)
  618. d = 0;
  619. else if (d >= img->multi.cnt)
  620. d = img->multi.cnt - 1;
  621. return img_frame_goto(img, d);
  622. }
  623. bool img_frame_animate(img_t *img, bool restart)
  624. {
  625. if (img == NULL || img->im == NULL || img->multi.cnt == 0)
  626. return false;
  627. if (img->multi.sel + 1 >= img->multi.cnt) {
  628. if (restart || GIF_LOOP) {
  629. img_frame_goto(img, 0);
  630. } else {
  631. img->multi.animate = false;
  632. return false;
  633. }
  634. } else if (!restart) {
  635. img_frame_goto(img, img->multi.sel + 1);
  636. }
  637. img->multi.animate = true;
  638. img->dirty = true;
  639. return true;
  640. }