Krolyxon
Commit d3a70a2

Branches

Refs →

nsxivthumbs.c

609 lines · 13.8 KB · C

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