A Simple X Image Viewer
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

image.c 17 KiB

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