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);
|
||||
}
|
||||
|
61
image.c
61
image.c
@ -25,7 +25,7 @@
|
||||
void imlib_init(win_t *win) {
|
||||
if (!win)
|
||||
return;
|
||||
|
||||
|
||||
imlib_context_set_display(win->env.dpy);
|
||||
imlib_context_set_visual(win->env.vis);
|
||||
imlib_context_set_colormap(win->env.cmap);
|
||||
@ -41,9 +41,64 @@ void img_load(img_t *img, char *filename) {
|
||||
|
||||
if (!(img->im = imlib_load_image(filename)))
|
||||
DIE("could not open image: %s", filename);
|
||||
|
||||
|
||||
imlib_context_set_image(img->im);
|
||||
|
||||
|
||||
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 */
|
||||
|
5
sxiv.h
5
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__); \
|
||||
@ -30,7 +33,7 @@
|
||||
fprintf(stderr, "\n"); \
|
||||
} while (0)
|
||||
|
||||
#define DIE(...) \
|
||||
#define DIE(...) \
|
||||
do { \
|
||||
fprintf(stderr, "sxiv: %s:%d: error: ", __FILE__, __LINE__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
|
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