A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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