| 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> |
| 47 | enum { 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> |
| 54 | enum { 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 | |
| 60 | static 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 | |
| 78 | void 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 |
| 111 | void 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 |
| 152 | static 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 |
| 169 | static 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 |
| 187 | static 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 |
| 355 | static 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); |
| 433 | fail: |
| 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 |
| 443 | static 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 | |
| 453 | static 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_set_changes_on_disk(); /* see img_load() for rationale */ |
| 510 | imlib_image_get_frame_info(&finfo); |
| 511 | assert(finfo.frame_count == (int)fcnt); |
| 512 | assert(finfo.canvas_w == img->w && finfo.canvas_h == img->h); |
| 513 | sx = finfo.frame_x; |
| 514 | sy = finfo.frame_y; |
| 515 | sw = finfo.frame_w; |
| 516 | sh = finfo.frame_h; |
| 517 | has_alpha = imlib_image_has_alpha(); |
| 518 | |
| 519 | imlib_context_set_image(canvas); |
| 520 | /* the dispose flags are explained in Imlib2's header */ |
| 521 | if (pflag & IMLIB_FRAME_DISPOSE_CLEAR) { |
| 522 | img_area_clear(px, py, pw, ph); |
| 523 | } else if (pflag & IMLIB_FRAME_DISPOSE_PREV) { |
| 524 | Imlib_Image p = m->cnt < 2 ? blank : m->frames[m->cnt - 2].im; |
| 525 | assert(m->cnt > 0); |
| 526 | img_area_clear(0, 0, img->w, img->h); |
| 527 | imlib_blend_image_onto_image(p, 1, px, py, pw, ph, px, py, pw, ph); |
| 528 | } |
| 529 | pflag = finfo.frame_flags; |
| 530 | if (pflag & (IMLIB_FRAME_DISPOSE_CLEAR | IMLIB_FRAME_DISPOSE_PREV)) { |
| 531 | /* remember these so we can "dispose" them before blending next frame */ |
| 532 | px = sx; |
| 533 | py = sy; |
| 534 | pw = sw; |
| 535 | ph = sh; |
| 536 | } |
| 537 | assert(imlib_context_get_operation() == IMLIB_OP_COPY); |
| 538 | imlib_image_set_has_alpha(has_alpha); |
| 539 | imlib_context_set_blend(!!(finfo.frame_flags & IMLIB_FRAME_BLEND)); |
| 540 | imlib_blend_image_onto_image(frame, has_alpha, 0, 0, sw, sh, sx, sy, sw, sh); |
| 541 | m->frames[m->cnt].im = canvas; |
| 542 | m->frames[m->cnt].delay = finfo.frame_delay; |
| 543 | m->length += m->frames[m->cnt].delay; |
| 544 | m->cnt++; |
| 545 | imlib_context_set_image(frame); |
| 546 | imlib_free_image(); |
| 547 | } |
| 548 | imlib_context_set_image(blank); |
| 549 | imlib_free_image(); |
| 550 | img_multiframe_context_set(img); |
| 551 | return m->cnt > 0; |
| 552 | } |
| 553 | #endif /* HAVE_IMLIB2_MULTI_FRAME */ |
| 554 | |
| 555 | Imlib_Image img_open(const fileinfo_t *file) |
| 556 | { |
| 557 | struct stat st; |
| 558 | Imlib_Image im = NULL; |
| 559 | |
| 560 | if (access(file->path, R_OK) == 0 && |
| 561 | stat(file->path, &st) == 0 && S_ISREG(st.st_mode) && |
| 562 | #if HAVE_IMLIB2_MULTI_FRAME |
| 563 | (im = imlib_load_image_frame(file->path, 1)) != NULL) |
| 564 | #else |
| 565 | (im = imlib_load_image_immediately(file->path)) != NULL) |
| 566 | #endif |
| 567 | { |
| 568 | imlib_context_set_image(im); |
| 569 | } |
| 570 | if (im == NULL && (file->flags & FF_WARN)) |
| 571 | error(0, 0, "%s: Error opening image", file->name); |
| 572 | return im; |
| 573 | } |
| 574 | |
| 575 | bool img_load(img_t *img, const fileinfo_t *file) |
| 576 | { |
| 577 | const char *fmt; |
| 578 | bool animated = false; |
| 579 | |
| 580 | if ((img->im = img_open(file)) == NULL) |
| 581 | return false; |
| 582 | |
| 583 | /* ensure that the image's timestamp is checked when loading from cache |
| 584 | * to avoid issues like: https://codeberg.org/nsxiv/nsxiv/issues/436 |
| 585 | */ |
| 586 | imlib_image_set_changes_on_disk(); |
| 587 | |
| 588 | /* since v1.7.5, Imlib2 can parse exif orientation from jpeg files. |
| 589 | * this version also happens to be the first one which defines the |
| 590 | * IMLIB2_VERSION macro. |
| 591 | */ |
| 592 | #if HAVE_LIBEXIF && !defined(IMLIB2_VERSION) |
| 593 | exif_auto_orientate(file); |
| 594 | #endif |
| 595 | |
| 596 | #if HAVE_IMLIB2_MULTI_FRAME |
| 597 | animated = img_load_multiframe(img, file); |
| 598 | #endif |
| 599 | |
| 600 | if ((fmt = imlib_image_format()) != NULL) { /* NOLINT: fmt might be unused, not worth fixing */ |
| 601 | #if HAVE_LIBGIF && !HAVE_IMLIB2_MULTI_FRAME |
| 602 | if (STREQ(fmt, "gif")) |
| 603 | img_load_gif(img, file); |
| 604 | #endif |
| 605 | #if HAVE_LIBWEBP && !HAVE_IMLIB2_MULTI_FRAME |
| 606 | if (STREQ(fmt, "webp")) |
| 607 | img_load_webp(img, file); |
| 608 | #endif |
| 609 | #if HAVE_LIBEXIF && defined(IMLIB2_VERSION) |
| 610 | if (!STREQ(fmt, "jpeg") && !STREQ(fmt, "jpg")) |
| 611 | exif_auto_orientate(file); |
| 612 | #endif |
| 613 | } |
| 614 | /* for animated images, we want the _canvas_ width/height, which |
| 615 | * img_load_multiframe() sets already. |
| 616 | */ |
| 617 | if (!animated) { |
| 618 | img->w = imlib_image_get_width(); |
| 619 | img->h = imlib_image_get_height(); |
| 620 | } |
| 621 | img->checkpan = true; |
| 622 | img->dirty = true; |
| 623 | |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | CLEANUP void img_close(img_t *img, bool decache) |
| 628 | { |
| 629 | unsigned int i; |
| 630 | void (*free_img)(void) = decache ? imlib_free_image_and_decache : imlib_free_image; |
| 631 | |
| 632 | if (img->multi.cnt > 0) { |
| 633 | for (i = 0; i < img->multi.cnt; i++) { |
| 634 | imlib_context_set_image(img->multi.frames[i].im); |
| 635 | free_img(); |
| 636 | } |
| 637 | img->multi.cnt = 0; |
| 638 | img->im = NULL; |
| 639 | } else if (img->im != NULL) { |
| 640 | imlib_context_set_image(img->im); |
| 641 | free_img(); |
| 642 | img->im = NULL; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | static void img_check_pan(img_t *img, bool moved) |
| 647 | { |
| 648 | win_t *win; |
| 649 | float w, h, ox, oy; |
| 650 | |
| 651 | win = img->win; |
| 652 | w = img->w * img->zoom; |
| 653 | h = img->h * img->zoom; |
| 654 | ox = img->x; |
| 655 | oy = img->y; |
| 656 | |
| 657 | if (w < win->w) |
| 658 | img->x = (win->w - w) / 2; |
| 659 | else if (img->x > 0) |
| 660 | img->x = 0; |
| 661 | else if (img->x + w < win->w) |
| 662 | img->x = win->w - w; |
| 663 | if (h < win->h) |
| 664 | img->y = (win->h - h) / 2; |
| 665 | else if (img->y > 0) |
| 666 | img->y = 0; |
| 667 | else if (img->y + h < win->h) |
| 668 | img->y = win->h - h; |
| 669 | |
| 670 | if (!moved && (ox != img->x || oy != img->y)) |
| 671 | img->dirty = true; |
| 672 | } |
| 673 | |
| 674 | static bool img_fit(img_t *img) |
| 675 | { |
| 676 | float z, zw, zh; |
| 677 | |
| 678 | if (img->scalemode == SCALE_ZOOM) |
| 679 | return false; |
| 680 | |
| 681 | zw = (float)img->win->w / (float)img->w; |
| 682 | zh = (float)img->win->h / (float)img->h; |
| 683 | |
| 684 | switch (img->scalemode) { |
| 685 | case SCALE_FILL: |
| 686 | z = MAX(zw, zh); |
| 687 | break; |
| 688 | case SCALE_WIDTH: |
| 689 | z = zw; |
| 690 | break; |
| 691 | case SCALE_HEIGHT: |
| 692 | z = zh; |
| 693 | break; |
| 694 | default: |
| 695 | z = MIN(zw, zh); |
| 696 | break; |
| 697 | } |
| 698 | z = MIN(z, img->scalemode == SCALE_DOWN ? 1.0 : ZOOM_MAX); |
| 699 | |
| 700 | if (ABS(img->zoom - z) > 1.0 / MAX(img->w, img->h)) { |
| 701 | img->zoom = z; |
| 702 | img->dirty = title_dirty = true; |
| 703 | return true; |
| 704 | } else { |
| 705 | return false; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | void img_render(img_t *img) |
| 710 | { |
| 711 | win_t *win; |
| 712 | int sx, sy, sw, sh; |
| 713 | int dx, dy, dw, dh; |
| 714 | Imlib_Image bg; |
| 715 | |
| 716 | win = img->win; |
| 717 | img_fit(img); |
| 718 | |
| 719 | if (img->checkpan) { |
| 720 | img_check_pan(img, false); |
| 721 | img->checkpan = false; |
| 722 | } |
| 723 | |
| 724 | if (!img->dirty) |
| 725 | return; |
| 726 | |
| 727 | /* calculate source and destination offsets: |
| 728 | * - part of image drawn on full window, or |
| 729 | * - full image drawn on part of window |
| 730 | */ |
| 731 | if (img->x <= 0) { |
| 732 | sx = -img->x / img->zoom + 0.5; |
| 733 | sw = win->w / img->zoom; |
| 734 | dx = 0; |
| 735 | dw = win->w; |
| 736 | } else { |
| 737 | sx = 0; |
| 738 | sw = img->w; |
| 739 | dx = img->x; |
| 740 | dw = MAX(img->w * img->zoom, 1); |
| 741 | } |
| 742 | if (img->y <= 0) { |
| 743 | sy = -img->y / img->zoom + 0.5; |
| 744 | sh = win->h / img->zoom; |
| 745 | dy = win->bar.top ? win->bar.h : 0; |
| 746 | dh = win->h; |
| 747 | } else { |
| 748 | sy = 0; |
| 749 | sh = img->h; |
| 750 | dy = img->y + (win->bar.top ? win->bar.h : 0); |
| 751 | dh = MAX(img->h * img->zoom, 1); |
| 752 | } |
| 753 | |
| 754 | win_clear(win); |
| 755 | |
| 756 | imlib_context_set_image(img->im); |
| 757 | imlib_context_set_anti_alias(img->anti_alias); |
| 758 | imlib_context_set_drawable(win->buf.pm); |
| 759 | |
| 760 | /* manual blending, for performance reasons. |
| 761 | * see https://phab.enlightenment.org/T8969#156167 for more details. |
| 762 | */ |
| 763 | if (imlib_image_has_alpha()) { |
| 764 | if ((bg = imlib_create_image(dw, dh)) == NULL) { |
| 765 | error(0, ENOMEM, "Failed to create image"); |
| 766 | goto fallback; |
| 767 | } |
| 768 | imlib_context_set_image(bg); |
| 769 | imlib_image_set_has_alpha(0); |
| 770 | |
| 771 | if (img->alpha_layer) { |
| 772 | int i, c, r; |
| 773 | uint32_t col[2] = { 0xFF666666, 0xFF999999 }; |
| 774 | uint32_t *data = imlib_image_get_data(); |
| 775 | |
| 776 | for (r = 0; r < dh; r++) { |
| 777 | i = r * dw; |
| 778 | if (r == 0 || r == 8) { |
| 779 | for (c = 0; c < dw; c++) |
| 780 | data[i++] = col[!(c & 8) ^ !r]; |
| 781 | } else { |
| 782 | memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0])); |
| 783 | } |
| 784 | } |
| 785 | imlib_image_put_back_data(data); |
| 786 | } else { |
| 787 | XColor c = win->win_bg; |
| 788 | imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF); |
| 789 | imlib_image_fill_rectangle(0, 0, dw, dh); |
| 790 | } |
| 791 | imlib_context_set_blend(1); |
| 792 | imlib_context_set_operation(IMLIB_OP_COPY); |
| 793 | imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh); |
| 794 | imlib_context_set_color_modifier(NULL); |
| 795 | imlib_render_image_on_drawable(dx, dy); |
| 796 | imlib_free_image(); |
| 797 | imlib_context_set_color_modifier(img->cmod); |
| 798 | } else { |
| 799 | fallback: |
| 800 | imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); |
| 801 | } |
| 802 | img->dirty = false; |
| 803 | } |
| 804 | |
| 805 | bool img_fit_win(img_t *img, scalemode_t sm) |
| 806 | { |
| 807 | float oz; |
| 808 | |
| 809 | oz = img->zoom; |
| 810 | img->scalemode = sm; |
| 811 | |
| 812 | if (img_fit(img)) { |
| 813 | img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz; |
| 814 | img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz; |
| 815 | img->checkpan = true; |
| 816 | return true; |
| 817 | } else { |
| 818 | return false; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | bool img_zoom_to(img_t *img, float z) |
| 823 | { |
| 824 | int x, y; |
| 825 | if (ZOOM_MIN <= z && z <= ZOOM_MAX) { |
| 826 | win_cursor_pos(img->win, &x, &y); |
| 827 | if (x < 0 || (unsigned int)x >= img->win->w || |
| 828 | y < 0 || (unsigned int)y >= img->win->h) |
| 829 | { |
| 830 | x = img->win->w / 2; |
| 831 | y = img->win->h / 2; |
| 832 | } |
| 833 | img->x = x - (x - img->x) * z / img->zoom; |
| 834 | img->y = y - (y - img->y) * z / img->zoom; |
| 835 | img->zoom = z; |
| 836 | img->scalemode = SCALE_ZOOM; |
| 837 | img->dirty = img->checkpan = title_dirty = true; |
| 838 | return true; |
| 839 | } else { |
| 840 | return false; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | bool img_zoom(img_t *img, int d) |
| 845 | { |
| 846 | int i = d > 0 ? 0 : (int)ARRLEN(zoom_levels) - 1; |
| 847 | while (i >= 0 && i < (int)ARRLEN(zoom_levels) && |
| 848 | (d > 0 ? zoom_levels[i] / 100 <= img->zoom : zoom_levels[i] / 100 >= img->zoom)) |
| 849 | { |
| 850 | i += d; |
| 851 | } |
| 852 | i = MIN(MAX(i, 0), (int)ARRLEN(zoom_levels) - 1); |
| 853 | return img_zoom_to(img, zoom_levels[i] / 100); |
| 854 | } |
| 855 | |
| 856 | bool img_pos(img_t *img, float x, float y) |
| 857 | { |
| 858 | float ox, oy; |
| 859 | |
| 860 | ox = img->x; |
| 861 | oy = img->y; |
| 862 | |
| 863 | img->x = x; |
| 864 | img->y = y; |
| 865 | |
| 866 | img_check_pan(img, true); |
| 867 | |
| 868 | if (ox != img->x || oy != img->y) { |
| 869 | img->dirty = true; |
| 870 | return true; |
| 871 | } else { |
| 872 | return false; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | static bool img_move(img_t *img, float dx, float dy) |
| 877 | { |
| 878 | return img_pos(img, img->x + dx, img->y + dy); |
| 879 | } |
| 880 | |
| 881 | bool img_pan(img_t *img, direction_t dir, int d) |
| 882 | { |
| 883 | /* d < 0: screen-wise |
| 884 | * d = 0: 1/PAN_FRACTION of screen |
| 885 | * d > 0: num of pixels |
| 886 | */ |
| 887 | float x, y; |
| 888 | |
| 889 | if (d > 0) { |
| 890 | x = y = MAX(1, (float)d * img->zoom); |
| 891 | } else { |
| 892 | x = img->win->w / (d < 0 ? 1 : PAN_FRACTION); |
| 893 | y = img->win->h / (d < 0 ? 1 : PAN_FRACTION); |
| 894 | } |
| 895 | |
| 896 | switch (dir) { |
| 897 | case DIR_LEFT: |
| 898 | return img_move(img, x, 0.0); |
| 899 | case DIR_RIGHT: |
| 900 | return img_move(img, -x, 0.0); |
| 901 | case DIR_UP: |
| 902 | return img_move(img, 0.0, y); |
| 903 | case DIR_DOWN: |
| 904 | return img_move(img, 0.0, -y); |
| 905 | } |
| 906 | return false; |
| 907 | } |
| 908 | |
| 909 | bool img_pan_center(img_t *img) |
| 910 | { |
| 911 | float x, y; |
| 912 | x = (img->win->w - img->w * img->zoom) / 2.0; |
| 913 | y = (img->win->h - img->h * img->zoom) / 2.0; |
| 914 | return img_pos(img, x, y); |
| 915 | } |
| 916 | |
| 917 | bool img_pan_edge(img_t *img, direction_t dir) |
| 918 | { |
| 919 | float ox, oy; |
| 920 | |
| 921 | ox = img->x; |
| 922 | oy = img->y; |
| 923 | |
| 924 | if (dir & DIR_LEFT) |
| 925 | img->x = 0; |
| 926 | if (dir & DIR_RIGHT) |
| 927 | img->x = img->win->w - img->w * img->zoom; |
| 928 | if (dir & DIR_UP) |
| 929 | img->y = 0; |
| 930 | if (dir & DIR_DOWN) |
| 931 | img->y = img->win->h - img->h * img->zoom; |
| 932 | |
| 933 | img_check_pan(img, true); |
| 934 | |
| 935 | if (ox != img->x || oy != img->y) { |
| 936 | img->dirty = true; |
| 937 | return true; |
| 938 | } else { |
| 939 | return false; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | void img_rotate(img_t *img, degree_t d) |
| 944 | { |
| 945 | unsigned int i, tmp; |
| 946 | float ox, oy; |
| 947 | |
| 948 | imlib_context_set_image(img->im); |
| 949 | imlib_image_orientate(d); |
| 950 | |
| 951 | for (i = 0; i < img->multi.cnt; i++) { |
| 952 | if (i != img->multi.sel) { |
| 953 | imlib_context_set_image(img->multi.frames[i].im); |
| 954 | imlib_image_orientate(d); |
| 955 | } |
| 956 | } |
| 957 | if (d == DEGREE_90 || d == DEGREE_270) { |
| 958 | ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom; |
| 959 | oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom; |
| 960 | |
| 961 | img->x = oy + (img->win->w - img->win->h) / 2; |
| 962 | img->y = ox + (img->win->h - img->win->w) / 2; |
| 963 | |
| 964 | tmp = img->w; |
| 965 | img->w = img->h; |
| 966 | img->h = tmp; |
| 967 | img->checkpan = true; |
| 968 | } |
| 969 | img->dirty = true; |
| 970 | } |
| 971 | |
| 972 | void img_flip(img_t *img, flipdir_t d) |
| 973 | { |
| 974 | unsigned int i; |
| 975 | void (*imlib_flip_op[3])(void) = { |
| 976 | imlib_image_flip_horizontal, |
| 977 | imlib_image_flip_vertical, |
| 978 | imlib_image_flip_diagonal |
| 979 | }; |
| 980 | |
| 981 | d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1; |
| 982 | |
| 983 | if (d < 0 || d >= ARRLEN(imlib_flip_op)) |
| 984 | return; |
| 985 | |
| 986 | imlib_context_set_image(img->im); |
| 987 | imlib_flip_op[d](); |
| 988 | |
| 989 | for (i = 0; i < img->multi.cnt; i++) { |
| 990 | if (i != img->multi.sel) { |
| 991 | imlib_context_set_image(img->multi.frames[i].im); |
| 992 | imlib_flip_op[d](); |
| 993 | } |
| 994 | } |
| 995 | img->dirty = true; |
| 996 | } |
| 997 | |
| 998 | void img_toggle_antialias(img_t *img) |
| 999 | { |
| 1000 | img->anti_alias = !img->anti_alias; |
| 1001 | imlib_context_set_image(img->im); |
| 1002 | imlib_context_set_anti_alias(img->anti_alias); |
| 1003 | img->dirty = true; |
| 1004 | } |
| 1005 | |
| 1006 | static double steps_to_range(int d, double max, double offset) |
| 1007 | { |
| 1008 | return offset + d * ((d <= 0 ? 1.0 : (max - 1.0)) / CC_STEPS); |
| 1009 | } |
| 1010 | |
| 1011 | void img_update_color_modifiers(img_t *img) |
| 1012 | { |
| 1013 | assert(imlib_context_get_color_modifier() == img->cmod); |
| 1014 | imlib_reset_color_modifier(); |
| 1015 | |
| 1016 | if (img->gamma != 0) |
| 1017 | imlib_modify_color_modifier_gamma(steps_to_range(img->gamma, GAMMA_MAX, 1.0)); |
| 1018 | if (img->brightness != 0) |
| 1019 | imlib_modify_color_modifier_brightness(steps_to_range(img->brightness, BRIGHTNESS_MAX, 0.0)); |
| 1020 | if (img->contrast != 0) |
| 1021 | imlib_modify_color_modifier_contrast(steps_to_range(img->contrast, CONTRAST_MAX, 1.0)); |
| 1022 | |
| 1023 | img->dirty = true; |
| 1024 | } |
| 1025 | |
| 1026 | bool img_change_color_modifier(img_t *img, int d, int *target) |
| 1027 | { |
| 1028 | int value = d == 0 ? 0 : MIN(MAX(*target + d, -CC_STEPS), CC_STEPS); |
| 1029 | |
| 1030 | if (*target == value) |
| 1031 | return false; |
| 1032 | |
| 1033 | *target = value; |
| 1034 | img_update_color_modifiers(img); |
| 1035 | return true; |
| 1036 | } |
| 1037 | |
| 1038 | static bool img_frame_goto(img_t *img, int n) |
| 1039 | { |
| 1040 | if (n < 0 || (unsigned int)n >= img->multi.cnt || (unsigned int)n == img->multi.sel) |
| 1041 | return false; |
| 1042 | |
| 1043 | img->multi.sel = n; |
| 1044 | img->im = img->multi.frames[n].im; |
| 1045 | |
| 1046 | imlib_context_set_image(img->im); |
| 1047 | img->w = imlib_image_get_width(); |
| 1048 | img->h = imlib_image_get_height(); |
| 1049 | img->checkpan = true; |
| 1050 | img->dirty = true; |
| 1051 | |
| 1052 | return true; |
| 1053 | } |
| 1054 | |
| 1055 | bool img_frame_navigate(img_t *img, int d) |
| 1056 | { |
| 1057 | if (img->multi.cnt == 0 || d == 0) |
| 1058 | return false; |
| 1059 | |
| 1060 | d += img->multi.sel; |
| 1061 | d = MAX(0, MIN(d, (int)img->multi.cnt - 1)); |
| 1062 | |
| 1063 | return img_frame_goto(img, d); |
| 1064 | } |
| 1065 | |
| 1066 | bool img_frame_animate(img_t *img) |
| 1067 | { |
| 1068 | if (img->multi.cnt > 0) |
| 1069 | return img_frame_goto(img, (img->multi.sel + 1) % img->multi.cnt); |
| 1070 | else |
| 1071 | return false; |
| 1072 | } |