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