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.

image.c 4.9 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 if (img->cp) {
  103. /* only useful after zooming */
  104. img_check_pan(img, win);
  105. img->cp = 0;
  106. }
  107. /* calculate source and destination offsets */
  108. if (img->x < 0) {
  109. sx = -img->x / img->zoom;
  110. sw = win->w / img->zoom;
  111. dx = 0;
  112. dw = win->w;
  113. } else {
  114. sx = 0;
  115. sw = img->w;
  116. dx = img->x;
  117. dw = img->w * img->zoom;
  118. }
  119. if (img->y < 0) {
  120. sy = -img->y / img->zoom;
  121. sh = win->h / img->zoom;
  122. dy = 0;
  123. dh = win->h;
  124. } else {
  125. sy = 0;
  126. sh = img->h;
  127. dy = img->y;
  128. dh = img->h * img->zoom;
  129. }
  130. win_clear(win);
  131. imlib_context_set_drawable(win->pm);
  132. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  133. win_draw(win);
  134. }
  135. int img_zoom(img_t *img, float z) {
  136. if (!img)
  137. return 0;
  138. if (z < zoom_min)
  139. z = zoom_min;
  140. else if (z > zoom_max)
  141. z = zoom_max;
  142. if (z != img->zoom) {
  143. img->x -= (img->w * z - img->w * img->zoom) / 2;
  144. img->y -= (img->h * z - img->h * img->zoom) / 2;
  145. img->zoom = z;
  146. img->cp = 1;
  147. return 1;
  148. } else {
  149. return 0;
  150. }
  151. }
  152. int img_zoom_in(img_t *img) {
  153. int i;
  154. if (!img)
  155. return 0;
  156. for (i = 1; i < zl_cnt; ++i) {
  157. if (zoom_levels[i] > img->zoom * 100.0)
  158. return img_zoom(img, zoom_levels[i] / 100.0);
  159. }
  160. return 0;
  161. }
  162. int img_zoom_out(img_t *img) {
  163. int i;
  164. if (!img)
  165. return 0;
  166. for (i = zl_cnt - 2; i >= 0; --i) {
  167. if (zoom_levels[i] < img->zoom * 100.0)
  168. return img_zoom(img, zoom_levels[i] / 100.0);
  169. }
  170. return 0;
  171. }
  172. int img_pan(img_t *img, win_t *win, pandir_t dir) {
  173. int ox, oy;
  174. if (!img || !win)
  175. return 0;
  176. ox = img->x;
  177. oy = img->y;
  178. switch (dir) {
  179. case PAN_LEFT:
  180. img->x += win->w / 5;
  181. break;
  182. case PAN_RIGHT:
  183. img->x -= win->w / 5;
  184. break;
  185. case PAN_UP:
  186. img->y += win->h / 5;
  187. break;
  188. case PAN_DOWN:
  189. img->y -= win->h / 5;
  190. break;
  191. }
  192. img_check_pan(img, win);
  193. return ox != img->x || oy != img->y;
  194. }