Krolyxon
Commit 0639047

Branches

Refs →

nsxivimage.c

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