A Simple X Image Viewer
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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