A Simple X Image Viewer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

114 行
2.7 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 <Imlib2.h>
  20. #include "config.h"
  21. #include "thumbs.h"
  22. #include "util.h"
  23. const int thumb_dim = THUMB_SIZE + 10;
  24. void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) {
  25. int i, w, h;
  26. float z, zw, zh;
  27. thumb_t *t;
  28. Imlib_Image *im;
  29. if (!tns || !win || !fnames || !fcnt)
  30. return;
  31. tns->thumbs = (thumb_t*) s_malloc(fcnt * sizeof(thumb_t));
  32. tns->cnt = fcnt;
  33. tns->loaded = 0;
  34. for (i = 0; i < fcnt; ++i) {
  35. if (!(im = imlib_load_image(fnames[i])))
  36. continue;
  37. imlib_context_set_image(im);
  38. w = imlib_image_get_width();
  39. h = imlib_image_get_height();
  40. zw = (float) THUMB_SIZE / (float) w;
  41. zh = (float) THUMB_SIZE / (float) h;
  42. z = MIN(zw, zh);
  43. t = &tns->thumbs[i];
  44. t->w = z * w;
  45. t->h = z * h;
  46. t->pm = win_create_pixmap(win, t->w, t->h);
  47. imlib_context_set_drawable(t->pm);
  48. imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
  49. 0, 0, t->w, t->h);
  50. imlib_free_image();
  51. }
  52. }
  53. void tns_free(tns_t *tns, win_t *win) {
  54. int i;
  55. if (!tns)
  56. return;
  57. for (i = 0; i < tns->cnt; ++i)
  58. win_free_pixmap(win, tns->thumbs[i].pm);
  59. free(tns->thumbs);
  60. tns->thumbs = NULL;
  61. }
  62. void tns_render(tns_t *tns, win_t *win) {
  63. int i, cnt, x, y;
  64. if (!tns || !win)
  65. return;
  66. tns->cols = win->w / thumb_dim;
  67. tns->rows = win->h / thumb_dim;
  68. cnt = tns->cols * tns->rows;
  69. if (tns->first && tns->first + cnt > tns->cnt)
  70. tns->first = MAX(0, tns->cnt - cnt);
  71. cnt = MIN(tns->first + cnt, tns->cnt);
  72. win_clear(win);
  73. x = y = 5;
  74. i = tns->first;
  75. while (i < cnt) {
  76. tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
  77. tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
  78. win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
  79. tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
  80. if (++i % tns->cols == 0) {
  81. x = 5;
  82. y += thumb_dim;
  83. } else {
  84. x += thumb_dim;
  85. }
  86. }
  87. win_draw(win);
  88. }