Krolyxon
Commit 79c7e61

Branches

Refs →

nsxiv79c7e61

Render image on window

Author
Bert
Date
2011-01-18 20:11 +0100
Parent
bb4edc7
Commit
79c7e6178e9b577158408157ec23a477556ecf16

+75 −7

6 files changed

  1. M app.c +3 −1
  2. M image.c +58 −3
  3. M image.h +2 −2
  4. M sxiv.h +4 −1
  5. M window.c +7 −0
  6. M window.h +1 −0
M app.c View
@@ -28,7 +28,7 @@ void app_init(app_t *app) {
2828
2929 app->fileidx = 0;
3030
31− app->img.zoom = 100;
31+ app->img.zoom = 1.0;
3232 app->img.scalemode = SCALE_MODE;
3333
3434 app->win.w = WIN_WIDTH;
@@ -52,4 +52,6 @@ void app_load_image(app_t *app) {
5252 return;
5353
5454 img_load(&app->img, app->filenames[app->fileidx]);
55+
56+ img_render(&app->img, &app->win);
5557 }
M image.c View
@@ -25,7 +25,7 @@
2525 void imlib_init(win_t *win) {
2626 if (!win)
2727 return;
28−
28+
2929 imlib_context_set_display(win->env.dpy);
3030 imlib_context_set_visual(win->env.vis);
3131 imlib_context_set_colormap(win->env.cmap);
@@ -41,9 +41,64 @@ void img_load(img_t *img, char *filename) {
4141
4242 if (!(img->im = imlib_load_image(filename)))
4343 DIE("could not open image: %s", filename);
44−
44+
4545 imlib_context_set_image(img->im);
46−
46+
4747 img->w = imlib_image_get_width();
4848 img->h = imlib_image_get_height();
4949 }
50+
51+void img_render(img_t *img, win_t *win) {
52+ float zw, zh;
53+ unsigned int sx, sy, sw, sh;
54+ unsigned int dx, dy, dw, dh;
55+
56+ if (!img || !win)
57+ return;
58+
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+
65+ if (img->zoom * 100.0 < ZOOM_MIN)
66+ img->zoom = ZOOM_MIN / 100.0;
67+ else if (img->zoom * 100.0 > ZOOM_MAX)
68+ img->zoom = ZOOM_MAX / 100.0;
69+
70+ if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
71+ img->zoom = 1.0;
72+ }
73+
74+ /* center image in window */
75+ img->x = (win->w - img->w * img->zoom) / 2;
76+ img->y = (win->h - img->h * img->zoom) / 2;
77+
78+ if (img->x < 0) {
79+ sx = -img->x / img->zoom;
80+ sw = (img->x + win->w) / img->zoom;
81+ dx = 0;
82+ dw = win->w;
83+ } else {
84+ sx = 0;
85+ sw = img->w;
86+ dx = img->x;
87+ dw = img->w * img->zoom;
88+ }
89+ if (img->y < 0) {
90+ sy = -img->y / img->zoom;
91+ sh = (img->y + win->h) / img->zoom;
92+ dy = 0;
93+ dh = win->h;
94+ } else {
95+ sy = 0;
96+ sh = img->h;
97+ dy = img->y;
98+ dh = img->h * img->zoom;
99+ }
100+
101+ win_clear(win);
102+
103+ imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
104+}
M image.h View
@@ -30,7 +30,7 @@ typedef enum scalemode_e {
3030 } scalemode_t;
3131
3232 typedef struct img_s {
33− int zoom;
33+ float zoom;
3434 scalemode_t scalemode;
3535 int w;
3636 int h;
@@ -43,6 +43,6 @@ typedef struct img_s {
4343 void imlib_init(win_t*);
4444
4545 void img_load(img_t*, char*);
46−void img_display(img_t*, win_t*);
46+void img_render(img_t*, win_t*);
4747
4848 #endif /* IMAGE_H */
M sxiv.h View
@@ -23,6 +23,9 @@
2323
2424 #define VERSION "git-20110117"
2525
26+#define MIN(a,b) ((a) < (b) ? (a) : (b))
27+#define MAX(a,b) ((a) > (b) ? (a) : (b))
28+
2629 #define WARN(...) \
2730 do { \
2831 fprintf(stderr, "sxiv: %s:%d: warning: ", __FILE__, __LINE__); \
@@ -30,7 +33,7 @@
3033 fprintf(stderr, "\n"); \
3134 } while (0)
3235
33−#define DIE(...) \
36+#define DIE(...) \
3437 do { \
3538 fprintf(stderr, "sxiv: %s:%d: error: ", __FILE__, __LINE__); \
3639 fprintf(stderr, __VA_ARGS__); \
M window.c View
@@ -106,3 +106,10 @@ int win_configure(win_t *win, XConfigureEvent *cev) {
106106 win->bw = cev->border_width;
107107 return changed;
108108 }
109+
110+void win_clear(win_t *win) {
111+ if (!win)
112+ return;
113+
114+ XClearWindow(win->env.dpy, win->xwin);
115+}
M window.h View
@@ -47,5 +47,6 @@ void win_open(win_t*);
4747 void win_close(win_t*);
4848
4949 int win_configure(win_t*, XConfigureEvent*);
50+void win_clear(win_t*);
5051
5152 #endif /* WINDOW_H */