Krolyxon
Commit 4ec8fd5

Branches

Refs →

nsxivthumbs.c

597 lines · 13.7 KB · C

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