Krolyxon
Commit eccd7de

Branches

Refs →

nsxivimage.c

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