| 1 | /* Copyright 2011, 2012 Bert Muennich |
| 2 | * |
| 3 | * This file is part of sxiv. |
| 4 | * |
| 5 | * sxiv is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published |
| 7 | * by the Free Software Foundation; either version 2 of the License, |
| 8 | * or (at your option) any later version. |
| 9 | * |
| 10 | * sxiv is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with sxiv. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #define _POSIX_C_SOURCE 200112L |
| 20 | #define _IMAGE_CONFIG |
| 21 | #define _RENDER_CONFIG |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #if HAVE_GIFLIB |
| 29 | #include <gif_lib.h> |
| 30 | enum { MIN_GIF_DELAY = 25 }; |
| 31 | #endif |
| 32 | |
| 33 | #include "exif.h" |
| 34 | #include "image.h" |
| 35 | #include "options.h" |
| 36 | #include "util.h" |
| 37 | #include "config.h" |
| 38 | |
| 39 | float zoom_min; |
| 40 | float zoom_max; |
| 41 | |
| 42 | static int zoomdiff(float z1, float z2) |
| 43 | { |
| 44 | return (int) (z1 * 1000.0 - z2 * 1000.0); |
| 45 | } |
| 46 | |
| 47 | static void img_apply_gamma(img_t *img) |
| 48 | { |
| 49 | if (img == NULL || img->im == NULL || img->cmod == NULL) |
| 50 | return; |
| 51 | |
| 52 | if (img->gamma == 0) { |
| 53 | imlib_context_set_color_modifier(NULL); |
| 54 | } else { |
| 55 | double range = img->gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0; |
| 56 | |
| 57 | imlib_context_set_color_modifier(img->cmod); |
| 58 | imlib_reset_color_modifier(); |
| 59 | imlib_modify_color_modifier_gamma(1.0 + img->gamma * (range / GAMMA_RANGE)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void img_init(img_t *img, win_t *win) |
| 64 | { |
| 65 | zoom_min = zoom_levels[0] / 100.0; |
| 66 | zoom_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0; |
| 67 | |
| 68 | if (img == NULL || win == NULL) |
| 69 | return; |
| 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 | |
| 75 | img->im = NULL; |
| 76 | img->win = win; |
| 77 | img->scalemode = options->scalemode; |
| 78 | img->zoom = options->zoom; |
| 79 | img->zoom = MAX(img->zoom, zoom_min); |
| 80 | img->zoom = MIN(img->zoom, zoom_max); |
| 81 | img->checkpan = false; |
| 82 | img->dirty = false; |
| 83 | img->aa = RENDER_ANTI_ALIAS; |
| 84 | img->alpha = !RENDER_WHITE_ALPHA; |
| 85 | img->multi.cap = img->multi.cnt = 0; |
| 86 | img->multi.animate = false; |
| 87 | img->multi.length = img->multi.repeat = 0; |
| 88 | |
| 89 | img->cmod = imlib_create_color_modifier(); |
| 90 | img->gamma = MIN(MAX(options->gamma, -GAMMA_RANGE), GAMMA_RANGE); |
| 91 | |
| 92 | img->ss.on = options->slideshow > 0; |
| 93 | img->ss.delay = options->slideshow > 0 ? options->slideshow : SLIDESHOW_DELAY; |
| 94 | } |
| 95 | |
| 96 | void exif_auto_orientate(const fileinfo_t *file) |
| 97 | { |
| 98 | switch (exif_orientation(file)) { |
| 99 | case 5: |
| 100 | imlib_image_orientate(1); |
| 101 | case 2: |
| 102 | imlib_image_flip_vertical(); |
| 103 | break; |
| 104 | case 3: |
| 105 | imlib_image_orientate(2); |
| 106 | break; |
| 107 | case 7: |
| 108 | imlib_image_orientate(1); |
| 109 | case 4: |
| 110 | imlib_image_flip_horizontal(); |
| 111 | break; |
| 112 | case 6: |
| 113 | imlib_image_orientate(1); |
| 114 | break; |
| 115 | case 8: |
| 116 | imlib_image_orientate(3); |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | #if HAVE_GIFLIB |
| 122 | bool img_load_gif(img_t *img, const fileinfo_t *file) |
| 123 | { |
| 124 | GifFileType *gif; |
| 125 | GifRowType *rows = NULL; |
| 126 | GifRecordType rec; |
| 127 | ColorMapObject *cmap; |
| 128 | DATA32 bgpixel, *data, *ptr; |
| 129 | DATA32 *prev_frame = NULL; |
| 130 | Imlib_Image im; |
| 131 | int i, j, bg, r, g, b; |
| 132 | int x, y, w, h, sw, sh; |
| 133 | int px, py, pw, ph; |
| 134 | int intoffset[] = { 0, 4, 2, 1 }; |
| 135 | int intjump[] = { 8, 8, 4, 2 }; |
| 136 | int transp = -1; |
| 137 | unsigned int disposal = 0, prev_disposal = 0; |
| 138 | unsigned int delay = 0; |
| 139 | bool err = false; |
| 140 | |
| 141 | if (img->multi.cap == 0) { |
| 142 | img->multi.cap = 8; |
| 143 | img->multi.frames = (img_frame_t*) |
| 144 | s_malloc(sizeof(img_frame_t) * img->multi.cap); |
| 145 | } |
| 146 | img->multi.cnt = img->multi.sel = 0; |
| 147 | img->multi.length = img->multi.repeat = 0; |
| 148 | |
| 149 | #if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 |
| 150 | gif = DGifOpenFileName(file->path, NULL); |
| 151 | #else |
| 152 | gif = DGifOpenFileName(file->path); |
| 153 | #endif |
| 154 | if (gif == NULL) { |
| 155 | warn("could not open gif file: %s", file->name); |
| 156 | return false; |
| 157 | } |
| 158 | bg = gif->SBackGroundColor; |
| 159 | sw = gif->SWidth; |
| 160 | sh = gif->SHeight; |
| 161 | px = py = pw = ph = 0; |
| 162 | |
| 163 | do { |
| 164 | if (DGifGetRecordType(gif, &rec) == GIF_ERROR) { |
| 165 | err = true; |
| 166 | break; |
| 167 | } |
| 168 | if (rec == EXTENSION_RECORD_TYPE) { |
| 169 | int ext_code; |
| 170 | GifByteType *ext = NULL; |
| 171 | |
| 172 | DGifGetExtension(gif, &ext_code, &ext); |
| 173 | while (ext) { |
| 174 | if (ext_code == GRAPHICS_EXT_FUNC_CODE) { |
| 175 | if (ext[1] & 1) |
| 176 | transp = (int) ext[4]; |
| 177 | else |
| 178 | transp = -1; |
| 179 | |
| 180 | delay = 10 * ((unsigned int) ext[3] << 8 | (unsigned int) ext[2]); |
| 181 | if (delay) |
| 182 | delay = MAX(delay, MIN_GIF_DELAY); |
| 183 | |
| 184 | disposal = (unsigned int) ext[1] >> 2 & 0x7; |
| 185 | } else if (ext_code == APPLICATION_EXT_FUNC_CODE) { |
| 186 | if (ext[0] == 11 && memcmp(ext+1, "NETSCAPE2.0", 11) == 0) { |
| 187 | DGifGetExtensionNext(gif, &ext); |
| 188 | if (ext && ext[0] == 3 && ext[1] == 1) |
| 189 | img->multi.repeat = ((int) ext[3] << 8 | (int) ext[2]) - 1; |
| 190 | } |
| 191 | } |
| 192 | ext = NULL; |
| 193 | DGifGetExtensionNext(gif, &ext); |
| 194 | } |
| 195 | } else if (rec == IMAGE_DESC_RECORD_TYPE) { |
| 196 | if (DGifGetImageDesc(gif) == GIF_ERROR) { |
| 197 | err = true; |
| 198 | break; |
| 199 | } |
| 200 | x = gif->Image.Left; |
| 201 | y = gif->Image.Top; |
| 202 | w = gif->Image.Width; |
| 203 | h = gif->Image.Height; |
| 204 | |
| 205 | rows = (GifRowType*) s_malloc(h * sizeof(GifRowType)); |
| 206 | for (i = 0; i < h; i++) |
| 207 | rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType)); |
| 208 | if (gif->Image.Interlace) { |
| 209 | for (i = 0; i < 4; i++) { |
| 210 | for (j = intoffset[i]; j < h; j += intjump[i]) |
| 211 | DGifGetLine(gif, rows[j], w); |
| 212 | } |
| 213 | } else { |
| 214 | for (i = 0; i < h; i++) |
| 215 | DGifGetLine(gif, rows[i], w); |
| 216 | } |
| 217 | |
| 218 | ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh); |
| 219 | cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap; |
| 220 | r = cmap->Colors[bg].Red; |
| 221 | g = cmap->Colors[bg].Green; |
| 222 | b = cmap->Colors[bg].Blue; |
| 223 | bgpixel = 0x00ffffff & (r << 16 | g << 8 | b); |
| 224 | |
| 225 | for (i = 0; i < sh; i++) { |
| 226 | for (j = 0; j < sw; j++) { |
| 227 | if (i < y || i >= y + h || j < x || j >= x + w || |
| 228 | rows[i-y][j-x] == transp) |
| 229 | { |
| 230 | if (prev_frame != NULL && (prev_disposal != 2 || |
| 231 | i < py || i >= py + ph || j < px || j >= px + pw)) |
| 232 | { |
| 233 | *ptr = prev_frame[i * sw + j]; |
| 234 | } else { |
| 235 | *ptr = bgpixel; |
| 236 | } |
| 237 | } else { |
| 238 | r = cmap->Colors[rows[i-y][j-x]].Red; |
| 239 | g = cmap->Colors[rows[i-y][j-x]].Green; |
| 240 | b = cmap->Colors[rows[i-y][j-x]].Blue; |
| 241 | *ptr = 0xff << 24 | r << 16 | g << 8 | b; |
| 242 | } |
| 243 | ptr++; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | im = imlib_create_image_using_copied_data(sw, sh, data); |
| 248 | |
| 249 | for (i = 0; i < h; i++) |
| 250 | free(rows[i]); |
| 251 | free(rows); |
| 252 | free(data); |
| 253 | |
| 254 | if (im == NULL) { |
| 255 | err = true; |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | imlib_context_set_image(im); |
| 260 | imlib_image_set_format("gif"); |
| 261 | if (transp >= 0) |
| 262 | imlib_image_set_has_alpha(1); |
| 263 | |
| 264 | if (disposal != 3) |
| 265 | prev_frame = imlib_image_get_data_for_reading_only(); |
| 266 | prev_disposal = disposal; |
| 267 | px = x, py = y, pw = w, ph = h; |
| 268 | |
| 269 | if (img->multi.cnt == img->multi.cap) { |
| 270 | img->multi.cap *= 2; |
| 271 | img->multi.frames = (img_frame_t*) |
| 272 | s_realloc(img->multi.frames, |
| 273 | img->multi.cap * sizeof(img_frame_t)); |
| 274 | } |
| 275 | img->multi.frames[img->multi.cnt].im = im; |
| 276 | img->multi.frames[img->multi.cnt].delay = delay ? delay : GIF_DELAY; |
| 277 | img->multi.length += img->multi.frames[img->multi.cnt].delay; |
| 278 | img->multi.cnt++; |
| 279 | } |
| 280 | } while (rec != TERMINATE_RECORD_TYPE); |
| 281 | |
| 282 | DGifCloseFile(gif); |
| 283 | |
| 284 | if (err && !file->loaded) |
| 285 | warn("corrupted gif file: %s", file->name); |
| 286 | |
| 287 | if (img->multi.cnt > 1) { |
| 288 | imlib_context_set_image(img->im); |
| 289 | imlib_free_image(); |
| 290 | img->im = img->multi.frames[0].im; |
| 291 | img->multi.animate = GIF_AUTOPLAY; |
| 292 | } else if (img->multi.cnt == 1) { |
| 293 | imlib_context_set_image(img->multi.frames[0].im); |
| 294 | imlib_free_image(); |
| 295 | img->multi.cnt = 0; |
| 296 | img->multi.animate = false; |
| 297 | } |
| 298 | |
| 299 | imlib_context_set_image(img->im); |
| 300 | |
| 301 | return !err; |
| 302 | } |
| 303 | #endif /* HAVE_GIFLIB */ |
| 304 | |
| 305 | bool img_load(img_t *img, const fileinfo_t *file) |
| 306 | { |
| 307 | const char *fmt; |
| 308 | |
| 309 | if (img == NULL || file == NULL || file->name == NULL || file->path == NULL) |
| 310 | return false; |
| 311 | |
| 312 | if (access(file->path, R_OK) < 0 || |
| 313 | (img->im = imlib_load_image(file->path)) == NULL) |
| 314 | { |
| 315 | warn("could not open image: %s", file->name); |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | imlib_context_set_image(img->im); |
| 320 | imlib_image_set_changes_on_disk(); |
| 321 | |
| 322 | if ((fmt = imlib_image_format()) == NULL) { |
| 323 | warn("could not open image: %s", file->name); |
| 324 | return false; |
| 325 | } |
| 326 | if (STREQ(fmt, "jpeg")) |
| 327 | exif_auto_orientate(file); |
| 328 | #if HAVE_GIFLIB |
| 329 | if (STREQ(fmt, "gif")) |
| 330 | img_load_gif(img, file); |
| 331 | #endif |
| 332 | |
| 333 | img_apply_gamma(img); |
| 334 | |
| 335 | img->w = imlib_image_get_width(); |
| 336 | img->h = imlib_image_get_height(); |
| 337 | img->checkpan = true; |
| 338 | img->dirty = true; |
| 339 | |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | void img_close(img_t *img, bool decache) |
| 344 | { |
| 345 | int i; |
| 346 | |
| 347 | if (img == NULL) |
| 348 | return; |
| 349 | |
| 350 | if (img->multi.cnt > 0) { |
| 351 | for (i = 0; i < img->multi.cnt; i++) { |
| 352 | imlib_context_set_image(img->multi.frames[i].im); |
| 353 | imlib_free_image(); |
| 354 | } |
| 355 | img->multi.cnt = 0; |
| 356 | img->im = NULL; |
| 357 | } else if (img->im != NULL) { |
| 358 | imlib_context_set_image(img->im); |
| 359 | if (decache) |
| 360 | imlib_free_image_and_decache(); |
| 361 | else |
| 362 | imlib_free_image(); |
| 363 | img->im = NULL; |
| 364 | } |
| 365 | |
| 366 | if (img->cmod) |
| 367 | imlib_context_set_color_modifier(NULL); |
| 368 | } |
| 369 | |
| 370 | void img_check_pan(img_t *img, bool moved) |
| 371 | { |
| 372 | win_t *win; |
| 373 | int ox, oy; |
| 374 | float w, h; |
| 375 | |
| 376 | if (img == NULL || img->im == NULL || img->win == NULL) |
| 377 | return; |
| 378 | |
| 379 | win = img->win; |
| 380 | w = img->w * img->zoom; |
| 381 | h = img->h * img->zoom; |
| 382 | ox = img->x; |
| 383 | oy = img->y; |
| 384 | |
| 385 | if (w < win->w) |
| 386 | img->x = (win->w - w) / 2; |
| 387 | else if (img->x > 0) |
| 388 | img->x = 0; |
| 389 | else if (img->x + w < win->w) |
| 390 | img->x = win->w - w; |
| 391 | if (h < win->h) |
| 392 | img->y = (win->h - h) / 2; |
| 393 | else if (img->y > 0) |
| 394 | img->y = 0; |
| 395 | else if (img->y + h < win->h) |
| 396 | img->y = win->h - h; |
| 397 | |
| 398 | if (!moved && (ox != img->x || oy != img->y)) |
| 399 | img->dirty = true; |
| 400 | } |
| 401 | |
| 402 | bool img_fit(img_t *img) |
| 403 | { |
| 404 | float z, zmax, zw, zh; |
| 405 | |
| 406 | if (img == NULL || img->im == NULL || img->win == NULL) |
| 407 | return false; |
| 408 | if (img->scalemode == SCALE_ZOOM) |
| 409 | return false; |
| 410 | |
| 411 | zmax = img->scalemode == SCALE_DOWN ? 1.0 : zoom_max; |
| 412 | zw = (float) img->win->w / (float) img->w; |
| 413 | zh = (float) img->win->h / (float) img->h; |
| 414 | |
| 415 | switch (img->scalemode) { |
| 416 | case SCALE_WIDTH: |
| 417 | z = zw; |
| 418 | break; |
| 419 | case SCALE_HEIGHT: |
| 420 | z = zh; |
| 421 | break; |
| 422 | default: |
| 423 | z = MIN(zw, zh); |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | z = MAX(z, zoom_min); |
| 428 | z = MIN(z, zmax); |
| 429 | |
| 430 | if (zoomdiff(z, img->zoom) != 0) { |
| 431 | img->zoom = z; |
| 432 | img->dirty = true; |
| 433 | return true; |
| 434 | } else { |
| 435 | return false; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void img_render(img_t *img) |
| 440 | { |
| 441 | win_t *win; |
| 442 | int sx, sy, sw, sh; |
| 443 | int dx, dy, dw, dh; |
| 444 | Imlib_Image bg; |
| 445 | unsigned long c; |
| 446 | |
| 447 | if (img == NULL || img->im == NULL || img->win == NULL) |
| 448 | return; |
| 449 | |
| 450 | win = img->win; |
| 451 | img_fit(img); |
| 452 | |
| 453 | if (img->checkpan) { |
| 454 | img_check_pan(img, false); |
| 455 | img->checkpan = false; |
| 456 | } |
| 457 | |
| 458 | if (!img->dirty) |
| 459 | return; |
| 460 | |
| 461 | /* calculate source and destination offsets: |
| 462 | * - part of image drawn on full window, or |
| 463 | * - full image drawn on part of window |
| 464 | */ |
| 465 | if (img->x <= 0) { |
| 466 | sx = -img->x / img->zoom; |
| 467 | sw = win->w / img->zoom; |
| 468 | dx = 0; |
| 469 | dw = win->w; |
| 470 | } else { |
| 471 | sx = 0; |
| 472 | sw = img->w; |
| 473 | dx = img->x; |
| 474 | dw = img->w * img->zoom; |
| 475 | } |
| 476 | if (img->y <= 0) { |
| 477 | sy = -img->y / img->zoom; |
| 478 | sh = win->h / img->zoom; |
| 479 | dy = 0; |
| 480 | dh = win->h; |
| 481 | } else { |
| 482 | sy = 0; |
| 483 | sh = img->h; |
| 484 | dy = img->y; |
| 485 | dh = img->h * img->zoom; |
| 486 | } |
| 487 | |
| 488 | win_clear(win); |
| 489 | |
| 490 | imlib_context_set_image(img->im); |
| 491 | imlib_context_set_anti_alias(img->aa); |
| 492 | imlib_context_set_drawable(win->pm); |
| 493 | |
| 494 | if (imlib_image_has_alpha()) { |
| 495 | bg = imlib_create_image(dw, dh); |
| 496 | imlib_context_set_image(bg); |
| 497 | imlib_image_set_has_alpha(0); |
| 498 | |
| 499 | if (img->alpha) |
| 500 | c = win->fullscreen ? win->fscol : win->bgcol; |
| 501 | else |
| 502 | c = win->white; |
| 503 | imlib_context_set_color(c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF, 0xFF); |
| 504 | imlib_image_fill_rectangle(0, 0, dw, dh); |
| 505 | |
| 506 | imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh); |
| 507 | imlib_context_set_color_modifier(NULL); |
| 508 | imlib_render_image_on_drawable(dx, dy); |
| 509 | |
| 510 | imlib_free_image(); |
| 511 | if (img->gamma != 0) |
| 512 | imlib_context_set_color_modifier(img->cmod); |
| 513 | } else { |
| 514 | imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); |
| 515 | } |
| 516 | img->dirty = false; |
| 517 | } |
| 518 | |
| 519 | bool img_fit_win(img_t *img, scalemode_t sm) |
| 520 | { |
| 521 | float oz; |
| 522 | |
| 523 | if (img == NULL || img->im == NULL || img->win == NULL) |
| 524 | return false; |
| 525 | |
| 526 | oz = img->zoom; |
| 527 | img->scalemode = sm; |
| 528 | |
| 529 | if (img_fit(img)) { |
| 530 | img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz; |
| 531 | img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz; |
| 532 | img->checkpan = true; |
| 533 | return true; |
| 534 | } else { |
| 535 | return false; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | bool img_zoom(img_t *img, float z) |
| 540 | { |
| 541 | if (img == NULL || img->im == NULL || img->win == NULL) |
| 542 | return false; |
| 543 | |
| 544 | z = MAX(z, zoom_min); |
| 545 | z = MIN(z, zoom_max); |
| 546 | |
| 547 | img->scalemode = SCALE_ZOOM; |
| 548 | |
| 549 | if (zoomdiff(z, img->zoom) != 0) { |
| 550 | img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * z / img->zoom; |
| 551 | img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * z / img->zoom; |
| 552 | img->zoom = z; |
| 553 | img->checkpan = true; |
| 554 | img->dirty = true; |
| 555 | return true; |
| 556 | } else { |
| 557 | return false; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | bool img_zoom_in(img_t *img) |
| 562 | { |
| 563 | int i; |
| 564 | float z; |
| 565 | |
| 566 | if (img == NULL || img->im == NULL) |
| 567 | return false; |
| 568 | |
| 569 | for (i = 1; i < ARRLEN(zoom_levels); i++) { |
| 570 | z = zoom_levels[i] / 100.0; |
| 571 | if (zoomdiff(z, img->zoom) > 0) |
| 572 | return img_zoom(img, z); |
| 573 | } |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | bool img_zoom_out(img_t *img) |
| 578 | { |
| 579 | int i; |
| 580 | float z; |
| 581 | |
| 582 | if (img == NULL || img->im == NULL) |
| 583 | return false; |
| 584 | |
| 585 | for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) { |
| 586 | z = zoom_levels[i] / 100.0; |
| 587 | if (zoomdiff(z, img->zoom) < 0) |
| 588 | return img_zoom(img, z); |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | bool img_move(img_t *img, float dx, float dy) |
| 594 | { |
| 595 | float ox, oy; |
| 596 | |
| 597 | if (img == NULL || img->im == NULL) |
| 598 | return false; |
| 599 | |
| 600 | ox = img->x; |
| 601 | oy = img->y; |
| 602 | |
| 603 | img->x += dx; |
| 604 | img->y += dy; |
| 605 | |
| 606 | img_check_pan(img, true); |
| 607 | |
| 608 | if (ox != img->x || oy != img->y) { |
| 609 | img->dirty = true; |
| 610 | return true; |
| 611 | } else { |
| 612 | return false; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | bool img_pan(img_t *img, direction_t dir, int d) |
| 617 | { |
| 618 | /* d < 0: screen-wise |
| 619 | * d = 0: 1/5 of screen |
| 620 | * d > 0: num of pixels |
| 621 | */ |
| 622 | float x, y; |
| 623 | |
| 624 | if (img == NULL || img->im == NULL || img->win == NULL) |
| 625 | return false; |
| 626 | |
| 627 | if (d > 0) { |
| 628 | x = y = MAX(1, (float) d * img->zoom); |
| 629 | } else { |
| 630 | x = img->win->w / (d < 0 ? 1 : 5); |
| 631 | y = img->win->h / (d < 0 ? 1 : 5); |
| 632 | } |
| 633 | |
| 634 | switch (dir) { |
| 635 | case DIR_LEFT: |
| 636 | return img_move(img, x, 0.0); |
| 637 | case DIR_RIGHT: |
| 638 | return img_move(img, -x, 0.0); |
| 639 | case DIR_UP: |
| 640 | return img_move(img, 0.0, y); |
| 641 | case DIR_DOWN: |
| 642 | return img_move(img, 0.0, -y); |
| 643 | } |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | bool img_pan_edge(img_t *img, direction_t dir) |
| 648 | { |
| 649 | int ox, oy; |
| 650 | |
| 651 | if (img == NULL || img->im == NULL || img->win == NULL) |
| 652 | return false; |
| 653 | |
| 654 | ox = img->x; |
| 655 | oy = img->y; |
| 656 | |
| 657 | switch (dir) { |
| 658 | case DIR_LEFT: |
| 659 | img->x = 0; |
| 660 | break; |
| 661 | case DIR_RIGHT: |
| 662 | img->x = img->win->w - img->w * img->zoom; |
| 663 | break; |
| 664 | case DIR_UP: |
| 665 | img->y = 0; |
| 666 | break; |
| 667 | case DIR_DOWN: |
| 668 | img->y = img->win->h - img->h * img->zoom; |
| 669 | break; |
| 670 | } |
| 671 | |
| 672 | img_check_pan(img, true); |
| 673 | |
| 674 | if (ox != img->x || oy != img->y) { |
| 675 | img->dirty = true; |
| 676 | return true; |
| 677 | } else { |
| 678 | return false; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | void img_rotate(img_t *img, degree_t d) |
| 683 | { |
| 684 | int i, ox, oy, tmp; |
| 685 | |
| 686 | if (img == NULL || img->im == NULL || img->win == NULL) |
| 687 | return; |
| 688 | |
| 689 | imlib_context_set_image(img->im); |
| 690 | imlib_image_orientate(d); |
| 691 | |
| 692 | for (i = 0; i < img->multi.cnt; i++) { |
| 693 | if (i != img->multi.sel) { |
| 694 | imlib_context_set_image(img->multi.frames[i].im); |
| 695 | imlib_image_orientate(d); |
| 696 | } |
| 697 | } |
| 698 | if (d == DEGREE_90 || d == DEGREE_270) { |
| 699 | ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom; |
| 700 | oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom; |
| 701 | |
| 702 | img->x = oy + (img->win->w - img->win->h) / 2; |
| 703 | img->y = ox + (img->win->h - img->win->w) / 2; |
| 704 | |
| 705 | tmp = img->w; |
| 706 | img->w = img->h; |
| 707 | img->h = tmp; |
| 708 | img->checkpan = true; |
| 709 | } |
| 710 | img->dirty = true; |
| 711 | } |
| 712 | |
| 713 | void img_flip(img_t *img, flipdir_t d) |
| 714 | { |
| 715 | int i; |
| 716 | void (*imlib_flip_op[3])(void) = { |
| 717 | imlib_image_flip_horizontal, |
| 718 | imlib_image_flip_vertical, |
| 719 | imlib_image_flip_diagonal |
| 720 | }; |
| 721 | |
| 722 | d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1; |
| 723 | |
| 724 | if (img == NULL || img->im == NULL || d < 0 || d >= ARRLEN(imlib_flip_op)) |
| 725 | return; |
| 726 | |
| 727 | imlib_context_set_image(img->im); |
| 728 | imlib_flip_op[d](); |
| 729 | |
| 730 | for (i = 0; i < img->multi.cnt; i++) { |
| 731 | if (i != img->multi.sel) { |
| 732 | imlib_context_set_image(img->multi.frames[i].im); |
| 733 | imlib_flip_op[d](); |
| 734 | } |
| 735 | } |
| 736 | img->dirty = true; |
| 737 | } |
| 738 | |
| 739 | void img_toggle_antialias(img_t *img) |
| 740 | { |
| 741 | if (img == NULL || img->im == NULL) |
| 742 | return; |
| 743 | |
| 744 | img->aa = !img->aa; |
| 745 | imlib_context_set_image(img->im); |
| 746 | imlib_context_set_anti_alias(img->aa); |
| 747 | img->dirty = true; |
| 748 | } |
| 749 | |
| 750 | bool img_change_gamma(img_t *img, int d) |
| 751 | { |
| 752 | /* d < 0: decrease gamma |
| 753 | * d = 0: reset gamma |
| 754 | * d > 0: increase gamma |
| 755 | */ |
| 756 | int gamma; |
| 757 | |
| 758 | if (img == NULL || img->im == NULL) |
| 759 | return false; |
| 760 | |
| 761 | if (d == 0) |
| 762 | gamma = 0; |
| 763 | else if (d < 0) |
| 764 | gamma = MAX(-GAMMA_RANGE, img->gamma - 1); |
| 765 | else |
| 766 | gamma = MIN(+GAMMA_RANGE, img->gamma + 1); |
| 767 | |
| 768 | if (img->gamma != gamma) { |
| 769 | img->gamma = gamma; |
| 770 | img_apply_gamma(img); |
| 771 | img->dirty = true; |
| 772 | return true; |
| 773 | } else { |
| 774 | return false; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | bool img_frame_goto(img_t *img, int n) |
| 779 | { |
| 780 | if (img == NULL || img->im == NULL) |
| 781 | return false; |
| 782 | if (n < 0 || n >= img->multi.cnt || n == img->multi.sel) |
| 783 | return false; |
| 784 | |
| 785 | img->multi.sel = n; |
| 786 | img->im = img->multi.frames[n].im; |
| 787 | |
| 788 | imlib_context_set_image(img->im); |
| 789 | img->w = imlib_image_get_width(); |
| 790 | img->h = imlib_image_get_height(); |
| 791 | img->checkpan = true; |
| 792 | img->dirty = true; |
| 793 | |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | bool img_frame_navigate(img_t *img, int d) |
| 798 | { |
| 799 | if (img == NULL|| img->im == NULL || img->multi.cnt == 0 || d == 0) |
| 800 | return false; |
| 801 | |
| 802 | d += img->multi.sel; |
| 803 | if (d < 0) |
| 804 | d = 0; |
| 805 | else if (d >= img->multi.cnt) |
| 806 | d = img->multi.cnt - 1; |
| 807 | |
| 808 | return img_frame_goto(img, d); |
| 809 | } |
| 810 | |
| 811 | bool img_frame_animate(img_t *img, bool restart) |
| 812 | { |
| 813 | if (img == NULL || img->im == NULL || img->multi.cnt == 0) |
| 814 | return false; |
| 815 | |
| 816 | if (img->multi.sel + 1 >= img->multi.cnt) { |
| 817 | if (restart || GIF_LOOP == 1) { |
| 818 | img_frame_goto(img, 0); |
| 819 | } else if (GIF_LOOP == -1 && img->multi.repeat != 0) { |
| 820 | if (img->multi.repeat > 0) |
| 821 | img->multi.repeat--; |
| 822 | img_frame_goto(img, 0); |
| 823 | } else { |
| 824 | img->multi.animate = false; |
| 825 | return false; |
| 826 | } |
| 827 | } else if (!restart) { |
| 828 | img_frame_goto(img, img->multi.sel + 1); |
| 829 | } |
| 830 | img->multi.animate = true; |
| 831 | img->dirty = true; |
| 832 | |
| 833 | return true; |
| 834 | } |