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.
 
 
 
 
 
 

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