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.

image.c 16 KiB

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