A Simple X Image Viewer
 
 
 
 
 
 

129 lines
3.0 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. int img_load(img_t *img, const char *filename) {
  36. Imlib_Image *im;
  37. if (!img || !filename)
  38. return -1;
  39. if (imlib_context_get_image())
  40. imlib_free_image();
  41. if (!(im = imlib_load_image(filename))) {
  42. WARN("could not open image: %s", filename);
  43. return -1;
  44. }
  45. imlib_context_set_image(im);
  46. img->w = imlib_image_get_width();
  47. img->h = imlib_image_get_height();
  48. return 0;
  49. }
  50. void img_display(img_t *img, win_t *win) {
  51. float zw, zh;
  52. if (!img || !win || !imlib_context_get_image())
  53. return;
  54. /* set zoom level to fit image into window */
  55. if (img->scalemode != SCALE_ZOOM) {
  56. zw = (float) win->w / (float) img->w;
  57. zh = (float) win->h / (float) img->h;
  58. img->zoom = MIN(zw, zh);
  59. if (img->zoom * 100.0 < ZOOM_MIN)
  60. img->zoom = ZOOM_MIN / 100.0;
  61. else if (img->zoom * 100.0 > ZOOM_MAX)
  62. img->zoom = ZOOM_MAX / 100.0;
  63. if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
  64. img->zoom = 1.0;
  65. }
  66. /* center image in window */
  67. img->x = (win->w - img->w * img->zoom) / 2;
  68. img->y = (win->h - img->h * img->zoom) / 2;
  69. win_clear(win);
  70. img_render(img, win, 0, 0, win->w, win->h);
  71. }
  72. void img_render(img_t *img, win_t *win, int x, int y, int w, int h) {
  73. int sx, sy, sw, sh;
  74. int dx, dy, dw, dh;
  75. if (!img || !win || !imlib_context_get_image())
  76. return;
  77. if (img->x < x) {
  78. sx = (x - img->x) / img->zoom;
  79. sw = MIN(w / img->zoom, img->w - sx);
  80. dx = x;
  81. dw = sw * img->zoom;
  82. } else {
  83. sx = 0;
  84. sw = MIN((w - img->x + x) / img->zoom, img->w);
  85. dx = img->x;
  86. dw = sw * img->zoom;
  87. }
  88. if (img->y < y) {
  89. sy = (y - img->y) / img->zoom;
  90. sh = MIN(h / img->zoom, img->h - sy);
  91. dy = y;
  92. dh = sh * img->zoom;
  93. } else {
  94. sy = 0;
  95. sh = MIN((h - img->y + y) / img->zoom, img->h);
  96. dy = img->y;
  97. dh = sh * img->zoom;
  98. }
  99. if (sw < 0 || sh < 0)
  100. return;
  101. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  102. }