Krolyxon
Commit b0b16f1

Branches

Refs →

nsxivimage.c

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