A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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