Krolyxon
Commit ee908ca

Branches

Refs →

nsxivthumbs.c

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