Krolyxon
Commit 045abbd

Branches

Refs →

nsxivimage.c

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