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