Krolyxon
Commit 4396031

Branches

Refs →

nsxivimage.c

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