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.

image.c 16 KiB

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