Krolyxon
Commit 22ca3b5

Branches

Refs →

nsxivimage.c

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