Krolyxon
Commit 03eb664

Branches

Refs →

nsxivimage.c

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