A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

114 řádky
2.7 KiB

  1. /* sxiv: image.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 <stdio.h>
  20. #include <Imlib2.h>
  21. #include "sxiv.h"
  22. #include "image.h"
  23. void imlib_init(win_t *win) {
  24. if (!win)
  25. return;
  26. imlib_context_set_display(win->env.dpy);
  27. imlib_context_set_visual(win->env.vis);
  28. imlib_context_set_colormap(win->env.cmap);
  29. imlib_context_set_drawable(win->xwin);
  30. }
  31. void imlib_destroy() {
  32. if (imlib_context_get_image())
  33. imlib_free_image();
  34. }
  35. void img_load(img_t *img, const char *filename) {
  36. Imlib_Image *im;
  37. if (!img || !filename)
  38. return;
  39. if (imlib_context_get_image())
  40. imlib_free_image();
  41. if (!(im = imlib_load_image(filename)))
  42. DIE("could not open image: %s", filename);
  43. imlib_context_set_image(im);
  44. img->w = imlib_image_get_width();
  45. img->h = imlib_image_get_height();
  46. }
  47. void img_render(img_t *img, win_t *win) {
  48. float zw, zh;
  49. unsigned int sx, sy, sw, sh;
  50. unsigned int dx, dy, dw, dh;
  51. if (!img || !win || !imlib_context_get_image())
  52. return;
  53. /* set zoom level to fit image into window */
  54. if (img->scalemode != SCALE_ZOOM) {
  55. zw = (float) win->w / (float) img->w;
  56. zh = (float) win->h / (float) img->h;
  57. img->zoom = MIN(zw, zh);
  58. if (img->zoom * 100.0 < ZOOM_MIN)
  59. img->zoom = ZOOM_MIN / 100.0;
  60. else if (img->zoom * 100.0 > ZOOM_MAX)
  61. img->zoom = ZOOM_MAX / 100.0;
  62. if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
  63. img->zoom = 1.0;
  64. }
  65. /* center image in window */
  66. img->x = (win->w - img->w * img->zoom) / 2;
  67. img->y = (win->h - img->h * img->zoom) / 2;
  68. if (img->x < 0) {
  69. sx = -img->x / img->zoom;
  70. sw = (img->x + win->w) / img->zoom;
  71. dx = 0;
  72. dw = win->w;
  73. } else {
  74. sx = 0;
  75. sw = img->w;
  76. dx = img->x;
  77. dw = img->w * img->zoom;
  78. }
  79. if (img->y < 0) {
  80. sy = -img->y / img->zoom;
  81. sh = (img->y + win->h) / img->zoom;
  82. dy = 0;
  83. dh = win->h;
  84. } else {
  85. sy = 0;
  86. sh = img->h;
  87. dy = img->y;
  88. dh = img->h * img->zoom;
  89. }
  90. win_clear(win);
  91. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  92. }