Render image on window
This commit is contained in:
parent
bb4edc756c
commit
79c7e6178e
4
app.c
4
app.c
@ -28,7 +28,7 @@ void app_init(app_t *app) {
|
||||
|
||||
app->fileidx = 0;
|
||||
|
||||
app->img.zoom = 100;
|
||||
app->img.zoom = 1.0;
|
||||
app->img.scalemode = SCALE_MODE;
|
||||
|
||||
app->win.w = WIN_WIDTH;
|
||||
@ -52,4 +52,6 @@ void app_load_image(app_t *app) {
|
||||
return;
|
||||
|
||||
img_load(&app->img, app->filenames[app->fileidx]);
|
||||
|
||||
img_render(&app->img, &app->win);
|
||||
}
|
||||
|
55
image.c
55
image.c
@ -47,3 +47,58 @@ void img_load(img_t *img, char *filename) {
|
||||
img->w = imlib_image_get_width();
|
||||
img->h = imlib_image_get_height();
|
||||
}
|
||||
|
||||
void img_render(img_t *img, win_t *win) {
|
||||
float zw, zh;
|
||||
unsigned int sx, sy, sw, sh;
|
||||
unsigned int dx, dy, dw, dh;
|
||||
|
||||
if (!img || !win)
|
||||
return;
|
||||
|
||||
/* set zoom level to fit image into window */
|
||||
if (img->scalemode != SCALE_ZOOM) {
|
||||
zw = (float) win->w / (float) img->w;
|
||||
zh = (float) win->h / (float) img->h;
|
||||
img->zoom = MIN(zw, zh);
|
||||
|
||||
if (img->zoom * 100.0 < ZOOM_MIN)
|
||||
img->zoom = ZOOM_MIN / 100.0;
|
||||
else if (img->zoom * 100.0 > ZOOM_MAX)
|
||||
img->zoom = ZOOM_MAX / 100.0;
|
||||
|
||||
if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
|
||||
img->zoom = 1.0;
|
||||
}
|
||||
|
||||
/* center image in window */
|
||||
img->x = (win->w - img->w * img->zoom) / 2;
|
||||
img->y = (win->h - img->h * img->zoom) / 2;
|
||||
|
||||
if (img->x < 0) {
|
||||
sx = -img->x / img->zoom;
|
||||
sw = (img->x + win->w) / img->zoom;
|
||||
dx = 0;
|
||||
dw = win->w;
|
||||
} else {
|
||||
sx = 0;
|
||||
sw = img->w;
|
||||
dx = img->x;
|
||||
dw = img->w * img->zoom;
|
||||
}
|
||||
if (img->y < 0) {
|
||||
sy = -img->y / img->zoom;
|
||||
sh = (img->y + win->h) / img->zoom;
|
||||
dy = 0;
|
||||
dh = win->h;
|
||||
} else {
|
||||
sy = 0;
|
||||
sh = img->h;
|
||||
dy = img->y;
|
||||
dh = img->h * img->zoom;
|
||||
}
|
||||
|
||||
win_clear(win);
|
||||
|
||||
imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
|
||||
}
|
||||
|
4
image.h
4
image.h
@ -30,7 +30,7 @@ typedef enum scalemode_e {
|
||||
} scalemode_t;
|
||||
|
||||
typedef struct img_s {
|
||||
int zoom;
|
||||
float zoom;
|
||||
scalemode_t scalemode;
|
||||
int w;
|
||||
int h;
|
||||
@ -43,6 +43,6 @@ typedef struct img_s {
|
||||
void imlib_init(win_t*);
|
||||
|
||||
void img_load(img_t*, char*);
|
||||
void img_display(img_t*, win_t*);
|
||||
void img_render(img_t*, win_t*);
|
||||
|
||||
#endif /* IMAGE_H */
|
||||
|
3
sxiv.h
3
sxiv.h
@ -23,6 +23,9 @@
|
||||
|
||||
#define VERSION "git-20110117"
|
||||
|
||||
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
||||
#define MAX(a,b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
#define WARN(...) \
|
||||
do { \
|
||||
fprintf(stderr, "sxiv: %s:%d: warning: ", __FILE__, __LINE__); \
|
||||
|
7
window.c
7
window.c
@ -106,3 +106,10 @@ int win_configure(win_t *win, XConfigureEvent *cev) {
|
||||
win->bw = cev->border_width;
|
||||
return changed;
|
||||
}
|
||||
|
||||
void win_clear(win_t *win) {
|
||||
if (!win)
|
||||
return;
|
||||
|
||||
XClearWindow(win->env.dpy, win->xwin);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user