| 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 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #include <Imlib2.h> |
| 23 | |
| 24 | #include "sxiv.h" |
| 25 | #include "image.h" |
| 26 | |
| 27 | void imlib_init(win_t *win) { |
| 28 | if (!win) |
| 29 | return; |
| 30 | |
| 31 | imlib_context_set_display(win->env.dpy); |
| 32 | imlib_context_set_visual(win->env.vis); |
| 33 | imlib_context_set_colormap(win->env.cmap); |
| 34 | imlib_context_set_drawable(win->xwin); |
| 35 | } |
| 36 | |
| 37 | void imlib_destroy() { |
| 38 | if (imlib_context_get_image()) |
| 39 | imlib_free_image(); |
| 40 | } |
| 41 | |
| 42 | int img_load(img_t *img, const char *filename) { |
| 43 | Imlib_Image *im; |
| 44 | |
| 45 | if (!img || !filename) |
| 46 | return -1; |
| 47 | |
| 48 | if (imlib_context_get_image()) |
| 49 | imlib_free_image(); |
| 50 | |
| 51 | if (!(im = imlib_load_image(filename))) { |
| 52 | WARN("could not open image: %s", filename); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | imlib_context_set_image(im); |
| 57 | |
| 58 | img->w = imlib_image_get_width(); |
| 59 | img->h = imlib_image_get_height(); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | void img_display(img_t *img, win_t *win) { |
| 65 | float zw, zh; |
| 66 | |
| 67 | if (!img || !win || !imlib_context_get_image()) |
| 68 | return; |
| 69 | |
| 70 | /* set zoom level to fit image into window */ |
| 71 | if (img->scalemode != SCALE_ZOOM) { |
| 72 | zw = (float) win->w / (float) img->w; |
| 73 | zh = (float) win->h / (float) img->h; |
| 74 | img->zoom = MIN(zw, zh); |
| 75 | |
| 76 | if (img->zoom * 100.0 < ZOOM_MIN) |
| 77 | img->zoom = ZOOM_MIN / 100.0; |
| 78 | else if (img->zoom * 100.0 > ZOOM_MAX) |
| 79 | img->zoom = ZOOM_MAX / 100.0; |
| 80 | |
| 81 | if (img->scalemode == SCALE_DOWN && img->zoom > 1.0) |
| 82 | img->zoom = 1.0; |
| 83 | } |
| 84 | |
| 85 | /* center image in window */ |
| 86 | img->x = (win->w - img->w * img->zoom) / 2; |
| 87 | img->y = (win->h - img->h * img->zoom) / 2; |
| 88 | |
| 89 | win_clear(win); |
| 90 | |
| 91 | img_render(img, win, 0, 0, win->w, win->h); |
| 92 | } |
| 93 | |
| 94 | void img_render(img_t *img, win_t *win, int x, int y, int w, int h) { |
| 95 | int sx, sy, sw, sh; |
| 96 | int dx, dy, dw, dh; |
| 97 | |
| 98 | if (!img || !win || !imlib_context_get_image()) |
| 99 | return; |
| 100 | |
| 101 | if (img->x < x) { |
| 102 | sx = (x - img->x) / img->zoom; |
| 103 | sw = MIN(w / img->zoom, img->w - sx); |
| 104 | dx = x; |
| 105 | dw = sw * img->zoom; |
| 106 | } else { |
| 107 | sx = 0; |
| 108 | sw = MIN((w - img->x + x) / img->zoom, img->w); |
| 109 | dx = img->x; |
| 110 | dw = sw * img->zoom; |
| 111 | } |
| 112 | if (img->y < y) { |
| 113 | sy = (y - img->y) / img->zoom; |
| 114 | sh = MIN(h / img->zoom, img->h - sy); |
| 115 | dy = y; |
| 116 | dh = sh * img->zoom; |
| 117 | } else { |
| 118 | sy = 0; |
| 119 | sh = MIN((h - img->y + y) / img->zoom, img->h); |
| 120 | dy = img->y; |
| 121 | dh = sh * img->zoom; |
| 122 | } |
| 123 | |
| 124 | if (sw < 0 || sh < 0) |
| 125 | return; |
| 126 | |
| 127 | imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); |
| 128 | } |