A Simple X Image Viewer
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

image.c 17 KiB

13 yıl önce
13 yıl önce
13 yıl önce
13 yıl önce
13 yıl önce
13 yıl önce
13 yıl önce
13 yıl önce
13 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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 { 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. if (img == NULL || win == NULL)
  46. return;
  47. imlib_context_set_display(win->env.dpy);
  48. imlib_context_set_visual(win->env.vis);
  49. imlib_context_set_colormap(win->env.cmap);
  50. img->im = NULL;
  51. img->win = win;
  52. img->scalemode = options->scalemode;
  53. img->zoom = options->zoom;
  54. img->zoom = MAX(img->zoom, zoom_min);
  55. img->zoom = MIN(img->zoom, zoom_max);
  56. img->checkpan = false;
  57. img->dirty = false;
  58. img->aa = ANTI_ALIAS;
  59. img->alpha = ALPHA_LAYER;
  60. img->multi.cap = img->multi.cnt = 0;
  61. img->multi.animate = options->animate;
  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;
  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. s_malloc(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. warn("could not open gif file: %s", 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*) s_malloc(h * sizeof(GifRowType));
  175. for (i = 0; i < h; i++)
  176. rows[i] = (GifRowType) s_malloc(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*) s_malloc(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 = 0xff << 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. s_realloc(img->multi.frames,
  234. img->multi.cap * sizeof(img_frame_t));
  235. }
  236. img->multi.frames[img->multi.cnt].im = im;
  237. img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY;
  238. img->multi.length += img->multi.frames[img->multi.cnt].delay;
  239. img->multi.cnt++;
  240. }
  241. } while (rec != TERMINATE_RECORD_TYPE);
  242. #if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 && GIFLIB_MINOR >= 1
  243. DGifCloseFile(gif, NULL);
  244. #else
  245. DGifCloseFile(gif);
  246. #endif
  247. if (err && (file->flags & FF_WARN))
  248. warn("corrupted gif file: %s", file->name);
  249. if (img->multi.cnt > 1) {
  250. imlib_context_set_image(img->im);
  251. imlib_free_image();
  252. img->im = img->multi.frames[0].im;
  253. } else if (img->multi.cnt == 1) {
  254. imlib_context_set_image(img->multi.frames[0].im);
  255. imlib_free_image();
  256. img->multi.cnt = 0;
  257. }
  258. imlib_context_set_image(img->im);
  259. return !err;
  260. }
  261. #endif /* HAVE_GIFLIB */
  262. bool img_load(img_t *img, const fileinfo_t *file)
  263. {
  264. const char *fmt;
  265. if (img == NULL || file == NULL || file->name == NULL || file->path == NULL)
  266. return false;
  267. if (access(file->path, R_OK) < 0 ||
  268. (img->im = imlib_load_image(file->path)) == NULL)
  269. {
  270. if (file->flags & FF_WARN)
  271. warn("could not open image: %s", file->name);
  272. return false;
  273. }
  274. imlib_context_set_image(img->im);
  275. imlib_image_set_changes_on_disk();
  276. #if HAVE_LIBEXIF
  277. exif_auto_orientate(file);
  278. #endif
  279. if ((fmt = imlib_image_format()) != NULL) {
  280. #if HAVE_GIFLIB
  281. if (STREQ(fmt, "gif"))
  282. img_load_gif(img, file);
  283. #endif
  284. }
  285. img->w = imlib_image_get_width();
  286. img->h = imlib_image_get_height();
  287. img->checkpan = true;
  288. img->dirty = true;
  289. return true;
  290. }
  291. void img_close(img_t *img, bool decache)
  292. {
  293. int i;
  294. if (img == NULL)
  295. return;
  296. if (img->multi.cnt > 0) {
  297. for (i = 0; i < img->multi.cnt; i++) {
  298. imlib_context_set_image(img->multi.frames[i].im);
  299. imlib_free_image();
  300. }
  301. img->multi.cnt = 0;
  302. img->im = NULL;
  303. } else if (img->im != NULL) {
  304. imlib_context_set_image(img->im);
  305. if (decache)
  306. imlib_free_image_and_decache();
  307. else
  308. imlib_free_image();
  309. img->im = NULL;
  310. }
  311. }
  312. void img_check_pan(img_t *img, bool moved)
  313. {
  314. win_t *win;
  315. int ox, oy;
  316. float w, h;
  317. if (img == NULL || img->im == NULL || img->win == NULL)
  318. return;
  319. win = img->win;
  320. w = img->w * img->zoom;
  321. h = img->h * img->zoom;
  322. ox = img->x;
  323. oy = img->y;
  324. if (w < win->w)
  325. img->x = (win->w - w) / 2;
  326. else if (img->x > 0)
  327. img->x = 0;
  328. else if (img->x + w < win->w)
  329. img->x = win->w - w;
  330. if (h < win->h)
  331. img->y = (win->h - h) / 2;
  332. else if (img->y > 0)
  333. img->y = 0;
  334. else if (img->y + h < win->h)
  335. img->y = win->h - h;
  336. if (!moved && (ox != img->x || oy != img->y))
  337. img->dirty = true;
  338. }
  339. bool img_fit(img_t *img)
  340. {
  341. float z, zmax, zw, zh;
  342. if (img == NULL || img->im == NULL || img->win == NULL)
  343. return false;
  344. if (img->scalemode == SCALE_ZOOM)
  345. return false;
  346. zmax = img->scalemode == SCALE_DOWN ? 1.0 : zoom_max;
  347. zw = (float) img->win->w / (float) img->w;
  348. zh = (float) img->win->h / (float) img->h;
  349. switch (img->scalemode) {
  350. case SCALE_WIDTH:
  351. z = zw;
  352. break;
  353. case SCALE_HEIGHT:
  354. z = zh;
  355. break;
  356. default:
  357. z = MIN(zw, zh);
  358. break;
  359. }
  360. z = MAX(z, zoom_min);
  361. z = MIN(z, zmax);
  362. if (zoomdiff(z, img->zoom) != 0) {
  363. img->zoom = z;
  364. img->dirty = true;
  365. return true;
  366. } else {
  367. return false;
  368. }
  369. }
  370. void img_render(img_t *img)
  371. {
  372. win_t *win;
  373. int sx, sy, sw, sh;
  374. int dx, dy, dw, dh;
  375. Imlib_Image bg;
  376. unsigned long c;
  377. if (img == NULL || img->im == NULL || img->win == NULL)
  378. return;
  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;
  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;
  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. die("could not allocate memory");
  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 : win->bgcol;
  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. if (img == NULL || img->im == NULL || img->win == NULL)
  455. return false;
  456. oz = img->zoom;
  457. img->scalemode = sm;
  458. if (img_fit(img)) {
  459. img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
  460. img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
  461. img->checkpan = true;
  462. return true;
  463. } else {
  464. return false;
  465. }
  466. }
  467. bool img_zoom(img_t *img, float z)
  468. {
  469. if (img == NULL || img->im == NULL || img->win == NULL)
  470. return false;
  471. z = MAX(z, zoom_min);
  472. z = MIN(z, zoom_max);
  473. img->scalemode = SCALE_ZOOM;
  474. if (zoomdiff(z, img->zoom) != 0) {
  475. img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * z / img->zoom;
  476. img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * z / img->zoom;
  477. img->zoom = z;
  478. img->checkpan = true;
  479. img->dirty = true;
  480. return true;
  481. } else {
  482. return false;
  483. }
  484. }
  485. bool img_zoom_in(img_t *img)
  486. {
  487. int i;
  488. float z;
  489. if (img == NULL || img->im == NULL)
  490. return false;
  491. for (i = 1; i < ARRLEN(zoom_levels); i++) {
  492. z = zoom_levels[i] / 100.0;
  493. if (zoomdiff(z, img->zoom) > 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. if (img == NULL || img->im == NULL)
  503. return false;
  504. for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
  505. z = zoom_levels[i] / 100.0;
  506. if (zoomdiff(z, img->zoom) < 0)
  507. return img_zoom(img, z);
  508. }
  509. return false;
  510. }
  511. bool img_move(img_t *img, float dx, float dy)
  512. {
  513. float ox, oy;
  514. if (img == NULL || img->im == NULL)
  515. return false;
  516. ox = img->x;
  517. oy = img->y;
  518. img->x += dx;
  519. img->y += dy;
  520. img_check_pan(img, true);
  521. if (ox != img->x || oy != img->y) {
  522. img->dirty = true;
  523. return true;
  524. } else {
  525. return false;
  526. }
  527. }
  528. bool img_pan(img_t *img, direction_t dir, int d)
  529. {
  530. /* d < 0: screen-wise
  531. * d = 0: 1/5 of screen
  532. * d > 0: num of pixels
  533. */
  534. float x, y;
  535. if (img == NULL || img->im == NULL || img->win == NULL)
  536. return false;
  537. if (d > 0) {
  538. x = y = MAX(1, (float) d * img->zoom);
  539. } else {
  540. x = img->win->w / (d < 0 ? 1 : 5);
  541. y = img->win->h / (d < 0 ? 1 : 5);
  542. }
  543. switch (dir) {
  544. case DIR_LEFT:
  545. return img_move(img, x, 0.0);
  546. case DIR_RIGHT:
  547. return img_move(img, -x, 0.0);
  548. case DIR_UP:
  549. return img_move(img, 0.0, y);
  550. case DIR_DOWN:
  551. return img_move(img, 0.0, -y);
  552. }
  553. return false;
  554. }
  555. bool img_pan_edge(img_t *img, direction_t dir)
  556. {
  557. int ox, oy;
  558. if (img == NULL || img->im == NULL || img->win == NULL)
  559. return false;
  560. ox = img->x;
  561. oy = img->y;
  562. if (dir & DIR_LEFT)
  563. img->x = 0;
  564. if (dir & DIR_RIGHT)
  565. img->x = img->win->w - img->w * img->zoom;
  566. if (dir & DIR_UP)
  567. img->y = 0;
  568. if (dir & DIR_DOWN)
  569. img->y = img->win->h - img->h * img->zoom;
  570. img_check_pan(img, true);
  571. if (ox != img->x || oy != img->y) {
  572. img->dirty = true;
  573. return true;
  574. } else {
  575. return false;
  576. }
  577. }
  578. void img_rotate(img_t *img, degree_t d)
  579. {
  580. int i, ox, oy, tmp;
  581. if (img == NULL || img->im == NULL || img->win == NULL)
  582. return;
  583. imlib_context_set_image(img->im);
  584. imlib_image_orientate(d);
  585. for (i = 0; i < img->multi.cnt; i++) {
  586. if (i != img->multi.sel) {
  587. imlib_context_set_image(img->multi.frames[i].im);
  588. imlib_image_orientate(d);
  589. }
  590. }
  591. if (d == DEGREE_90 || d == DEGREE_270) {
  592. ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
  593. oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
  594. img->x = oy + (img->win->w - img->win->h) / 2;
  595. img->y = ox + (img->win->h - img->win->w) / 2;
  596. tmp = img->w;
  597. img->w = img->h;
  598. img->h = tmp;
  599. img->checkpan = true;
  600. }
  601. img->dirty = true;
  602. }
  603. void img_flip(img_t *img, flipdir_t d)
  604. {
  605. int i;
  606. void (*imlib_flip_op[3])(void) = {
  607. imlib_image_flip_horizontal,
  608. imlib_image_flip_vertical,
  609. imlib_image_flip_diagonal
  610. };
  611. d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
  612. if (img == NULL || img->im == NULL || d < 0 || d >= ARRLEN(imlib_flip_op))
  613. return;
  614. imlib_context_set_image(img->im);
  615. imlib_flip_op[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_flip_op[d]();
  620. }
  621. }
  622. img->dirty = true;
  623. }
  624. void img_toggle_antialias(img_t *img)
  625. {
  626. if (img == NULL || img->im == NULL)
  627. return;
  628. img->aa = !img->aa;
  629. imlib_context_set_image(img->im);
  630. imlib_context_set_anti_alias(img->aa);
  631. img->dirty = true;
  632. }
  633. bool img_change_gamma(img_t *img, int d)
  634. {
  635. /* d < 0: decrease gamma
  636. * d = 0: reset gamma
  637. * d > 0: increase gamma
  638. */
  639. int gamma;
  640. double range;
  641. if (img == NULL)
  642. return false;
  643. if (d == 0)
  644. gamma = 0;
  645. else
  646. gamma = MIN(MAX(img->gamma + d, -GAMMA_RANGE), GAMMA_RANGE);
  647. if (img->gamma != gamma) {
  648. imlib_reset_color_modifier();
  649. if (gamma != 0) {
  650. range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
  651. imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
  652. }
  653. img->gamma = gamma;
  654. img->dirty = true;
  655. return true;
  656. } else {
  657. return false;
  658. }
  659. }
  660. bool img_frame_goto(img_t *img, int n)
  661. {
  662. if (img == NULL || img->im == NULL)
  663. return false;
  664. if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
  665. return false;
  666. img->multi.sel = n;
  667. img->im = img->multi.frames[n].im;
  668. imlib_context_set_image(img->im);
  669. img->w = imlib_image_get_width();
  670. img->h = imlib_image_get_height();
  671. img->checkpan = true;
  672. img->dirty = true;
  673. return true;
  674. }
  675. bool img_frame_navigate(img_t *img, int d)
  676. {
  677. if (img == NULL|| img->im == NULL || img->multi.cnt == 0 || d == 0)
  678. return false;
  679. d += img->multi.sel;
  680. if (d < 0)
  681. d = 0;
  682. else if (d >= img->multi.cnt)
  683. d = img->multi.cnt - 1;
  684. return img_frame_goto(img, d);
  685. }
  686. bool img_frame_animate(img_t *img)
  687. {
  688. if (img == NULL || img->im == NULL || img->multi.cnt == 0)
  689. return false;
  690. if (img->multi.sel + 1 >= img->multi.cnt)
  691. img_frame_goto(img, 0);
  692. else
  693. img_frame_goto(img, img->multi.sel + 1);
  694. img->dirty = true;
  695. return true;
  696. }