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.
 
 
 
 
 
 

810 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. #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(img_t *img, float z)
  37. {
  38. return (int) ((img->w * z - img->w * img->zoom) + (img->h * z - img->h * img->zoom));
  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. Imlib_Image img_open(const fileinfo_t *file)
  262. {
  263. struct stat st;
  264. Imlib_Image im = NULL;
  265. if (access(file->path, R_OK) == 0 &&
  266. stat(file->path, &st) == 0 && S_ISREG(st.st_mode))
  267. {
  268. im = imlib_load_image(file->path);
  269. if (im != NULL) {
  270. imlib_context_set_image(im);
  271. if (imlib_image_get_data_for_reading_only() == NULL) {
  272. imlib_free_image();
  273. im = NULL;
  274. }
  275. }
  276. }
  277. if (im == NULL && (file->flags & FF_WARN))
  278. error(0, 0, "%s: Error opening image", file->name);
  279. return im;
  280. }
  281. bool img_load(img_t *img, const fileinfo_t *file)
  282. {
  283. const char *fmt;
  284. if ((img->im = img_open(file)) == NULL)
  285. return false;
  286. imlib_image_set_changes_on_disk();
  287. #if HAVE_LIBEXIF
  288. exif_auto_orientate(file);
  289. #endif
  290. if ((fmt = imlib_image_format()) != NULL) {
  291. #if HAVE_GIFLIB
  292. if (STREQ(fmt, "gif"))
  293. img_load_gif(img, file);
  294. #endif
  295. }
  296. img->w = imlib_image_get_width();
  297. img->h = imlib_image_get_height();
  298. img->checkpan = true;
  299. img->dirty = true;
  300. return true;
  301. }
  302. CLEANUP void img_close(img_t *img, bool decache)
  303. {
  304. int i;
  305. if (img->multi.cnt > 0) {
  306. for (i = 0; i < img->multi.cnt; i++) {
  307. imlib_context_set_image(img->multi.frames[i].im);
  308. imlib_free_image();
  309. }
  310. img->multi.cnt = 0;
  311. img->im = NULL;
  312. } else if (img->im != NULL) {
  313. imlib_context_set_image(img->im);
  314. if (decache)
  315. imlib_free_image_and_decache();
  316. else
  317. imlib_free_image();
  318. img->im = NULL;
  319. }
  320. }
  321. void img_check_pan(img_t *img, bool moved)
  322. {
  323. win_t *win;
  324. float w, h, ox, oy;
  325. win = img->win;
  326. w = img->w * img->zoom;
  327. h = img->h * img->zoom;
  328. ox = img->x;
  329. oy = img->y;
  330. if (w < win->w)
  331. img->x = (win->w - w) / 2;
  332. else if (img->x > 0)
  333. img->x = 0;
  334. else if (img->x + w < win->w)
  335. img->x = win->w - w;
  336. if (h < win->h)
  337. img->y = (win->h - h) / 2;
  338. else if (img->y > 0)
  339. img->y = 0;
  340. else if (img->y + h < win->h)
  341. img->y = win->h - h;
  342. if (!moved && (ox != img->x || oy != img->y))
  343. img->dirty = true;
  344. }
  345. bool img_fit(img_t *img)
  346. {
  347. float z, zmax, zw, zh;
  348. if (img->scalemode == SCALE_ZOOM)
  349. return false;
  350. zmax = img->scalemode == SCALE_DOWN ? 1.0 : zoom_max;
  351. // Kanon added: If too small, make bigger
  352. if (img->scalemode == SCALE_DOWN) {
  353. if (img->w <= 600) {
  354. zmax = 1.5;
  355. } else if (img->h <=400) {
  356. zmax = 1.5;
  357. }
  358. }
  359. zw = (float) img->win->w / (float) img->w;
  360. zh = (float) img->win->h / (float) img->h;
  361. switch (img->scalemode) {
  362. case SCALE_WIDTH:
  363. z = zw;
  364. break;
  365. case SCALE_HEIGHT:
  366. z = zh;
  367. break;
  368. default:
  369. z = MIN(zw, zh);
  370. break;
  371. }
  372. z = MIN(z, zmax);
  373. if (zoomdiff(img, z) != 0) {
  374. img->zoom = z;
  375. img->dirty = true;
  376. return true;
  377. } else {
  378. return false;
  379. }
  380. }
  381. void img_render(img_t *img)
  382. {
  383. win_t *win;
  384. int sx, sy, sw, sh;
  385. int dx, dy, dw, dh;
  386. Imlib_Image bg;
  387. unsigned long c;
  388. win = img->win;
  389. img_fit(img);
  390. if (img->checkpan) {
  391. img_check_pan(img, false);
  392. img->checkpan = false;
  393. }
  394. if (!img->dirty)
  395. return;
  396. /* calculate source and destination offsets:
  397. * - part of image drawn on full window, or
  398. * - full image drawn on part of window
  399. */
  400. if (img->x <= 0) {
  401. sx = -img->x / img->zoom + 0.5;
  402. sw = win->w / img->zoom;
  403. dx = 0;
  404. dw = win->w;
  405. } else {
  406. sx = 0;
  407. sw = img->w;
  408. dx = img->x;
  409. dw = img->w * img->zoom;
  410. }
  411. if (img->y <= 0) {
  412. sy = -img->y / img->zoom + 0.5;
  413. sh = win->h / img->zoom;
  414. dy = 0;
  415. dh = win->h;
  416. } else {
  417. sy = 0;
  418. sh = img->h;
  419. dy = img->y;
  420. dh = img->h * img->zoom;
  421. }
  422. win_clear(win);
  423. imlib_context_set_image(img->im);
  424. imlib_context_set_anti_alias(img->aa);
  425. imlib_context_set_drawable(win->buf.pm);
  426. if (imlib_image_has_alpha()) {
  427. if ((bg = imlib_create_image(dw, dh)) == NULL)
  428. error(EXIT_FAILURE, ENOMEM, NULL);
  429. imlib_context_set_image(bg);
  430. imlib_image_set_has_alpha(0);
  431. if (img->alpha) {
  432. int i, c, r;
  433. DATA32 col[2] = { 0xFF666666, 0xFF999999 };
  434. DATA32 * data = imlib_image_get_data();
  435. for (r = 0; r < dh; r++) {
  436. i = r * dw;
  437. if (r == 0 || r == 8) {
  438. for (c = 0; c < dw; c++)
  439. data[i++] = col[!(c & 8) ^ !r];
  440. } else {
  441. memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
  442. }
  443. }
  444. imlib_image_put_back_data(data);
  445. } else {
  446. c = win->bg.pixel;
  447. imlib_context_set_color(c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF, 0xFF);
  448. imlib_image_fill_rectangle(0, 0, dw, dh);
  449. }
  450. imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
  451. imlib_context_set_color_modifier(NULL);
  452. imlib_render_image_on_drawable(dx, dy);
  453. imlib_free_image();
  454. imlib_context_set_color_modifier(img->cmod);
  455. } else {
  456. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  457. }
  458. img->dirty = false;
  459. }
  460. bool img_fit_win(img_t *img, scalemode_t sm)
  461. {
  462. float oz;
  463. oz = img->zoom;
  464. img->scalemode = sm;
  465. if (img_fit(img)) {
  466. img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
  467. img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
  468. img->checkpan = true;
  469. return true;
  470. } else {
  471. return false;
  472. }
  473. }
  474. bool img_zoom(img_t *img, float z)
  475. {
  476. z = MAX(z, zoom_min);
  477. z = MIN(z, zoom_max);
  478. img->scalemode = SCALE_ZOOM;
  479. if (zoomdiff(img, z) != 0) {
  480. int x, y;
  481. win_cursor_pos(img->win, &x, &y);
  482. if (x < 0 || x >= img->win->w || y < 0 || y >= img->win->h) {
  483. x = img->win->w / 2;
  484. y = img->win->h / 2;
  485. }
  486. img->x = x - (x - img->x) * z / img->zoom;
  487. img->y = y - (y - img->y) * z / img->zoom;
  488. img->zoom = z;
  489. img->checkpan = true;
  490. img->dirty = true;
  491. return true;
  492. } else {
  493. return false;
  494. }
  495. }
  496. bool img_zoom_in(img_t *img)
  497. {
  498. int i;
  499. float z;
  500. for (i = 0; i < ARRLEN(zoom_levels); i++) {
  501. z = zoom_levels[i] / 100.0;
  502. if (zoomdiff(img, z) > 0)
  503. return img_zoom(img, z);
  504. }
  505. return false;
  506. }
  507. bool img_zoom_out(img_t *img)
  508. {
  509. int i;
  510. float z;
  511. for (i = ARRLEN(zoom_levels) - 1; i >= 0; i--) {
  512. z = zoom_levels[i] / 100.0;
  513. if (zoomdiff(img, z) < 0)
  514. return img_zoom(img, z);
  515. }
  516. return false;
  517. }
  518. bool img_pos(img_t *img, float x, float y)
  519. {
  520. float ox, oy;
  521. ox = img->x;
  522. oy = img->y;
  523. img->x = x;
  524. img->y = y;
  525. img_check_pan(img, true);
  526. if (ox != img->x || oy != img->y) {
  527. img->dirty = true;
  528. return true;
  529. } else {
  530. return false;
  531. }
  532. }
  533. bool img_move(img_t *img, float dx, float dy)
  534. {
  535. return img_pos(img, img->x + dx, img->y + dy);
  536. }
  537. bool img_pan(img_t *img, direction_t dir, int d)
  538. {
  539. /* d < 0: screen-wise
  540. * d = 0: 1/PAN_FRACTION of screen
  541. * d > 0: num of pixels
  542. */
  543. float x, y;
  544. if (d > 0) {
  545. x = y = MAX(1, (float) d * img->zoom);
  546. } else {
  547. x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
  548. y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
  549. }
  550. switch (dir) {
  551. case DIR_LEFT:
  552. return img_move(img, x, 0.0);
  553. case DIR_RIGHT:
  554. return img_move(img, -x, 0.0);
  555. case DIR_UP:
  556. return img_move(img, 0.0, y);
  557. case DIR_DOWN:
  558. return img_move(img, 0.0, -y);
  559. }
  560. return false;
  561. }
  562. bool img_pan_edge(img_t *img, direction_t dir)
  563. {
  564. float ox, oy;
  565. ox = img->x;
  566. oy = img->y;
  567. if (dir & DIR_LEFT)
  568. img->x = 0;
  569. if (dir & DIR_RIGHT)
  570. img->x = img->win->w - img->w * img->zoom;
  571. if (dir & DIR_UP)
  572. img->y = 0;
  573. if (dir & DIR_DOWN)
  574. img->y = img->win->h - img->h * img->zoom;
  575. img_check_pan(img, true);
  576. if (ox != img->x || oy != img->y) {
  577. img->dirty = true;
  578. return true;
  579. } else {
  580. return false;
  581. }
  582. }
  583. void img_rotate(img_t *img, degree_t d)
  584. {
  585. int i, tmp;
  586. float ox, oy;
  587. imlib_context_set_image(img->im);
  588. imlib_image_orientate(d);
  589. for (i = 0; i < img->multi.cnt; i++) {
  590. if (i != img->multi.sel) {
  591. imlib_context_set_image(img->multi.frames[i].im);
  592. imlib_image_orientate(d);
  593. }
  594. }
  595. if (d == DEGREE_90 || d == DEGREE_270) {
  596. ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
  597. oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
  598. img->x = oy + (img->win->w - img->win->h) / 2;
  599. img->y = ox + (img->win->h - img->win->w) / 2;
  600. tmp = img->w;
  601. img->w = img->h;
  602. img->h = tmp;
  603. img->checkpan = true;
  604. }
  605. img->dirty = true;
  606. }
  607. void img_flip(img_t *img, flipdir_t d)
  608. {
  609. int i;
  610. void (*imlib_flip_op[3])(void) = {
  611. imlib_image_flip_horizontal,
  612. imlib_image_flip_vertical,
  613. imlib_image_flip_diagonal
  614. };
  615. d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
  616. if (d < 0 || d >= ARRLEN(imlib_flip_op))
  617. return;
  618. imlib_context_set_image(img->im);
  619. imlib_flip_op[d]();
  620. for (i = 0; i < img->multi.cnt; i++) {
  621. if (i != img->multi.sel) {
  622. imlib_context_set_image(img->multi.frames[i].im);
  623. imlib_flip_op[d]();
  624. }
  625. }
  626. img->dirty = true;
  627. }
  628. void img_toggle_antialias(img_t *img)
  629. {
  630. img->aa = !img->aa;
  631. imlib_context_set_image(img->im);
  632. imlib_context_set_anti_alias(img->aa);
  633. img->dirty = true;
  634. }
  635. bool img_change_gamma(img_t *img, int d)
  636. {
  637. /* d < 0: decrease gamma
  638. * d = 0: reset gamma
  639. * d > 0: increase gamma
  640. */
  641. int gamma;
  642. double range;
  643. if (d == 0)
  644. gamma = 0;
  645. else
  646. gamma = MIN(MAX(img->gamma + d, -GAMMA_RANGE), GAMMA_RANGE);
  647. if (img->gamma != gamma) {
  648. imlib_reset_color_modifier();
  649. if (gamma != 0) {
  650. range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
  651. imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
  652. }
  653. img->gamma = gamma;
  654. img->dirty = true;
  655. return true;
  656. } else {
  657. return false;
  658. }
  659. }
  660. bool img_frame_goto(img_t *img, int n)
  661. {
  662. if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
  663. return false;
  664. img->multi.sel = n;
  665. img->im = img->multi.frames[n].im;
  666. imlib_context_set_image(img->im);
  667. img->w = imlib_image_get_width();
  668. img->h = imlib_image_get_height();
  669. img->checkpan = true;
  670. img->dirty = true;
  671. return true;
  672. }
  673. bool img_frame_navigate(img_t *img, int d)
  674. {
  675. if (img->multi.cnt == 0 || d == 0)
  676. return false;
  677. d += img->multi.sel;
  678. if (d < 0)
  679. d = 0;
  680. else if (d >= img->multi.cnt)
  681. d = img->multi.cnt - 1;
  682. return img_frame_goto(img, d);
  683. }
  684. bool img_frame_animate(img_t *img)
  685. {
  686. if (img->multi.cnt == 0)
  687. return false;
  688. if (img->multi.sel + 1 >= img->multi.cnt)
  689. img_frame_goto(img, 0);
  690. else
  691. img_frame_goto(img, img->multi.sel + 1);
  692. img->dirty = true;
  693. return true;
  694. }