A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

206 řádky
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->re = 0;
  52. img->w = imlib_image_get_width();
  53. img->h = imlib_image_get_height();
  54. return 0;
  55. }
  56. void img_check_pan(img_t *img, win_t *win) {
  57. if (!img)
  58. return;
  59. if (img->w * img->zoom > win->w) {
  60. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  61. img->x = 0;
  62. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  63. img->x = win->w - img->w * img->zoom;
  64. } else {
  65. img->x = (win->w - img->w * img->zoom) / 2;
  66. }
  67. if (img->h * img->zoom > win->h) {
  68. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  69. img->y = 0;
  70. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  71. img->y = win->h - img->h * img->zoom;
  72. } else {
  73. img->y = (win->h - img->h * img->zoom) / 2;
  74. }
  75. }
  76. void img_render(img_t *img, win_t *win) {
  77. int sx, sy, sw, sh;
  78. int dx, dy, dw, dh;
  79. float zw, zh;
  80. if (!img || !win || !imlib_context_get_image())
  81. return;
  82. if (!img->re) {
  83. /* rendered for the first time */
  84. img->re = 1;
  85. /* set zoom level to fit image into window */
  86. if (img->scalemode != SCALE_ZOOM) {
  87. zw = (float) win->w / (float) img->w;
  88. zh = (float) win->h / (float) img->h;
  89. img->zoom = MIN(zw, zh);
  90. if (img->zoom < zoom_min)
  91. img->zoom = zoom_min;
  92. else if (img->zoom > zoom_max)
  93. img->zoom = zoom_max;
  94. if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
  95. img->zoom = 1.0;
  96. }
  97. /* center image in window */
  98. img->x = (win->w - img->w * img->zoom) / 2;
  99. img->y = (win->h - img->h * img->zoom) / 2;
  100. } else {
  101. /* typically after zooming and panning */
  102. img_check_pan(img, win);
  103. }
  104. if (img->x < 0) {
  105. sx = -img->x / img->zoom;
  106. sw = win->w / img->zoom;
  107. dx = 0;
  108. dw = win->w;
  109. } else {
  110. sx = 0;
  111. sw = img->w;
  112. dx = img->x;
  113. dw = img->w * img->zoom;
  114. }
  115. if (img->y < 0) {
  116. sy = -img->y / img->zoom;
  117. sh = win->h / img->zoom;
  118. dy = 0;
  119. dh = win->h;
  120. } else {
  121. sy = 0;
  122. sh = img->h;
  123. dy = img->y;
  124. dh = img->h * img->zoom;
  125. }
  126. win_clear(win);
  127. imlib_context_set_drawable(win->pm);
  128. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  129. win_draw(win);
  130. }
  131. int img_zoom(img_t *img, float z) {
  132. if (!img)
  133. return 0;
  134. if (z < zoom_min)
  135. z = zoom_min;
  136. else if (z > zoom_max)
  137. z = zoom_max;
  138. if (z != img->zoom) {
  139. img->x -= (img->w * z - img->w * img->zoom) / 2;
  140. img->y -= (img->h * z - img->h * img->zoom) / 2;
  141. img->zoom = z;
  142. return 1;
  143. } else {
  144. return 0;
  145. }
  146. }
  147. int img_zoom_in(img_t *img) {
  148. int i;
  149. if (!img)
  150. return 0;
  151. for (i = 1; i < zl_cnt; ++i) {
  152. if (zoom_levels[i] > img->zoom * 100.0)
  153. return img_zoom(img, zoom_levels[i] / 100.0);
  154. }
  155. return 0;
  156. }
  157. int img_zoom_out(img_t *img) {
  158. int i;
  159. if (!img)
  160. return 0;
  161. for (i = zl_cnt - 2; i >= 0; --i) {
  162. if (zoom_levels[i] < img->zoom * 100.0)
  163. return img_zoom(img, zoom_levels[i] / 100.0);
  164. }
  165. return 0;
  166. }