Krolyxon
Commit e777adf

Branches

Refs →

nsxivimage.c

899 lines · 20.2 KB · C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021 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, *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#if HAVE_LIBEXIF
443 exif_auto_orientate(file);
444#endif
445
446 if ((fmt = imlib_image_format()) != NULL) {
447#if HAVE_LIBGIF
448 if (STREQ(fmt, "gif"))
449 img_load_gif(img, file);
450#endif
451#if HAVE_LIBWEBP
452 if (STREQ(fmt, "webp"))
453 img_load_webp(img, 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 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 = 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 XColor c;
555
556 win = img->win;
557 img_fit(img);
558
559 if (img->checkpan) {
560 img_check_pan(img, false);
561 img->checkpan = false;
562 }
563
564 if (!img->dirty)
565 return;
566
567 /* calculate source and destination offsets:
568 * - part of image drawn on full window, or
569 * - full image drawn on part of window
570 */
571 if (img->x <= 0) {
572 sx = -img->x / img->zoom + 0.5;
573 sw = win->w / img->zoom;
574 dx = 0;
575 dw = win->w;
576 } else {
577 sx = 0;
578 sw = img->w;
579 dx = img->x;
580 dw = MAX(img->w * img->zoom, 1);
581 }
582 if (img->y <= 0) {
583 sy = -img->y / img->zoom + 0.5;
584 sh = win->h / img->zoom;
585 dy = 0;
586 dh = win->h;
587 } else {
588 sy = 0;
589 sh = img->h;
590 dy = img->y;
591 dh = MAX(img->h * img->zoom, 1);
592 }
593
594 win_clear(win);
595
596 imlib_context_set_image(img->im);
597 imlib_context_set_anti_alias(img->aa);
598 imlib_context_set_drawable(win->buf.pm);
599
600 if (imlib_image_has_alpha()) {
601 if ((bg = imlib_create_image(dw, dh)) == NULL)
602 error(EXIT_FAILURE, ENOMEM, NULL);
603 imlib_context_set_image(bg);
604 imlib_image_set_has_alpha(0);
605
606 if (img->alpha) {
607 int i, c, r;
608 DATA32 col[2] = { 0xFF666666, 0xFF999999 };
609 DATA32 * data = imlib_image_get_data();
610
611 for (r = 0; r < dh; r++) {
612 i = r * dw;
613 if (r == 0 || r == 8) {
614 for (c = 0; c < dw; c++)
615 data[i++] = col[!(c & 8) ^ !r];
616 } else {
617 memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
618 }
619 }
620 imlib_image_put_back_data(data);
621 } else {
622 c = win->win_bg;
623 imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF);
624 imlib_image_fill_rectangle(0, 0, dw, dh);
625 }
626 imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
627 imlib_context_set_color_modifier(NULL);
628 imlib_render_image_on_drawable(dx, dy);
629 imlib_free_image();
630 imlib_context_set_color_modifier(img->cmod);
631 } else {
632 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
633 }
634 img->dirty = false;
635}
636
637bool img_fit_win(img_t *img, scalemode_t sm)
638{
639 float oz;
640
641 oz = img->zoom;
642 img->scalemode = sm;
643
644 if (img_fit(img)) {
645 img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
646 img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
647 img->checkpan = true;
648 return true;
649 } else {
650 return false;
651 }
652}
653
654bool img_zoom_to(img_t *img, float z)
655{
656 int x, y;
657 if (ZOOM_MIN <= z && z <= ZOOM_MAX) {
658 win_cursor_pos(img->win, &x, &y);
659 if (x < 0 || x >= img->win->w || y < 0 || y >= img->win->h) {
660 x = img->win->w / 2;
661 y = img->win->h / 2;
662 }
663 img->x = x - (x - img->x) * z / img->zoom;
664 img->y = y - (y - img->y) * z / img->zoom;
665 img->zoom = z;
666 img->scalemode = SCALE_ZOOM;
667 img->checkpan = true;
668 img->dirty = true;
669 return true;
670 } else {
671 return false;
672 }
673}
674
675bool img_zoom(img_t *img, int d)
676{
677 int i = d > 0 ? 0 : ARRLEN(zoom_levels)-1;
678 while (i >= 0 && i < ARRLEN(zoom_levels) && (d > 0 ?
679 zoom_levels[i]/100 <= img->zoom : zoom_levels[i]/100 >= img->zoom))
680 {
681 i += d;
682 }
683 i = MIN(MAX(i, 0), ARRLEN(zoom_levels)-1);
684 return img_zoom_to(img, zoom_levels[i]/100);
685}
686
687bool img_pos(img_t *img, float x, float y)
688{
689 float ox, oy;
690
691 ox = img->x;
692 oy = img->y;
693
694 img->x = x;
695 img->y = y;
696
697 img_check_pan(img, true);
698
699 if (ox != img->x || oy != img->y) {
700 img->dirty = true;
701 return true;
702 } else {
703 return false;
704 }
705}
706
707static bool img_move(img_t *img, float dx, float dy)
708{
709 return img_pos(img, img->x + dx, img->y + dy);
710}
711
712bool img_pan(img_t *img, direction_t dir, int d)
713{
714 /* d < 0: screen-wise
715 * d = 0: 1/PAN_FRACTION of screen
716 * d > 0: num of pixels
717 */
718 float x, y;
719
720 if (d > 0) {
721 x = y = MAX(1, (float) d * img->zoom);
722 } else {
723 x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
724 y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
725 }
726
727 switch (dir) {
728 case DIR_LEFT:
729 return img_move(img, x, 0.0);
730 case DIR_RIGHT:
731 return img_move(img, -x, 0.0);
732 case DIR_UP:
733 return img_move(img, 0.0, y);
734 case DIR_DOWN:
735 return img_move(img, 0.0, -y);
736 }
737 return false;
738}
739
740bool img_pan_edge(img_t *img, direction_t dir)
741{
742 float ox, oy;
743
744 ox = img->x;
745 oy = img->y;
746
747 if (dir & DIR_LEFT)
748 img->x = 0;
749 if (dir & DIR_RIGHT)
750 img->x = img->win->w - img->w * img->zoom;
751 if (dir & DIR_UP)
752 img->y = 0;
753 if (dir & DIR_DOWN)
754 img->y = img->win->h - img->h * img->zoom;
755
756 img_check_pan(img, true);
757
758 if (ox != img->x || oy != img->y) {
759 img->dirty = true;
760 return true;
761 } else {
762 return false;
763 }
764}
765
766void img_rotate(img_t *img, degree_t d)
767{
768 int i, tmp;
769 float ox, oy;
770
771 imlib_context_set_image(img->im);
772 imlib_image_orientate(d);
773
774 for (i = 0; i < img->multi.cnt; i++) {
775 if (i != img->multi.sel) {
776 imlib_context_set_image(img->multi.frames[i].im);
777 imlib_image_orientate(d);
778 }
779 }
780 if (d == DEGREE_90 || d == DEGREE_270) {
781 ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
782 oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
783
784 img->x = oy + (img->win->w - img->win->h) / 2;
785 img->y = ox + (img->win->h - img->win->w) / 2;
786
787 tmp = img->w;
788 img->w = img->h;
789 img->h = tmp;
790 img->checkpan = true;
791 }
792 img->dirty = true;
793}
794
795void img_flip(img_t *img, flipdir_t d)
796{
797 int i;
798 void (*imlib_flip_op[3])(void) = {
799 imlib_image_flip_horizontal,
800 imlib_image_flip_vertical,
801 imlib_image_flip_diagonal
802 };
803
804 d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
805
806 if (d < 0 || d >= ARRLEN(imlib_flip_op))
807 return;
808
809 imlib_context_set_image(img->im);
810 imlib_flip_op[d]();
811
812 for (i = 0; i < img->multi.cnt; i++) {
813 if (i != img->multi.sel) {
814 imlib_context_set_image(img->multi.frames[i].im);
815 imlib_flip_op[d]();
816 }
817 }
818 img->dirty = true;
819}
820
821void img_toggle_antialias(img_t *img)
822{
823 img->aa = !img->aa;
824 imlib_context_set_image(img->im);
825 imlib_context_set_anti_alias(img->aa);
826 img->dirty = true;
827}
828
829bool img_change_gamma(img_t *img, int d)
830{
831 /* d < 0: decrease gamma
832 * d = 0: reset gamma
833 * d > 0: increase gamma
834 */
835 int gamma;
836 double range;
837
838 if (d == 0)
839 gamma = 0;
840 else
841 gamma = MIN(MAX(img->gamma + d, -GAMMA_RANGE), GAMMA_RANGE);
842
843 if (img->gamma != gamma) {
844 imlib_reset_color_modifier();
845 if (gamma) {
846 range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
847 imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
848 }
849 img->gamma = gamma;
850 img->dirty = true;
851 return true;
852 } else {
853 return false;
854 }
855}
856
857static bool img_frame_goto(img_t *img, int n)
858{
859 if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
860 return false;
861
862 img->multi.sel = n;
863 img->im = img->multi.frames[n].im;
864
865 imlib_context_set_image(img->im);
866 img->w = imlib_image_get_width();
867 img->h = imlib_image_get_height();
868 img->checkpan = true;
869 img->dirty = true;
870
871 return true;
872}
873
874bool img_frame_navigate(img_t *img, int d)
875{
876 if (img->multi.cnt == 0 || d == 0)
877 return false;
878
879 d += img->multi.sel;
880 if (d < 0)
881 d = 0;
882 else if (d >= img->multi.cnt)
883 d = img->multi.cnt - 1;
884
885 return img_frame_goto(img, d);
886}
887
888bool img_frame_animate(img_t *img)
889{
890 if (img->multi.cnt == 0)
891 return false;
892
893 if (img->multi.sel + 1 >= img->multi.cnt)
894 img_frame_goto(img, 0);
895 else
896 img_frame_goto(img, img->multi.sel + 1);
897 img->dirty = true;
898 return true;
899}