Krolyxon
Commit 7f71ddf

Branches

Refs →

nsxivimage.c

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