Krolyxon
Commit 7a94a5e

Branches

Refs →

nsxivimage.c

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