A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

868 řádky
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 { MIN_GIF_DELAY = 25 };
  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 = false;
  75. img->multi.length = img->multi.repeat = 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 = img->multi.repeat = 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. if (delay)
  173. delay = MAX(delay, MIN_GIF_DELAY);
  174. disposal = (unsigned int) ext[1] >> 2 & 0x7;
  175. } else if (ext_code == APPLICATION_EXT_FUNC_CODE) {
  176. if (ext[0] == 11 && memcmp(ext+1, "NETSCAPE2.0", 11) == 0) {
  177. DGifGetExtensionNext(gif, &ext);
  178. if (ext && ext[0] == 3 && ext[1] == 1)
  179. img->multi.repeat = ((int) ext[3] << 8 | (int) ext[2]) - 1;
  180. }
  181. }
  182. ext = NULL;
  183. DGifGetExtensionNext(gif, &ext);
  184. }
  185. } else if (rec == IMAGE_DESC_RECORD_TYPE) {
  186. if (DGifGetImageDesc(gif) == GIF_ERROR) {
  187. err = true;
  188. break;
  189. }
  190. x = gif->Image.Left;
  191. y = gif->Image.Top;
  192. w = gif->Image.Width;
  193. h = gif->Image.Height;
  194. rows = (GifRowType*) s_malloc(h * sizeof(GifRowType));
  195. for (i = 0; i < h; i++)
  196. rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType));
  197. if (gif->Image.Interlace) {
  198. for (i = 0; i < 4; i++) {
  199. for (j = intoffset[i]; j < h; j += intjump[i])
  200. DGifGetLine(gif, rows[j], w);
  201. }
  202. } else {
  203. for (i = 0; i < h; i++)
  204. DGifGetLine(gif, rows[i], w);
  205. }
  206. ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh);
  207. cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
  208. r = cmap->Colors[bg].Red;
  209. g = cmap->Colors[bg].Green;
  210. b = cmap->Colors[bg].Blue;
  211. bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
  212. for (i = 0; i < sh; i++) {
  213. for (j = 0; j < sw; j++) {
  214. if (i < y || i >= y + h || j < x || j >= x + w ||
  215. rows[i-y][j-x] == transp)
  216. {
  217. if (prev_frame != NULL && (prev_disposal != 2 ||
  218. i < py || i >= py + ph || j < px || j >= px + pw))
  219. {
  220. *ptr = prev_frame[i * sw + j];
  221. } else {
  222. *ptr = bgpixel;
  223. }
  224. } else {
  225. r = cmap->Colors[rows[i-y][j-x]].Red;
  226. g = cmap->Colors[rows[i-y][j-x]].Green;
  227. b = cmap->Colors[rows[i-y][j-x]].Blue;
  228. *ptr = 0xff << 24 | r << 16 | g << 8 | b;
  229. }
  230. ptr++;
  231. }
  232. }
  233. im = imlib_create_image_using_copied_data(sw, sh, data);
  234. for (i = 0; i < h; i++)
  235. free(rows[i]);
  236. free(rows);
  237. free(data);
  238. if (im == NULL) {
  239. err = true;
  240. break;
  241. }
  242. imlib_context_set_image(im);
  243. imlib_image_set_format("gif");
  244. if (transp >= 0)
  245. imlib_image_set_has_alpha(1);
  246. if (disposal != 3)
  247. prev_frame = imlib_image_get_data_for_reading_only();
  248. prev_disposal = disposal;
  249. px = x, py = y, pw = w, ph = h;
  250. if (img->multi.cnt == img->multi.cap) {
  251. img->multi.cap *= 2;
  252. img->multi.frames = (img_frame_t*)
  253. s_realloc(img->multi.frames,
  254. img->multi.cap * sizeof(img_frame_t));
  255. }
  256. img->multi.frames[img->multi.cnt].im = im;
  257. img->multi.frames[img->multi.cnt].delay = delay ? delay : GIF_DELAY;
  258. img->multi.length += img->multi.frames[img->multi.cnt].delay;
  259. img->multi.cnt++;
  260. }
  261. } while (rec != TERMINATE_RECORD_TYPE);
  262. #if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 && GIFLIB_MINOR >= 1
  263. DGifCloseFile(gif, NULL);
  264. #else
  265. DGifCloseFile(gif);
  266. #endif
  267. if (err && !file->loaded)
  268. warn("corrupted gif file: %s", file->name);
  269. if (img->multi.cnt > 1) {
  270. imlib_context_set_image(img->im);
  271. imlib_free_image();
  272. img->im = img->multi.frames[0].im;
  273. img->multi.animate = GIF_AUTOPLAY;
  274. } else if (img->multi.cnt == 1) {
  275. imlib_context_set_image(img->multi.frames[0].im);
  276. imlib_free_image();
  277. img->multi.cnt = 0;
  278. img->multi.animate = false;
  279. }
  280. imlib_context_set_image(img->im);
  281. return !err;
  282. }
  283. #endif /* HAVE_GIFLIB */
  284. bool img_load(img_t *img, const fileinfo_t *file)
  285. {
  286. const char *fmt;
  287. if (img == NULL || file == NULL || file->name == NULL || file->path == NULL)
  288. return false;
  289. if (access(file->path, R_OK) < 0 ||
  290. (img->im = imlib_load_image(file->path)) == NULL)
  291. {
  292. warn("could not open image: %s", file->name);
  293. return false;
  294. }
  295. imlib_context_set_image(img->im);
  296. imlib_image_set_changes_on_disk();
  297. #if HAVE_LIBEXIF
  298. exif_auto_orientate(file);
  299. #endif
  300. if ((fmt = imlib_image_format()) != NULL) {
  301. #if HAVE_GIFLIB
  302. if (STREQ(fmt, "gif"))
  303. img_load_gif(img, file);
  304. #endif
  305. }
  306. img_apply_gamma(img);
  307. img->w = imlib_image_get_width();
  308. img->h = imlib_image_get_height();
  309. img->checkpan = true;
  310. img->dirty = true;
  311. return true;
  312. }
  313. void img_close(img_t *img, bool decache)
  314. {
  315. int i;
  316. if (img == NULL)
  317. return;
  318. if (img->multi.cnt > 0) {
  319. for (i = 0; i < img->multi.cnt; i++) {
  320. imlib_context_set_image(img->multi.frames[i].im);
  321. imlib_free_image();
  322. }
  323. img->multi.cnt = 0;
  324. img->im = NULL;
  325. } else if (img->im != NULL) {
  326. imlib_context_set_image(img->im);
  327. if (decache)
  328. imlib_free_image_and_decache();
  329. else
  330. imlib_free_image();
  331. img->im = NULL;
  332. }
  333. if (img->cmod)
  334. imlib_context_set_color_modifier(NULL);
  335. }
  336. void img_check_pan(img_t *img, bool moved)
  337. {
  338. win_t *win;
  339. int ox, oy;
  340. float w, h;
  341. if (img == NULL || img->im == NULL || img->win == NULL)
  342. return;
  343. win = img->win;
  344. w = img->w * img->zoom;
  345. h = img->h * img->zoom;
  346. ox = img->x;
  347. oy = img->y;
  348. if (w < win->w)
  349. img->x = (win->w - w) / 2;
  350. else if (img->x > 0)
  351. img->x = 0;
  352. else if (img->x + w < win->w)
  353. img->x = win->w - w;
  354. if (h < win->h)
  355. img->y = (win->h - h) / 2;
  356. else if (img->y > 0)
  357. img->y = 0;
  358. else if (img->y + h < win->h)
  359. img->y = win->h - h;
  360. if (!moved && (ox != img->x || oy != img->y))
  361. img->dirty = true;
  362. }
  363. bool img_fit(img_t *img)
  364. {
  365. float z, zmax, zw, zh;
  366. if (img == NULL || img->im == NULL || img->win == NULL)
  367. return false;
  368. if (img->scalemode == SCALE_ZOOM)
  369. return false;
  370. zmax = img->scalemode == SCALE_DOWN ? 1.0 : zoom_max;
  371. zw = (float) img->win->w / (float) img->w;
  372. zh = (float) img->win->h / (float) img->h;
  373. switch (img->scalemode) {
  374. case SCALE_WIDTH:
  375. z = zw;
  376. break;
  377. case SCALE_HEIGHT:
  378. z = zh;
  379. break;
  380. default:
  381. z = MIN(zw, zh);
  382. break;
  383. }
  384. z = MAX(z, zoom_min);
  385. z = MIN(z, zmax);
  386. if (zoomdiff(z, img->zoom) != 0) {
  387. img->zoom = z;
  388. img->dirty = true;
  389. return true;
  390. } else {
  391. return false;
  392. }
  393. }
  394. void img_render(img_t *img)
  395. {
  396. win_t *win;
  397. int sx, sy, sw, sh;
  398. int dx, dy, dw, dh;
  399. Imlib_Image bg;
  400. unsigned long c;
  401. if (img == NULL || img->im == NULL || img->win == NULL)
  402. return;
  403. win = img->win;
  404. img_fit(img);
  405. if (img->checkpan) {
  406. img_check_pan(img, false);
  407. img->checkpan = false;
  408. }
  409. if (!img->dirty)
  410. return;
  411. /* calculate source and destination offsets:
  412. * - part of image drawn on full window, or
  413. * - full image drawn on part of window
  414. */
  415. if (img->x <= 0) {
  416. sx = -img->x / img->zoom;
  417. sw = win->w / img->zoom;
  418. dx = 0;
  419. dw = win->w;
  420. } else {
  421. sx = 0;
  422. sw = img->w;
  423. dx = img->x;
  424. dw = img->w * img->zoom;
  425. }
  426. if (img->y <= 0) {
  427. sy = -img->y / img->zoom;
  428. sh = win->h / img->zoom;
  429. dy = 0;
  430. dh = win->h;
  431. } else {
  432. sy = 0;
  433. sh = img->h;
  434. dy = img->y;
  435. dh = img->h * img->zoom;
  436. }
  437. win_clear(win);
  438. imlib_context_set_image(img->im);
  439. imlib_context_set_anti_alias(img->aa);
  440. imlib_context_set_drawable(win->pm);
  441. if (imlib_image_has_alpha()) {
  442. if ((bg = imlib_create_image(dw, dh)) == NULL)
  443. die("could not allocate memory");
  444. imlib_context_set_image(bg);
  445. imlib_image_set_has_alpha(0);
  446. if (img->alpha) {
  447. int i, c, r;
  448. DATA32 col[2] = { 0xFF666666, 0xFF999999 };
  449. DATA32 * data = imlib_image_get_data();
  450. for (r = 0; r < dh; r++) {
  451. i = r * dw;
  452. if (r == 0 || r == 8) {
  453. for (c = 0; c < dw; c++)
  454. data[i++] = col[!(c & 8) ^ !r];
  455. } else {
  456. memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
  457. }
  458. }
  459. imlib_image_put_back_data(data);
  460. } else {
  461. c = win->fullscreen ? win->fscol : win->bgcol;
  462. imlib_context_set_color(c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF, 0xFF);
  463. imlib_image_fill_rectangle(0, 0, dw, dh);
  464. }
  465. imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
  466. imlib_context_set_color_modifier(NULL);
  467. imlib_render_image_on_drawable(dx, dy);
  468. imlib_free_image();
  469. if (img->gamma != 0)
  470. imlib_context_set_color_modifier(img->cmod);
  471. } else {
  472. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  473. }
  474. img->dirty = false;
  475. }
  476. bool img_fit_win(img_t *img, scalemode_t sm)
  477. {
  478. float oz;
  479. if (img == NULL || img->im == NULL || img->win == NULL)
  480. return false;
  481. oz = img->zoom;
  482. img->scalemode = sm;
  483. if (img_fit(img)) {
  484. img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
  485. img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
  486. img->checkpan = true;
  487. return true;
  488. } else {
  489. return false;
  490. }
  491. }
  492. bool img_zoom(img_t *img, float z)
  493. {
  494. if (img == NULL || img->im == NULL || img->win == NULL)
  495. return false;
  496. z = MAX(z, zoom_min);
  497. z = MIN(z, zoom_max);
  498. img->scalemode = SCALE_ZOOM;
  499. if (zoomdiff(z, img->zoom) != 0) {
  500. img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * z / img->zoom;
  501. img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * z / img->zoom;
  502. img->zoom = z;
  503. img->checkpan = true;
  504. img->dirty = true;
  505. return true;
  506. } else {
  507. return false;
  508. }
  509. }
  510. bool img_zoom_in(img_t *img)
  511. {
  512. int i;
  513. float z;
  514. if (img == NULL || img->im == NULL)
  515. return false;
  516. for (i = 1; i < ARRLEN(zoom_levels); i++) {
  517. z = zoom_levels[i] / 100.0;
  518. if (zoomdiff(z, img->zoom) > 0)
  519. return img_zoom(img, z);
  520. }
  521. return false;
  522. }
  523. bool img_zoom_out(img_t *img)
  524. {
  525. int i;
  526. float z;
  527. if (img == NULL || img->im == NULL)
  528. return false;
  529. for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
  530. z = zoom_levels[i] / 100.0;
  531. if (zoomdiff(z, img->zoom) < 0)
  532. return img_zoom(img, z);
  533. }
  534. return false;
  535. }
  536. bool img_move(img_t *img, float dx, float dy)
  537. {
  538. float ox, oy;
  539. if (img == NULL || img->im == NULL)
  540. return false;
  541. ox = img->x;
  542. oy = img->y;
  543. img->x += dx;
  544. img->y += dy;
  545. img_check_pan(img, true);
  546. if (ox != img->x || oy != img->y) {
  547. img->dirty = true;
  548. return true;
  549. } else {
  550. return false;
  551. }
  552. }
  553. bool img_pan(img_t *img, direction_t dir, int d)
  554. {
  555. /* d < 0: screen-wise
  556. * d = 0: 1/5 of screen
  557. * d > 0: num of pixels
  558. */
  559. float x, y;
  560. if (img == NULL || img->im == NULL || img->win == NULL)
  561. return false;
  562. if (d > 0) {
  563. x = y = MAX(1, (float) d * img->zoom);
  564. } else {
  565. x = img->win->w / (d < 0 ? 1 : 5);
  566. y = img->win->h / (d < 0 ? 1 : 5);
  567. }
  568. switch (dir) {
  569. case DIR_LEFT:
  570. return img_move(img, x, 0.0);
  571. case DIR_RIGHT:
  572. return img_move(img, -x, 0.0);
  573. case DIR_UP:
  574. return img_move(img, 0.0, y);
  575. case DIR_DOWN:
  576. return img_move(img, 0.0, -y);
  577. }
  578. return false;
  579. }
  580. bool img_pan_edge(img_t *img, direction_t dir)
  581. {
  582. int ox, oy;
  583. if (img == NULL || img->im == NULL || img->win == NULL)
  584. return false;
  585. ox = img->x;
  586. oy = img->y;
  587. switch (dir) {
  588. case DIR_LEFT:
  589. img->x = 0;
  590. break;
  591. case DIR_RIGHT:
  592. img->x = img->win->w - img->w * img->zoom;
  593. break;
  594. case DIR_UP:
  595. img->y = 0;
  596. break;
  597. case DIR_DOWN:
  598. img->y = img->win->h - img->h * img->zoom;
  599. break;
  600. }
  601. img_check_pan(img, true);
  602. if (ox != img->x || oy != img->y) {
  603. img->dirty = true;
  604. return true;
  605. } else {
  606. return false;
  607. }
  608. }
  609. void img_rotate(img_t *img, degree_t d)
  610. {
  611. int i, ox, oy, tmp;
  612. if (img == NULL || img->im == NULL || img->win == NULL)
  613. return;
  614. imlib_context_set_image(img->im);
  615. imlib_image_orientate(d);
  616. for (i = 0; i < img->multi.cnt; i++) {
  617. if (i != img->multi.sel) {
  618. imlib_context_set_image(img->multi.frames[i].im);
  619. imlib_image_orientate(d);
  620. }
  621. }
  622. if (d == DEGREE_90 || d == DEGREE_270) {
  623. ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
  624. oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
  625. img->x = oy + (img->win->w - img->win->h) / 2;
  626. img->y = ox + (img->win->h - img->win->w) / 2;
  627. tmp = img->w;
  628. img->w = img->h;
  629. img->h = tmp;
  630. img->checkpan = true;
  631. }
  632. img->dirty = true;
  633. }
  634. void img_flip(img_t *img, flipdir_t d)
  635. {
  636. int i;
  637. void (*imlib_flip_op[3])(void) = {
  638. imlib_image_flip_horizontal,
  639. imlib_image_flip_vertical,
  640. imlib_image_flip_diagonal
  641. };
  642. d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
  643. if (img == NULL || img->im == NULL || d < 0 || d >= ARRLEN(imlib_flip_op))
  644. return;
  645. imlib_context_set_image(img->im);
  646. imlib_flip_op[d]();
  647. for (i = 0; i < img->multi.cnt; i++) {
  648. if (i != img->multi.sel) {
  649. imlib_context_set_image(img->multi.frames[i].im);
  650. imlib_flip_op[d]();
  651. }
  652. }
  653. img->dirty = true;
  654. }
  655. void img_toggle_antialias(img_t *img)
  656. {
  657. if (img == NULL || img->im == NULL)
  658. return;
  659. img->aa = !img->aa;
  660. imlib_context_set_image(img->im);
  661. imlib_context_set_anti_alias(img->aa);
  662. img->dirty = true;
  663. }
  664. bool img_change_gamma(img_t *img, int d)
  665. {
  666. /* d < 0: decrease gamma
  667. * d = 0: reset gamma
  668. * d > 0: increase gamma
  669. */
  670. int gamma;
  671. if (img == NULL || img->im == NULL)
  672. return false;
  673. if (d == 0)
  674. gamma = 0;
  675. else if (d < 0)
  676. gamma = MAX(-GAMMA_RANGE, img->gamma - 1);
  677. else
  678. gamma = MIN(+GAMMA_RANGE, img->gamma + 1);
  679. if (img->gamma != gamma) {
  680. img->gamma = gamma;
  681. img_apply_gamma(img);
  682. img->dirty = true;
  683. return true;
  684. } else {
  685. return false;
  686. }
  687. }
  688. bool img_frame_goto(img_t *img, int n)
  689. {
  690. if (img == NULL || img->im == NULL)
  691. return false;
  692. if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
  693. return false;
  694. img->multi.sel = n;
  695. img->im = img->multi.frames[n].im;
  696. imlib_context_set_image(img->im);
  697. img->w = imlib_image_get_width();
  698. img->h = imlib_image_get_height();
  699. img->checkpan = true;
  700. img->dirty = true;
  701. return true;
  702. }
  703. bool img_frame_navigate(img_t *img, int d)
  704. {
  705. if (img == NULL|| img->im == NULL || img->multi.cnt == 0 || d == 0)
  706. return false;
  707. d += img->multi.sel;
  708. if (d < 0)
  709. d = 0;
  710. else if (d >= img->multi.cnt)
  711. d = img->multi.cnt - 1;
  712. return img_frame_goto(img, d);
  713. }
  714. bool img_frame_animate(img_t *img, bool restart)
  715. {
  716. if (img == NULL || img->im == NULL || img->multi.cnt == 0)
  717. return false;
  718. if (img->multi.sel + 1 >= img->multi.cnt) {
  719. if (restart || GIF_LOOP == 1) {
  720. img_frame_goto(img, 0);
  721. } else if (GIF_LOOP == -1 && img->multi.repeat != 0) {
  722. if (img->multi.repeat > 0)
  723. img->multi.repeat--;
  724. img_frame_goto(img, 0);
  725. } else {
  726. img->multi.animate = false;
  727. return false;
  728. }
  729. } else if (!restart) {
  730. img_frame_goto(img, img->multi.sel + 1);
  731. }
  732. img->multi.animate = true;
  733. img->dirty = true;
  734. return true;
  735. }