Krolyxon
Commit b11384a

Branches

Refs →

nsxivimage.c

911 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 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->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 {
417 im = imlib_load_image(file->path);
418 if (im != NULL) {
419 imlib_context_set_image(im);
420 if (imlib_image_get_data_for_reading_only() == NULL) {
421 imlib_free_image();
422 im = NULL;
423 }
424 }
425 }
426 if (im == NULL && (file->flags & FF_WARN))
427 error(0, 0, "%s: Error opening image", file->name);
428 return im;
429}
430
431bool img_load(img_t *img, const fileinfo_t *file)
432{
433 const char *fmt;
434
435 if ((img->im = img_open(file)) == NULL)
436 return false;
437
438 imlib_image_set_changes_on_disk();
439
440/* since v1.7.5, Imlib2 can parse exif orientation from jpeg files.
441 * this version also happens to be the first one which defines the
442 * IMLIB2_VERSION macro.
443 */
444#if HAVE_LIBEXIF && !defined(IMLIB2_VERSION)
445 exif_auto_orientate(file);
446#endif
447
448 if ((fmt = imlib_image_format()) != NULL) { /* NOLINT: fmt might be unused, not worth fixing */
449#if HAVE_LIBGIF
450 if (STREQ(fmt, "gif"))
451 img_load_gif(img, file);
452#endif
453#if HAVE_LIBWEBP
454 if (STREQ(fmt, "webp"))
455 img_load_webp(img, file);
456#endif
457#if HAVE_LIBEXIF && defined(IMLIB2_VERSION)
458 if (!STREQ(fmt, "jpeg") && !STREQ(fmt, "jpg"))
459 exif_auto_orientate(file);
460#endif
461 }
462 img->w = imlib_image_get_width();
463 img->h = imlib_image_get_height();
464 img->checkpan = true;
465 img->dirty = true;
466
467 return true;
468}
469
470CLEANUP void img_close(img_t *img, bool decache)
471{
472 unsigned int i;
473
474 if (img->multi.cnt > 0) {
475 for (i = 0; i < img->multi.cnt; i++) {
476 imlib_context_set_image(img->multi.frames[i].im);
477 imlib_free_image();
478 }
479 img->multi.cnt = 0;
480 img->im = NULL;
481 } else if (img->im != NULL) {
482 imlib_context_set_image(img->im);
483 if (decache)
484 imlib_free_image_and_decache();
485 else
486 imlib_free_image();
487 img->im = NULL;
488 }
489}
490
491static void img_check_pan(img_t *img, bool moved)
492{
493 win_t *win;
494 float w, h, ox, oy;
495
496 win = img->win;
497 w = img->w * img->zoom;
498 h = img->h * img->zoom;
499 ox = img->x;
500 oy = img->y;
501
502 if (w < win->w)
503 img->x = (win->w - w) / 2;
504 else if (img->x > 0)
505 img->x = 0;
506 else if (img->x + w < win->w)
507 img->x = win->w - w;
508 if (h < win->h)
509 img->y = (win->h - h) / 2;
510 else if (img->y > 0)
511 img->y = 0;
512 else if (img->y + h < win->h)
513 img->y = win->h - h;
514
515 if (!moved && (ox != img->x || oy != img->y))
516 img->dirty = true;
517}
518
519static bool img_fit(img_t *img)
520{
521 float z, zw, zh;
522
523 if (img->scalemode == SCALE_ZOOM)
524 return false;
525
526 zw = (float) img->win->w / (float) img->w;
527 zh = (float) img->win->h / (float) img->h;
528
529 switch (img->scalemode) {
530 case SCALE_FILL:
531 z = MAX(zw, zh);
532 break;
533 case SCALE_WIDTH:
534 z = zw;
535 break;
536 case SCALE_HEIGHT:
537 z = zh;
538 break;
539 default:
540 z = MIN(zw, zh);
541 break;
542 }
543 z = MIN(z, img->scalemode == SCALE_DOWN ? 1.0 : ZOOM_MAX);
544
545 if (ABS(img->zoom - z) > 1.0/MAX(img->w, img->h)) {
546 img->zoom = z;
547 img->dirty = title_dirty = true;
548 return true;
549 } else {
550 return false;
551 }
552}
553
554void img_render(img_t *img)
555{
556 win_t *win;
557 int sx, sy, sw, sh;
558 int dx, dy, dw, dh;
559 Imlib_Image bg;
560
561 win = img->win;
562 img_fit(img);
563
564 if (img->checkpan) {
565 img_check_pan(img, false);
566 img->checkpan = false;
567 }
568
569 if (!img->dirty)
570 return;
571
572 /* calculate source and destination offsets:
573 * - part of image drawn on full window, or
574 * - full image drawn on part of window
575 */
576 if (img->x <= 0) {
577 sx = -img->x / img->zoom + 0.5;
578 sw = win->w / img->zoom;
579 dx = 0;
580 dw = win->w;
581 } else {
582 sx = 0;
583 sw = img->w;
584 dx = img->x;
585 dw = MAX(img->w * img->zoom, 1);
586 }
587 if (img->y <= 0) {
588 sy = -img->y / img->zoom + 0.5;
589 sh = win->h / img->zoom;
590 dy = win->bar.top ? win->bar.h : 0;
591 dh = win->h;
592 } else {
593 sy = 0;
594 sh = img->h;
595 dy = img->y + (win->bar.top ? win->bar.h : 0);
596 dh = MAX(img->h * img->zoom, 1);
597 }
598
599 win_clear(win);
600
601 imlib_context_set_image(img->im);
602 imlib_context_set_anti_alias(img->anti_alias);
603 imlib_context_set_drawable(win->buf.pm);
604
605 /* manual blending, for performance reasons.
606 * see https://phab.enlightenment.org/T8969#156167 for more details.
607 */
608 if (imlib_image_has_alpha()) {
609 if ((bg = imlib_create_image(dw, dh)) == NULL) {
610 error(0, ENOMEM, "Failed to create image");
611 goto fallback;
612 }
613 imlib_context_set_image(bg);
614 imlib_image_set_has_alpha(0);
615
616 if (img->alpha) {
617 int i, c, r;
618 uint32_t col[2] = { 0xFF666666, 0xFF999999 };
619 uint32_t *data = imlib_image_get_data();
620
621 for (r = 0; r < dh; r++) {
622 i = r * dw;
623 if (r == 0 || r == 8) {
624 for (c = 0; c < dw; c++)
625 data[i++] = col[!(c & 8) ^ !r];
626 } else {
627 memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
628 }
629 }
630 imlib_image_put_back_data(data);
631 } else {
632 XColor c = win->win_bg;
633 imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF);
634 imlib_image_fill_rectangle(0, 0, dw, dh);
635 }
636 imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
637 imlib_context_set_color_modifier(NULL);
638 imlib_render_image_on_drawable(dx, dy);
639 imlib_free_image();
640 imlib_context_set_color_modifier(img->cmod);
641 } else {
642fallback:
643 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
644 }
645 img->dirty = false;
646}
647
648bool img_fit_win(img_t *img, scalemode_t sm)
649{
650 float oz;
651
652 oz = img->zoom;
653 img->scalemode = sm;
654
655 if (img_fit(img)) {
656 img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
657 img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
658 img->checkpan = true;
659 return true;
660 } else {
661 return false;
662 }
663}
664
665bool img_zoom_to(img_t *img, float z)
666{
667 int x, y;
668 if (ZOOM_MIN <= z && z <= ZOOM_MAX) {
669 win_cursor_pos(img->win, &x, &y);
670 if (x < 0 || (unsigned int)x >= img->win->w ||
671 y < 0 || (unsigned int)y >= img->win->h)
672 {
673 x = img->win->w / 2;
674 y = img->win->h / 2;
675 }
676 img->x = x - (x - img->x) * z / img->zoom;
677 img->y = y - (y - img->y) * z / img->zoom;
678 img->zoom = z;
679 img->scalemode = SCALE_ZOOM;
680 img->dirty = img->checkpan = title_dirty = true;
681 return true;
682 } else {
683 return false;
684 }
685}
686
687bool img_zoom(img_t *img, int d)
688{
689 int i = d > 0 ? 0 : (int)ARRLEN(zoom_levels)-1;
690 while (i >= 0 && i < (int)ARRLEN(zoom_levels) &&
691 (d > 0 ? zoom_levels[i]/100 <= img->zoom : zoom_levels[i]/100 >= img->zoom))
692 {
693 i += d;
694 }
695 i = MIN(MAX(i, 0), (int)ARRLEN(zoom_levels)-1);
696 return img_zoom_to(img, zoom_levels[i]/100);
697}
698
699bool img_pos(img_t *img, float x, float y)
700{
701 float ox, oy;
702
703 ox = img->x;
704 oy = img->y;
705
706 img->x = x;
707 img->y = y;
708
709 img_check_pan(img, true);
710
711 if (ox != img->x || oy != img->y) {
712 img->dirty = true;
713 return true;
714 } else {
715 return false;
716 }
717}
718
719static bool img_move(img_t *img, float dx, float dy)
720{
721 return img_pos(img, img->x + dx, img->y + dy);
722}
723
724bool img_pan(img_t *img, direction_t dir, int d)
725{
726 /* d < 0: screen-wise
727 * d = 0: 1/PAN_FRACTION of screen
728 * d > 0: num of pixels
729 */
730 float x, y;
731
732 if (d > 0) {
733 x = y = MAX(1, (float) d * img->zoom);
734 } else {
735 x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
736 y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
737 }
738
739 switch (dir) {
740 case DIR_LEFT:
741 return img_move(img, x, 0.0);
742 case DIR_RIGHT:
743 return img_move(img, -x, 0.0);
744 case DIR_UP:
745 return img_move(img, 0.0, y);
746 case DIR_DOWN:
747 return img_move(img, 0.0, -y);
748 }
749 return false;
750}
751
752bool img_pan_center(img_t *img)
753{
754 float x, y;
755 x = (img->win->w - img->w * img->zoom) / 2.0;
756 y = (img->win->h - img->h * img->zoom) / 2.0;
757 return img_pos(img, x, y);
758}
759
760bool img_pan_edge(img_t *img, direction_t dir)
761{
762 float ox, oy;
763
764 ox = img->x;
765 oy = img->y;
766
767 if (dir & DIR_LEFT)
768 img->x = 0;
769 if (dir & DIR_RIGHT)
770 img->x = img->win->w - img->w * img->zoom;
771 if (dir & DIR_UP)
772 img->y = 0;
773 if (dir & DIR_DOWN)
774 img->y = img->win->h - img->h * img->zoom;
775
776 img_check_pan(img, true);
777
778 if (ox != img->x || oy != img->y) {
779 img->dirty = true;
780 return true;
781 } else {
782 return false;
783 }
784}
785
786void img_rotate(img_t *img, degree_t d)
787{
788 unsigned int i, tmp;
789 float ox, oy;
790
791 imlib_context_set_image(img->im);
792 imlib_image_orientate(d);
793
794 for (i = 0; i < img->multi.cnt; i++) {
795 if (i != img->multi.sel) {
796 imlib_context_set_image(img->multi.frames[i].im);
797 imlib_image_orientate(d);
798 }
799 }
800 if (d == DEGREE_90 || d == DEGREE_270) {
801 ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
802 oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
803
804 img->x = oy + (img->win->w - img->win->h) / 2;
805 img->y = ox + (img->win->h - img->win->w) / 2;
806
807 tmp = img->w;
808 img->w = img->h;
809 img->h = tmp;
810 img->checkpan = true;
811 }
812 img->dirty = true;
813}
814
815void img_flip(img_t *img, flipdir_t d)
816{
817 unsigned int i;
818 void (*imlib_flip_op[3])(void) = {
819 imlib_image_flip_horizontal,
820 imlib_image_flip_vertical,
821 imlib_image_flip_diagonal
822 };
823
824 d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
825
826 if (d < 0 || d >= ARRLEN(imlib_flip_op))
827 return;
828
829 imlib_context_set_image(img->im);
830 imlib_flip_op[d]();
831
832 for (i = 0; i < img->multi.cnt; i++) {
833 if (i != img->multi.sel) {
834 imlib_context_set_image(img->multi.frames[i].im);
835 imlib_flip_op[d]();
836 }
837 }
838 img->dirty = true;
839}
840
841void img_toggle_antialias(img_t *img)
842{
843 img->anti_alias = !img->anti_alias;
844 imlib_context_set_image(img->im);
845 imlib_context_set_anti_alias(img->anti_alias);
846 img->dirty = true;
847}
848
849bool img_change_gamma(img_t *img, int d)
850{
851 /* d < 0: decrease gamma
852 * d = 0: reset gamma
853 * d > 0: increase gamma
854 */
855 int gamma;
856 double range;
857
858 if (d == 0)
859 gamma = 0;
860 else
861 gamma = MIN(MAX(img->gamma + d, -GAMMA_RANGE), GAMMA_RANGE);
862
863 if (img->gamma != gamma) {
864 imlib_reset_color_modifier();
865 if (gamma) {
866 range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
867 imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
868 }
869 img->gamma = gamma;
870 img->dirty = true;
871 return true;
872 } else {
873 return false;
874 }
875}
876
877static bool img_frame_goto(img_t *img, int n)
878{
879 if (n < 0 || (unsigned int)n >= img->multi.cnt || (unsigned int)n == img->multi.sel)
880 return false;
881
882 img->multi.sel = n;
883 img->im = img->multi.frames[n].im;
884
885 imlib_context_set_image(img->im);
886 img->w = imlib_image_get_width();
887 img->h = imlib_image_get_height();
888 img->checkpan = true;
889 img->dirty = true;
890
891 return true;
892}
893
894bool img_frame_navigate(img_t *img, int d)
895{
896 if (img->multi.cnt == 0 || d == 0)
897 return false;
898
899 d += img->multi.sel;
900 d = MAX(0, MIN(d, (int)img->multi.cnt - 1));
901
902 return img_frame_goto(img, d);
903}
904
905bool img_frame_animate(img_t *img)
906{
907 if (img->multi.cnt > 0)
908 return img_frame_goto(img, (img->multi.sel + 1) % img->multi.cnt);
909 else
910 return false;
911}