A Simple X Image Viewer
 
 
 
 
 
 

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