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.
 
 
 
 
 
 

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