Krolyxon
Commit a9d2b5b

Branches

Refs →

nsxivimage.c

913 lines · 21.0 KB · C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021-2022 nsxiv contributors
3 *
4 * This file is a part of nsxiv.
5 *
6 * nsxiv is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
10 *
11 * nsxiv is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "nsxiv.h"
21#define INCLUDE_IMAGE_CONFIG
22#include "config.h"
23
24#include <assert.h>
25#include <errno.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <unistd.h>
31
32#if HAVE_LIBEXIF
33#include <libexif/exif-data.h>
34#endif
35
36#if HAVE_LIBGIF
37#include <gif_lib.h>
38enum { DEF_GIF_DELAY = 75 };
39#endif
40
41#if HAVE_LIBWEBP
42#include <webp/decode.h>
43#include <webp/demux.h>
44enum { DEF_WEBP_DELAY = 75 };
45#endif
46
47#define ZOOM_MIN (zoom_levels[0] / 100)
48#define ZOOM_MAX (zoom_levels[ARRLEN(zoom_levels)-1] / 100)
49
50static int calc_cache_size(void)
51{
52 int cache;
53 long pages, page_size;
54
55 if (CACHE_SIZE_MEM_PERCENTAGE <= 0)
56 return 0;
57
58 pages = sysconf(_SC_PHYS_PAGES);
59 page_size = sysconf(_SC_PAGE_SIZE);
60 if (pages < 0 || page_size < 0)
61 return CACHE_SIZE_FALLBACK;
62 cache = (pages/100) * CACHE_SIZE_MEM_PERCENTAGE;
63 cache *= page_size;
64
65 return MIN(cache, CACHE_SIZE_LIMIT);
66}
67
68void img_init(img_t *img, win_t *win)
69{
70 imlib_context_set_display(win->env.dpy);
71 imlib_context_set_visual(win->env.vis);
72 imlib_context_set_colormap(win->env.cmap);
73 imlib_set_cache_size(calc_cache_size());
74
75 img->im = NULL;
76 img->win = win;
77 img->scalemode = options->scalemode;
78 img->zoom = options->zoom;
79 img->zoom = MAX(img->zoom, ZOOM_MIN);
80 img->zoom = MIN(img->zoom, ZOOM_MAX);
81 img->checkpan = false;
82 img->dirty = false;
83 img->aa = ANTI_ALIAS;
84 img->alpha = ALPHA_LAYER;
85 img->multi.cap = img->multi.cnt = 0;
86 img->multi.animate = options->animate;
87 img->multi.framedelay = options->framerate > 0 ? 1000 / options->framerate : 0;
88 img->multi.length = 0;
89
90 img->cmod = imlib_create_color_modifier();
91 imlib_context_set_color_modifier(img->cmod);
92 img_change_gamma(img, options->gamma);
93
94 img->ss.on = options->slideshow > 0;
95 img->ss.delay = options->slideshow > 0 ? options->slideshow : SLIDESHOW_DELAY * 10;
96}
97
98#if HAVE_LIBEXIF
99void exif_auto_orientate(const fileinfo_t *file)
100{
101 ExifData *ed;
102 ExifEntry *entry;
103 int byte_order, orientation = 0;
104
105 if ((ed = exif_data_new_from_file(file->path)) == NULL)
106 return;
107 byte_order = exif_data_get_byte_order(ed);
108 entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION);
109 if (entry != NULL)
110 orientation = exif_get_short(entry->data, byte_order);
111 exif_data_unref(ed);
112
113 switch (orientation) {
114 case 5:
115 imlib_image_orientate(1);
116 /* fall through */
117 case 2:
118 imlib_image_flip_vertical();
119 break;
120 case 3:
121 imlib_image_orientate(2);
122 break;
123 case 7:
124 imlib_image_orientate(1);
125 /* fall through */
126 case 4:
127 imlib_image_flip_horizontal();
128 break;
129 case 6:
130 imlib_image_orientate(1);
131 break;
132 case 8:
133 imlib_image_orientate(3);
134 break;
135 }
136}
137#endif
138
139#if HAVE_LIBGIF || HAVE_LIBWEBP
140static void img_multiframe_context_set(img_t *img)
141{
142 if (img->multi.cnt > 1) {
143 imlib_context_set_image(img->im);
144 imlib_free_image();
145 img->im = img->multi.frames[0].im;
146 } else if (img->multi.cnt == 1) {
147 imlib_context_set_image(img->multi.frames[0].im);
148 imlib_free_image();
149 img->multi.cnt = 0;
150 }
151
152 imlib_context_set_image(img->im);
153}
154#endif
155
156#if HAVE_LIBGIF
157static bool img_load_gif(img_t *img, const fileinfo_t *file)
158{
159 GifFileType *gif;
160 GifRowType *rows = NULL;
161 GifRecordType rec;
162 ColorMapObject *cmap;
163 DATA32 bgpixel = 0, *data, *ptr;
164 DATA32 *prev_frame = NULL;
165 Imlib_Image im;
166 int i, j, bg, r, g, b;
167 int x, y, w, h, sw, sh;
168 int px, py, pw, ph;
169 int intoffset[] = { 0, 4, 2, 1 };
170 int intjump[] = { 8, 8, 4, 2 };
171 int transp = -1;
172 unsigned int disposal = 0, prev_disposal = 0;
173 unsigned int delay = 0;
174 bool err = false;
175
176 if (img->multi.cap == 0) {
177 img->multi.cap = 8;
178 img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t));
179 }
180 img->multi.cnt = img->multi.sel = 0;
181 img->multi.length = 0;
182
183#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5
184 gif = DGifOpenFileName(file->path, NULL);
185#else
186 gif = DGifOpenFileName(file->path);
187#endif
188 if (gif == NULL) {
189 error(0, 0, "%s: Error opening gif image", file->name);
190 return false;
191 }
192 bg = gif->SBackGroundColor;
193 sw = gif->SWidth;
194 sh = gif->SHeight;
195 px = py = pw = ph = 0;
196
197 do {
198 if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
199 err = true;
200 break;
201 }
202 if (rec == EXTENSION_RECORD_TYPE) {
203 int ext_code;
204 GifByteType *ext = NULL;
205
206 DGifGetExtension(gif, &ext_code, &ext);
207 while (ext) {
208 if (ext_code == GRAPHICS_EXT_FUNC_CODE) {
209 if (ext[1] & 1)
210 transp = (int) ext[4];
211 else
212 transp = -1;
213
214 delay = 10 * ((unsigned int) ext[3] << 8 | (unsigned int) ext[2]);
215 disposal = (unsigned int) ext[1] >> 2 & 0x7;
216 }
217 ext = NULL;
218 DGifGetExtensionNext(gif, &ext);
219 }
220 } else if (rec == IMAGE_DESC_RECORD_TYPE) {
221 if (DGifGetImageDesc(gif) == GIF_ERROR) {
222 err = true;
223 break;
224 }
225 x = gif->Image.Left;
226 y = gif->Image.Top;
227 w = gif->Image.Width;
228 h = gif->Image.Height;
229
230 rows = emalloc(h * sizeof(GifRowType));
231 for (i = 0; i < h; i++)
232 rows[i] = emalloc(w * sizeof(GifPixelType));
233 if (gif->Image.Interlace) {
234 for (i = 0; i < 4; i++) {
235 for (j = intoffset[i]; j < h; j += intjump[i])
236 DGifGetLine(gif, rows[j], w);
237 }
238 } else {
239 for (i = 0; i < h; i++)
240 DGifGetLine(gif, rows[i], w);
241 }
242
243 ptr = data = emalloc(sw * sh * sizeof(DATA32));
244 cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
245 /* if bg > cmap->ColorCount, it is transparent black already */
246 if (cmap && bg >= 0 && bg < cmap->ColorCount) {
247 r = cmap->Colors[bg].Red;
248 g = cmap->Colors[bg].Green;
249 b = cmap->Colors[bg].Blue;
250 bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
251 }
252
253 for (i = 0; i < sh; i++) {
254 for (j = 0; j < sw; j++) {
255 if (i < y || i >= y + h || j < x || j >= x + w ||
256 rows[i-y][j-x] == transp)
257 {
258 if (prev_frame != NULL && (prev_disposal != 2 ||
259 i < py || i >= py + ph || j < px || j >= px + pw))
260 {
261 *ptr = prev_frame[i * sw + j];
262 } else {
263 *ptr = bgpixel;
264 }
265 } else {
266 assert(cmap != NULL);
267 r = cmap->Colors[rows[i-y][j-x]].Red;
268 g = cmap->Colors[rows[i-y][j-x]].Green;
269 b = cmap->Colors[rows[i-y][j-x]].Blue;
270 *ptr = 0xffu << 24 | r << 16 | g << 8 | b;
271 }
272 ptr++;
273 }
274 }
275
276 im = imlib_create_image_using_copied_data(sw, sh, data);
277
278 for (i = 0; i < h; i++)
279 free(rows[i]);
280 free(rows);
281 free(data);
282
283 if (im == NULL) {
284 err = true;
285 break;
286 }
287
288 imlib_context_set_image(im);
289 imlib_image_set_format("gif");
290 if (transp >= 0)
291 imlib_image_set_has_alpha(1);
292
293 if (disposal != 3)
294 prev_frame = imlib_image_get_data_for_reading_only();
295 prev_disposal = disposal;
296 px = x, py = y, pw = w, ph = h;
297
298 if (img->multi.cnt == img->multi.cap) {
299 img->multi.cap *= 2;
300 img->multi.frames = erealloc(img->multi.frames,
301 img->multi.cap * sizeof(img_frame_t));
302 }
303 img->multi.frames[img->multi.cnt].im = im;
304 delay = img->multi.framedelay > 0 ? img->multi.framedelay : delay;
305 img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY;
306 img->multi.length += img->multi.frames[img->multi.cnt].delay;
307 img->multi.cnt++;
308 }
309 } while (rec != TERMINATE_RECORD_TYPE);
310
311#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 && GIFLIB_MINOR >= 1
312 DGifCloseFile(gif, NULL);
313#else
314 DGifCloseFile(gif);
315#endif
316
317 if (err && (file->flags & FF_WARN))
318 error(0, 0, "%s: Corrupted gif file", file->name);
319
320 img_multiframe_context_set(img);
321
322 return !err;
323}
324#endif /* HAVE_LIBGIF */
325
326
327#if HAVE_LIBWEBP
328static bool img_load_webp(img_t *img, const fileinfo_t *file)
329{
330 FILE *webp_file;
331 WebPData data;
332 Imlib_Image im = NULL;
333 struct WebPAnimDecoderOptions opts;
334 WebPAnimDecoder *dec = NULL;
335 struct WebPAnimInfo info;
336 unsigned char *buf = NULL, *bytes = NULL;
337 int ts;
338 const WebPDemuxer *demux;
339 WebPIterator iter;
340 unsigned long flags;
341 unsigned int delay;
342 bool err = false;
343
344 if ((webp_file = fopen(file->path, "rb")) == NULL) {
345 error(0, errno, "%s: Error opening webp image", file->name);
346 return false;
347 }
348 fseek(webp_file, 0L, SEEK_END);
349 data.size = ftell(webp_file);
350 rewind(webp_file);
351 bytes = emalloc(data.size);
352 if ((err = fread(bytes, 1, data.size, webp_file) != data.size)) {
353 error(0, 0, "%s: Error reading webp image", file->name);
354 goto fail;
355 }
356 data.bytes = bytes;
357
358 /* Setup the WebP Animation Decoder */
359 if ((err = !WebPAnimDecoderOptionsInit(&opts))) {
360 error(0, 0, "%s: WebP library version mismatch", file->name);
361 goto fail;
362 }
363 opts.color_mode = MODE_BGRA;
364 /* NOTE: Multi-threaded decoding may cause problems on some system */
365 opts.use_threads = true;
366 dec = WebPAnimDecoderNew(&data, &opts);
367 if ((err = (dec == NULL) || !WebPAnimDecoderGetInfo(dec, &info))) {
368 error(0, 0, "%s: WebP parsing or memory error (file is corrupt?)", file->name);
369 goto fail;
370 }
371 demux = WebPAnimDecoderGetDemuxer(dec);
372
373 /* Get global information for the image */
374 flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);
375 img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
376 img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
377
378 if (info.frame_count > img->multi.cap) {
379 img->multi.cap = info.frame_count;
380 img->multi.frames = erealloc(img->multi.frames,
381 img->multi.cap * sizeof(img_frame_t));
382 }
383
384 /* Load and decode frames (also works on images with only 1 frame) */
385 img->multi.cnt = img->multi.sel = 0;
386 while (WebPAnimDecoderGetNext(dec, &buf, &ts)) {
387 im = imlib_create_image_using_copied_data(
388 info.canvas_width, info.canvas_height, (DATA32*)buf);
389 imlib_context_set_image(im);
390 imlib_image_set_format("webp");
391 /* Get an iterator of this frame - used for frame info (duration, etc.) */
392 WebPDemuxGetFrame(demux, img->multi.cnt+1, &iter);
393 imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG);
394 /* Store info for this frame */
395 img->multi.frames[img->multi.cnt].im = im;
396 delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY;
397 img->multi.frames[img->multi.cnt].delay = delay;
398 img->multi.length += img->multi.frames[img->multi.cnt].delay;
399 img->multi.cnt++;
400 }
401 WebPDemuxReleaseIterator(&iter);
402
403 img_multiframe_context_set(img);
404fail:
405 if (dec != NULL)
406 WebPAnimDecoderDelete(dec);
407 free(bytes);
408 fclose(webp_file);
409 return !err;
410}
411#endif /* HAVE_LIBWEBP */
412
413Imlib_Image img_open(const fileinfo_t *file)
414{
415 struct stat st;
416 Imlib_Image im = NULL;
417
418 if (access(file->path, R_OK) == 0 &&
419 stat(file->path, &st) == 0 && S_ISREG(st.st_mode))
420 {
421 im = imlib_load_image(file->path);
422 if (im != NULL) {
423 imlib_context_set_image(im);
424 if (imlib_image_get_data_for_reading_only() == NULL) {
425 imlib_free_image();
426 im = NULL;
427 }
428 }
429 }
430 if (im == NULL && (file->flags & FF_WARN))
431 error(0, 0, "%s: Error opening image", file->name);
432 return im;
433}
434
435bool img_load(img_t *img, const fileinfo_t *file)
436{
437 const char *fmt;
438
439 if ((img->im = img_open(file)) == NULL)
440 return false;
441
442 imlib_image_set_changes_on_disk();
443
444/* since v1.7.5, Imlib2 can parse exif orientation from jpeg files.
445 * this version also happens to be the first one which defines the
446 * IMLIB2_VERSION macro.
447 */
448#if HAVE_LIBEXIF && !defined(IMLIB2_VERSION)
449 exif_auto_orientate(file);
450#endif
451
452 if ((fmt = imlib_image_format()) != NULL) { /* NOLINT: fmt might be unused, not worth fixing */
453#if HAVE_LIBGIF
454 if (STREQ(fmt, "gif"))
455 img_load_gif(img, file);
456#endif
457#if HAVE_LIBWEBP
458 if (STREQ(fmt, "webp"))
459 img_load_webp(img, file);
460#endif
461#if HAVE_LIBEXIF && defined(IMLIB2_VERSION)
462 if (!STREQ(fmt, "jpeg") && !STREQ(fmt, "jpg"))
463 exif_auto_orientate(file);
464#endif
465 }
466 img->w = imlib_image_get_width();
467 img->h = imlib_image_get_height();
468 img->checkpan = true;
469 img->dirty = true;
470
471 return true;
472}
473
474CLEANUP void img_close(img_t *img, bool decache)
475{
476 int i;
477
478 if (img->multi.cnt > 0) {
479 for (i = 0; i < img->multi.cnt; i++) {
480 imlib_context_set_image(img->multi.frames[i].im);
481 imlib_free_image();
482 }
483 img->multi.cnt = 0;
484 img->im = NULL;
485 } else if (img->im != NULL) {
486 imlib_context_set_image(img->im);
487 if (decache)
488 imlib_free_image_and_decache();
489 else
490 imlib_free_image();
491 img->im = NULL;
492 }
493}
494
495static void img_check_pan(img_t *img, bool moved)
496{
497 win_t *win;
498 float w, h, ox, oy;
499
500 win = img->win;
501 w = img->w * img->zoom;
502 h = img->h * img->zoom;
503 ox = img->x;
504 oy = img->y;
505
506 if (w < win->w)
507 img->x = (win->w - w) / 2;
508 else if (img->x > 0)
509 img->x = 0;
510 else if (img->x + w < win->w)
511 img->x = win->w - w;
512 if (h < win->h)
513 img->y = (win->h - h) / 2;
514 else if (img->y > 0)
515 img->y = 0;
516 else if (img->y + h < win->h)
517 img->y = win->h - h;
518
519 if (!moved && (ox != img->x || oy != img->y))
520 img->dirty = true;
521}
522
523static bool img_fit(img_t *img)
524{
525 float z, zw, zh;
526
527 if (img->scalemode == SCALE_ZOOM)
528 return false;
529
530 zw = (float) img->win->w / (float) img->w;
531 zh = (float) img->win->h / (float) img->h;
532
533 switch (img->scalemode) {
534 case SCALE_FILL:
535 z = MAX(zw, zh);
536 break;
537 case SCALE_WIDTH:
538 z = zw;
539 break;
540 case SCALE_HEIGHT:
541 z = zh;
542 break;
543 default:
544 z = MIN(zw, zh);
545 break;
546 }
547 z = MIN(z, img->scalemode == SCALE_DOWN ? 1.0 : ZOOM_MAX);
548
549 if (ABS(img->zoom - z) > 1.0/MAX(img->w, img->h)) {
550 img->zoom = z;
551 img->dirty = title_dirty = true;
552 return true;
553 } else {
554 return false;
555 }
556}
557
558void img_render(img_t *img)
559{
560 win_t *win;
561 int sx, sy, sw, sh;
562 int dx, dy, dw, dh;
563 Imlib_Image bg;
564
565 win = img->win;
566 img_fit(img);
567
568 if (img->checkpan) {
569 img_check_pan(img, false);
570 img->checkpan = false;
571 }
572
573 if (!img->dirty)
574 return;
575
576 /* calculate source and destination offsets:
577 * - part of image drawn on full window, or
578 * - full image drawn on part of window
579 */
580 if (img->x <= 0) {
581 sx = -img->x / img->zoom + 0.5;
582 sw = win->w / img->zoom;
583 dx = 0;
584 dw = win->w;
585 } else {
586 sx = 0;
587 sw = img->w;
588 dx = img->x;
589 dw = MAX(img->w * img->zoom, 1);
590 }
591 if (img->y <= 0) {
592 sy = -img->y / img->zoom + 0.5;
593 sh = win->h / img->zoom;
594 dy = win->bar.top ? win->bar.h : 0;
595 dh = win->h;
596 } else {
597 sy = 0;
598 sh = img->h;
599 dy = img->y + (win->bar.top ? win->bar.h : 0);
600 dh = MAX(img->h * img->zoom, 1);
601 }
602
603 win_clear(win);
604
605 imlib_context_set_image(img->im);
606 imlib_context_set_anti_alias(img->aa);
607 imlib_context_set_drawable(win->buf.pm);
608
609 /* manual blending, for performance reasons.
610 * see https://phab.enlightenment.org/T8969#156167 for more details.
611 */
612 if (imlib_image_has_alpha()) {
613 if ((bg = imlib_create_image(dw, dh)) == NULL) {
614 error(0, ENOMEM, "Failed to create image");
615 goto fallback;
616 }
617 imlib_context_set_image(bg);
618 imlib_image_set_has_alpha(0);
619
620 if (img->alpha) {
621 int i, c, r;
622 DATA32 col[2] = { 0xFF666666, 0xFF999999 };
623 DATA32 * data = imlib_image_get_data();
624
625 for (r = 0; r < dh; r++) {
626 i = r * dw;
627 if (r == 0 || r == 8) {
628 for (c = 0; c < dw; c++)
629 data[i++] = col[!(c & 8) ^ !r];
630 } else {
631 memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
632 }
633 }
634 imlib_image_put_back_data(data);
635 } else {
636 XColor c = win->win_bg;
637 imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF);
638 imlib_image_fill_rectangle(0, 0, dw, dh);
639 }
640 imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
641 imlib_context_set_color_modifier(NULL);
642 imlib_render_image_on_drawable(dx, dy);
643 imlib_free_image();
644 imlib_context_set_color_modifier(img->cmod);
645 } else {
646fallback:
647 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
648 }
649 img->dirty = false;
650}
651
652bool img_fit_win(img_t *img, scalemode_t sm)
653{
654 float oz;
655
656 oz = img->zoom;
657 img->scalemode = sm;
658
659 if (img_fit(img)) {
660 img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
661 img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
662 img->checkpan = true;
663 return true;
664 } else {
665 return false;
666 }
667}
668
669bool img_zoom_to(img_t *img, float z)
670{
671 int x, y;
672 if (ZOOM_MIN <= z && z <= ZOOM_MAX) {
673 win_cursor_pos(img->win, &x, &y);
674 if (x < 0 || x >= img->win->w || y < 0 || y >= img->win->h) {
675 x = img->win->w / 2;
676 y = img->win->h / 2;
677 }
678 img->x = x - (x - img->x) * z / img->zoom;
679 img->y = y - (y - img->y) * z / img->zoom;
680 img->zoom = z;
681 img->scalemode = SCALE_ZOOM;
682 img->dirty = img->checkpan = title_dirty = true;
683 return true;
684 } else {
685 return false;
686 }
687}
688
689bool img_zoom(img_t *img, int d)
690{
691 int i = d > 0 ? 0 : ARRLEN(zoom_levels)-1;
692 while (i >= 0 && i < ARRLEN(zoom_levels) && (d > 0 ?
693 zoom_levels[i]/100 <= img->zoom : zoom_levels[i]/100 >= img->zoom))
694 {
695 i += d;
696 }
697 i = MIN(MAX(i, 0), ARRLEN(zoom_levels)-1);
698 return img_zoom_to(img, zoom_levels[i]/100);
699}
700
701bool img_pos(img_t *img, float x, float y)
702{
703 float ox, oy;
704
705 ox = img->x;
706 oy = img->y;
707
708 img->x = x;
709 img->y = y;
710
711 img_check_pan(img, true);
712
713 if (ox != img->x || oy != img->y) {
714 img->dirty = true;
715 return true;
716 } else {
717 return false;
718 }
719}
720
721static bool img_move(img_t *img, float dx, float dy)
722{
723 return img_pos(img, img->x + dx, img->y + dy);
724}
725
726bool img_pan(img_t *img, direction_t dir, int d)
727{
728 /* d < 0: screen-wise
729 * d = 0: 1/PAN_FRACTION of screen
730 * d > 0: num of pixels
731 */
732 float x, y;
733
734 if (d > 0) {
735 x = y = MAX(1, (float) d * img->zoom);
736 } else {
737 x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
738 y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
739 }
740
741 switch (dir) {
742 case DIR_LEFT:
743 return img_move(img, x, 0.0);
744 case DIR_RIGHT:
745 return img_move(img, -x, 0.0);
746 case DIR_UP:
747 return img_move(img, 0.0, y);
748 case DIR_DOWN:
749 return img_move(img, 0.0, -y);
750 }
751 return false;
752}
753
754bool img_pan_center(img_t *img)
755{
756 float x, y;
757 x = (img->win->w - img->w * img->zoom) / 2.0;
758 y = (img->win->h - img->h * img->zoom) / 2.0;
759 return img_pos(img, x, y);
760}
761
762bool img_pan_edge(img_t *img, direction_t dir)
763{
764 float ox, oy;
765
766 ox = img->x;
767 oy = img->y;
768
769 if (dir & DIR_LEFT)
770 img->x = 0;
771 if (dir & DIR_RIGHT)
772 img->x = img->win->w - img->w * img->zoom;
773 if (dir & DIR_UP)
774 img->y = 0;
775 if (dir & DIR_DOWN)
776 img->y = img->win->h - img->h * img->zoom;
777
778 img_check_pan(img, true);
779
780 if (ox != img->x || oy != img->y) {
781 img->dirty = true;
782 return true;
783 } else {
784 return false;
785 }
786}
787
788void img_rotate(img_t *img, degree_t d)
789{
790 int i, tmp;
791 float ox, oy;
792
793 imlib_context_set_image(img->im);
794 imlib_image_orientate(d);
795
796 for (i = 0; i < img->multi.cnt; i++) {
797 if (i != img->multi.sel) {
798 imlib_context_set_image(img->multi.frames[i].im);
799 imlib_image_orientate(d);
800 }
801 }
802 if (d == DEGREE_90 || d == DEGREE_270) {
803 ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
804 oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
805
806 img->x = oy + (img->win->w - img->win->h) / 2;
807 img->y = ox + (img->win->h - img->win->w) / 2;
808
809 tmp = img->w;
810 img->w = img->h;
811 img->h = tmp;
812 img->checkpan = true;
813 }
814 img->dirty = true;
815}
816
817void img_flip(img_t *img, flipdir_t d)
818{
819 int i;
820 void (*imlib_flip_op[3])(void) = {
821 imlib_image_flip_horizontal,
822 imlib_image_flip_vertical,
823 imlib_image_flip_diagonal
824 };
825
826 d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
827
828 if (d < 0 || d >= ARRLEN(imlib_flip_op))
829 return;
830
831 imlib_context_set_image(img->im);
832 imlib_flip_op[d]();
833
834 for (i = 0; i < img->multi.cnt; i++) {
835 if (i != img->multi.sel) {
836 imlib_context_set_image(img->multi.frames[i].im);
837 imlib_flip_op[d]();
838 }
839 }
840 img->dirty = true;
841}
842
843void img_toggle_antialias(img_t *img)
844{
845 img->aa = !img->aa;
846 imlib_context_set_image(img->im);
847 imlib_context_set_anti_alias(img->aa);
848 img->dirty = true;
849}
850
851bool img_change_gamma(img_t *img, int d)
852{
853 /* d < 0: decrease gamma
854 * d = 0: reset gamma
855 * d > 0: increase gamma
856 */
857 int gamma;
858 double range;
859
860 if (d == 0)
861 gamma = 0;
862 else
863 gamma = MIN(MAX(img->gamma + d, -GAMMA_RANGE), GAMMA_RANGE);
864
865 if (img->gamma != gamma) {
866 imlib_reset_color_modifier();
867 if (gamma) {
868 range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
869 imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
870 }
871 img->gamma = gamma;
872 img->dirty = true;
873 return true;
874 } else {
875 return false;
876 }
877}
878
879static bool img_frame_goto(img_t *img, int n)
880{
881 if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
882 return false;
883
884 img->multi.sel = n;
885 img->im = img->multi.frames[n].im;
886
887 imlib_context_set_image(img->im);
888 img->w = imlib_image_get_width();
889 img->h = imlib_image_get_height();
890 img->checkpan = true;
891 img->dirty = true;
892
893 return true;
894}
895
896bool img_frame_navigate(img_t *img, int d)
897{
898 if (img->multi.cnt == 0 || d == 0)
899 return false;
900
901 d += img->multi.sel;
902 d = MAX(0, MIN(d, img->multi.cnt - 1));
903
904 return img_frame_goto(img, d);
905}
906
907bool img_frame_animate(img_t *img)
908{
909 if (img->multi.cnt > 0)
910 return img_frame_goto(img, (img->multi.sel + 1) % img->multi.cnt);
911 else
912 return false;
913}