A Simple X Image Viewer
 
 
 
 
 
 

128 lines
2.8 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. }
  30. void imlib_destroy() {
  31. if (imlib_context_get_image())
  32. imlib_free_image();
  33. }
  34. int img_load(img_t *img, const char *filename) {
  35. Imlib_Image *im;
  36. if (!img || !filename)
  37. return -1;
  38. if (imlib_context_get_image())
  39. imlib_free_image();
  40. if (!(im = imlib_load_image(filename))) {
  41. WARN("could not open image: %s", filename);
  42. return -1;
  43. }
  44. imlib_context_set_image(im);
  45. img->w = imlib_image_get_width();
  46. img->h = imlib_image_get_height();
  47. return 0;
  48. }
  49. void img_display(img_t *img, win_t *win) {
  50. float zw, zh;
  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. img_render(img, win);
  69. }
  70. void img_render(img_t *img, win_t *win) {
  71. int sx, sy, sw, sh;
  72. int dx, dy, dw, dh;
  73. if (!img || !win || !imlib_context_get_image())
  74. return;
  75. if (img->x < 0) {
  76. sx = -img->x / img->zoom;
  77. sw = win->w / img->zoom;
  78. dx = 0;
  79. dw = win->w;
  80. } else {
  81. sx = 0;
  82. sw = img->w;
  83. dx = img->x;
  84. dw = img->w * img->zoom;
  85. }
  86. if (img->y < 0) {
  87. sy = -img->y / img->zoom;
  88. sh = win->h / img->zoom;
  89. dy = 0;
  90. dh = win->h;
  91. } else {
  92. sy = 0;
  93. sh = img->h;
  94. dy = img->y;
  95. dh = img->h * img->zoom;
  96. }
  97. win_clear(win);
  98. imlib_context_set_drawable(win->pm);
  99. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  100. win_draw(win);
  101. }