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 17 KiB

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