thumbs.c 14 KB

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