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