| 1 | /* sxiv: image.c |
| 2 | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> |
| 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 | #include <unistd.h> |
| 20 | |
| 21 | #ifdef HAVE_GIFLIB |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <gif_lib.h> |
| 26 | #endif |
| 27 | |
| 28 | #include "image.h" |
| 29 | #include "options.h" |
| 30 | #include "util.h" |
| 31 | |
| 32 | #define _IMAGE_CONFIG |
| 33 | #include "config.h" |
| 34 | |
| 35 | enum { MIN_GIF_DELAY = 50 }; |
| 36 | |
| 37 | int zl_cnt; |
| 38 | float zoom_min; |
| 39 | float zoom_max; |
| 40 | |
| 41 | void img_init(img_t *img, win_t *win) { |
| 42 | zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]); |
| 43 | zoom_min = zoom_levels[0] / 100.0; |
| 44 | zoom_max = zoom_levels[zl_cnt - 1] / 100.0; |
| 45 | |
| 46 | if (img) { |
| 47 | img->im = NULL; |
| 48 | img->multi.cap = img->multi.cnt = 0; |
| 49 | img->multi.animate = 0; |
| 50 | img->zoom = options->zoom; |
| 51 | img->zoom = MAX(img->zoom, zoom_min); |
| 52 | img->zoom = MIN(img->zoom, zoom_max); |
| 53 | img->aa = options->aa; |
| 54 | img->alpha = 1; |
| 55 | } |
| 56 | |
| 57 | if (win) { |
| 58 | imlib_context_set_display(win->env.dpy); |
| 59 | imlib_context_set_visual(win->env.vis); |
| 60 | imlib_context_set_colormap(win->env.cmap); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #ifdef HAVE_GIFLIB |
| 65 | /* originally based on, but in it's current form merely inspired by Imlib2's |
| 66 | * src/modules/loaders/loader_gif.c:load(), written by Carsten Haitzler. |
| 67 | */ |
| 68 | int img_load_gif(img_t *img, const fileinfo_t *file) { |
| 69 | GifFileType *gif; |
| 70 | GifRowType *rows = NULL; |
| 71 | GifRecordType rec; |
| 72 | ColorMapObject *cmap; |
| 73 | DATA32 bgpixel, *data, *ptr; |
| 74 | DATA32 *prev_frame = NULL; |
| 75 | Imlib_Image *im; |
| 76 | int i, j, bg, r, g, b; |
| 77 | int x, y, w, h, sw, sh; |
| 78 | int intoffset[] = { 0, 4, 2, 1 }; |
| 79 | int intjump[] = { 8, 8, 4, 2 }; |
| 80 | int err = 0, transp = -1; |
| 81 | unsigned int delay = 0; |
| 82 | |
| 83 | if (img->multi.cap == 0) { |
| 84 | img->multi.cap = 8; |
| 85 | img->multi.frames = (img_frame_t*) |
| 86 | s_malloc(sizeof(img_frame_t) * img->multi.cap); |
| 87 | } |
| 88 | img->multi.cnt = 0; |
| 89 | img->multi.sel = 0; |
| 90 | |
| 91 | gif = DGifOpenFileName(file->path); |
| 92 | if (!gif) { |
| 93 | warn("could not open gif file: %s", file->name); |
| 94 | return 0; |
| 95 | } |
| 96 | bg = gif->SBackGroundColor; |
| 97 | sw = gif->SWidth; |
| 98 | sh = gif->SHeight; |
| 99 | |
| 100 | do { |
| 101 | if (DGifGetRecordType(gif, &rec) == GIF_ERROR) { |
| 102 | warn("could not open gif file: %s", file->name); |
| 103 | err = 1; |
| 104 | break; |
| 105 | } |
| 106 | if (rec == EXTENSION_RECORD_TYPE) { |
| 107 | int ext_code; |
| 108 | GifByteType *ext = NULL; |
| 109 | |
| 110 | DGifGetExtension(gif, &ext_code, &ext); |
| 111 | while (ext) { |
| 112 | if (ext_code == 0xf9) { |
| 113 | if (ext[1] & 1) |
| 114 | transp = (int) ext[4]; |
| 115 | else |
| 116 | transp = -1; |
| 117 | |
| 118 | delay = 10 * ((unsigned int) ext[3] << 8 | (unsigned int) ext[2]); |
| 119 | if (delay) |
| 120 | delay = MAX(delay, MIN_GIF_DELAY); |
| 121 | } |
| 122 | ext = NULL; |
| 123 | DGifGetExtensionNext(gif, &ext); |
| 124 | } |
| 125 | } else if (rec == IMAGE_DESC_RECORD_TYPE) { |
| 126 | if (DGifGetImageDesc(gif) == GIF_ERROR) { |
| 127 | warn("could not open gif frame # %d: %s", img->multi.cnt, file->name); |
| 128 | err = 1; |
| 129 | break; |
| 130 | } |
| 131 | x = gif->Image.Left; |
| 132 | y = gif->Image.Top; |
| 133 | w = gif->Image.Width; |
| 134 | h = gif->Image.Height; |
| 135 | |
| 136 | rows = (GifRowType*) s_malloc(h * sizeof(GifRowType)); |
| 137 | for (i = 0; i < h; i++) |
| 138 | rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType)); |
| 139 | if (gif->Image.Interlace) { |
| 140 | for (i = 0; i < 4; i++) { |
| 141 | for (j = intoffset[i]; j < h; j += intjump[i]) |
| 142 | DGifGetLine(gif, rows[j], w); |
| 143 | } |
| 144 | } else { |
| 145 | for (i = 0; i < h; i++) |
| 146 | DGifGetLine(gif, rows[i], w); |
| 147 | } |
| 148 | |
| 149 | ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh); |
| 150 | cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap; |
| 151 | r = cmap->Colors[bg].Red; |
| 152 | g = cmap->Colors[bg].Green; |
| 153 | b = cmap->Colors[bg].Blue; |
| 154 | bgpixel = 0x00ffffff & (r << 16 | g << 8 | b); |
| 155 | |
| 156 | for (i = 0; i < sh; i++) { |
| 157 | for (j = 0; j < sw; j++) { |
| 158 | if (i < y || i >= y + h || j < x || j >= x + w) { |
| 159 | if (transp >= 0 && prev_frame) |
| 160 | *ptr = prev_frame[i * sw + j]; |
| 161 | else |
| 162 | *ptr = bgpixel; |
| 163 | } else if (rows[i-y][j-x] == transp) { |
| 164 | if (prev_frame) |
| 165 | *ptr = prev_frame[i * sw + j]; |
| 166 | else |
| 167 | *ptr = bgpixel; |
| 168 | } else { |
| 169 | r = cmap->Colors[rows[i-y][j-x]].Red; |
| 170 | g = cmap->Colors[rows[i-y][j-x]].Green; |
| 171 | b = cmap->Colors[rows[i-y][j-x]].Blue; |
| 172 | *ptr = 0xff << 24 | r << 16 | g << 8 | b; |
| 173 | } |
| 174 | ptr++; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | im = imlib_create_image_using_copied_data(sw, sh, data); |
| 179 | |
| 180 | for (i = 0; i < h; i++) |
| 181 | free(rows[i]); |
| 182 | free(rows); |
| 183 | free(data); |
| 184 | |
| 185 | if (!im) { |
| 186 | warn("could not open gif frame # %d: %s", img->multi.cnt, file->name); |
| 187 | err = 1; |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | imlib_context_set_image(im); |
| 192 | prev_frame = imlib_image_get_data_for_reading_only(); |
| 193 | |
| 194 | imlib_image_set_format("gif"); |
| 195 | if (transp >= 0) |
| 196 | imlib_image_set_has_alpha(1); |
| 197 | |
| 198 | if (img->multi.cnt == img->multi.cap) { |
| 199 | img->multi.cap *= 2; |
| 200 | img->multi.frames = (img_frame_t*) |
| 201 | s_realloc(img->multi.frames, |
| 202 | img->multi.cap * sizeof(img_frame_t)); |
| 203 | } |
| 204 | img->multi.frames[img->multi.cnt].im = im; |
| 205 | img->multi.frames[img->multi.cnt].delay = delay ? delay : GIF_DELAY; |
| 206 | img->multi.cnt++; |
| 207 | } |
| 208 | } while (rec != TERMINATE_RECORD_TYPE); |
| 209 | |
| 210 | DGifCloseFile(gif); |
| 211 | |
| 212 | if (!err && img->multi.cnt > 1) { |
| 213 | imlib_context_set_image(img->im); |
| 214 | imlib_free_image(); |
| 215 | img->im = img->multi.frames[0].im; |
| 216 | img->multi.animate = GIF_AUTOPLAY; |
| 217 | } else { |
| 218 | for (i = 0; i < img->multi.cnt; i++) { |
| 219 | imlib_context_set_image(img->multi.frames[i].im); |
| 220 | imlib_free_image(); |
| 221 | } |
| 222 | img->multi.cnt = 0; |
| 223 | img->multi.animate = 0; |
| 224 | } |
| 225 | |
| 226 | imlib_context_set_image(img->im); |
| 227 | |
| 228 | return !err; |
| 229 | } |
| 230 | #endif /* HAVE_GIFLIB */ |
| 231 | |
| 232 | int img_load(img_t *img, const fileinfo_t *file) { |
| 233 | const char *fmt; |
| 234 | |
| 235 | if (!img || !file || !file->name || !file->path) |
| 236 | return 0; |
| 237 | |
| 238 | if (access(file->path, R_OK) || !(img->im = imlib_load_image(file->path))) { |
| 239 | warn("could not open image: %s", file->name); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | imlib_context_set_image(img->im); |
| 244 | imlib_image_set_changes_on_disk(); |
| 245 | imlib_context_set_anti_alias(img->aa); |
| 246 | |
| 247 | fmt = imlib_image_format(); |
| 248 | #ifdef HAVE_GIFLIB |
| 249 | if (!strcmp(fmt, "gif")) |
| 250 | img_load_gif(img, file); |
| 251 | #else |
| 252 | /* avoid unused-but-set-variable warning */ |
| 253 | (void) fmt; |
| 254 | #endif |
| 255 | |
| 256 | img->scalemode = options->scalemode; |
| 257 | img->re = 0; |
| 258 | img->checkpan = 0; |
| 259 | |
| 260 | img->w = imlib_image_get_width(); |
| 261 | img->h = imlib_image_get_height(); |
| 262 | |
| 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | void img_close(img_t *img, int decache) { |
| 267 | int i; |
| 268 | |
| 269 | if (!img) |
| 270 | return; |
| 271 | |
| 272 | if (img->multi.cnt) { |
| 273 | for (i = 0; i < img->multi.cnt; i++) { |
| 274 | imlib_context_set_image(img->multi.frames[i].im); |
| 275 | imlib_free_image(); |
| 276 | } |
| 277 | img->multi.cnt = 0; |
| 278 | img->im = NULL; |
| 279 | } else if (img->im) { |
| 280 | imlib_context_set_image(img->im); |
| 281 | if (decache) |
| 282 | imlib_free_image_and_decache(); |
| 283 | else |
| 284 | imlib_free_image(); |
| 285 | img->im = NULL; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void img_check_pan(img_t *img, win_t *win) { |
| 290 | if (!img || !win) |
| 291 | return; |
| 292 | |
| 293 | if (img->w * img->zoom > win->w) { |
| 294 | if (img->x > 0 && img->x + img->w * img->zoom > win->w) |
| 295 | img->x = 0; |
| 296 | if (img->x < 0 && img->x + img->w * img->zoom < win->w) |
| 297 | img->x = win->w - img->w * img->zoom; |
| 298 | } else { |
| 299 | img->x = (win->w - img->w * img->zoom) / 2; |
| 300 | } |
| 301 | if (img->h * img->zoom > win->h) { |
| 302 | if (img->y > 0 && img->y + img->h * img->zoom > win->h) |
| 303 | img->y = 0; |
| 304 | if (img->y < 0 && img->y + img->h * img->zoom < win->h) |
| 305 | img->y = win->h - img->h * img->zoom; |
| 306 | } else { |
| 307 | img->y = (win->h - img->h * img->zoom) / 2; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | int img_fit(img_t *img, win_t *win) { |
| 312 | float oz, zw, zh; |
| 313 | |
| 314 | if (!img || !win) |
| 315 | return 0; |
| 316 | |
| 317 | oz = img->zoom; |
| 318 | zw = (float) win->w / (float) img->w; |
| 319 | zh = (float) win->h / (float) img->h; |
| 320 | |
| 321 | img->zoom = MIN(zw, zh); |
| 322 | img->zoom = MAX(img->zoom, zoom_min); |
| 323 | img->zoom = MIN(img->zoom, zoom_max); |
| 324 | |
| 325 | return oz != img->zoom; |
| 326 | } |
| 327 | |
| 328 | void img_render(img_t *img, win_t *win) { |
| 329 | int sx, sy, sw, sh; |
| 330 | int dx, dy, dw, dh; |
| 331 | |
| 332 | if (!img || !img->im || !win) |
| 333 | return; |
| 334 | |
| 335 | if (img->scalemode != SCALE_ZOOM) { |
| 336 | img_fit(img, win); |
| 337 | if (img->scalemode == SCALE_DOWN && img->zoom > 1.0) |
| 338 | img->zoom = 1.0; |
| 339 | } |
| 340 | |
| 341 | if (!img->re) { |
| 342 | /* rendered for the first time */ |
| 343 | img->re = 1; |
| 344 | if (img->zoom * img->w <= win->w) |
| 345 | img->x = (win->w - img->w * img->zoom) / 2; |
| 346 | else |
| 347 | img->x = 0; |
| 348 | if (img->zoom * img->h <= win->h) |
| 349 | img->y = (win->h - img->h * img->zoom) / 2; |
| 350 | else |
| 351 | img->y = 0; |
| 352 | } |
| 353 | |
| 354 | if (img->checkpan) { |
| 355 | img_check_pan(img, win); |
| 356 | img->checkpan = 0; |
| 357 | } |
| 358 | |
| 359 | /* calculate source and destination offsets */ |
| 360 | if (img->x < 0) { |
| 361 | sx = -img->x / img->zoom; |
| 362 | sw = win->w / img->zoom; |
| 363 | dx = 0; |
| 364 | dw = win->w; |
| 365 | } else { |
| 366 | sx = 0; |
| 367 | sw = img->w; |
| 368 | dx = img->x; |
| 369 | dw = img->w * img->zoom; |
| 370 | } |
| 371 | if (img->y < 0) { |
| 372 | sy = -img->y / img->zoom; |
| 373 | sh = win->h / img->zoom; |
| 374 | dy = 0; |
| 375 | dh = win->h; |
| 376 | } else { |
| 377 | sy = 0; |
| 378 | sh = img->h; |
| 379 | dy = img->y; |
| 380 | dh = img->h * img->zoom; |
| 381 | } |
| 382 | |
| 383 | win_clear(win); |
| 384 | |
| 385 | imlib_context_set_image(img->im); |
| 386 | |
| 387 | if (imlib_image_has_alpha() && !img->alpha) |
| 388 | win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white); |
| 389 | |
| 390 | imlib_context_set_drawable(win->pm); |
| 391 | imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); |
| 392 | |
| 393 | win_draw(win); |
| 394 | } |
| 395 | |
| 396 | int img_fit_win(img_t *img, win_t *win) { |
| 397 | if (!img || !img->im || !win) |
| 398 | return 0; |
| 399 | |
| 400 | img->scalemode = SCALE_FIT; |
| 401 | return img_fit(img, win); |
| 402 | } |
| 403 | |
| 404 | int img_center(img_t *img, win_t *win) { |
| 405 | int ox, oy; |
| 406 | |
| 407 | if (!img || !win) |
| 408 | return 0; |
| 409 | |
| 410 | ox = img->x; |
| 411 | oy = img->y; |
| 412 | |
| 413 | img->x = (win->w - img->w * img->zoom) / 2; |
| 414 | img->y = (win->h - img->h * img->zoom) / 2; |
| 415 | |
| 416 | return ox != img->x || oy != img->y; |
| 417 | } |
| 418 | |
| 419 | int img_zoom(img_t *img, win_t *win, float z) { |
| 420 | if (!img || !img->im || !win) |
| 421 | return 0; |
| 422 | |
| 423 | z = MAX(z, zoom_min); |
| 424 | z = MIN(z, zoom_max); |
| 425 | |
| 426 | img->scalemode = SCALE_ZOOM; |
| 427 | |
| 428 | if (z != img->zoom) { |
| 429 | img->x = win->w / 2 - (win->w / 2 - img->x) * z / img->zoom; |
| 430 | img->y = win->h / 2 - (win->h / 2 - img->y) * z / img->zoom; |
| 431 | img->zoom = z; |
| 432 | img->checkpan = 1; |
| 433 | return 1; |
| 434 | } else { |
| 435 | return 0; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | int img_zoom_in(img_t *img, win_t *win) { |
| 440 | int i; |
| 441 | |
| 442 | if (!img || !img->im || !win) |
| 443 | return 0; |
| 444 | |
| 445 | for (i = 1; i < zl_cnt; i++) { |
| 446 | if (zoom_levels[i] > img->zoom * 100.0) |
| 447 | return img_zoom(img, win, zoom_levels[i] / 100.0); |
| 448 | } |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | int img_zoom_out(img_t *img, win_t *win) { |
| 453 | int i; |
| 454 | |
| 455 | if (!img || !img->im || !win) |
| 456 | return 0; |
| 457 | |
| 458 | for (i = zl_cnt - 2; i >= 0; i--) { |
| 459 | if (zoom_levels[i] < img->zoom * 100.0) |
| 460 | return img_zoom(img, win, zoom_levels[i] / 100.0); |
| 461 | } |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | int img_move(img_t *img, win_t *win, int dx, int dy) { |
| 466 | int ox, oy; |
| 467 | |
| 468 | if (!img || !img->im || !win) |
| 469 | return 0; |
| 470 | |
| 471 | ox = img->x; |
| 472 | oy = img->y; |
| 473 | |
| 474 | img->x += dx; |
| 475 | img->y += dy; |
| 476 | |
| 477 | img_check_pan(img, win); |
| 478 | |
| 479 | return ox != img->x || oy != img->y; |
| 480 | } |
| 481 | |
| 482 | int img_pan(img_t *img, win_t *win, direction_t dir, int screen) { |
| 483 | if (!img || !img->im || !win) |
| 484 | return 0; |
| 485 | |
| 486 | switch (dir) { |
| 487 | case DIR_LEFT: |
| 488 | return img_move(img, win, win->w / (screen ? 1 : 5), 0); |
| 489 | case DIR_RIGHT: |
| 490 | return img_move(img, win, win->w / (screen ? 1 : 5) * -1, 0); |
| 491 | case DIR_UP: |
| 492 | return img_move(img, win, 0, win->h / (screen ? 1 : 5)); |
| 493 | case DIR_DOWN: |
| 494 | return img_move(img, win, 0, win->h / (screen ? 1 : 5) * -1); |
| 495 | } |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | int img_pan_edge(img_t *img, win_t *win, direction_t dir) { |
| 501 | int ox, oy; |
| 502 | |
| 503 | if (!img || !img->im || !win) |
| 504 | return 0; |
| 505 | |
| 506 | ox = img->x; |
| 507 | oy = img->y; |
| 508 | |
| 509 | switch (dir) { |
| 510 | case DIR_LEFT: |
| 511 | img->x = 0; |
| 512 | break; |
| 513 | case DIR_RIGHT: |
| 514 | img->x = win->w - img->w * img->zoom; |
| 515 | break; |
| 516 | case DIR_UP: |
| 517 | img->y = 0; |
| 518 | break; |
| 519 | case DIR_DOWN: |
| 520 | img->y = win->h - img->h * img->zoom; |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | img_check_pan(img, win); |
| 525 | |
| 526 | return ox != img->x || oy != img->y; |
| 527 | } |
| 528 | |
| 529 | void img_rotate(img_t *img, win_t *win, int d) { |
| 530 | int ox, oy, tmp; |
| 531 | |
| 532 | if (!img || !img->im || !win) |
| 533 | return; |
| 534 | |
| 535 | ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom; |
| 536 | oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom; |
| 537 | |
| 538 | imlib_context_set_image(img->im); |
| 539 | imlib_image_orientate(d); |
| 540 | |
| 541 | img->x = oy + (win->w - win->h) / 2; |
| 542 | img->y = ox + (win->h - win->w) / 2; |
| 543 | |
| 544 | tmp = img->w; |
| 545 | img->w = img->h; |
| 546 | img->h = tmp; |
| 547 | |
| 548 | img->checkpan = 1; |
| 549 | } |
| 550 | |
| 551 | void img_rotate_left(img_t *img, win_t *win) { |
| 552 | img_rotate(img, win, 3); |
| 553 | } |
| 554 | |
| 555 | void img_rotate_right(img_t *img, win_t *win) { |
| 556 | img_rotate(img, win, 1); |
| 557 | } |
| 558 | |
| 559 | void img_toggle_antialias(img_t *img) { |
| 560 | if (img && img->im) { |
| 561 | img->aa ^= 1; |
| 562 | imlib_context_set_image(img->im); |
| 563 | imlib_context_set_anti_alias(img->aa); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | int img_frame_goto(img_t *img, int n) { |
| 568 | if (!img || n < 0 || n >= img->multi.cnt) |
| 569 | return 0; |
| 570 | |
| 571 | if (n == img->multi.sel) |
| 572 | return 0; |
| 573 | |
| 574 | img->multi.sel = n; |
| 575 | img->im = img->multi.frames[n].im; |
| 576 | |
| 577 | imlib_context_set_image(img->im); |
| 578 | img->w = imlib_image_get_width(); |
| 579 | img->h = imlib_image_get_height(); |
| 580 | img->checkpan = 1; |
| 581 | |
| 582 | return 1; |
| 583 | } |
| 584 | |
| 585 | int img_frame_navigate(img_t *img, int d) { |
| 586 | if (!img || !img->multi.cnt || !d) |
| 587 | return 0; |
| 588 | |
| 589 | d += img->multi.sel; |
| 590 | if (d < 0) |
| 591 | d = 0; |
| 592 | else if (d >= img->multi.cnt) |
| 593 | d = img->multi.cnt - 1; |
| 594 | |
| 595 | return img_frame_goto(img, d); |
| 596 | } |
| 597 | |
| 598 | int img_frame_animate(img_t *img, int restart) { |
| 599 | if (!img || !img->multi.cnt) |
| 600 | return 0; |
| 601 | |
| 602 | if (img->multi.sel + 1 >= img->multi.cnt) { |
| 603 | if (restart || GIF_LOOP) { |
| 604 | img_frame_goto(img, 0); |
| 605 | } else { |
| 606 | img->multi.animate = 0; |
| 607 | return 0; |
| 608 | } |
| 609 | } else if (!restart) { |
| 610 | img_frame_goto(img, img->multi.sel + 1); |
| 611 | } |
| 612 | |
| 613 | img->multi.animate = 1; |
| 614 | |
| 615 | return img->multi.frames[img->multi.sel].delay; |
| 616 | } |