A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

151 line
3.4 KiB

  1. /* sxiv: thumbs.c
  2. * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <pthread.h>
  21. #include <Imlib2.h>
  22. #include "config.h"
  23. #include "thumbs.h"
  24. #include "util.h"
  25. typedef struct tload_s {
  26. const char **filenames;
  27. tns_t *tns;
  28. win_t *win;
  29. } tload_t;
  30. const int thumb_dim = THUMB_SIZE + 10;
  31. pthread_t loader;
  32. tload_t tinfo;
  33. void* thread_load(void *arg) {
  34. int i, w, h;
  35. float z, zw, zh;
  36. tload_t *tl;
  37. thumb_t *t;
  38. Imlib_Image *im;
  39. tl = (tload_t*) arg;
  40. for (i = 0; i < tl->tns->cnt; ++i) {
  41. if (!(im = imlib_load_image(tl->filenames[i])))
  42. continue;
  43. imlib_context_set_image(im);
  44. w = imlib_image_get_width();
  45. h = imlib_image_get_height();
  46. zw = (float) THUMB_SIZE / (float) w;
  47. zh = (float) THUMB_SIZE / (float) h;
  48. z = MIN(zw, zh);
  49. t = &tl->tns->thumbs[i];
  50. t->w = z * w;
  51. t->h = z * h;
  52. t->pm = win_create_pixmap(tl->win, t->w, t->h);
  53. imlib_context_set_drawable(t->pm);
  54. imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
  55. 0, 0, t->w, t->h);
  56. t->loaded = 1;
  57. imlib_free_image();
  58. }
  59. tl->tns->loaded = 1;
  60. return 0;
  61. }
  62. void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) {
  63. pthread_attr_t tattr;
  64. if (!tns || !win || !fnames || !fcnt)
  65. return;
  66. tns->thumbs = (thumb_t*) s_malloc(fcnt * sizeof(thumb_t));
  67. memset(tns->thumbs, 0, fcnt * sizeof(thumb_t));
  68. tns->cnt = fcnt;
  69. tns->loaded = 0;
  70. tinfo.filenames = fnames;
  71. tinfo.tns = tns;
  72. tinfo.win = win;
  73. pthread_attr_init(&tattr);
  74. pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
  75. if (pthread_create(&loader, &tattr, thread_load, (void*) &tinfo))
  76. die("could not create thread");
  77. }
  78. void tns_free(tns_t *tns, win_t *win) {
  79. int i;
  80. if (!tns)
  81. return;
  82. for (i = 0; i < tns->cnt; ++i)
  83. win_free_pixmap(win, tns->thumbs[i].pm);
  84. free(tns->thumbs);
  85. tns->thumbs = NULL;
  86. }
  87. void tns_render(tns_t *tns, win_t *win) {
  88. int i, cnt, x, y;
  89. if (!tns || !win)
  90. return;
  91. tns->cols = win->w / thumb_dim;
  92. tns->rows = win->h / thumb_dim;
  93. cnt = tns->cols * tns->rows;
  94. if (tns->first && tns->first + cnt > tns->cnt)
  95. tns->first = MAX(0, tns->cnt - cnt);
  96. cnt = MIN(tns->first + cnt, tns->cnt);
  97. win_clear(win);
  98. x = y = 5;
  99. i = tns->first;
  100. while (i < cnt) {
  101. if (!tns->thumbs[i].loaded)
  102. continue;
  103. tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
  104. tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
  105. win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
  106. tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
  107. if (++i % tns->cols == 0) {
  108. x = 5;
  109. y += thumb_dim;
  110. } else {
  111. x += thumb_dim;
  112. }
  113. }
  114. win_draw(win);
  115. }