Krolyxon
Commit ca7368a

Branches

Refs →

nsxivimage.c

915 lines · 20.9 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 r = cmap->Colors[bg].Red;
207 g = cmap->Colors[bg].Green;
208 b = cmap->Colors[bg].Blue;
209 bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
210
211 for (i = 0; i < sh; i++) {
212 for (j = 0; j < sw; j++) {
213 if (i < y || i >= y + h || j < x || j >= x + w ||
214 rows[i-y][j-x] == transp)
215 {
216 if (prev_frame != NULL && (prev_disposal != 2 ||
217 i < py || i >= py + ph || j < px || j >= px + pw))
218 {
219 *ptr = prev_frame[i * sw + j];
220 } else {
221 *ptr = bgpixel;
222 }
223 } else {
224 r = cmap->Colors[rows[i-y][j-x]].Red;
225 g = cmap->Colors[rows[i-y][j-x]].Green;
226 b = cmap->Colors[rows[i-y][j-x]].Blue;
227 *ptr = 0xffu << 24 | r << 16 | g << 8 | b;
228 }
229 ptr++;
230 }
231 }
232
233 im = imlib_create_image_using_copied_data(sw, sh, data);
234
235 for (i = 0; i < h; i++)
236 free(rows[i]);
237 free(rows);
238 free(data);
239
240 if (im == NULL) {
241 err = true;
242 break;
243 }
244
245 imlib_context_set_image(im);
246 imlib_image_set_format("gif");
247 if (transp >= 0)
248 imlib_image_set_has_alpha(1);
249
250 if (disposal != 3)
251 prev_frame = imlib_image_get_data_for_reading_only();
252 prev_disposal = disposal;
253 px = x, py = y, pw = w, ph = h;
254
255 if (img->multi.cnt == img->multi.cap) {
256 img->multi.cap *= 2;
257 img->multi.frames = erealloc(img->multi.frames,
258 img->multi.cap * sizeof(img_frame_t));
259 }
260 img->multi.frames[img->multi.cnt].im = im;
261 delay = img->multi.framedelay > 0 ? img->multi.framedelay : delay;
262 img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY;
263 img->multi.length += img->multi.frames[img->multi.cnt].delay;
264 img->multi.cnt++;
265 }
266 } while (rec != TERMINATE_RECORD_TYPE);
267
268#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 && GIFLIB_MINOR >= 1
269 DGifCloseFile(gif, NULL);
270#else
271 DGifCloseFile(gif);
272#endif
273
274 if (err && (file->flags & FF_WARN))
275 error(0, 0, "%s: Corrupted gif file", file->name);
276
277 if (img->multi.cnt > 1) {
278 imlib_context_set_image(img->im);
279 imlib_free_image();
280 img->im = img->multi.frames[0].im;
281 } else if (img->multi.cnt == 1) {
282 imlib_context_set_image(img->multi.frames[0].im);
283 imlib_free_image();
284 img->multi.cnt = 0;
285 }
286
287 imlib_context_set_image(img->im);
288
289 return !err;
290}
291#endif /* HAVE_LIBGIF */
292
293
294#if HAVE_LIBWEBP
295static bool is_webp(const char *path)
296{
297 /* The size (in bytes) of the largest amount of data required to verify a WebP image. */
298 enum { max = 30 };
299 const unsigned char fmt[max];
300 bool ret = false;
301 FILE *f;
302
303 if ((f = fopen(path, "rb")) != NULL) {
304 if (fread((unsigned char *) fmt, 1, max, f) == max)
305 ret = WebPGetInfo(fmt, max, NULL, NULL);
306 fclose(f);
307 }
308 return ret;
309}
310
311/* fframe img
312 * NULL NULL = do nothing
313 * x NULL = load the first frame as an Imlib_Image
314 * NULL x = load all frames into img->multi.
315 */
316static bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img)
317{
318 FILE *webp_file;
319 WebPData data;
320
321 Imlib_Image im = NULL;
322 struct WebPAnimDecoderOptions opts;
323 WebPAnimDecoder *dec = NULL;
324 struct WebPAnimInfo info;
325 unsigned char *buf = NULL;
326 int ts;
327 const WebPDemuxer *demux;
328 WebPIterator iter;
329 unsigned long flags;
330 unsigned int delay;
331 bool err = false;
332 data.bytes = NULL;
333
334 if ((err = fframe == NULL && img == NULL))
335 goto fail;
336
337 if ((err = (webp_file = fopen(file->path, "rb")) == NULL)) {
338 error(0, 0, "%s: Error opening webp image", file->name);
339 goto fail;
340 }
341 fseek(webp_file, 0L, SEEK_END);
342 data.size = ftell(webp_file);
343 rewind(webp_file);
344 data.bytes = emalloc(data.size);
345 if ((err = fread((unsigned char *)data.bytes, 1, data.size, webp_file) != data.size)) {
346 error(0, 0, "%s: Error reading webp image", file->name);
347 goto fail;
348 }
349
350 /* Setup the WebP Animation Decoder */
351 if ((err = !WebPAnimDecoderOptionsInit(&opts))) {
352 error(0, 0, "%s: WebP library version mismatch", file->name);
353 goto fail;
354 }
355 opts.color_mode = MODE_BGRA;
356 /* NOTE: Multi-threaded decoding may cause problems on some system */
357 opts.use_threads = true;
358 dec = WebPAnimDecoderNew(&data, &opts);
359 if ((err = (dec == NULL) || !WebPAnimDecoderGetInfo(dec, &info))) {
360 error(0, 0, "%s: WebP parsing or memory error (file is corrupt?)", file->name);
361 goto fail;
362 }
363 demux = WebPAnimDecoderGetDemuxer(dec);
364
365 if (img == NULL) { /* Only get the first frame and put it into fframe. */
366 if ((err = !WebPAnimDecoderGetNext(dec, &buf, &ts))) {
367 error(0, 0, "%s: Error loading first frame", file->name);
368 goto fail;
369 }
370 *fframe = imlib_create_image_using_copied_data(
371 info.canvas_width, info.canvas_height, (DATA32*)buf);
372 imlib_context_set_image(*fframe);
373 imlib_image_set_has_alpha(WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS) & ALPHA_FLAG);
374 } else {
375 /* Get global information for the image */
376 flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);
377 img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
378 img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
379
380 if (img->multi.cap == 0) {
381 img->multi.cap = info.frame_count;
382 img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t));
383 } else if (info.frame_count > img->multi.cap) {
384 img->multi.cap = info.frame_count;
385 img->multi.frames = erealloc(img->multi.frames,
386 img->multi.cap * sizeof(img_frame_t));
387 }
388
389 /* Load and decode frames (also works on images with only 1 frame) */
390 img->multi.cnt = img->multi.sel = 0;
391 while (WebPAnimDecoderGetNext(dec, &buf, &ts)) {
392 im = imlib_create_image_using_copied_data(
393 info.canvas_width, info.canvas_height, (DATA32*)buf);
394 imlib_context_set_image(im);
395 imlib_image_set_format("webp");
396 /* Get an iterator of this frame - used for frame info (duration, etc.) */
397 WebPDemuxGetFrame(demux, img->multi.cnt+1, &iter);
398 imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG);
399 /* Store info for this frame */
400 img->multi.frames[img->multi.cnt].im = im;
401 delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY;
402 img->multi.frames[img->multi.cnt].delay = delay;
403 img->multi.length += img->multi.frames[img->multi.cnt].delay;
404 img->multi.cnt++;
405 }
406 WebPDemuxReleaseIterator(&iter);
407
408 if (img->multi.cnt > 1) {
409 imlib_context_set_image(img->im);
410 imlib_free_image();
411 img->im = img->multi.frames[0].im;
412 } else if (img->multi.cnt == 1) {
413 imlib_context_set_image(img->multi.frames[0].im);
414 imlib_free_image();
415 img->multi.cnt = 0;
416 }
417 imlib_context_set_image(img->im);
418 }
419 imlib_image_set_format("webp");
420fail:
421 if (dec != NULL)
422 WebPAnimDecoderDelete(dec);
423 free((unsigned char *)data.bytes);
424 return !err;
425}
426#endif /* HAVE_LIBWEBP */
427
428Imlib_Image img_open(const fileinfo_t *file)
429{
430 struct stat st;
431 Imlib_Image im = NULL;
432
433 if (access(file->path, R_OK) == 0 &&
434 stat(file->path, &st) == 0 && S_ISREG(st.st_mode))
435 {
436#if HAVE_LIBWEBP
437 if (is_webp(file->path))
438 img_load_webp(file, &im, NULL);
439 else
440#endif
441 im = imlib_load_image(file->path);
442 if (im != NULL) {
443 imlib_context_set_image(im);
444 if (imlib_image_get_data_for_reading_only() == NULL) {
445 imlib_free_image();
446 im = NULL;
447 }
448 }
449 }
450 if (im == NULL && (file->flags & FF_WARN))
451 error(0, 0, "%s: Error opening image", file->name);
452 return im;
453}
454
455bool img_load(img_t *img, const fileinfo_t *file)
456{
457 const char *fmt;
458
459 if ((img->im = img_open(file)) == NULL)
460 return false;
461
462 imlib_image_set_changes_on_disk();
463
464#if HAVE_LIBEXIF
465 exif_auto_orientate(file);
466#endif
467
468 if ((fmt = imlib_image_format()) != NULL) {
469#if HAVE_LIBGIF
470 if (STREQ(fmt, "gif"))
471 img_load_gif(img, file);
472#endif
473#if HAVE_LIBWEBP
474 if (STREQ(fmt, "webp"))
475 img_load_webp(file, NULL, img);
476#endif
477 }
478 img->w = imlib_image_get_width();
479 img->h = imlib_image_get_height();
480 img->checkpan = true;
481 img->dirty = true;
482
483 return true;
484}
485
486CLEANUP void img_close(img_t *img, bool decache)
487{
488 int i;
489
490 if (img->multi.cnt > 0) {
491 for (i = 0; i < img->multi.cnt; i++) {
492 imlib_context_set_image(img->multi.frames[i].im);
493 imlib_free_image();
494 }
495 img->multi.cnt = 0;
496 img->im = NULL;
497 } else if (img->im != NULL) {
498 imlib_context_set_image(img->im);
499 if (decache)
500 imlib_free_image_and_decache();
501 else
502 imlib_free_image();
503 img->im = NULL;
504 }
505}
506
507static void img_check_pan(img_t *img, bool moved)
508{
509 win_t *win;
510 float w, h, ox, oy;
511
512 win = img->win;
513 w = img->w * img->zoom;
514 h = img->h * img->zoom;
515 ox = img->x;
516 oy = img->y;
517
518 if (w < win->w)
519 img->x = (win->w - w) / 2;
520 else if (img->x > 0)
521 img->x = 0;
522 else if (img->x + w < win->w)
523 img->x = win->w - w;
524 if (h < win->h)
525 img->y = (win->h - h) / 2;
526 else if (img->y > 0)
527 img->y = 0;
528 else if (img->y + h < win->h)
529 img->y = win->h - h;
530
531 if (!moved && (ox != img->x || oy != img->y))
532 img->dirty = true;
533}
534
535static bool img_fit(img_t *img)
536{
537 float z, zw, zh;
538
539 if (img->scalemode == SCALE_ZOOM)
540 return false;
541
542 zw = (float) img->win->w / (float) img->w;
543 zh = (float) img->win->h / (float) img->h;
544
545 switch (img->scalemode) {
546 case SCALE_FILL:
547 z = MAX(zw, zh);
548 break;
549 case SCALE_WIDTH:
550 z = zw;
551 break;
552 case SCALE_HEIGHT:
553 z = zh;
554 break;
555 default:
556 z = MIN(zw, zh);
557 break;
558 }
559 z = MIN(z, img->scalemode == SCALE_DOWN ? 1.0 : ZOOM_MAX);
560
561 if (ABS(img->zoom - z) > 1.0/MAX(img->w, img->h)) {
562 img->zoom = z;
563 img->dirty = true;
564 return true;
565 } else {
566 return false;
567 }
568}
569
570void img_render(img_t *img)
571{
572 win_t *win;
573 int sx, sy, sw, sh;
574 int dx, dy, dw, dh;
575 Imlib_Image bg;
576 XColor c;
577
578 win = img->win;
579 img_fit(img);
580
581 if (img->checkpan) {
582 img_check_pan(img, false);
583 img->checkpan = false;
584 }
585
586 if (!img->dirty)
587 return;
588
589 /* calculate source and destination offsets:
590 * - part of image drawn on full window, or
591 * - full image drawn on part of window
592 */
593 if (img->x <= 0) {
594 sx = -img->x / img->zoom + 0.5;
595 sw = win->w / img->zoom;
596 dx = 0;
597 dw = win->w;
598 } else {
599 sx = 0;
600 sw = img->w;
601 dx = img->x;
602 dw = img->w * img->zoom;
603 }
604 if (img->y <= 0) {
605 sy = -img->y / img->zoom + 0.5;
606 sh = win->h / img->zoom;
607 dy = 0;
608 dh = win->h;
609 } else {
610 sy = 0;
611 sh = img->h;
612 dy = img->y;
613 dh = img->h * img->zoom;
614 }
615
616 win_clear(win);
617
618 imlib_context_set_image(img->im);
619 imlib_context_set_anti_alias(img->aa);
620 imlib_context_set_drawable(win->buf.pm);
621
622 if (imlib_image_has_alpha()) {
623 if ((bg = imlib_create_image(dw, dh)) == NULL)
624 error(EXIT_FAILURE, ENOMEM, NULL);
625 imlib_context_set_image(bg);
626 imlib_image_set_has_alpha(0);
627
628 if (img->alpha) {
629 int i, c, r;
630 DATA32 col[2] = { 0xFF666666, 0xFF999999 };
631 DATA32 * data = imlib_image_get_data();
632
633 for (r = 0; r < dh; r++) {
634 i = r * dw;
635 if (r == 0 || r == 8) {
636 for (c = 0; c < dw; c++)
637 data[i++] = col[!(c & 8) ^ !r];
638 } else {
639 memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
640 }
641 }
642 imlib_image_put_back_data(data);
643 } else {
644 c = win->win_bg;
645 imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF);
646 imlib_image_fill_rectangle(0, 0, dw, dh);
647 }
648 imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
649 imlib_context_set_color_modifier(NULL);
650 imlib_render_image_on_drawable(dx, dy);
651 imlib_free_image();
652 imlib_context_set_color_modifier(img->cmod);
653 } else {
654 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
655 }
656 img->dirty = false;
657}
658
659bool img_fit_win(img_t *img, scalemode_t sm)
660{
661 float oz;
662
663 oz = img->zoom;
664 img->scalemode = sm;
665
666 if (img_fit(img)) {
667 img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
668 img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
669 img->checkpan = true;
670 return true;
671 } else {
672 return false;
673 }
674}
675
676bool img_zoom_to(img_t *img, float z)
677{
678 int x, y;
679 if (ZOOM_MIN <= z && z <= ZOOM_MAX) {
680 win_cursor_pos(img->win, &x, &y);
681 if (x < 0 || x >= img->win->w || y < 0 || y >= img->win->h) {
682 x = img->win->w / 2;
683 y = img->win->h / 2;
684 }
685 img->x = x - (x - img->x) * z / img->zoom;
686 img->y = y - (y - img->y) * z / img->zoom;
687 img->zoom = z;
688 img->scalemode = SCALE_ZOOM;
689 img->checkpan = true;
690 img->dirty = true;
691 return true;
692 } else {
693 return false;
694 }
695}
696
697bool img_zoom(img_t *img, int d)
698{
699 const float z = img->zoom * (d > 0 ? ZOOM_STEP : 1/ZOOM_STEP);
700 return img_zoom_to(img, z);
701}
702
703bool img_pos(img_t *img, float x, float y)
704{
705 float ox, oy;
706
707 ox = img->x;
708 oy = img->y;
709
710 img->x = x;
711 img->y = y;
712
713 img_check_pan(img, true);
714
715 if (ox != img->x || oy != img->y) {
716 img->dirty = true;
717 return true;
718 } else {
719 return false;
720 }
721}
722
723static bool img_move(img_t *img, float dx, float dy)
724{
725 return img_pos(img, img->x + dx, img->y + dy);
726}
727
728bool img_pan(img_t *img, direction_t dir, int d)
729{
730 /* d < 0: screen-wise
731 * d = 0: 1/PAN_FRACTION of screen
732 * d > 0: num of pixels
733 */
734 float x, y;
735
736 if (d > 0) {
737 x = y = MAX(1, (float) d * img->zoom);
738 } else {
739 x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
740 y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
741 }
742
743 switch (dir) {
744 case DIR_LEFT:
745 return img_move(img, x, 0.0);
746 case DIR_RIGHT:
747 return img_move(img, -x, 0.0);
748 case DIR_UP:
749 return img_move(img, 0.0, y);
750 case DIR_DOWN:
751 return img_move(img, 0.0, -y);
752 }
753 return false;
754}
755
756bool img_pan_edge(img_t *img, direction_t dir)
757{
758 float ox, oy;
759
760 ox = img->x;
761 oy = img->y;
762
763 if (dir & DIR_LEFT)
764 img->x = 0;
765 if (dir & DIR_RIGHT)
766 img->x = img->win->w - img->w * img->zoom;
767 if (dir & DIR_UP)
768 img->y = 0;
769 if (dir & DIR_DOWN)
770 img->y = img->win->h - img->h * img->zoom;
771
772 img_check_pan(img, true);
773
774 if (ox != img->x || oy != img->y) {
775 img->dirty = true;
776 return true;
777 } else {
778 return false;
779 }
780}
781
782void img_rotate(img_t *img, degree_t d)
783{
784 int i, tmp;
785 float ox, oy;
786
787 imlib_context_set_image(img->im);
788 imlib_image_orientate(d);
789
790 for (i = 0; i < img->multi.cnt; i++) {
791 if (i != img->multi.sel) {
792 imlib_context_set_image(img->multi.frames[i].im);
793 imlib_image_orientate(d);
794 }
795 }
796 if (d == DEGREE_90 || d == DEGREE_270) {
797 ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
798 oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
799
800 img->x = oy + (img->win->w - img->win->h) / 2;
801 img->y = ox + (img->win->h - img->win->w) / 2;
802
803 tmp = img->w;
804 img->w = img->h;
805 img->h = tmp;
806 img->checkpan = true;
807 }
808 img->dirty = true;
809}
810
811void img_flip(img_t *img, flipdir_t d)
812{
813 int i;
814 void (*imlib_flip_op[3])(void) = {
815 imlib_image_flip_horizontal,
816 imlib_image_flip_vertical,
817 imlib_image_flip_diagonal
818 };
819
820 d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
821
822 if (d < 0 || d >= ARRLEN(imlib_flip_op))
823 return;
824
825 imlib_context_set_image(img->im);
826 imlib_flip_op[d]();
827
828 for (i = 0; i < img->multi.cnt; i++) {
829 if (i != img->multi.sel) {
830 imlib_context_set_image(img->multi.frames[i].im);
831 imlib_flip_op[d]();
832 }
833 }
834 img->dirty = true;
835}
836
837void img_toggle_antialias(img_t *img)
838{
839 img->aa = !img->aa;
840 imlib_context_set_image(img->im);
841 imlib_context_set_anti_alias(img->aa);
842 img->dirty = true;
843}
844
845bool img_change_gamma(img_t *img, int d)
846{
847 /* d < 0: decrease gamma
848 * d = 0: reset gamma
849 * d > 0: increase gamma
850 */
851 int gamma;
852 double range;
853
854 if (d == 0)
855 gamma = 0;
856 else
857 gamma = MIN(MAX(img->gamma + d, -GAMMA_RANGE), GAMMA_RANGE);
858
859 if (img->gamma != gamma) {
860 imlib_reset_color_modifier();
861 if (gamma) {
862 range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
863 imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
864 }
865 img->gamma = gamma;
866 img->dirty = true;
867 return true;
868 } else {
869 return false;
870 }
871}
872
873static bool img_frame_goto(img_t *img, int n)
874{
875 if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
876 return false;
877
878 img->multi.sel = n;
879 img->im = img->multi.frames[n].im;
880
881 imlib_context_set_image(img->im);
882 img->w = imlib_image_get_width();
883 img->h = imlib_image_get_height();
884 img->checkpan = true;
885 img->dirty = true;
886
887 return true;
888}
889
890bool img_frame_navigate(img_t *img, int d)
891{
892 if (img->multi.cnt == 0 || d == 0)
893 return false;
894
895 d += img->multi.sel;
896 if (d < 0)
897 d = 0;
898 else if (d >= img->multi.cnt)
899 d = img->multi.cnt - 1;
900
901 return img_frame_goto(img, d);
902}
903
904bool img_frame_animate(img_t *img)
905{
906 if (img->multi.cnt == 0)
907 return false;
908
909 if (img->multi.sel + 1 >= img->multi.cnt)
910 img_frame_goto(img, 0);
911 else
912 img_frame_goto(img, img->multi.sel + 1);
913 img->dirty = true;
914 return true;
915}