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