Krolyxon
Commit 733916a

Branches

Refs →

nsxivimage.c

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