Krolyxon
Commit 3b6db44

Branches

Refs →

nsxivimage.c

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