A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

609 wiersze
14 KiB

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