Krolyxon
Commit 4048059

Branches

Refs →

nsxivimage.c

1077 lines · 26.5 KB · C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021-2023 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 INCLUDE_IMAGE_CONFIG
22#include "config.h"
23
24#include <assert.h>
25#include <errno.h>
26#include <stdint.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/stat.h>
30#include <unistd.h>
31
32#if HAVE_LIBEXIF
33#include <libexif/exif-data.h>
34#endif
35
36#ifdef IMLIB2_VERSION
37 #if IMLIB2_VERSION >= IMLIB2_VERSION_(1, 8, 0)
38 #define HAVE_IMLIB2_MULTI_FRAME 1
39 #endif
40#endif
41#ifndef HAVE_IMLIB2_MULTI_FRAME
42 #define HAVE_IMLIB2_MULTI_FRAME 0
43#endif
44
45#if HAVE_LIBGIF && !HAVE_IMLIB2_MULTI_FRAME
46#include <gif_lib.h>
47enum { DEF_GIF_DELAY = 75 };
48#endif
49
50#if HAVE_LIBWEBP && !HAVE_IMLIB2_MULTI_FRAME
51#include <stdio.h>
52#include <webp/decode.h>
53#include <webp/demux.h>
54enum { DEF_WEBP_DELAY = 75 };
55#endif
56
57#if HAVE_IMLIB2_MULTI_FRAME
58enum { DEF_ANIM_DELAY = 75 };
59#endif
60
61#define ZOOM_MIN (zoom_levels[0] / 100)
62#define ZOOM_MAX (zoom_levels[ARRLEN(zoom_levels) - 1] / 100)
63
64static int calc_cache_size(void)
65{
66 long cache, pages = -1, page_size = -1;
67
68 if (CACHE_SIZE_MEM_PERCENTAGE <= 0)
69 return 0;
70#ifdef _SC_PHYS_PAGES /* _SC_PHYS_PAGES isn't POSIX */
71 pages = sysconf(_SC_PHYS_PAGES);
72 page_size = sysconf(_SC_PAGE_SIZE);
73#endif
74 if (pages < 0 || page_size < 0)
75 return CACHE_SIZE_FALLBACK;
76 cache = (pages / 100) * CACHE_SIZE_MEM_PERCENTAGE;
77 cache *= page_size;
78
79 return MIN(cache, CACHE_SIZE_LIMIT);
80}
81
82void img_init(img_t *img, win_t *win)
83{
84 imlib_context_set_display(win->env.dpy);
85 imlib_context_set_visual(win->env.vis);
86 imlib_context_set_colormap(win->env.cmap);
87 imlib_set_cache_size(calc_cache_size());
88
89 img->im = NULL;
90 img->win = win;
91 img->scalemode = options->scalemode;
92 img->zoom = options->zoom;
93 img->zoom = MAX(img->zoom, ZOOM_MIN);
94 img->zoom = MIN(img->zoom, ZOOM_MAX);
95 img->checkpan = false;
96 img->dirty = false;
97 img->anti_alias = options->anti_alias;
98 img->alpha_layer = options->alpha_layer;
99 img->multi.cap = img->multi.cnt = 0;
100 img->multi.animate = options->animate;
101 img->multi.framedelay = options->framerate > 0 ? 1000 / options->framerate : 0;
102 img->multi.length = 0;
103
104 img->cmod = imlib_create_color_modifier();
105 imlib_context_set_color_modifier(img->cmod);
106 img->brightness = 0;
107 img->contrast = 0;
108 img_change_color_modifier(img, options->gamma, &img->gamma);
109
110 img->ss.on = options->slideshow > 0;
111 img->ss.delay = options->slideshow > 0 ? options->slideshow : SLIDESHOW_DELAY * 10u;
112}
113
114#if HAVE_LIBEXIF
115void exif_auto_orientate(const fileinfo_t *file)
116{
117 ExifData *ed;
118 ExifEntry *entry;
119 int byte_order, orientation = 0;
120
121 if ((ed = exif_data_new_from_file(file->path)) == NULL)
122 return;
123 byte_order = exif_data_get_byte_order(ed);
124 entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION);
125 if (entry != NULL)
126 orientation = exif_get_short(entry->data, byte_order);
127 exif_data_unref(ed);
128
129 switch (orientation) {
130 case 5:
131 imlib_image_orientate(1);
132 /* fall through */
133 case 2:
134 imlib_image_flip_vertical();
135 break;
136 case 3:
137 imlib_image_orientate(2);
138 break;
139 case 7:
140 imlib_image_orientate(1);
141 /* fall through */
142 case 4:
143 imlib_image_flip_horizontal();
144 break;
145 case 6:
146 imlib_image_orientate(1);
147 break;
148 case 8:
149 imlib_image_orientate(3);
150 break;
151 }
152}
153#endif
154
155#if HAVE_LIBGIF || HAVE_LIBWEBP || HAVE_IMLIB2_MULTI_FRAME
156static void img_multiframe_context_set(img_t *img)
157{
158 if (img->multi.cnt > 1) {
159 imlib_context_set_image(img->im);
160 imlib_free_image();
161 img->im = img->multi.frames[0].im;
162 } else if (img->multi.cnt == 1) {
163 imlib_context_set_image(img->multi.frames[0].im);
164 imlib_free_image();
165 img->multi.cnt = 0;
166 }
167
168 imlib_context_set_image(img->im);
169}
170#endif
171
172#if (HAVE_LIBGIF || HAVE_LIBWEBP) && !HAVE_IMLIB2_MULTI_FRAME
173static void img_multiframe_deprecation_notice(void)
174{
175 static bool warned;
176 if (!warned) {
177 error(0, 0, "\n"
178 "################################################################\n"
179 "# DEPRECATION NOTICE #\n"
180 "################################################################\n"
181 "# Internal multi-frame gif and webp loaders are deprecated and #\n"
182 "# will be removed soon. Please upgrade to Imlib2 v1.8.0 for #\n"
183 "# multi-frame/animated image support. #\n"
184 "################################################################");
185 warned = true;
186 }
187}
188#endif
189
190#if HAVE_LIBGIF && !HAVE_IMLIB2_MULTI_FRAME
191static bool img_load_gif(img_t *img, const fileinfo_t *file)
192{
193 GifFileType *gif;
194 GifRowType *rows = NULL;
195 GifRecordType rec;
196 ColorMapObject *cmap;
197 uint32_t bgpixel = 0, *data, *ptr;
198 uint32_t *prev_frame = NULL;
199 Imlib_Image im;
200 int i, j, bg, r, g, b;
201 int x, y, w, h, sw, sh;
202 int px, py, pw, ph;
203 int intoffset[] = { 0, 4, 2, 1 };
204 int intjump[] = { 8, 8, 4, 2 };
205 int transp = -1;
206 unsigned int disposal = 0, prev_disposal = 0;
207 unsigned int delay = 0;
208 bool err = false;
209 multi_img_t *m = &img->multi;
210
211 img_multiframe_deprecation_notice();
212
213#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5
214 gif = DGifOpenFileName(file->path, NULL);
215#else
216 gif = DGifOpenFileName(file->path);
217#endif
218 if (gif == NULL) {
219 error(0, 0, "%s: Error opening gif image", file->name);
220 return false;
221 }
222 bg = gif->SBackGroundColor;
223 sw = gif->SWidth;
224 sh = gif->SHeight;
225 px = py = pw = ph = 0;
226
227 m->length = m->cnt = m->sel = 0;
228 do {
229 if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
230 err = true;
231 break;
232 }
233 if (rec == EXTENSION_RECORD_TYPE) {
234 int ext_code;
235 GifByteType *ext = NULL;
236
237 DGifGetExtension(gif, &ext_code, &ext);
238 while (ext) {
239 if (ext_code == GRAPHICS_EXT_FUNC_CODE) {
240 if (ext[1] & 1)
241 transp = (int)ext[4];
242 else
243 transp = -1;
244
245 delay = 10 * ((unsigned int)ext[3] << 8 | (unsigned int)ext[2]);
246 disposal = (unsigned int)ext[1] >> 2 & 0x7;
247 }
248 ext = NULL;
249 DGifGetExtensionNext(gif, &ext);
250 }
251 } else if (rec == IMAGE_DESC_RECORD_TYPE) {
252 if (DGifGetImageDesc(gif) == GIF_ERROR) {
253 err = true;
254 break;
255 }
256 x = gif->Image.Left;
257 y = gif->Image.Top;
258 w = gif->Image.Width;
259 h = gif->Image.Height;
260
261 rows = emalloc(h * sizeof(*rows));
262 for (i = 0; i < h; i++)
263 rows[i] = emalloc(w * sizeof(*rows[i]));
264 if (gif->Image.Interlace) {
265 for (i = 0; i < 4; i++) {
266 for (j = intoffset[i]; j < h; j += intjump[i])
267 DGifGetLine(gif, rows[j], w);
268 }
269 } else {
270 for (i = 0; i < h; i++)
271 DGifGetLine(gif, rows[i], w);
272 }
273
274 ptr = data = emalloc(sw * sh * sizeof(*data));
275 cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
276 /* if bg > cmap->ColorCount, it is transparent black already */
277 if (cmap && bg >= 0 && bg < cmap->ColorCount) {
278 r = cmap->Colors[bg].Red;
279 g = cmap->Colors[bg].Green;
280 b = cmap->Colors[bg].Blue;
281 bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
282 }
283
284 for (i = 0; i < sh; i++) {
285 for (j = 0; j < sw; j++) {
286 if (i < y || i >= y + h || j < x || j >= x + w ||
287 rows[i - y][j - x] == transp)
288 {
289 if (prev_frame != NULL &&
290 (prev_disposal != 2 || i < py || i >= py + ph ||
291 j < px || j >= px + pw))
292 {
293 *ptr = prev_frame[i * sw + j];
294 } else {
295 *ptr = bgpixel;
296 }
297 } else {
298 assert(cmap != NULL);
299 r = cmap->Colors[rows[i - y][j - x]].Red;
300 g = cmap->Colors[rows[i - y][j - x]].Green;
301 b = cmap->Colors[rows[i - y][j - x]].Blue;
302 *ptr = 0xffu << 24 | r << 16 | g << 8 | b;
303 }
304 ptr++;
305 }
306 }
307
308 im = imlib_create_image_using_copied_data(sw, sh, data);
309
310 for (i = 0; i < h; i++)
311 free(rows[i]);
312 free(rows);
313 free(data);
314
315 if (im == NULL) {
316 err = true;
317 break;
318 }
319
320 imlib_context_set_image(im);
321 imlib_image_set_format("gif");
322 if (transp >= 0)
323 imlib_image_set_has_alpha(1);
324
325 if (disposal != 3)
326 prev_frame = imlib_image_get_data_for_reading_only();
327 prev_disposal = disposal;
328 px = x, py = y, pw = w, ph = h;
329
330 assert(m->cnt <= m->cap);
331 if (m->cnt == m->cap) {
332 m->cap = m->cap == 0 ? 16 : (m->cap * 2);
333 m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames));
334 }
335 m->frames[m->cnt].im = im;
336 delay = m->framedelay > 0 ? m->framedelay : delay;
337 m->frames[m->cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY;
338 m->length += m->frames[m->cnt].delay;
339 m->cnt++;
340 }
341 } while (rec != TERMINATE_RECORD_TYPE);
342
343#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 && GIFLIB_MINOR >= 1
344 DGifCloseFile(gif, NULL);
345#else
346 DGifCloseFile(gif);
347#endif
348
349 if (err && (file->flags & FF_WARN))
350 error(0, 0, "%s: Corrupted gif file", file->name);
351
352 img_multiframe_context_set(img);
353
354 return !err;
355}
356#endif /* HAVE_LIBGIF */
357
358#if HAVE_LIBWEBP && !HAVE_IMLIB2_MULTI_FRAME
359static bool img_load_webp(img_t *img, const fileinfo_t *file)
360{
361 FILE *webp_file;
362 WebPData data;
363 Imlib_Image im = NULL;
364 struct WebPAnimDecoderOptions opts;
365 WebPAnimDecoder *dec = NULL;
366 struct WebPAnimInfo info;
367 unsigned char *buf = NULL, *bytes = NULL;
368 int ts;
369 const WebPDemuxer *demux;
370 WebPIterator iter;
371 unsigned long flags;
372 unsigned int delay;
373 bool err = false;
374 multi_img_t *m = &img->multi;
375
376 img_multiframe_deprecation_notice();
377
378 if ((webp_file = fopen(file->path, "rb")) == NULL) {
379 error(0, errno, "%s: Error opening webp image", file->name);
380 return false;
381 }
382 fseek(webp_file, 0L, SEEK_END);
383 data.size = ftell(webp_file);
384 rewind(webp_file);
385 bytes = emalloc(data.size);
386 if ((err = fread(bytes, 1, data.size, webp_file) != data.size)) {
387 error(0, 0, "%s: Error reading webp image", file->name);
388 goto fail;
389 }
390 data.bytes = bytes;
391
392 /* Setup the WebP Animation Decoder */
393 if ((err = !WebPAnimDecoderOptionsInit(&opts))) {
394 error(0, 0, "%s: WebP library version mismatch", file->name);
395 goto fail;
396 }
397 opts.color_mode = MODE_BGRA;
398 /* NOTE: Multi-threaded decoding may cause problems on some system */
399 opts.use_threads = true;
400 dec = WebPAnimDecoderNew(&data, &opts);
401 if ((err = (dec == NULL) || !WebPAnimDecoderGetInfo(dec, &info))) {
402 error(0, 0, "%s: WebP parsing or memory error (file is corrupt?)", file->name);
403 goto fail;
404 }
405 demux = WebPAnimDecoderGetDemuxer(dec);
406
407 /* Get global information for the image */
408 flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);
409 img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
410 img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
411
412 if (info.frame_count > m->cap) {
413 m->cap = info.frame_count;
414 m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames));
415 }
416
417 /* Load and decode frames (also works on images with only 1 frame) */
418 m->length = m->cnt = m->sel = 0;
419 while (WebPAnimDecoderGetNext(dec, &buf, &ts)) {
420 im = imlib_create_image_using_copied_data(info.canvas_width, info.canvas_height,
421 (uint32_t *)buf);
422 imlib_context_set_image(im);
423 imlib_image_set_format("webp");
424 /* Get an iterator of this frame - used for frame info (duration, etc.) */
425 WebPDemuxGetFrame(demux, m->cnt + 1, &iter);
426 imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG);
427 /* Store info for this frame */
428 m->frames[m->cnt].im = im;
429 delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY;
430 m->frames[m->cnt].delay = delay;
431 m->length += m->frames[m->cnt].delay;
432 m->cnt++;
433 }
434 WebPDemuxReleaseIterator(&iter);
435
436 img_multiframe_context_set(img);
437fail:
438 if (dec != NULL)
439 WebPAnimDecoderDelete(dec);
440 free(bytes);
441 fclose(webp_file);
442 return !err;
443}
444#endif /* HAVE_LIBWEBP */
445
446#if HAVE_IMLIB2_MULTI_FRAME
447static void img_area_clear(int x, int y, int w, int h)
448{
449 assert(x >= 0 && y >= 0);
450 assert(w > 0 && h > 0);
451 imlib_image_set_has_alpha(1);
452 imlib_context_set_blend(0);
453 imlib_context_set_color(0, 0, 0, 0);
454 imlib_image_fill_rectangle(x, y, w, h);
455}
456
457static bool img_load_multiframe(img_t *img, const fileinfo_t *file)
458{
459 unsigned int n, fcnt;
460 Imlib_Image blank;
461 Imlib_Frame_Info finfo;
462 int px, py, pw, ph, pflag;
463 multi_img_t *m = &img->multi;
464
465 imlib_context_set_image(img->im);
466 imlib_image_get_frame_info(&finfo);
467 if ((fcnt = finfo.frame_count) <= 1 || !(finfo.frame_flags & IMLIB_IMAGE_ANIMATED))
468 return false;
469 img->w = finfo.canvas_w;
470 img->h = finfo.canvas_h;
471
472 if (fcnt > m->cap) {
473 m->cap = fcnt;
474 m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames));
475 }
476
477 if ((blank = imlib_create_image(img->w, img->h)) == NULL) {
478 error(0, 0, "%s: couldn't create image", file->name);
479 return false;
480 }
481 imlib_context_set_image(blank);
482 img_area_clear(0, 0, img->w, img->h);
483
484 imlib_context_set_dither(0);
485 imlib_context_set_anti_alias(0);
486 imlib_context_set_color_modifier(NULL);
487 imlib_context_set_operation(IMLIB_OP_COPY);
488
489 /*
490 * Imlib2 gives back a "raw frame", we need to blend it on top of the
491 * previous frame ourselves if necessary to get the fully decoded frame.
492 */
493 pflag = m->length = m->cnt = m->sel = 0;
494 px = py = pw = ph = 0;
495 for (n = 1; n <= fcnt; ++n) {
496 Imlib_Image frame, canvas;
497 int sx, sy, sw, sh;
498 bool has_alpha;
499
500 imlib_context_set_image(m->cnt < 1 ? blank : m->frames[m->cnt - 1].im);
501 if ((canvas = imlib_clone_image()) == NULL ||
502 (frame = imlib_load_image_frame(file->path, n)) == NULL)
503 {
504 if (canvas != NULL) {
505 imlib_context_set_image(canvas);
506 imlib_free_image();
507 }
508 error(0, 0, "%s: failed to load frame %d", file->name, n);
509 break;
510 }
511
512 imlib_context_set_image(frame);
513 imlib_image_set_changes_on_disk(); /* see img_load() for rationale */
514 imlib_image_get_frame_info(&finfo);
515 assert(finfo.frame_count == (int)fcnt);
516 assert(finfo.canvas_w == img->w && finfo.canvas_h == img->h);
517 sx = finfo.frame_x;
518 sy = finfo.frame_y;
519 sw = finfo.frame_w;
520 sh = finfo.frame_h;
521 has_alpha = imlib_image_has_alpha();
522
523 imlib_context_set_image(canvas);
524 /* the dispose flags are explained in Imlib2's header */
525 if (pflag & IMLIB_FRAME_DISPOSE_CLEAR) {
526 img_area_clear(px, py, pw, ph);
527 } else if (pflag & IMLIB_FRAME_DISPOSE_PREV) {
528 Imlib_Image p = m->cnt < 2 ? blank : m->frames[m->cnt - 2].im;
529 assert(m->cnt > 0);
530 img_area_clear(0, 0, img->w, img->h);
531 imlib_blend_image_onto_image(p, 1, px, py, pw, ph, px, py, pw, ph);
532 }
533 pflag = finfo.frame_flags;
534 if (pflag & (IMLIB_FRAME_DISPOSE_CLEAR | IMLIB_FRAME_DISPOSE_PREV)) {
535 /* remember these so we can "dispose" them before blending next frame */
536 px = sx;
537 py = sy;
538 pw = sw;
539 ph = sh;
540 }
541 assert(imlib_context_get_operation() == IMLIB_OP_COPY);
542 imlib_image_set_has_alpha(has_alpha);
543 imlib_context_set_blend(!!(finfo.frame_flags & IMLIB_FRAME_BLEND));
544 imlib_blend_image_onto_image(frame, has_alpha, 0, 0, sw, sh, sx, sy, sw, sh);
545 m->frames[m->cnt].im = canvas;
546 m->frames[m->cnt].delay = finfo.frame_delay ? finfo.frame_delay : DEF_ANIM_DELAY;
547 m->length += m->frames[m->cnt].delay;
548 m->cnt++;
549 imlib_context_set_image(frame);
550 imlib_free_image();
551 }
552 imlib_context_set_image(blank);
553 imlib_free_image();
554 img_multiframe_context_set(img);
555 imlib_context_set_color_modifier(img->cmod); /* restore cmod */
556 return m->cnt > 0;
557}
558#endif /* HAVE_IMLIB2_MULTI_FRAME */
559
560Imlib_Image img_open(const fileinfo_t *file)
561{
562 struct stat st;
563 Imlib_Image im = NULL;
564
565 if (access(file->path, R_OK) == 0 &&
566 stat(file->path, &st) == 0 && S_ISREG(st.st_mode) &&
567#if HAVE_IMLIB2_MULTI_FRAME
568 (im = imlib_load_image_frame(file->path, 1)) != NULL)
569#else
570 (im = imlib_load_image_immediately(file->path)) != NULL)
571#endif
572 {
573 imlib_context_set_image(im);
574 }
575 if (im == NULL && (file->flags & FF_WARN))
576 error(0, 0, "%s: Error opening image", file->name);
577 return im;
578}
579
580bool img_load(img_t *img, const fileinfo_t *file)
581{
582 const char *fmt;
583 bool animated = false;
584
585 if ((img->im = img_open(file)) == NULL)
586 return false;
587
588 /* ensure that the image's timestamp is checked when loading from cache
589 * to avoid issues like: https://codeberg.org/nsxiv/nsxiv/issues/436
590 */
591 imlib_image_set_changes_on_disk();
592
593/* since v1.7.5, Imlib2 can parse exif orientation from jpeg files.
594 * this version also happens to be the first one which defines the
595 * IMLIB2_VERSION macro.
596 */
597#if HAVE_LIBEXIF && !defined(IMLIB2_VERSION)
598 exif_auto_orientate(file);
599#endif
600
601#if HAVE_IMLIB2_MULTI_FRAME
602 animated = img_load_multiframe(img, file);
603#endif
604
605 if ((fmt = imlib_image_format()) != NULL) { /* NOLINT: fmt might be unused, not worth fixing */
606#if HAVE_LIBGIF && !HAVE_IMLIB2_MULTI_FRAME
607 if (STREQ(fmt, "gif"))
608 img_load_gif(img, file);
609#endif
610#if HAVE_LIBWEBP && !HAVE_IMLIB2_MULTI_FRAME
611 if (STREQ(fmt, "webp"))
612 img_load_webp(img, file);
613#endif
614#if HAVE_LIBEXIF && defined(IMLIB2_VERSION)
615 if (!STREQ(fmt, "jpeg") && !STREQ(fmt, "jpg"))
616 exif_auto_orientate(file);
617#endif
618 }
619 /* for animated images, we want the _canvas_ width/height, which
620 * img_load_multiframe() sets already.
621 */
622 if (!animated) {
623 img->w = imlib_image_get_width();
624 img->h = imlib_image_get_height();
625 }
626 img->checkpan = true;
627 img->dirty = true;
628
629 return true;
630}
631
632CLEANUP void img_close(img_t *img, bool decache)
633{
634 unsigned int i;
635 void (*free_img)(void) = decache ? imlib_free_image_and_decache : imlib_free_image;
636
637 if (img->multi.cnt > 0) {
638 for (i = 0; i < img->multi.cnt; i++) {
639 imlib_context_set_image(img->multi.frames[i].im);
640 free_img();
641 }
642 img->multi.cnt = 0;
643 img->im = NULL;
644 } else if (img->im != NULL) {
645 imlib_context_set_image(img->im);
646 free_img();
647 img->im = NULL;
648 }
649}
650
651static void img_check_pan(img_t *img, bool moved)
652{
653 win_t *win;
654 float w, h, ox, oy;
655
656 win = img->win;
657 w = img->w * img->zoom;
658 h = img->h * img->zoom;
659 ox = img->x;
660 oy = img->y;
661
662 if (w < win->w)
663 img->x = (win->w - w) / 2;
664 else if (img->x > 0)
665 img->x = 0;
666 else if (img->x + w < win->w)
667 img->x = win->w - w;
668 if (h < win->h)
669 img->y = (win->h - h) / 2;
670 else if (img->y > 0)
671 img->y = 0;
672 else if (img->y + h < win->h)
673 img->y = win->h - h;
674
675 if (!moved && (ox != img->x || oy != img->y))
676 img->dirty = true;
677}
678
679static bool img_fit(img_t *img)
680{
681 float z, zw, zh;
682
683 if (img->scalemode == SCALE_ZOOM)
684 return false;
685
686 zw = (float)img->win->w / (float)img->w;
687 zh = (float)img->win->h / (float)img->h;
688
689 switch (img->scalemode) {
690 case SCALE_FILL:
691 z = MAX(zw, zh);
692 break;
693 case SCALE_WIDTH:
694 z = zw;
695 break;
696 case SCALE_HEIGHT:
697 z = zh;
698 break;
699 default:
700 z = MIN(zw, zh);
701 break;
702 }
703 z = MIN(z, img->scalemode == SCALE_DOWN ? 1.0 : ZOOM_MAX);
704
705 if (ABS(img->zoom - z) > 1.0 / MAX(img->w, img->h)) {
706 img->zoom = z;
707 img->dirty = title_dirty = true;
708 return true;
709 } else {
710 return false;
711 }
712}
713
714void img_render(img_t *img)
715{
716 win_t *win;
717 int sx, sy, sw, sh;
718 int dx, dy, dw, dh;
719 Imlib_Image bg;
720
721 win = img->win;
722 img_fit(img);
723
724 if (img->checkpan) {
725 img_check_pan(img, false);
726 img->checkpan = false;
727 }
728
729 if (!img->dirty)
730 return;
731
732 /* calculate source and destination offsets:
733 * - part of image drawn on full window, or
734 * - full image drawn on part of window
735 */
736 if (img->x <= 0) {
737 sx = -img->x / img->zoom + 0.5;
738 sw = win->w / img->zoom;
739 dx = 0;
740 dw = win->w;
741 } else {
742 sx = 0;
743 sw = img->w;
744 dx = img->x;
745 dw = MAX(img->w * img->zoom, 1);
746 }
747 if (img->y <= 0) {
748 sy = -img->y / img->zoom + 0.5;
749 sh = win->h / img->zoom;
750 dy = win->bar.top ? win->bar.h : 0;
751 dh = win->h;
752 } else {
753 sy = 0;
754 sh = img->h;
755 dy = img->y + (win->bar.top ? win->bar.h : 0);
756 dh = MAX(img->h * img->zoom, 1);
757 }
758
759 win_clear(win);
760
761 imlib_context_set_image(img->im);
762 imlib_context_set_anti_alias(img->anti_alias);
763 imlib_context_set_drawable(win->buf.pm);
764
765 /* manual blending, for performance reasons.
766 * see https://phab.enlightenment.org/T8969#156167 for more details.
767 */
768 if (imlib_image_has_alpha()) {
769 if ((bg = imlib_create_image(dw, dh)) == NULL) {
770 error(0, ENOMEM, "Failed to create image");
771 goto fallback;
772 }
773 imlib_context_set_image(bg);
774 imlib_image_set_has_alpha(0);
775
776 if (img->alpha_layer) {
777 int i, c, r;
778 uint32_t col[2] = { 0xFF666666, 0xFF999999 };
779 uint32_t *data = imlib_image_get_data();
780
781 for (r = 0; r < dh; r++) {
782 i = r * dw;
783 if (r == 0 || r == 8) {
784 for (c = 0; c < dw; c++)
785 data[i++] = col[!(c & 8) ^ !r];
786 } else {
787 memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
788 }
789 }
790 imlib_image_put_back_data(data);
791 } else {
792 XColor c = win->win_bg;
793 imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF);
794 imlib_image_fill_rectangle(0, 0, dw, dh);
795 }
796 imlib_context_set_blend(1);
797 imlib_context_set_operation(IMLIB_OP_COPY);
798 imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
799 imlib_context_set_color_modifier(NULL);
800 imlib_render_image_on_drawable(dx, dy);
801 imlib_free_image();
802 imlib_context_set_color_modifier(img->cmod);
803 } else {
804fallback:
805 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
806 }
807 img->dirty = false;
808}
809
810bool img_fit_win(img_t *img, scalemode_t sm)
811{
812 float oz;
813
814 oz = img->zoom;
815 img->scalemode = sm;
816
817 if (img_fit(img)) {
818 img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
819 img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
820 img->checkpan = true;
821 return true;
822 } else {
823 return false;
824 }
825}
826
827bool img_zoom_to(img_t *img, float z)
828{
829 int x, y;
830 if (ZOOM_MIN <= z && z <= ZOOM_MAX) {
831 win_cursor_pos(img->win, &x, &y);
832 if (x < 0 || (unsigned int)x >= img->win->w ||
833 y < 0 || (unsigned int)y >= img->win->h)
834 {
835 x = img->win->w / 2;
836 y = img->win->h / 2;
837 }
838 img->x = x - (x - img->x) * z / img->zoom;
839 img->y = y - (y - img->y) * z / img->zoom;
840 img->zoom = z;
841 img->scalemode = SCALE_ZOOM;
842 img->dirty = img->checkpan = title_dirty = true;
843 return true;
844 } else {
845 return false;
846 }
847}
848
849bool img_zoom(img_t *img, int d)
850{
851 int i = d > 0 ? 0 : (int)ARRLEN(zoom_levels) - 1;
852 while (i >= 0 && i < (int)ARRLEN(zoom_levels) &&
853 (d > 0 ? zoom_levels[i] / 100 <= img->zoom : zoom_levels[i] / 100 >= img->zoom))
854 {
855 i += d;
856 }
857 i = MIN(MAX(i, 0), (int)ARRLEN(zoom_levels) - 1);
858 return img_zoom_to(img, zoom_levels[i] / 100);
859}
860
861bool img_pos(img_t *img, float x, float y)
862{
863 float ox, oy;
864
865 ox = img->x;
866 oy = img->y;
867
868 img->x = x;
869 img->y = y;
870
871 img_check_pan(img, true);
872
873 if (ox != img->x || oy != img->y) {
874 img->dirty = true;
875 return true;
876 } else {
877 return false;
878 }
879}
880
881static bool img_move(img_t *img, float dx, float dy)
882{
883 return img_pos(img, img->x + dx, img->y + dy);
884}
885
886bool img_pan(img_t *img, direction_t dir, int d)
887{
888 /* d < 0: screen-wise
889 * d = 0: 1/PAN_FRACTION of screen
890 * d > 0: num of pixels
891 */
892 float x, y;
893
894 if (d > 0) {
895 x = y = MAX(1, (float)d * img->zoom);
896 } else {
897 x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
898 y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
899 }
900
901 switch (dir) {
902 case DIR_LEFT:
903 return img_move(img, x, 0.0);
904 case DIR_RIGHT:
905 return img_move(img, -x, 0.0);
906 case DIR_UP:
907 return img_move(img, 0.0, y);
908 case DIR_DOWN:
909 return img_move(img, 0.0, -y);
910 }
911 return false;
912}
913
914bool img_pan_center(img_t *img)
915{
916 float x, y;
917 x = (img->win->w - img->w * img->zoom) / 2.0;
918 y = (img->win->h - img->h * img->zoom) / 2.0;
919 return img_pos(img, x, y);
920}
921
922bool img_pan_edge(img_t *img, direction_t dir)
923{
924 float ox, oy;
925
926 ox = img->x;
927 oy = img->y;
928
929 if (dir & DIR_LEFT)
930 img->x = 0;
931 if (dir & DIR_RIGHT)
932 img->x = img->win->w - img->w * img->zoom;
933 if (dir & DIR_UP)
934 img->y = 0;
935 if (dir & DIR_DOWN)
936 img->y = img->win->h - img->h * img->zoom;
937
938 img_check_pan(img, true);
939
940 if (ox != img->x || oy != img->y) {
941 img->dirty = true;
942 return true;
943 } else {
944 return false;
945 }
946}
947
948void img_rotate(img_t *img, degree_t d)
949{
950 unsigned int i, tmp;
951 float ox, oy;
952
953 imlib_context_set_image(img->im);
954 imlib_image_orientate(d);
955
956 for (i = 0; i < img->multi.cnt; i++) {
957 if (i != img->multi.sel) {
958 imlib_context_set_image(img->multi.frames[i].im);
959 imlib_image_orientate(d);
960 }
961 }
962 if (d == DEGREE_90 || d == DEGREE_270) {
963 ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
964 oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
965
966 img->x = oy + (img->win->w - img->win->h) / 2;
967 img->y = ox + (img->win->h - img->win->w) / 2;
968
969 tmp = img->w;
970 img->w = img->h;
971 img->h = tmp;
972 img->checkpan = true;
973 }
974 img->dirty = true;
975}
976
977void img_flip(img_t *img, flipdir_t d)
978{
979 unsigned int i;
980 void (*imlib_flip_op[3])(void) = {
981 imlib_image_flip_horizontal,
982 imlib_image_flip_vertical,
983 imlib_image_flip_diagonal
984 };
985
986 d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
987
988 if (d < 0 || d >= ARRLEN(imlib_flip_op))
989 return;
990
991 imlib_context_set_image(img->im);
992 imlib_flip_op[d]();
993
994 for (i = 0; i < img->multi.cnt; i++) {
995 if (i != img->multi.sel) {
996 imlib_context_set_image(img->multi.frames[i].im);
997 imlib_flip_op[d]();
998 }
999 }
1000 img->dirty = true;
1001}
1002
1003void img_toggle_antialias(img_t *img)
1004{
1005 img->anti_alias = !img->anti_alias;
1006 imlib_context_set_image(img->im);
1007 imlib_context_set_anti_alias(img->anti_alias);
1008 img->dirty = true;
1009}
1010
1011static double steps_to_range(int d, double max, double offset)
1012{
1013 return offset + d * ((d <= 0 ? 1.0 : (max - 1.0)) / CC_STEPS);
1014}
1015
1016void img_update_color_modifiers(img_t *img)
1017{
1018 assert(imlib_context_get_color_modifier() == img->cmod);
1019 imlib_reset_color_modifier();
1020
1021 if (img->gamma != 0)
1022 imlib_modify_color_modifier_gamma(steps_to_range(img->gamma, GAMMA_MAX, 1.0));
1023 if (img->brightness != 0)
1024 imlib_modify_color_modifier_brightness(steps_to_range(img->brightness, BRIGHTNESS_MAX, 0.0));
1025 if (img->contrast != 0)
1026 imlib_modify_color_modifier_contrast(steps_to_range(img->contrast, CONTRAST_MAX, 1.0));
1027
1028 img->dirty = true;
1029}
1030
1031bool img_change_color_modifier(img_t *img, int d, int *target)
1032{
1033 int value = d == 0 ? 0 : MIN(MAX(*target + d, -CC_STEPS), CC_STEPS);
1034
1035 if (*target == value)
1036 return false;
1037
1038 *target = value;
1039 img_update_color_modifiers(img);
1040 return true;
1041}
1042
1043static bool img_frame_goto(img_t *img, int n)
1044{
1045 if (n < 0 || (unsigned int)n >= img->multi.cnt || (unsigned int)n == img->multi.sel)
1046 return false;
1047
1048 img->multi.sel = n;
1049 img->im = img->multi.frames[n].im;
1050
1051 imlib_context_set_image(img->im);
1052 img->w = imlib_image_get_width();
1053 img->h = imlib_image_get_height();
1054 img->checkpan = true;
1055 img->dirty = true;
1056
1057 return true;
1058}
1059
1060bool img_frame_navigate(img_t *img, int d)
1061{
1062 if (img->multi.cnt == 0 || d == 0)
1063 return false;
1064
1065 d += img->multi.sel;
1066 d = MAX(0, MIN(d, (int)img->multi.cnt - 1));
1067
1068 return img_frame_goto(img, d);
1069}
1070
1071bool img_frame_animate(img_t *img)
1072{
1073 if (img->multi.cnt > 0)
1074 return img_frame_goto(img, (img->multi.sel + 1) % img->multi.cnt);
1075 else
1076 return false;
1077}