A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

207 lines
4.3 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. int zl_cnt;
  24. float zoom_min;
  25. float zoom_max;
  26. void imlib_init(win_t *win) {
  27. if (!win)
  28. return;
  29. imlib_context_set_display(win->env.dpy);
  30. imlib_context_set_visual(win->env.vis);
  31. imlib_context_set_colormap(win->env.cmap);
  32. zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
  33. zoom_min = zoom_levels[0] / 100.0;
  34. zoom_max = zoom_levels[zl_cnt - 1] / 100.0;
  35. }
  36. void imlib_destroy() {
  37. if (imlib_context_get_image())
  38. imlib_free_image();
  39. }
  40. int img_load(img_t *img, const char *filename) {
  41. Imlib_Image *im;
  42. if (!img || !filename)
  43. return -1;
  44. if (imlib_context_get_image())
  45. imlib_free_image();
  46. if (!(im = imlib_load_image(filename))) {
  47. WARN("could not open image: %s", filename);
  48. return -1;
  49. }
  50. imlib_context_set_image(im);
  51. img->w = imlib_image_get_width();
  52. img->h = imlib_image_get_height();
  53. return 0;
  54. }
  55. void img_display(img_t *img, win_t *win) {
  56. float zw, zh;
  57. if (!img || !win || !imlib_context_get_image())
  58. return;
  59. /* set zoom level to fit image into window */
  60. if (img->scalemode != SCALE_ZOOM) {
  61. zw = (float) win->w / (float) img->w;
  62. zh = (float) win->h / (float) img->h;
  63. img->zoom = MIN(zw, zh);
  64. if (img->zoom < zoom_min)
  65. img->zoom = zoom_min;
  66. else if (img->zoom > zoom_max)
  67. img->zoom = zoom_max;
  68. if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
  69. img->zoom = 1.0;
  70. }
  71. /* center image in window */
  72. img->x = (win->w - img->w * img->zoom) / 2;
  73. img->y = (win->h - img->h * img->zoom) / 2;
  74. img_render(img, win);
  75. }
  76. void img_check_pan(img_t *img, win_t *win) {
  77. if (!img)
  78. return;
  79. if (img->w * img->zoom > win->w) {
  80. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  81. img->x = 0;
  82. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  83. img->x = win->w - img->w * img->zoom;
  84. } else {
  85. img->x = (win->w - img->w * img->zoom) / 2;
  86. }
  87. if (img->h * img->zoom > win->h) {
  88. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  89. img->y = 0;
  90. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  91. img->y = win->h - img->h * img->zoom;
  92. } else {
  93. img->y = (win->h - img->h * img->zoom) / 2;
  94. }
  95. }
  96. void img_render(img_t *img, win_t *win) {
  97. int sx, sy, sw, sh;
  98. int dx, dy, dw, dh;
  99. if (!img || !win || !imlib_context_get_image())
  100. return;
  101. img_check_pan(img, win);
  102. if (img->x < 0) {
  103. sx = -img->x / img->zoom;
  104. sw = win->w / img->zoom;
  105. dx = 0;
  106. dw = win->w;
  107. } else {
  108. sx = 0;
  109. sw = img->w;
  110. dx = img->x;
  111. dw = img->w * img->zoom;
  112. }
  113. if (img->y < 0) {
  114. sy = -img->y / img->zoom;
  115. sh = win->h / img->zoom;
  116. dy = 0;
  117. dh = win->h;
  118. } else {
  119. sy = 0;
  120. sh = img->h;
  121. dy = img->y;
  122. dh = img->h * img->zoom;
  123. }
  124. win_clear(win);
  125. imlib_context_set_drawable(win->pm);
  126. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  127. win_draw(win);
  128. }
  129. int img_zoom(img_t *img, float z) {
  130. if (!img)
  131. return 0;
  132. if (z < zoom_min)
  133. z = zoom_min;
  134. else if (z > zoom_max)
  135. z = zoom_max;
  136. if (z != img->zoom) {
  137. img->x -= (img->w * z - img->w * img->zoom) / 2;
  138. img->y -= (img->h * z - img->h * img->zoom) / 2;
  139. img->zoom = z;
  140. return 1;
  141. } else {
  142. return 0;
  143. }
  144. }
  145. int img_zoom_in(img_t *img) {
  146. int i;
  147. if (!img)
  148. return 0;
  149. for (i = 1; i < zl_cnt; ++i) {
  150. if (zoom_levels[i] > img->zoom * 100.0)
  151. return img_zoom(img, zoom_levels[i] / 100.0);
  152. }
  153. return 0;
  154. }
  155. int img_zoom_out(img_t *img) {
  156. int i;
  157. if (!img)
  158. return 0;
  159. for (i = zl_cnt - 2; i >= 0; --i) {
  160. if (zoom_levels[i] < img->zoom * 100.0)
  161. return img_zoom(img, zoom_levels[i] / 100.0);
  162. }
  163. return 0;
  164. }