A Simple X Image Viewer
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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