Krolyxon
Commit 494578c

Branches

Refs →

nsxivimage.c

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