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