Krolyxon
Commit 34d4295

Branches

Refs →

nsxivimage.c

1067 lines · 26.0 KB · C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021-2022 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 = 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 && (prev_disposal != 2 ||
286 i < py || i >= py + ph || j < px || j >= px + pw))
287 {
288 *ptr = prev_frame[i * sw + j];
289 } else {
290 *ptr = bgpixel;
291 }
292 } else {
293 assert(cmap != NULL);
294 r = cmap->Colors[rows[i-y][j-x]].Red;
295 g = cmap->Colors[rows[i-y][j-x]].Green;
296 b = cmap->Colors[rows[i-y][j-x]].Blue;
297 *ptr = 0xffu << 24 | r << 16 | g << 8 | b;
298 }
299 ptr++;
300 }
301 }
302
303 im = imlib_create_image_using_copied_data(sw, sh, data);
304
305 for (i = 0; i < h; i++)
306 free(rows[i]);
307 free(rows);
308 free(data);
309
310 if (im == NULL) {
311 err = true;
312 break;
313 }
314
315 imlib_context_set_image(im);
316 imlib_image_set_format("gif");
317 if (transp >= 0)
318 imlib_image_set_has_alpha(1);
319
320 if (disposal != 3)
321 prev_frame = imlib_image_get_data_for_reading_only();
322 prev_disposal = disposal;
323 px = x, py = y, pw = w, ph = h;
324
325 assert(m->cnt <= m->cap);
326 if (m->cnt == m->cap) {
327 m->cap = m->cap == 0 ? 16 : (m->cap * 2);
328 m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames));
329 }
330 m->frames[m->cnt].im = im;
331 delay = m->framedelay > 0 ? m->framedelay : delay;
332 m->frames[m->cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY;
333 m->length += m->frames[m->cnt].delay;
334 m->cnt++;
335 }
336 } while (rec != TERMINATE_RECORD_TYPE);
337
338#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 && GIFLIB_MINOR >= 1
339 DGifCloseFile(gif, NULL);
340#else
341 DGifCloseFile(gif);
342#endif
343
344 if (err && (file->flags & FF_WARN))
345 error(0, 0, "%s: Corrupted gif file", file->name);
346
347 img_multiframe_context_set(img);
348
349 return !err;
350}
351#endif /* HAVE_LIBGIF */
352
353#if HAVE_LIBWEBP && !HAVE_IMLIB2_MULTI_FRAME
354static bool img_load_webp(img_t *img, const fileinfo_t *file)
355{
356 FILE *webp_file;
357 WebPData data;
358 Imlib_Image im = NULL;
359 struct WebPAnimDecoderOptions opts;
360 WebPAnimDecoder *dec = NULL;
361 struct WebPAnimInfo info;
362 unsigned char *buf = NULL, *bytes = NULL;
363 int ts;
364 const WebPDemuxer *demux;
365 WebPIterator iter;
366 unsigned long flags;
367 unsigned int delay;
368 bool err = false;
369 multi_img_t *m = &img->multi;
370
371 img_multiframe_deprecation_notice();
372
373 if ((webp_file = fopen(file->path, "rb")) == NULL) {
374 error(0, errno, "%s: Error opening webp image", file->name);
375 return false;
376 }
377 fseek(webp_file, 0L, SEEK_END);
378 data.size = ftell(webp_file);
379 rewind(webp_file);
380 bytes = emalloc(data.size);
381 if ((err = fread(bytes, 1, data.size, webp_file) != data.size)) {
382 error(0, 0, "%s: Error reading webp image", file->name);
383 goto fail;
384 }
385 data.bytes = bytes;
386
387 /* Setup the WebP Animation Decoder */
388 if ((err = !WebPAnimDecoderOptionsInit(&opts))) {
389 error(0, 0, "%s: WebP library version mismatch", file->name);
390 goto fail;
391 }
392 opts.color_mode = MODE_BGRA;
393 /* NOTE: Multi-threaded decoding may cause problems on some system */
394 opts.use_threads = true;
395 dec = WebPAnimDecoderNew(&data, &opts);
396 if ((err = (dec == NULL) || !WebPAnimDecoderGetInfo(dec, &info))) {
397 error(0, 0, "%s: WebP parsing or memory error (file is corrupt?)", file->name);
398 goto fail;
399 }
400 demux = WebPAnimDecoderGetDemuxer(dec);
401
402 /* Get global information for the image */
403 flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);
404 img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
405 img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
406
407 if (info.frame_count > m->cap) {
408 m->cap = info.frame_count;
409 m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames));
410 }
411
412 /* Load and decode frames (also works on images with only 1 frame) */
413 m->length = m->cnt = m->sel = 0;
414 while (WebPAnimDecoderGetNext(dec, &buf, &ts)) {
415 im = imlib_create_image_using_copied_data(
416 info.canvas_width, info.canvas_height, (uint32_t *)buf);
417 imlib_context_set_image(im);
418 imlib_image_set_format("webp");
419 /* Get an iterator of this frame - used for frame info (duration, etc.) */
420 WebPDemuxGetFrame(demux, m->cnt+1, &iter);
421 imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG);
422 /* Store info for this frame */
423 m->frames[m->cnt].im = im;
424 delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY;
425 m->frames[m->cnt].delay = delay;
426 m->length += m->frames[m->cnt].delay;
427 m->cnt++;
428 }
429 WebPDemuxReleaseIterator(&iter);
430
431 img_multiframe_context_set(img);
432fail:
433 if (dec != NULL)
434 WebPAnimDecoderDelete(dec);
435 free(bytes);
436 fclose(webp_file);
437 return !err;
438}
439#endif /* HAVE_LIBWEBP */
440
441#if HAVE_IMLIB2_MULTI_FRAME
442static void img_area_clear(int x, int y, int w, int h)
443{
444 assert(x >= 0 && y >= 0);
445 assert(w > 0 && h > 0);
446 imlib_image_set_has_alpha(1);
447 imlib_context_set_blend(0);
448 imlib_context_set_color(0, 0, 0, 0);
449 imlib_image_fill_rectangle(x, y, w, h);
450}
451
452static bool img_load_multiframe(img_t *img, const fileinfo_t *file)
453{
454 unsigned int n, fcnt;
455 Imlib_Image blank;
456 Imlib_Frame_Info finfo;
457 int px, py, pw, ph, pflag;
458 multi_img_t *m = &img->multi;
459
460 imlib_context_set_image(img->im);
461 imlib_image_get_frame_info(&finfo);
462 if ((fcnt = finfo.frame_count) <= 1 || !(finfo.frame_flags & IMLIB_IMAGE_ANIMATED))
463 return false;
464 img->w = finfo.canvas_w;
465 img->h = finfo.canvas_h;
466
467 if (fcnt > m->cap) {
468 m->cap = fcnt;
469 m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames));
470 }
471
472 imlib_context_set_dither(0);
473 imlib_context_set_anti_alias(0);
474 imlib_context_set_color_modifier(NULL);
475 imlib_context_set_operation(IMLIB_OP_COPY);
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 /*
485 * Imlib2 gives back a "raw frame", we need to blend it on top of the
486 * previous frame ourselves if necessary to get the fully decoded frame.
487 */
488 pflag = m->length = m->cnt = m->sel = 0;
489 px = py = pw = ph = 0;
490 for (n = 1; n <= fcnt; ++n) {
491 Imlib_Image frame, canvas;
492 int sx, sy, sw, sh;
493 bool has_alpha;
494
495 imlib_context_set_image(m->cnt < 1 ? blank : m->frames[m->cnt - 1].im);
496 if ((canvas = imlib_clone_image()) == NULL ||
497 (frame = imlib_load_image_frame(file->path, n)) == NULL)
498 {
499 if (canvas != NULL) {
500 imlib_context_set_image(canvas);
501 imlib_free_image();
502 }
503 error(0, 0, "%s: failed to load frame %d", file->name, n);
504 break;
505 }
506
507 imlib_context_set_image(frame);
508 imlib_image_get_frame_info(&finfo);
509 assert(finfo.frame_count == (int)fcnt);
510 assert(finfo.canvas_w == img->w && finfo.canvas_h == img->h);
511 sx = finfo.frame_x;
512 sy = finfo.frame_y;
513 sw = finfo.frame_w;
514 sh = finfo.frame_h;
515 has_alpha = imlib_image_has_alpha();
516
517 imlib_context_set_image(canvas);
518 /* the dispose flags are explained in Imlib2's header */
519 if (pflag & IMLIB_FRAME_DISPOSE_CLEAR) {
520 img_area_clear(px, py, pw, ph);
521 } else if (pflag & IMLIB_FRAME_DISPOSE_PREV) {
522 Imlib_Image p = m->cnt < 2 ? blank : m->frames[m->cnt - 2].im;
523 assert(m->cnt > 0);
524 img_area_clear(0, 0, img->w, img->h);
525 imlib_blend_image_onto_image(p, 1, px, py, pw, ph, px, py, pw, ph);
526 }
527 pflag = finfo.frame_flags;
528 if (pflag & (IMLIB_FRAME_DISPOSE_CLEAR | IMLIB_FRAME_DISPOSE_PREV)) {
529 /* remember these so we can "dispose" them before blending next frame */
530 px = sx;
531 py = sy;
532 pw = sw;
533 ph = sh;
534 }
535 assert(imlib_context_get_operation() == IMLIB_OP_COPY);
536 imlib_image_set_has_alpha(has_alpha);
537 imlib_context_set_blend(!!(finfo.frame_flags & IMLIB_FRAME_BLEND));
538 imlib_blend_image_onto_image(frame, has_alpha, 0, 0, sw, sh, sx, sy, sw, sh);
539 m->frames[m->cnt].im = canvas;
540 m->frames[m->cnt].delay = finfo.frame_delay;
541 m->length += m->frames[m->cnt].delay;
542 m->cnt++;
543 imlib_context_set_image(frame);
544 imlib_free_image();
545 }
546 imlib_context_set_image(blank);
547 imlib_free_image();
548 img_multiframe_context_set(img);
549 return m->cnt > 0;
550}
551#endif /* HAVE_IMLIB2_MULTI_FRAME */
552
553Imlib_Image img_open(const fileinfo_t *file)
554{
555 struct stat st;
556 Imlib_Image im = NULL;
557
558 if (access(file->path, R_OK) == 0 &&
559 stat(file->path, &st) == 0 && S_ISREG(st.st_mode) &&
560#if HAVE_IMLIB2_MULTI_FRAME
561 (im = imlib_load_image_frame(file->path, 1)) != NULL)
562#else
563 (im = imlib_load_image_immediately(file->path)) != NULL)
564#endif
565 {
566 imlib_context_set_image(im);
567 }
568 if (im == NULL && (file->flags & FF_WARN))
569 error(0, 0, "%s: Error opening image", file->name);
570 return im;
571}
572
573bool img_load(img_t *img, const fileinfo_t *file)
574{
575 const char *fmt;
576 bool animated = false;
577
578 if ((img->im = img_open(file)) == NULL)
579 return false;
580
581 imlib_image_set_changes_on_disk();
582
583/* since v1.7.5, Imlib2 can parse exif orientation from jpeg files.
584 * this version also happens to be the first one which defines the
585 * IMLIB2_VERSION macro.
586 */
587#if HAVE_LIBEXIF && !defined(IMLIB2_VERSION)
588 exif_auto_orientate(file);
589#endif
590
591#if HAVE_IMLIB2_MULTI_FRAME
592 animated = img_load_multiframe(img, file);
593#endif
594
595 if ((fmt = imlib_image_format()) != NULL) { /* NOLINT: fmt might be unused, not worth fixing */
596#if HAVE_LIBGIF && !HAVE_IMLIB2_MULTI_FRAME
597 if (STREQ(fmt, "gif"))
598 img_load_gif(img, file);
599#endif
600#if HAVE_LIBWEBP && !HAVE_IMLIB2_MULTI_FRAME
601 if (STREQ(fmt, "webp"))
602 img_load_webp(img, file);
603#endif
604#if HAVE_LIBEXIF && defined(IMLIB2_VERSION)
605 if (!STREQ(fmt, "jpeg") && !STREQ(fmt, "jpg"))
606 exif_auto_orientate(file);
607#endif
608 }
609 /* for animated images, we want the _canvas_ width/height, which
610 * img_load_multiframe() sets already.
611 */
612 if (!animated) {
613 img->w = imlib_image_get_width();
614 img->h = imlib_image_get_height();
615 }
616 img->checkpan = true;
617 img->dirty = true;
618
619 return true;
620}
621
622CLEANUP void img_close(img_t *img, bool decache)
623{
624 unsigned int i;
625 void (*free_img)(void) = decache ? imlib_free_image_and_decache : imlib_free_image;
626
627 if (img->multi.cnt > 0) {
628 for (i = 0; i < img->multi.cnt; i++) {
629 imlib_context_set_image(img->multi.frames[i].im);
630 free_img();
631 }
632 img->multi.cnt = 0;
633 img->im = NULL;
634 } else if (img->im != NULL) {
635 imlib_context_set_image(img->im);
636 free_img();
637 img->im = NULL;
638 }
639}
640
641static void img_check_pan(img_t *img, bool moved)
642{
643 win_t *win;
644 float w, h, ox, oy;
645
646 win = img->win;
647 w = img->w * img->zoom;
648 h = img->h * img->zoom;
649 ox = img->x;
650 oy = img->y;
651
652 if (w < win->w)
653 img->x = (win->w - w) / 2;
654 else if (img->x > 0)
655 img->x = 0;
656 else if (img->x + w < win->w)
657 img->x = win->w - w;
658 if (h < win->h)
659 img->y = (win->h - h) / 2;
660 else if (img->y > 0)
661 img->y = 0;
662 else if (img->y + h < win->h)
663 img->y = win->h - h;
664
665 if (!moved && (ox != img->x || oy != img->y))
666 img->dirty = true;
667}
668
669static bool img_fit(img_t *img)
670{
671 float z, zw, zh;
672
673 if (img->scalemode == SCALE_ZOOM)
674 return false;
675
676 zw = (float) img->win->w / (float) img->w;
677 zh = (float) img->win->h / (float) img->h;
678
679 switch (img->scalemode) {
680 case SCALE_FILL:
681 z = MAX(zw, zh);
682 break;
683 case SCALE_WIDTH:
684 z = zw;
685 break;
686 case SCALE_HEIGHT:
687 z = zh;
688 break;
689 default:
690 z = MIN(zw, zh);
691 break;
692 }
693 z = MIN(z, img->scalemode == SCALE_DOWN ? 1.0 : ZOOM_MAX);
694
695 if (ABS(img->zoom - z) > 1.0/MAX(img->w, img->h)) {
696 img->zoom = z;
697 img->dirty = title_dirty = true;
698 return true;
699 } else {
700 return false;
701 }
702}
703
704void img_render(img_t *img)
705{
706 win_t *win;
707 int sx, sy, sw, sh;
708 int dx, dy, dw, dh;
709 Imlib_Image bg;
710
711 win = img->win;
712 img_fit(img);
713
714 if (img->checkpan) {
715 img_check_pan(img, false);
716 img->checkpan = false;
717 }
718
719 if (!img->dirty)
720 return;
721
722 /* calculate source and destination offsets:
723 * - part of image drawn on full window, or
724 * - full image drawn on part of window
725 */
726 if (img->x <= 0) {
727 sx = -img->x / img->zoom + 0.5;
728 sw = win->w / img->zoom;
729 dx = 0;
730 dw = win->w;
731 } else {
732 sx = 0;
733 sw = img->w;
734 dx = img->x;
735 dw = MAX(img->w * img->zoom, 1);
736 }
737 if (img->y <= 0) {
738 sy = -img->y / img->zoom + 0.5;
739 sh = win->h / img->zoom;
740 dy = win->bar.top ? win->bar.h : 0;
741 dh = win->h;
742 } else {
743 sy = 0;
744 sh = img->h;
745 dy = img->y + (win->bar.top ? win->bar.h : 0);
746 dh = MAX(img->h * img->zoom, 1);
747 }
748
749 win_clear(win);
750
751 imlib_context_set_image(img->im);
752 imlib_context_set_anti_alias(img->anti_alias);
753 imlib_context_set_drawable(win->buf.pm);
754
755 /* manual blending, for performance reasons.
756 * see https://phab.enlightenment.org/T8969#156167 for more details.
757 */
758 if (imlib_image_has_alpha()) {
759 if ((bg = imlib_create_image(dw, dh)) == NULL) {
760 error(0, ENOMEM, "Failed to create image");
761 goto fallback;
762 }
763 imlib_context_set_image(bg);
764 imlib_image_set_has_alpha(0);
765
766 if (img->alpha) {
767 int i, c, r;
768 uint32_t col[2] = { 0xFF666666, 0xFF999999 };
769 uint32_t *data = imlib_image_get_data();
770
771 for (r = 0; r < dh; r++) {
772 i = r * dw;
773 if (r == 0 || r == 8) {
774 for (c = 0; c < dw; c++)
775 data[i++] = col[!(c & 8) ^ !r];
776 } else {
777 memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
778 }
779 }
780 imlib_image_put_back_data(data);
781 } else {
782 XColor c = win->win_bg;
783 imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF);
784 imlib_image_fill_rectangle(0, 0, dw, dh);
785 }
786 imlib_context_set_blend(1);
787 imlib_context_set_operation(IMLIB_OP_COPY);
788 imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
789 imlib_context_set_color_modifier(NULL);
790 imlib_render_image_on_drawable(dx, dy);
791 imlib_free_image();
792 imlib_context_set_color_modifier(img->cmod);
793 } else {
794fallback:
795 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
796 }
797 img->dirty = false;
798}
799
800bool img_fit_win(img_t *img, scalemode_t sm)
801{
802 float oz;
803
804 oz = img->zoom;
805 img->scalemode = sm;
806
807 if (img_fit(img)) {
808 img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
809 img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
810 img->checkpan = true;
811 return true;
812 } else {
813 return false;
814 }
815}
816
817bool img_zoom_to(img_t *img, float z)
818{
819 int x, y;
820 if (ZOOM_MIN <= z && z <= ZOOM_MAX) {
821 win_cursor_pos(img->win, &x, &y);
822 if (x < 0 || (unsigned int)x >= img->win->w ||
823 y < 0 || (unsigned int)y >= img->win->h)
824 {
825 x = img->win->w / 2;
826 y = img->win->h / 2;
827 }
828 img->x = x - (x - img->x) * z / img->zoom;
829 img->y = y - (y - img->y) * z / img->zoom;
830 img->zoom = z;
831 img->scalemode = SCALE_ZOOM;
832 img->dirty = img->checkpan = title_dirty = true;
833 return true;
834 } else {
835 return false;
836 }
837}
838
839bool img_zoom(img_t *img, int d)
840{
841 int i = d > 0 ? 0 : (int)ARRLEN(zoom_levels)-1;
842 while (i >= 0 && i < (int)ARRLEN(zoom_levels) &&
843 (d > 0 ? zoom_levels[i]/100 <= img->zoom : zoom_levels[i]/100 >= img->zoom))
844 {
845 i += d;
846 }
847 i = MIN(MAX(i, 0), (int)ARRLEN(zoom_levels)-1);
848 return img_zoom_to(img, zoom_levels[i]/100);
849}
850
851bool img_pos(img_t *img, float x, float y)
852{
853 float ox, oy;
854
855 ox = img->x;
856 oy = img->y;
857
858 img->x = x;
859 img->y = y;
860
861 img_check_pan(img, true);
862
863 if (ox != img->x || oy != img->y) {
864 img->dirty = true;
865 return true;
866 } else {
867 return false;
868 }
869}
870
871static bool img_move(img_t *img, float dx, float dy)
872{
873 return img_pos(img, img->x + dx, img->y + dy);
874}
875
876bool img_pan(img_t *img, direction_t dir, int d)
877{
878 /* d < 0: screen-wise
879 * d = 0: 1/PAN_FRACTION of screen
880 * d > 0: num of pixels
881 */
882 float x, y;
883
884 if (d > 0) {
885 x = y = MAX(1, (float) d * img->zoom);
886 } else {
887 x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
888 y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
889 }
890
891 switch (dir) {
892 case DIR_LEFT:
893 return img_move(img, x, 0.0);
894 case DIR_RIGHT:
895 return img_move(img, -x, 0.0);
896 case DIR_UP:
897 return img_move(img, 0.0, y);
898 case DIR_DOWN:
899 return img_move(img, 0.0, -y);
900 }
901 return false;
902}
903
904bool img_pan_center(img_t *img)
905{
906 float x, y;
907 x = (img->win->w - img->w * img->zoom) / 2.0;
908 y = (img->win->h - img->h * img->zoom) / 2.0;
909 return img_pos(img, x, y);
910}
911
912bool img_pan_edge(img_t *img, direction_t dir)
913{
914 float ox, oy;
915
916 ox = img->x;
917 oy = img->y;
918
919 if (dir & DIR_LEFT)
920 img->x = 0;
921 if (dir & DIR_RIGHT)
922 img->x = img->win->w - img->w * img->zoom;
923 if (dir & DIR_UP)
924 img->y = 0;
925 if (dir & DIR_DOWN)
926 img->y = img->win->h - img->h * img->zoom;
927
928 img_check_pan(img, true);
929
930 if (ox != img->x || oy != img->y) {
931 img->dirty = true;
932 return true;
933 } else {
934 return false;
935 }
936}
937
938void img_rotate(img_t *img, degree_t d)
939{
940 unsigned int i, tmp;
941 float ox, oy;
942
943 imlib_context_set_image(img->im);
944 imlib_image_orientate(d);
945
946 for (i = 0; i < img->multi.cnt; i++) {
947 if (i != img->multi.sel) {
948 imlib_context_set_image(img->multi.frames[i].im);
949 imlib_image_orientate(d);
950 }
951 }
952 if (d == DEGREE_90 || d == DEGREE_270) {
953 ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
954 oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
955
956 img->x = oy + (img->win->w - img->win->h) / 2;
957 img->y = ox + (img->win->h - img->win->w) / 2;
958
959 tmp = img->w;
960 img->w = img->h;
961 img->h = tmp;
962 img->checkpan = true;
963 }
964 img->dirty = true;
965}
966
967void img_flip(img_t *img, flipdir_t d)
968{
969 unsigned int i;
970 void (*imlib_flip_op[3])(void) = {
971 imlib_image_flip_horizontal,
972 imlib_image_flip_vertical,
973 imlib_image_flip_diagonal
974 };
975
976 d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
977
978 if (d < 0 || d >= ARRLEN(imlib_flip_op))
979 return;
980
981 imlib_context_set_image(img->im);
982 imlib_flip_op[d]();
983
984 for (i = 0; i < img->multi.cnt; i++) {
985 if (i != img->multi.sel) {
986 imlib_context_set_image(img->multi.frames[i].im);
987 imlib_flip_op[d]();
988 }
989 }
990 img->dirty = true;
991}
992
993void img_toggle_antialias(img_t *img)
994{
995 img->anti_alias = !img->anti_alias;
996 imlib_context_set_image(img->im);
997 imlib_context_set_anti_alias(img->anti_alias);
998 img->dirty = true;
999}
1000
1001static double steps_to_range(int d, double max, double offset)
1002{
1003 return offset + d * ((d <= 0 ? 1.0 : (max - 1.0)) / CC_STEPS);
1004}
1005
1006void img_update_color_modifiers(img_t *img)
1007{
1008 assert(imlib_context_get_color_modifier() == img->cmod);
1009 imlib_reset_color_modifier();
1010
1011 if (img->gamma != 0)
1012 imlib_modify_color_modifier_gamma(steps_to_range(img->gamma, GAMMA_MAX, 1.0));
1013 if (img->brightness != 0)
1014 imlib_modify_color_modifier_brightness(steps_to_range(img->brightness, BRIGHTNESS_MAX, 0.0));
1015 if (img->contrast != 0)
1016 imlib_modify_color_modifier_contrast(steps_to_range(img->contrast, CONTRAST_MAX, 1.0));
1017
1018 img->dirty = true;
1019}
1020
1021bool img_change_color_modifier(img_t *img, int d, int *target)
1022{
1023 int value = d == 0 ? 0 : MIN(MAX(*target + d, -CC_STEPS), CC_STEPS);
1024
1025 if (*target == value)
1026 return false;
1027
1028 *target = value;
1029 img_update_color_modifiers(img);
1030 return true;
1031}
1032
1033static bool img_frame_goto(img_t *img, int n)
1034{
1035 if (n < 0 || (unsigned int)n >= img->multi.cnt || (unsigned int)n == img->multi.sel)
1036 return false;
1037
1038 img->multi.sel = n;
1039 img->im = img->multi.frames[n].im;
1040
1041 imlib_context_set_image(img->im);
1042 img->w = imlib_image_get_width();
1043 img->h = imlib_image_get_height();
1044 img->checkpan = true;
1045 img->dirty = true;
1046
1047 return true;
1048}
1049
1050bool img_frame_navigate(img_t *img, int d)
1051{
1052 if (img->multi.cnt == 0 || d == 0)
1053 return false;
1054
1055 d += img->multi.sel;
1056 d = MAX(0, MIN(d, (int)img->multi.cnt - 1));
1057
1058 return img_frame_goto(img, d);
1059}
1060
1061bool img_frame_animate(img_t *img)
1062{
1063 if (img->multi.cnt > 0)
1064 return img_frame_goto(img, (img->multi.sel + 1) % img->multi.cnt);
1065 else
1066 return false;
1067}