A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

801 řádky
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. 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. int ox, oy;
  325. float w, h;
  326. win = img->win;
  327. w = img->w * img->zoom;
  328. h = img->h * img->zoom;
  329. ox = img->x;
  330. oy = img->y;
  331. if (w < win->w)
  332. img->x = (win->w - w) / 2;
  333. else if (img->x > 0)
  334. img->x = 0;
  335. else if (img->x + w < win->w)
  336. img->x = win->w - w;
  337. if (h < win->h)
  338. img->y = (win->h - h) / 2;
  339. else if (img->y > 0)
  340. img->y = 0;
  341. else if (img->y + h < win->h)
  342. img->y = win->h - h;
  343. if (!moved && (ox != img->x || oy != img->y))
  344. img->dirty = true;
  345. }
  346. bool img_fit(img_t *img)
  347. {
  348. float z, zmax, zw, zh;
  349. if (img->scalemode == SCALE_ZOOM)
  350. return false;
  351. zmax = img->scalemode == SCALE_DOWN ? 1.0 : zoom_max;
  352. zw = (float) img->win->w / (float) img->w;
  353. zh = (float) img->win->h / (float) img->h;
  354. switch (img->scalemode) {
  355. case SCALE_WIDTH:
  356. z = zw;
  357. break;
  358. case SCALE_HEIGHT:
  359. z = zh;
  360. break;
  361. default:
  362. z = MIN(zw, zh);
  363. break;
  364. }
  365. z = MAX(z, zoom_min);
  366. z = MIN(z, zmax);
  367. if (zoomdiff(z, img->zoom) != 0) {
  368. img->zoom = z;
  369. img->dirty = true;
  370. return true;
  371. } else {
  372. return false;
  373. }
  374. }
  375. void img_render(img_t *img)
  376. {
  377. win_t *win;
  378. int sx, sy, sw, sh;
  379. int dx, dy, dw, dh;
  380. Imlib_Image bg;
  381. unsigned long c;
  382. win = img->win;
  383. img_fit(img);
  384. if (img->checkpan) {
  385. img_check_pan(img, false);
  386. img->checkpan = false;
  387. }
  388. if (!img->dirty)
  389. return;
  390. /* calculate source and destination offsets:
  391. * - part of image drawn on full window, or
  392. * - full image drawn on part of window
  393. */
  394. if (img->x <= 0) {
  395. sx = -img->x / img->zoom + 0.5;
  396. sw = win->w / img->zoom;
  397. dx = 0;
  398. dw = win->w;
  399. } else {
  400. sx = 0;
  401. sw = img->w;
  402. dx = img->x;
  403. dw = img->w * img->zoom;
  404. }
  405. if (img->y <= 0) {
  406. sy = -img->y / img->zoom + 0.5;
  407. sh = win->h / img->zoom;
  408. dy = 0;
  409. dh = win->h;
  410. } else {
  411. sy = 0;
  412. sh = img->h;
  413. dy = img->y;
  414. dh = img->h * img->zoom;
  415. }
  416. win_clear(win);
  417. imlib_context_set_image(img->im);
  418. imlib_context_set_anti_alias(img->aa);
  419. imlib_context_set_drawable(win->buf.pm);
  420. if (imlib_image_has_alpha()) {
  421. if ((bg = imlib_create_image(dw, dh)) == NULL)
  422. error(EXIT_FAILURE, ENOMEM, NULL);
  423. imlib_context_set_image(bg);
  424. imlib_image_set_has_alpha(0);
  425. if (img->alpha) {
  426. int i, c, r;
  427. DATA32 col[2] = { 0xFF666666, 0xFF999999 };
  428. DATA32 * data = imlib_image_get_data();
  429. for (r = 0; r < dh; r++) {
  430. i = r * dw;
  431. if (r == 0 || r == 8) {
  432. for (c = 0; c < dw; c++)
  433. data[i++] = col[!(c & 8) ^ !r];
  434. } else {
  435. memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
  436. }
  437. }
  438. imlib_image_put_back_data(data);
  439. } else {
  440. c = win->fullscreen ? win->fscol.pixel : win->bgcol.pixel;
  441. imlib_context_set_color(c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF, 0xFF);
  442. imlib_image_fill_rectangle(0, 0, dw, dh);
  443. }
  444. imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
  445. imlib_context_set_color_modifier(NULL);
  446. imlib_render_image_on_drawable(dx, dy);
  447. imlib_free_image();
  448. imlib_context_set_color_modifier(img->cmod);
  449. } else {
  450. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  451. }
  452. img->dirty = false;
  453. }
  454. bool img_fit_win(img_t *img, scalemode_t sm)
  455. {
  456. float oz;
  457. oz = img->zoom;
  458. img->scalemode = sm;
  459. if (img_fit(img)) {
  460. img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
  461. img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
  462. img->checkpan = true;
  463. return true;
  464. } else {
  465. return false;
  466. }
  467. }
  468. bool img_zoom(img_t *img, float z)
  469. {
  470. z = MAX(z, zoom_min);
  471. z = MIN(z, zoom_max);
  472. img->scalemode = SCALE_ZOOM;
  473. if (zoomdiff(z, img->zoom) != 0) {
  474. int x, y;
  475. win_cursor_pos(img->win, &x, &y);
  476. if (x < 0 || x >= img->win->w || y < 0 || y >= img->win->h) {
  477. x = img->win->w / 2;
  478. y = img->win->h / 2;
  479. }
  480. img->x = x - (x - img->x) * z / img->zoom;
  481. img->y = y - (y - img->y) * z / img->zoom;
  482. img->zoom = z;
  483. img->checkpan = true;
  484. img->dirty = true;
  485. return true;
  486. } else {
  487. return false;
  488. }
  489. }
  490. bool img_zoom_in(img_t *img)
  491. {
  492. int i;
  493. float z;
  494. for (i = 1; i < ARRLEN(zoom_levels); i++) {
  495. z = zoom_levels[i] / 100.0;
  496. if (zoomdiff(z, img->zoom) > 0)
  497. return img_zoom(img, z);
  498. }
  499. return false;
  500. }
  501. bool img_zoom_out(img_t *img)
  502. {
  503. int i;
  504. float z;
  505. for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
  506. z = zoom_levels[i] / 100.0;
  507. if (zoomdiff(z, img->zoom) < 0)
  508. return img_zoom(img, z);
  509. }
  510. return false;
  511. }
  512. bool img_pos(img_t *img, float x, float y)
  513. {
  514. float ox, oy;
  515. ox = img->x;
  516. oy = img->y;
  517. img->x = x;
  518. img->y = y;
  519. img_check_pan(img, true);
  520. if (ox != img->x || oy != img->y) {
  521. img->dirty = true;
  522. return true;
  523. } else {
  524. return false;
  525. }
  526. }
  527. bool img_move(img_t *img, float dx, float dy)
  528. {
  529. return img_pos(img, img->x + dx, img->y + dy);
  530. }
  531. bool img_pan(img_t *img, direction_t dir, int d)
  532. {
  533. /* d < 0: screen-wise
  534. * d = 0: 1/PAN_FRACTION of screen
  535. * d > 0: num of pixels
  536. */
  537. float x, y;
  538. if (d > 0) {
  539. x = y = MAX(1, (float) d * img->zoom);
  540. } else {
  541. x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
  542. y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
  543. }
  544. switch (dir) {
  545. case DIR_LEFT:
  546. return img_move(img, x, 0.0);
  547. case DIR_RIGHT:
  548. return img_move(img, -x, 0.0);
  549. case DIR_UP:
  550. return img_move(img, 0.0, y);
  551. case DIR_DOWN:
  552. return img_move(img, 0.0, -y);
  553. }
  554. return false;
  555. }
  556. bool img_pan_edge(img_t *img, direction_t dir)
  557. {
  558. int ox, oy;
  559. ox = img->x;
  560. oy = img->y;
  561. if (dir & DIR_LEFT)
  562. img->x = 0;
  563. if (dir & DIR_RIGHT)
  564. img->x = img->win->w - img->w * img->zoom;
  565. if (dir & DIR_UP)
  566. img->y = 0;
  567. if (dir & DIR_DOWN)
  568. img->y = img->win->h - img->h * img->zoom;
  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 i, ox, oy, tmp;
  580. imlib_context_set_image(img->im);
  581. imlib_image_orientate(d);
  582. for (i = 0; i < img->multi.cnt; i++) {
  583. if (i != img->multi.sel) {
  584. imlib_context_set_image(img->multi.frames[i].im);
  585. imlib_image_orientate(d);
  586. }
  587. }
  588. if (d == DEGREE_90 || d == DEGREE_270) {
  589. ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
  590. oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
  591. img->x = oy + (img->win->w - img->win->h) / 2;
  592. img->y = ox + (img->win->h - img->win->w) / 2;
  593. tmp = img->w;
  594. img->w = img->h;
  595. img->h = tmp;
  596. img->checkpan = true;
  597. }
  598. img->dirty = true;
  599. }
  600. void img_flip(img_t *img, flipdir_t d)
  601. {
  602. int i;
  603. void (*imlib_flip_op[3])(void) = {
  604. imlib_image_flip_horizontal,
  605. imlib_image_flip_vertical,
  606. imlib_image_flip_diagonal
  607. };
  608. d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
  609. if (d < 0 || d >= ARRLEN(imlib_flip_op))
  610. return;
  611. imlib_context_set_image(img->im);
  612. imlib_flip_op[d]();
  613. for (i = 0; i < img->multi.cnt; i++) {
  614. if (i != img->multi.sel) {
  615. imlib_context_set_image(img->multi.frames[i].im);
  616. imlib_flip_op[d]();
  617. }
  618. }
  619. img->dirty = true;
  620. }
  621. void img_toggle_antialias(img_t *img)
  622. {
  623. img->aa = !img->aa;
  624. imlib_context_set_image(img->im);
  625. imlib_context_set_anti_alias(img->aa);
  626. img->dirty = true;
  627. }
  628. bool img_change_gamma(img_t *img, int d)
  629. {
  630. /* d < 0: decrease gamma
  631. * d = 0: reset gamma
  632. * d > 0: increase gamma
  633. */
  634. int gamma;
  635. double range;
  636. if (d == 0)
  637. gamma = 0;
  638. else
  639. gamma = MIN(MAX(img->gamma + d, -GAMMA_RANGE), GAMMA_RANGE);
  640. if (img->gamma != gamma) {
  641. imlib_reset_color_modifier();
  642. if (gamma != 0) {
  643. range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
  644. imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
  645. }
  646. img->gamma = gamma;
  647. img->dirty = true;
  648. return true;
  649. } else {
  650. return false;
  651. }
  652. }
  653. bool img_frame_goto(img_t *img, int n)
  654. {
  655. if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
  656. return false;
  657. img->multi.sel = n;
  658. img->im = img->multi.frames[n].im;
  659. imlib_context_set_image(img->im);
  660. img->w = imlib_image_get_width();
  661. img->h = imlib_image_get_height();
  662. img->checkpan = true;
  663. img->dirty = true;
  664. return true;
  665. }
  666. bool img_frame_navigate(img_t *img, int d)
  667. {
  668. if (img->multi.cnt == 0 || d == 0)
  669. return false;
  670. d += img->multi.sel;
  671. if (d < 0)
  672. d = 0;
  673. else if (d >= img->multi.cnt)
  674. d = img->multi.cnt - 1;
  675. return img_frame_goto(img, d);
  676. }
  677. bool img_frame_animate(img_t *img)
  678. {
  679. if (img->multi.cnt == 0)
  680. return false;
  681. if (img->multi.sel + 1 >= img->multi.cnt)
  682. img_frame_goto(img, 0);
  683. else
  684. img_frame_goto(img, img->multi.sel + 1);
  685. img->dirty = true;
  686. return true;
  687. }