A Simple X Image Viewer
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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