A Simple X Image Viewer
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

476 行
10 KiB

  1. /* sxiv: thumbs.c
  2. * Copyright (c) 2011 Bert Muennich <be.muennich at googlemail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  19. #define _FEATURE_CONFIG
  20. #define _THUMBS_CONFIG
  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. #include "thumbs.h"
  28. #include "util.h"
  29. #include "config.h"
  30. #if EXIF_SUPPORT
  31. void exif_auto_orientate(const fileinfo_t*);
  32. #endif
  33. const int thumb_dim = THUMB_SIZE + 10;
  34. char *cache_dir = NULL;
  35. bool tns_cache_enabled(void) {
  36. struct stat stats;
  37. return cache_dir != NULL && stat(cache_dir, &stats) == 0 &&
  38. S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0;
  39. }
  40. char* tns_cache_filepath(const char *filepath) {
  41. size_t len;
  42. char *cfile = NULL;
  43. if (cache_dir == NULL || filepath == NULL || *filepath != '/')
  44. return NULL;
  45. if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
  46. /* don't cache images inside the cache directory! */
  47. len = strlen(cache_dir) + strlen(filepath) + 6;
  48. cfile = (char*) s_malloc(len);
  49. snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
  50. }
  51. return cfile;
  52. }
  53. Imlib_Image* tns_cache_load(const char *filepath) {
  54. char *cfile;
  55. struct stat cstats, fstats;
  56. Imlib_Image *im = NULL;
  57. if (filepath == NULL)
  58. return NULL;
  59. if (stat(filepath, &fstats) < 0)
  60. return NULL;
  61. if ((cfile = tns_cache_filepath(filepath)) != NULL) {
  62. if (stat(cfile, &cstats) == 0 && cstats.st_mtime == fstats.st_mtime)
  63. im = imlib_load_image(cfile);
  64. free(cfile);
  65. }
  66. return im;
  67. }
  68. void tns_cache_write(thumb_t *t, bool force) {
  69. char *cfile, *dirend;
  70. struct stat cstats, fstats;
  71. struct utimbuf times;
  72. Imlib_Load_Error err = 0;
  73. if (t == NULL || t->im == NULL)
  74. return;
  75. if (t->file == NULL || t->file->name == NULL || t->file->path == NULL)
  76. return;
  77. if (stat(t->file->path, &fstats) < 0)
  78. return;
  79. if ((cfile = tns_cache_filepath(t->file->path)) != NULL) {
  80. if (force || stat(cfile, &cstats) < 0 ||
  81. cstats.st_mtime != fstats.st_mtime)
  82. {
  83. if ((dirend = strrchr(cfile, '/')) != NULL) {
  84. *dirend = '\0';
  85. err = r_mkdir(cfile);
  86. *dirend = '/';
  87. }
  88. if (err == 0) {
  89. imlib_context_set_image(t->im);
  90. imlib_image_set_format("png");
  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. } else {
  98. warn("could not cache thumbnail: %s", t->file->name);
  99. }
  100. }
  101. free(cfile);
  102. }
  103. }
  104. void tns_clean_cache(tns_t *tns) {
  105. int dirlen;
  106. bool delete;
  107. char *cfile, *filename, *tpos;
  108. r_dir_t dir;
  109. if (cache_dir == NULL)
  110. return;
  111. if (r_opendir(&dir, cache_dir) < 0) {
  112. warn("could not open thumbnail cache directory: %s", cache_dir);
  113. return;
  114. }
  115. dirlen = strlen(cache_dir);
  116. while ((cfile = r_readdir(&dir)) != NULL) {
  117. filename = cfile + dirlen;
  118. delete = false;
  119. if ((tpos = strrchr(filename, '.')) != NULL) {
  120. *tpos = '\0';
  121. if (access(filename, F_OK) < 0)
  122. delete = true;
  123. *tpos = '.';
  124. }
  125. if (delete) {
  126. if (unlink(cfile) < 0)
  127. warn("could not delete cache file: %s", cfile);
  128. }
  129. free(cfile);
  130. }
  131. r_closedir(&dir);
  132. }
  133. void tns_init(tns_t *tns, int cnt, win_t *win) {
  134. int len;
  135. char *homedir;
  136. if (tns == NULL)
  137. return;
  138. if (cnt > 0) {
  139. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  140. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  141. } else {
  142. tns->thumbs = NULL;
  143. }
  144. tns->cap = cnt;
  145. tns->cnt = tns->first = tns->sel = 0;
  146. tns->win = win;
  147. tns->alpha = true;
  148. tns->dirty = false;
  149. if ((homedir = getenv("HOME")) != NULL) {
  150. if (cache_dir != NULL)
  151. free(cache_dir);
  152. len = strlen(homedir) + 10;
  153. cache_dir = (char*) s_malloc(len * sizeof(char));
  154. snprintf(cache_dir, len, "%s/.sxiv", homedir);
  155. } else {
  156. warn("could not locate thumbnail cache directory");
  157. }
  158. }
  159. void tns_free(tns_t *tns) {
  160. int i;
  161. if (tns != NULL)
  162. return;
  163. if (tns->thumbs != NULL) {
  164. for (i = 0; i < tns->cnt; i++) {
  165. if (tns->thumbs[i].im != NULL) {
  166. imlib_context_set_image(tns->thumbs[i].im);
  167. imlib_free_image();
  168. }
  169. }
  170. free(tns->thumbs);
  171. tns->thumbs = NULL;
  172. }
  173. if (cache_dir != NULL) {
  174. free(cache_dir);
  175. cache_dir = NULL;
  176. }
  177. }
  178. bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
  179. bool force, bool silent)
  180. {
  181. int w, h;
  182. bool use_cache, cache_hit = false;
  183. float z, zw, zh;
  184. thumb_t *t;
  185. Imlib_Image *im;
  186. const char *fmt;
  187. if (tns == NULL || tns->thumbs == NULL)
  188. return false;
  189. if (file == NULL || file->name == NULL || file->path == NULL)
  190. return false;
  191. if (n < 0 || n >= tns->cap)
  192. return false;
  193. t = &tns->thumbs[n];
  194. t->file = file;
  195. if (t->im != NULL) {
  196. imlib_context_set_image(t->im);
  197. imlib_free_image();
  198. }
  199. if ((use_cache = tns_cache_enabled())) {
  200. if (!force && (im = tns_cache_load(file->path)) != NULL)
  201. cache_hit = true;
  202. }
  203. if (!cache_hit) {
  204. if (access(file->path, R_OK) < 0 ||
  205. (im = imlib_load_image(file->path)) == NULL)
  206. {
  207. if (!silent)
  208. warn("could not open image: %s", file->name);
  209. return false;
  210. }
  211. }
  212. imlib_context_set_image(im);
  213. imlib_context_set_anti_alias(1);
  214. if ((fmt = imlib_image_format()) == NULL) {
  215. if (!silent)
  216. warn("could not open image: %s", file->name);
  217. imlib_free_image_and_decache();
  218. return false;
  219. }
  220. #if EXIF_SUPPORT
  221. if (!cache_hit && STREQ(fmt, "jpeg"))
  222. exif_auto_orientate(file);
  223. #endif
  224. w = imlib_image_get_width();
  225. h = imlib_image_get_height();
  226. zw = (float) THUMB_SIZE / (float) w;
  227. zh = (float) THUMB_SIZE / (float) h;
  228. z = MIN(zw, zh);
  229. z = MIN(z, 1.0);
  230. t->w = z * w;
  231. t->h = z * h;
  232. t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
  233. if (t->im == NULL)
  234. die("could not allocate memory");
  235. imlib_free_image_and_decache();
  236. if (use_cache && !cache_hit)
  237. tns_cache_write(t, false);
  238. tns->dirty = true;
  239. return true;
  240. }
  241. void tns_check_view(tns_t *tns, bool scrolled) {
  242. int r;
  243. if (tns == NULL)
  244. return;
  245. tns->first -= tns->first % tns->cols;
  246. r = tns->sel % tns->cols;
  247. if (scrolled) {
  248. /* move selection into visible area */
  249. if (tns->sel >= tns->first + tns->cols * tns->rows)
  250. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  251. else if (tns->sel < tns->first)
  252. tns->sel = tns->first + r;
  253. } else {
  254. /* scroll to selection */
  255. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  256. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  257. tns->dirty = true;
  258. } else if (tns->first > tns->sel) {
  259. tns->first = tns->sel - r;
  260. tns->dirty = true;
  261. }
  262. }
  263. }
  264. void tns_render(tns_t *tns) {
  265. thumb_t *t;
  266. win_t *win;
  267. int i, cnt, r, x, y;
  268. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  269. return;
  270. if (!tns->dirty)
  271. return;
  272. win = tns->win;
  273. win_clear(win);
  274. imlib_context_set_drawable(win->pm);
  275. tns->cols = MAX(1, win->w / thumb_dim);
  276. tns->rows = MAX(1, win->h / thumb_dim);
  277. if (tns->cnt < tns->cols * tns->rows) {
  278. tns->first = 0;
  279. cnt = tns->cnt;
  280. } else {
  281. tns_check_view(tns, false);
  282. cnt = tns->cols * tns->rows;
  283. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  284. tns->first -= r - r % tns->cols;
  285. if (r > 0)
  286. cnt -= r % tns->cols;
  287. }
  288. r = cnt % tns->cols ? 1 : 0;
  289. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  290. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  291. for (i = 0; i < cnt; i++) {
  292. t = &tns->thumbs[tns->first + i];
  293. t->x = x + (THUMB_SIZE - t->w) / 2;
  294. t->y = y + (THUMB_SIZE - t->h) / 2;
  295. imlib_context_set_image(t->im);
  296. if (!tns->alpha && imlib_image_has_alpha())
  297. win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
  298. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  299. t->x, t->y, t->w, t->h);
  300. if ((i + 1) % tns->cols == 0) {
  301. x = tns->x;
  302. y += thumb_dim;
  303. } else {
  304. x += thumb_dim;
  305. }
  306. }
  307. tns->dirty = false;
  308. tns_highlight(tns, tns->sel, true);
  309. }
  310. void tns_highlight(tns_t *tns, int n, bool hl) {
  311. thumb_t *t;
  312. win_t *win;
  313. int x, y;
  314. unsigned long col;
  315. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  316. return;
  317. win = tns->win;
  318. if (n >= 0 && n < tns->cnt) {
  319. t = &tns->thumbs[n];
  320. if (hl)
  321. col = win->selcol;
  322. else if (win->fullscreen)
  323. col = win->black;
  324. else
  325. col = win->bgcol;
  326. x = t->x - (THUMB_SIZE - t->w) / 2;
  327. y = t->y - (THUMB_SIZE - t->h) / 2;
  328. win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
  329. false, 2, col);
  330. }
  331. win_draw(win);
  332. }
  333. bool tns_move_selection(tns_t *tns, direction_t dir) {
  334. int old;
  335. if (tns == NULL || tns->thumbs == NULL)
  336. return false;
  337. old = tns->sel;
  338. switch (dir) {
  339. case DIR_LEFT:
  340. if (tns->sel > 0)
  341. tns->sel--;
  342. break;
  343. case DIR_RIGHT:
  344. if (tns->sel < tns->cnt - 1)
  345. tns->sel++;
  346. break;
  347. case DIR_UP:
  348. if (tns->sel >= tns->cols)
  349. tns->sel -= tns->cols;
  350. break;
  351. case DIR_DOWN:
  352. if (tns->sel + tns->cols < tns->cnt)
  353. tns->sel += tns->cols;
  354. break;
  355. }
  356. if (tns->sel != old) {
  357. tns_highlight(tns, old, false);
  358. tns_check_view(tns, false);
  359. if (!tns->dirty)
  360. tns_highlight(tns, tns->sel, true);
  361. }
  362. return tns->sel != old;
  363. }
  364. bool tns_scroll(tns_t *tns, direction_t dir, bool screen) {
  365. int d, max, old;
  366. if (tns == NULL)
  367. return false;
  368. old = tns->first;
  369. d = tns->cols * (screen ? tns->rows : 1);
  370. if (dir == DIR_DOWN) {
  371. max = tns->cnt - tns->cols * tns->rows;
  372. if (tns->cnt % tns->cols != 0)
  373. max += tns->cols - tns->cnt % tns->cols;
  374. tns->first = MIN(tns->first + d, max);
  375. } else if (dir == DIR_UP) {
  376. tns->first = MAX(tns->first - d, 0);
  377. }
  378. if (tns->first != old) {
  379. tns_check_view(tns, true);
  380. tns->dirty = true;
  381. }
  382. return tns->first != old;
  383. }
  384. int tns_translate(tns_t *tns, int x, int y) {
  385. int n;
  386. if (tns == NULL || tns->thumbs == NULL)
  387. return -1;
  388. if (x < tns->x || y < tns->y)
  389. return -1;
  390. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  391. (x - tns->x) / thumb_dim;
  392. if (n >= tns->cnt)
  393. n = -1;
  394. return n;
  395. }