| 1 | /* Copyright 2011 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 <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
| 26 | #include <utime.h> |
| 27 | |
| 28 | #include "thumbs.h" |
| 29 | #include "util.h" |
| 30 | |
| 31 | #define _THUMBS_CONFIG |
| 32 | #include "config.h" |
| 33 | |
| 34 | #if HAVE_LIBEXIF |
| 35 | #include <libexif/exif-data.h> |
| 36 | void exif_auto_orientate(const fileinfo_t*); |
| 37 | #endif |
| 38 | |
| 39 | static char *cache_dir; |
| 40 | |
| 41 | char* tns_cache_filepath(const char *filepath) |
| 42 | { |
| 43 | size_t len; |
| 44 | char *cfile = NULL; |
| 45 | |
| 46 | if (*filepath != '/') |
| 47 | return NULL; |
| 48 | |
| 49 | if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) { |
| 50 | /* don't cache images inside the cache directory! */ |
| 51 | len = strlen(cache_dir) + strlen(filepath) + 2; |
| 52 | cfile = (char*) emalloc(len); |
| 53 | snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1); |
| 54 | } |
| 55 | return cfile; |
| 56 | } |
| 57 | |
| 58 | Imlib_Image tns_cache_load(const char *filepath, bool *outdated) |
| 59 | { |
| 60 | char *cfile; |
| 61 | struct stat cstats, fstats; |
| 62 | Imlib_Image im = NULL; |
| 63 | |
| 64 | if (stat(filepath, &fstats) < 0) |
| 65 | return NULL; |
| 66 | |
| 67 | if ((cfile = tns_cache_filepath(filepath)) != NULL) { |
| 68 | if (stat(cfile, &cstats) == 0) { |
| 69 | if (cstats.st_mtime == fstats.st_mtime) |
| 70 | im = imlib_load_image(cfile); |
| 71 | else |
| 72 | *outdated = true; |
| 73 | } |
| 74 | free(cfile); |
| 75 | } |
| 76 | return im; |
| 77 | } |
| 78 | |
| 79 | void tns_cache_write(Imlib_Image im, const char *filepath, bool force) |
| 80 | { |
| 81 | char *cfile, *dirend; |
| 82 | struct stat cstats, fstats; |
| 83 | struct utimbuf times; |
| 84 | Imlib_Load_Error err = 0; |
| 85 | |
| 86 | if (stat(filepath, &fstats) < 0) |
| 87 | return; |
| 88 | |
| 89 | if ((cfile = tns_cache_filepath(filepath)) != NULL) { |
| 90 | if (force || stat(cfile, &cstats) < 0 || |
| 91 | cstats.st_mtime != fstats.st_mtime) |
| 92 | { |
| 93 | if ((dirend = strrchr(cfile, '/')) != NULL) { |
| 94 | *dirend = '\0'; |
| 95 | if ((err = r_mkdir(cfile)) == -1) |
| 96 | error(0, errno, "%s", cfile); |
| 97 | *dirend = '/'; |
| 98 | } |
| 99 | if (err == 0) { |
| 100 | imlib_context_set_image(im); |
| 101 | if (imlib_image_has_alpha()) { |
| 102 | imlib_image_set_format("png"); |
| 103 | } else { |
| 104 | imlib_image_set_format("jpg"); |
| 105 | imlib_image_attach_data_value("quality", NULL, 90, NULL); |
| 106 | } |
| 107 | imlib_save_image_with_error_return(cfile, &err); |
| 108 | } |
| 109 | if (err == 0) { |
| 110 | times.actime = fstats.st_atime; |
| 111 | times.modtime = fstats.st_mtime; |
| 112 | utime(cfile, ×); |
| 113 | } |
| 114 | } |
| 115 | free(cfile); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void tns_clean_cache(tns_t *tns) |
| 120 | { |
| 121 | int dirlen; |
| 122 | bool delete; |
| 123 | char *cfile, *filename, *tpos; |
| 124 | r_dir_t dir; |
| 125 | |
| 126 | if (r_opendir(&dir, cache_dir) < 0) { |
| 127 | error(0, errno, "%s", cache_dir); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | dirlen = strlen(cache_dir); |
| 132 | |
| 133 | while ((cfile = r_readdir(&dir)) != NULL) { |
| 134 | filename = cfile + dirlen; |
| 135 | delete = false; |
| 136 | |
| 137 | if ((tpos = strrchr(filename, '.')) != NULL) { |
| 138 | *tpos = '\0'; |
| 139 | if (access(filename, F_OK) < 0) |
| 140 | delete = true; |
| 141 | *tpos = '.'; |
| 142 | } |
| 143 | if (delete) { |
| 144 | if (unlink(cfile) < 0) |
| 145 | error(0, errno, "%s", cfile); |
| 146 | } |
| 147 | free(cfile); |
| 148 | } |
| 149 | r_closedir(&dir); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, |
| 154 | win_t *win) |
| 155 | { |
| 156 | int len; |
| 157 | const char *homedir, *dsuffix = ""; |
| 158 | |
| 159 | if (cnt != NULL && *cnt > 0) { |
| 160 | tns->thumbs = (thumb_t*) emalloc(*cnt * sizeof(thumb_t)); |
| 161 | memset(tns->thumbs, 0, *cnt * sizeof(thumb_t)); |
| 162 | } else { |
| 163 | tns->thumbs = NULL; |
| 164 | } |
| 165 | tns->files = files; |
| 166 | tns->cnt = cnt; |
| 167 | tns->initnext = tns->loadnext = 0; |
| 168 | tns->first = tns->end = tns->r_first = tns->r_end = 0; |
| 169 | tns->sel = sel; |
| 170 | tns->win = win; |
| 171 | tns->dirty = false; |
| 172 | |
| 173 | tns->zl = 0; |
| 174 | tns_zoom(tns, 1); |
| 175 | |
| 176 | if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') { |
| 177 | homedir = getenv("HOME"); |
| 178 | dsuffix = "/.cache"; |
| 179 | } |
| 180 | if (homedir != NULL) { |
| 181 | free(cache_dir); |
| 182 | len = strlen(homedir) + strlen(dsuffix) + 6; |
| 183 | cache_dir = (char*) emalloc(len); |
| 184 | snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix); |
| 185 | } else { |
| 186 | error(0, 0, "Cache directory not found"); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | CLEANUP void tns_free(tns_t *tns) |
| 191 | { |
| 192 | int i; |
| 193 | |
| 194 | if (tns->thumbs != NULL) { |
| 195 | for (i = 0; i < *tns->cnt; i++) { |
| 196 | if (tns->thumbs[i].im != NULL) { |
| 197 | imlib_context_set_image(tns->thumbs[i].im); |
| 198 | imlib_free_image(); |
| 199 | } |
| 200 | } |
| 201 | free(tns->thumbs); |
| 202 | tns->thumbs = NULL; |
| 203 | } |
| 204 | |
| 205 | free(cache_dir); |
| 206 | cache_dir = NULL; |
| 207 | } |
| 208 | |
| 209 | Imlib_Image tns_scale_down(Imlib_Image im, int dim) |
| 210 | { |
| 211 | int w, h; |
| 212 | float z, zw, zh; |
| 213 | |
| 214 | imlib_context_set_image(im); |
| 215 | w = imlib_image_get_width(); |
| 216 | h = imlib_image_get_height(); |
| 217 | zw = (float) dim / (float) w; |
| 218 | zh = (float) dim / (float) h; |
| 219 | z = MIN(zw, zh); |
| 220 | z = MIN(z, 1.0); |
| 221 | |
| 222 | if (z < 1.0) { |
| 223 | imlib_context_set_anti_alias(1); |
| 224 | im = imlib_create_cropped_scaled_image(0, 0, w, h, |
| 225 | MAX(z * w, 1), MAX(z * h, 1)); |
| 226 | if (im == NULL) |
| 227 | error(EXIT_FAILURE, ENOMEM, NULL); |
| 228 | imlib_free_image_and_decache(); |
| 229 | } |
| 230 | return im; |
| 231 | } |
| 232 | |
| 233 | bool tns_load(tns_t *tns, int n, bool force, bool cache_only) |
| 234 | { |
| 235 | int w, h; |
| 236 | int maxwh = thumb_sizes[ARRLEN(thumb_sizes)-1]; |
| 237 | bool cache_hit = false; |
| 238 | char *cfile; |
| 239 | float zw, zh; |
| 240 | thumb_t *t; |
| 241 | fileinfo_t *file; |
| 242 | Imlib_Image im = NULL; |
| 243 | |
| 244 | if (n < 0 || n >= *tns->cnt) |
| 245 | return false; |
| 246 | file = &tns->files[n]; |
| 247 | if (file->name == NULL || file->path == NULL) |
| 248 | return false; |
| 249 | |
| 250 | t = &tns->thumbs[n]; |
| 251 | |
| 252 | if (t->im != NULL) { |
| 253 | imlib_context_set_image(t->im); |
| 254 | imlib_free_image(); |
| 255 | t->im = NULL; |
| 256 | } |
| 257 | |
| 258 | if (!force) { |
| 259 | if ((im = tns_cache_load(file->path, &force)) != NULL) { |
| 260 | imlib_context_set_image(im); |
| 261 | if (imlib_image_get_width() < maxwh && |
| 262 | imlib_image_get_height() < maxwh) |
| 263 | { |
| 264 | if ((cfile = tns_cache_filepath(file->path)) != NULL) { |
| 265 | unlink(cfile); |
| 266 | free(cfile); |
| 267 | } |
| 268 | imlib_free_image_and_decache(); |
| 269 | im = NULL; |
| 270 | } else { |
| 271 | cache_hit = true; |
| 272 | } |
| 273 | #if HAVE_LIBEXIF |
| 274 | } else if (!force) { |
| 275 | int pw = 0, ph = 0, x = 0, y = 0; |
| 276 | bool err; |
| 277 | ExifData *ed; |
| 278 | ExifEntry *entry; |
| 279 | ExifContent *ifd; |
| 280 | ExifByteOrder byte_order; |
| 281 | int tmpfd; |
| 282 | char tmppath[] = "/tmp/sxiv-XXXXXX"; |
| 283 | Imlib_Image tmpim; |
| 284 | |
| 285 | if ((ed = exif_data_new_from_file(file->path)) != NULL) { |
| 286 | if (ed->data != NULL && ed->size > 0 && |
| 287 | (tmpfd = mkstemp(tmppath)) >= 0) |
| 288 | { |
| 289 | err = write(tmpfd, ed->data, ed->size) != ed->size; |
| 290 | close(tmpfd); |
| 291 | |
| 292 | if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) { |
| 293 | byte_order = exif_data_get_byte_order(ed); |
| 294 | ifd = ed->ifd[EXIF_IFD_EXIF]; |
| 295 | entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION); |
| 296 | if (entry != NULL) |
| 297 | pw = exif_get_long(entry->data, byte_order); |
| 298 | entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION); |
| 299 | if (entry != NULL) |
| 300 | ph = exif_get_long(entry->data, byte_order); |
| 301 | |
| 302 | imlib_context_set_image(tmpim); |
| 303 | w = imlib_image_get_width(); |
| 304 | h = imlib_image_get_height(); |
| 305 | |
| 306 | if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) { |
| 307 | zw = (float) pw / (float) w; |
| 308 | zh = (float) ph / (float) h; |
| 309 | if (zw < zh) { |
| 310 | pw /= zh; |
| 311 | x = (w - pw) / 2; |
| 312 | w = pw; |
| 313 | } else if (zw > zh) { |
| 314 | ph /= zw; |
| 315 | y = (h - ph) / 2; |
| 316 | h = ph; |
| 317 | } |
| 318 | } |
| 319 | if (w >= maxwh || h >= maxwh) { |
| 320 | if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL) |
| 321 | error(EXIT_FAILURE, ENOMEM, NULL); |
| 322 | } |
| 323 | imlib_free_image_and_decache(); |
| 324 | } |
| 325 | unlink(tmppath); |
| 326 | } |
| 327 | exif_data_unref(ed); |
| 328 | } |
| 329 | #endif |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (im == NULL && (access(file->path, R_OK) < 0 || |
| 334 | (im = imlib_load_image(file->path)) == NULL)) |
| 335 | { |
| 336 | if (file->flags & FF_WARN) |
| 337 | error(0, 0, "%s: Error opening image", file->name); |
| 338 | return false; |
| 339 | } |
| 340 | imlib_context_set_image(im); |
| 341 | |
| 342 | if (!cache_hit) { |
| 343 | #if HAVE_LIBEXIF |
| 344 | exif_auto_orientate(file); |
| 345 | #endif |
| 346 | im = tns_scale_down(im, maxwh); |
| 347 | imlib_context_set_image(im); |
| 348 | if (imlib_image_get_width() == maxwh || imlib_image_get_height() == maxwh) |
| 349 | tns_cache_write(im, file->path, true); |
| 350 | } |
| 351 | |
| 352 | if (cache_only) { |
| 353 | imlib_free_image_and_decache(); |
| 354 | } else { |
| 355 | t->im = tns_scale_down(im, thumb_sizes[tns->zl]); |
| 356 | imlib_context_set_image(t->im); |
| 357 | t->w = imlib_image_get_width(); |
| 358 | t->h = imlib_image_get_height(); |
| 359 | tns->dirty = true; |
| 360 | } |
| 361 | file->flags |= FF_TN_INIT; |
| 362 | |
| 363 | if (n == tns->initnext) |
| 364 | while (++tns->initnext < *tns->cnt && ((++file)->flags & FF_TN_INIT)); |
| 365 | if (n == tns->loadnext && !cache_only) |
| 366 | while (++tns->loadnext < tns->end && (++t)->im != NULL); |
| 367 | |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | void tns_unload(tns_t *tns, int n) |
| 372 | { |
| 373 | thumb_t *t; |
| 374 | |
| 375 | if (n < 0 || n >= *tns->cnt) |
| 376 | return; |
| 377 | |
| 378 | t = &tns->thumbs[n]; |
| 379 | |
| 380 | if (t->im != NULL) { |
| 381 | imlib_context_set_image(t->im); |
| 382 | imlib_free_image(); |
| 383 | t->im = NULL; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void tns_check_view(tns_t *tns, bool scrolled) |
| 388 | { |
| 389 | int r; |
| 390 | |
| 391 | if (tns == NULL) |
| 392 | return; |
| 393 | |
| 394 | tns->first -= tns->first % tns->cols; |
| 395 | r = *tns->sel % tns->cols; |
| 396 | |
| 397 | if (scrolled) { |
| 398 | /* move selection into visible area */ |
| 399 | if (*tns->sel >= tns->first + tns->cols * tns->rows) |
| 400 | *tns->sel = tns->first + r + tns->cols * (tns->rows - 1); |
| 401 | else if (*tns->sel < tns->first) |
| 402 | *tns->sel = tns->first + r; |
| 403 | } else { |
| 404 | /* scroll to selection */ |
| 405 | if (tns->first + tns->cols * tns->rows <= *tns->sel) { |
| 406 | tns->first = *tns->sel - r - tns->cols * (tns->rows - 1); |
| 407 | tns->dirty = true; |
| 408 | } else if (tns->first > *tns->sel) { |
| 409 | tns->first = *tns->sel - r; |
| 410 | tns->dirty = true; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void tns_render(tns_t *tns) |
| 416 | { |
| 417 | thumb_t *t; |
| 418 | win_t *win; |
| 419 | int i, cnt, r, x, y; |
| 420 | |
| 421 | if (!tns->dirty) |
| 422 | return; |
| 423 | |
| 424 | win = tns->win; |
| 425 | win_clear(win); |
| 426 | imlib_context_set_drawable(win->buf.pm); |
| 427 | |
| 428 | tns->cols = MAX(1, win->w / tns->dim); |
| 429 | tns->rows = MAX(1, win->h / tns->dim); |
| 430 | |
| 431 | if (*tns->cnt < tns->cols * tns->rows) { |
| 432 | tns->first = 0; |
| 433 | cnt = *tns->cnt; |
| 434 | } else { |
| 435 | tns_check_view(tns, false); |
| 436 | cnt = tns->cols * tns->rows; |
| 437 | if ((r = tns->first + cnt - *tns->cnt) >= tns->cols) |
| 438 | tns->first -= r - r % tns->cols; |
| 439 | if (r > 0) |
| 440 | cnt -= r % tns->cols; |
| 441 | } |
| 442 | r = cnt % tns->cols ? 1 : 0; |
| 443 | tns->x = x = (win->w - MIN(cnt, tns->cols) * tns->dim) / 2 + tns->bw + 3; |
| 444 | tns->y = y = (win->h - (cnt / tns->cols + r) * tns->dim) / 2 + tns->bw + 3; |
| 445 | tns->loadnext = *tns->cnt; |
| 446 | tns->end = tns->first + cnt; |
| 447 | |
| 448 | for (i = tns->r_first; i < tns->r_end; i++) { |
| 449 | if ((i < tns->first || i >= tns->end) && tns->thumbs[i].im != NULL) |
| 450 | tns_unload(tns, i); |
| 451 | } |
| 452 | tns->r_first = tns->first; |
| 453 | tns->r_end = tns->end; |
| 454 | |
| 455 | for (i = tns->first; i < tns->end; i++) { |
| 456 | t = &tns->thumbs[i]; |
| 457 | if (t->im != NULL) { |
| 458 | t->x = x + (thumb_sizes[tns->zl] - t->w) / 2; |
| 459 | t->y = y + (thumb_sizes[tns->zl] - t->h) / 2; |
| 460 | imlib_context_set_image(t->im); |
| 461 | imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h); |
| 462 | if (tns->files[i].flags & FF_MARK) |
| 463 | tns_mark(tns, i, true); |
| 464 | } else { |
| 465 | tns->loadnext = MIN(tns->loadnext, i); |
| 466 | } |
| 467 | if ((i + 1) % tns->cols == 0) { |
| 468 | x = tns->x; |
| 469 | y += tns->dim; |
| 470 | } else { |
| 471 | x += tns->dim; |
| 472 | } |
| 473 | } |
| 474 | tns->dirty = false; |
| 475 | tns_highlight(tns, *tns->sel, true); |
| 476 | } |
| 477 | |
| 478 | void tns_mark(tns_t *tns, int n, bool mark) |
| 479 | { |
| 480 | if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) { |
| 481 | win_t *win = tns->win; |
| 482 | thumb_t *t = &tns->thumbs[n]; |
| 483 | unsigned long col = win->fullscreen ? win->fscol : win->bgcol; |
| 484 | int x = t->x + t->w, y = t->y + t->h; |
| 485 | |
| 486 | win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col); |
| 487 | win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col); |
| 488 | |
| 489 | if (mark) |
| 490 | col = win->selcol; |
| 491 | |
| 492 | win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col); |
| 493 | |
| 494 | if (!mark && n == *tns->sel) |
| 495 | tns_highlight(tns, n, true); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | void tns_highlight(tns_t *tns, int n, bool hl) |
| 500 | { |
| 501 | if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) { |
| 502 | win_t *win = tns->win; |
| 503 | thumb_t *t = &tns->thumbs[n]; |
| 504 | unsigned long col; |
| 505 | int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2; |
| 506 | |
| 507 | if (hl) |
| 508 | col = win->selcol; |
| 509 | else |
| 510 | col = win->fullscreen ? win->fscol : win->bgcol; |
| 511 | |
| 512 | win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh, |
| 513 | false, tns->bw, col); |
| 514 | |
| 515 | if (tns->files[n].flags & FF_MARK) |
| 516 | tns_mark(tns, n, true); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | bool tns_move_selection(tns_t *tns, direction_t dir, int cnt) |
| 521 | { |
| 522 | int old, max; |
| 523 | |
| 524 | old = *tns->sel; |
| 525 | cnt = cnt > 1 ? cnt : 1; |
| 526 | |
| 527 | switch (dir) { |
| 528 | case DIR_UP: |
| 529 | *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols); |
| 530 | break; |
| 531 | case DIR_DOWN: |
| 532 | max = tns->cols * ((*tns->cnt - 1) / tns->cols) + |
| 533 | MIN((*tns->cnt - 1) % tns->cols, *tns->sel % tns->cols); |
| 534 | *tns->sel = MIN(*tns->sel + cnt * tns->cols, max); |
| 535 | break; |
| 536 | case DIR_LEFT: |
| 537 | *tns->sel = MAX(*tns->sel - cnt, 0); |
| 538 | break; |
| 539 | case DIR_RIGHT: |
| 540 | *tns->sel = MIN(*tns->sel + cnt, *tns->cnt - 1); |
| 541 | break; |
| 542 | } |
| 543 | |
| 544 | if (*tns->sel != old) { |
| 545 | tns_highlight(tns, old, false); |
| 546 | tns_check_view(tns, false); |
| 547 | if (!tns->dirty) |
| 548 | tns_highlight(tns, *tns->sel, true); |
| 549 | } |
| 550 | return *tns->sel != old; |
| 551 | } |
| 552 | |
| 553 | bool tns_scroll(tns_t *tns, direction_t dir, bool screen) |
| 554 | { |
| 555 | int d, max, old; |
| 556 | |
| 557 | old = tns->first; |
| 558 | d = tns->cols * (screen ? tns->rows : 1); |
| 559 | |
| 560 | if (dir == DIR_DOWN) { |
| 561 | max = *tns->cnt - tns->cols * tns->rows; |
| 562 | if (*tns->cnt % tns->cols != 0) |
| 563 | max += tns->cols - *tns->cnt % tns->cols; |
| 564 | tns->first = MIN(tns->first + d, max); |
| 565 | } else if (dir == DIR_UP) { |
| 566 | tns->first = MAX(tns->first - d, 0); |
| 567 | } |
| 568 | |
| 569 | if (tns->first != old) { |
| 570 | tns_check_view(tns, true); |
| 571 | tns->dirty = true; |
| 572 | } |
| 573 | return tns->first != old; |
| 574 | } |
| 575 | |
| 576 | bool tns_zoom(tns_t *tns, int d) |
| 577 | { |
| 578 | int i, oldzl; |
| 579 | |
| 580 | oldzl = tns->zl; |
| 581 | tns->zl += -(d < 0) + (d > 0); |
| 582 | tns->zl = MAX(tns->zl, 0); |
| 583 | tns->zl = MIN(tns->zl, ARRLEN(thumb_sizes)-1); |
| 584 | |
| 585 | tns->bw = ((thumb_sizes[tns->zl] - 1) >> 5) + 1; |
| 586 | tns->bw = MIN(tns->bw, 4); |
| 587 | tns->dim = thumb_sizes[tns->zl] + 2 * tns->bw + 6; |
| 588 | |
| 589 | if (tns->zl != oldzl) { |
| 590 | for (i = 0; i < *tns->cnt; i++) |
| 591 | tns_unload(tns, i); |
| 592 | tns->dirty = true; |
| 593 | } |
| 594 | return tns->zl != oldzl; |
| 595 | } |
| 596 | |
| 597 | int tns_translate(tns_t *tns, int x, int y) |
| 598 | { |
| 599 | int n; |
| 600 | |
| 601 | if (x < tns->x || y < tns->y) |
| 602 | return -1; |
| 603 | |
| 604 | n = tns->first + (y - tns->y) / tns->dim * tns->cols + |
| 605 | (x - tns->x) / tns->dim; |
| 606 | if (n >= *tns->cnt) |
| 607 | n = -1; |
| 608 | |
| 609 | return n; |
| 610 | } |