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.
 
 
 
 
 
 

787 line
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. #include "sxiv.h"
  19. #define _IMAGE_CONFIG
  20. #include "config.h"
  21. #include <errno.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #if HAVE_LIBEXIF
  28. #include <libexif/exif-data.h>
  29. #endif
  30. #if HAVE_GIFLIB
  31. #include <gif_lib.h>
  32. enum { DEF_GIF_DELAY = 75 };
  33. #endif
  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. void img_init(img_t *img, win_t *win)
  41. {
  42. zoom_min = zoom_levels[0] / 100.0;
  43. zoom_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0;
  44. imlib_context_set_display(win->env.dpy);
  45. imlib_context_set_visual(win->env.vis);
  46. imlib_context_set_colormap(win->env.cmap);
  47. img->im = NULL;
  48. img->win = win;
  49. img->scalemode = options->scalemode;
  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 = ANTI_ALIAS;
  56. img->alpha = ALPHA_LAYER;
  57. img->multi.cap = img->multi.cnt = 0;
  58. img->multi.animate = options->animate;
  59. img->multi.framedelay = options->framerate > 0 ? 1000 / options->framerate : 0;
  60. img->multi.length = 0;
  61. img->cmod = imlib_create_color_modifier();
  62. imlib_context_set_color_modifier(img->cmod);
  63. img->gamma = MIN(MAX(options->gamma, -GAMMA_RANGE), GAMMA_RANGE);
  64. img->ss.on = options->slideshow > 0;
  65. img->ss.delay = options->slideshow > 0 ? options->slideshow : SLIDESHOW_DELAY * 10;
  66. }
  67. #if HAVE_LIBEXIF
  68. void exif_auto_orientate(const fileinfo_t *file)
  69. {
  70. ExifData *ed;
  71. ExifEntry *entry;
  72. int byte_order, orientation = 0;
  73. if ((ed = exif_data_new_from_file(file->path)) == NULL)
  74. return;
  75. byte_order = exif_data_get_byte_order(ed);
  76. entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION);
  77. if (entry != NULL)
  78. orientation = exif_get_short(entry->data, byte_order);
  79. exif_data_unref(ed);
  80. switch (orientation) {
  81. case 5:
  82. imlib_image_orientate(1);
  83. case 2:
  84. imlib_image_flip_vertical();
  85. break;
  86. case 3:
  87. imlib_image_orientate(2);
  88. break;
  89. case 7:
  90. imlib_image_orientate(1);
  91. case 4:
  92. imlib_image_flip_horizontal();
  93. break;
  94. case 6:
  95. imlib_image_orientate(1);
  96. break;
  97. case 8:
  98. imlib_image_orientate(3);
  99. break;
  100. }
  101. }
  102. #endif
  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. emalloc(sizeof(img_frame_t) * img->multi.cap);
  126. }
  127. img->multi.cnt = img->multi.sel = 0;
  128. img->multi.length = 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. error(0, 0, "%s: Error opening gif image", 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. disposal = (unsigned int) ext[1] >> 2 & 0x7;
  159. }
  160. ext = NULL;
  161. DGifGetExtensionNext(gif, &ext);
  162. }
  163. } else if (rec == IMAGE_DESC_RECORD_TYPE) {
  164. if (DGifGetImageDesc(gif) == GIF_ERROR) {
  165. err = true;
  166. break;
  167. }
  168. x = gif->Image.Left;
  169. y = gif->Image.Top;
  170. w = gif->Image.Width;
  171. h = gif->Image.Height;
  172. rows = (GifRowType*) emalloc(h * sizeof(GifRowType));
  173. for (i = 0; i < h; i++)
  174. rows[i] = (GifRowType) emalloc(w * sizeof(GifPixelType));
  175. if (gif->Image.Interlace) {
  176. for (i = 0; i < 4; i++) {
  177. for (j = intoffset[i]; j < h; j += intjump[i])
  178. DGifGetLine(gif, rows[j], w);
  179. }
  180. } else {
  181. for (i = 0; i < h; i++)
  182. DGifGetLine(gif, rows[i], w);
  183. }
  184. ptr = data = (DATA32*) emalloc(sizeof(DATA32) * sw * sh);
  185. cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
  186. r = cmap->Colors[bg].Red;
  187. g = cmap->Colors[bg].Green;
  188. b = cmap->Colors[bg].Blue;
  189. bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
  190. for (i = 0; i < sh; i++) {
  191. for (j = 0; j < sw; j++) {
  192. if (i < y || i >= y + h || j < x || j >= x + w ||
  193. rows[i-y][j-x] == transp)
  194. {
  195. if (prev_frame != NULL && (prev_disposal != 2 ||
  196. i < py || i >= py + ph || j < px || j >= px + pw))
  197. {
  198. *ptr = prev_frame[i * sw + j];
  199. } else {
  200. *ptr = bgpixel;
  201. }
  202. } else {
  203. r = cmap->Colors[rows[i-y][j-x]].Red;
  204. g = cmap->Colors[rows[i-y][j-x]].Green;
  205. b = cmap->Colors[rows[i-y][j-x]].Blue;
  206. *ptr = 0xffu << 24 | r << 16 | g << 8 | b;
  207. }
  208. ptr++;
  209. }
  210. }
  211. im = imlib_create_image_using_copied_data(sw, sh, data);
  212. for (i = 0; i < h; i++)
  213. free(rows[i]);
  214. free(rows);
  215. free(data);
  216. if (im == NULL) {
  217. err = true;
  218. break;
  219. }
  220. imlib_context_set_image(im);
  221. imlib_image_set_format("gif");
  222. if (transp >= 0)
  223. imlib_image_set_has_alpha(1);
  224. if (disposal != 3)
  225. prev_frame = imlib_image_get_data_for_reading_only();
  226. prev_disposal = disposal;
  227. px = x, py = y, pw = w, ph = h;
  228. if (img->multi.cnt == img->multi.cap) {
  229. img->multi.cap *= 2;
  230. img->multi.frames = (img_frame_t*)
  231. erealloc(img->multi.frames,
  232. img->multi.cap * sizeof(img_frame_t));
  233. }
  234. img->multi.frames[img->multi.cnt].im = im;
  235. delay = img->multi.framedelay > 0 ? img->multi.framedelay : delay;
  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. int x, y;
  463. win_cursor_pos(img->win, &x, &y);
  464. if (x < 0 || x >= img->win->w || y < 0 || y >= img->win->h) {
  465. x = img->win->w / 2;
  466. y = img->win->h / 2;
  467. }
  468. img->x = x - (x - img->x) * z / img->zoom;
  469. img->y = y - (y - 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. for (i = 1; i < ARRLEN(zoom_levels); i++) {
  483. z = zoom_levels[i] / 100.0;
  484. if (zoomdiff(z, img->zoom) > 0)
  485. return img_zoom(img, z);
  486. }
  487. return false;
  488. }
  489. bool img_zoom_out(img_t *img)
  490. {
  491. int i;
  492. float z;
  493. for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
  494. z = zoom_levels[i] / 100.0;
  495. if (zoomdiff(z, img->zoom) < 0)
  496. return img_zoom(img, z);
  497. }
  498. return false;
  499. }
  500. bool img_pos(img_t *img, float x, float y)
  501. {
  502. float ox, oy;
  503. ox = img->x;
  504. oy = img->y;
  505. img->x = x;
  506. img->y = y;
  507. img_check_pan(img, true);
  508. if (ox != img->x || oy != img->y) {
  509. img->dirty = true;
  510. return true;
  511. } else {
  512. return false;
  513. }
  514. }
  515. bool img_move(img_t *img, float dx, float dy)
  516. {
  517. return img_pos(img, img->x + dx, img->y + dy);
  518. }
  519. bool img_pan(img_t *img, direction_t dir, int d)
  520. {
  521. /* d < 0: screen-wise
  522. * d = 0: 1/PAN_FRACTION of screen
  523. * d > 0: num of pixels
  524. */
  525. float x, y;
  526. if (d > 0) {
  527. x = y = MAX(1, (float) d * img->zoom);
  528. } else {
  529. x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
  530. y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
  531. }
  532. switch (dir) {
  533. case DIR_LEFT:
  534. return img_move(img, x, 0.0);
  535. case DIR_RIGHT:
  536. return img_move(img, -x, 0.0);
  537. case DIR_UP:
  538. return img_move(img, 0.0, y);
  539. case DIR_DOWN:
  540. return img_move(img, 0.0, -y);
  541. }
  542. return false;
  543. }
  544. bool img_pan_edge(img_t *img, direction_t dir)
  545. {
  546. int ox, oy;
  547. ox = img->x;
  548. oy = img->y;
  549. if (dir & DIR_LEFT)
  550. img->x = 0;
  551. if (dir & DIR_RIGHT)
  552. img->x = img->win->w - img->w * img->zoom;
  553. if (dir & DIR_UP)
  554. img->y = 0;
  555. if (dir & DIR_DOWN)
  556. img->y = img->win->h - img->h * img->zoom;
  557. img_check_pan(img, true);
  558. if (ox != img->x || oy != img->y) {
  559. img->dirty = true;
  560. return true;
  561. } else {
  562. return false;
  563. }
  564. }
  565. void img_rotate(img_t *img, degree_t d)
  566. {
  567. int i, ox, oy, tmp;
  568. imlib_context_set_image(img->im);
  569. imlib_image_orientate(d);
  570. for (i = 0; i < img->multi.cnt; i++) {
  571. if (i != img->multi.sel) {
  572. imlib_context_set_image(img->multi.frames[i].im);
  573. imlib_image_orientate(d);
  574. }
  575. }
  576. if (d == DEGREE_90 || d == DEGREE_270) {
  577. ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
  578. oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
  579. img->x = oy + (img->win->w - img->win->h) / 2;
  580. img->y = ox + (img->win->h - img->win->w) / 2;
  581. tmp = img->w;
  582. img->w = img->h;
  583. img->h = tmp;
  584. img->checkpan = true;
  585. }
  586. img->dirty = true;
  587. }
  588. void img_flip(img_t *img, flipdir_t d)
  589. {
  590. int i;
  591. void (*imlib_flip_op[3])(void) = {
  592. imlib_image_flip_horizontal,
  593. imlib_image_flip_vertical,
  594. imlib_image_flip_diagonal
  595. };
  596. d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
  597. if (d < 0 || d >= ARRLEN(imlib_flip_op))
  598. return;
  599. imlib_context_set_image(img->im);
  600. imlib_flip_op[d]();
  601. for (i = 0; i < img->multi.cnt; i++) {
  602. if (i != img->multi.sel) {
  603. imlib_context_set_image(img->multi.frames[i].im);
  604. imlib_flip_op[d]();
  605. }
  606. }
  607. img->dirty = true;
  608. }
  609. void img_toggle_antialias(img_t *img)
  610. {
  611. img->aa = !img->aa;
  612. imlib_context_set_image(img->im);
  613. imlib_context_set_anti_alias(img->aa);
  614. img->dirty = true;
  615. }
  616. bool img_change_gamma(img_t *img, int d)
  617. {
  618. /* d < 0: decrease gamma
  619. * d = 0: reset gamma
  620. * d > 0: increase gamma
  621. */
  622. int gamma;
  623. double range;
  624. if (d == 0)
  625. gamma = 0;
  626. else
  627. gamma = MIN(MAX(img->gamma + d, -GAMMA_RANGE), GAMMA_RANGE);
  628. if (img->gamma != gamma) {
  629. imlib_reset_color_modifier();
  630. if (gamma != 0) {
  631. range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
  632. imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
  633. }
  634. img->gamma = gamma;
  635. img->dirty = true;
  636. return true;
  637. } else {
  638. return false;
  639. }
  640. }
  641. bool img_frame_goto(img_t *img, int n)
  642. {
  643. if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
  644. return false;
  645. img->multi.sel = n;
  646. img->im = img->multi.frames[n].im;
  647. imlib_context_set_image(img->im);
  648. img->w = imlib_image_get_width();
  649. img->h = imlib_image_get_height();
  650. img->checkpan = true;
  651. img->dirty = true;
  652. return true;
  653. }
  654. bool img_frame_navigate(img_t *img, int d)
  655. {
  656. if (img->multi.cnt == 0 || d == 0)
  657. return false;
  658. d += img->multi.sel;
  659. if (d < 0)
  660. d = 0;
  661. else if (d >= img->multi.cnt)
  662. d = img->multi.cnt - 1;
  663. return img_frame_goto(img, d);
  664. }
  665. bool img_frame_animate(img_t *img)
  666. {
  667. if (img->multi.cnt == 0)
  668. return false;
  669. if (img->multi.sel + 1 >= img->multi.cnt)
  670. img_frame_goto(img, 0);
  671. else
  672. img_frame_goto(img, img->multi.sel + 1);
  673. img->dirty = true;
  674. return true;
  675. }