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