Render image on window

This commit is contained in:
Bert 2011-01-18 20:11:28 +01:00
parent bb4edc756c
commit 79c7e6178e
6 changed files with 75 additions and 7 deletions

4
app.c
View file

@ -28,7 +28,7 @@ void app_init(app_t *app) {
app->fileidx = 0; app->fileidx = 0;
app->img.zoom = 100; app->img.zoom = 1.0;
app->img.scalemode = SCALE_MODE; app->img.scalemode = SCALE_MODE;
app->win.w = WIN_WIDTH; app->win.w = WIN_WIDTH;
@ -52,4 +52,6 @@ void app_load_image(app_t *app) {
return; return;
img_load(&app->img, app->filenames[app->fileidx]); img_load(&app->img, app->filenames[app->fileidx]);
img_render(&app->img, &app->win);
} }

55
image.c
View file

@ -47,3 +47,58 @@ void img_load(img_t *img, char *filename) {
img->w = imlib_image_get_width(); img->w = imlib_image_get_width();
img->h = imlib_image_get_height(); 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);
}

View file

@ -30,7 +30,7 @@ typedef enum scalemode_e {
} scalemode_t; } scalemode_t;
typedef struct img_s { typedef struct img_s {
int zoom; float zoom;
scalemode_t scalemode; scalemode_t scalemode;
int w; int w;
int h; int h;
@ -43,6 +43,6 @@ typedef struct img_s {
void imlib_init(win_t*); void imlib_init(win_t*);
void img_load(img_t*, char*); void img_load(img_t*, char*);
void img_display(img_t*, win_t*); void img_render(img_t*, win_t*);
#endif /* IMAGE_H */ #endif /* IMAGE_H */

5
sxiv.h
View file

@ -23,6 +23,9 @@
#define VERSION "git-20110117" #define VERSION "git-20110117"
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define WARN(...) \ #define WARN(...) \
do { \ do { \
fprintf(stderr, "sxiv: %s:%d: warning: ", __FILE__, __LINE__); \ fprintf(stderr, "sxiv: %s:%d: warning: ", __FILE__, __LINE__); \
@ -30,7 +33,7 @@
fprintf(stderr, "\n"); \ fprintf(stderr, "\n"); \
} while (0) } while (0)
#define DIE(...) \ #define DIE(...) \
do { \ do { \
fprintf(stderr, "sxiv: %s:%d: error: ", __FILE__, __LINE__); \ fprintf(stderr, "sxiv: %s:%d: error: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \ fprintf(stderr, __VA_ARGS__); \

View file

@ -106,3 +106,10 @@ int win_configure(win_t *win, XConfigureEvent *cev) {
win->bw = cev->border_width; win->bw = cev->border_width;
return changed; return changed;
} }
void win_clear(win_t *win) {
if (!win)
return;
XClearWindow(win->env.dpy, win->xwin);
}

View file

@ -47,5 +47,6 @@ void win_open(win_t*);
void win_close(win_t*); void win_close(win_t*);
int win_configure(win_t*, XConfigureEvent*); int win_configure(win_t*, XConfigureEvent*);
void win_clear(win_t*);
#endif /* WINDOW_H */ #endif /* WINDOW_H */