From 5ab0fa77f17013ac2a45fef471742529057b3d9f Mon Sep 17 00:00:00 2001 From: Kim Woelders Date: Thu, 25 Nov 2021 12:58:24 +0100 Subject: [PATCH] imlib2_view: Enable grabbing/viewing drawables --- src/bin/imlib2_view.c | 99 ++++++++++++++++++++++++++++++++----------- 1 file changed, 74 insertions(+), 25 deletions(-) diff --git a/src/bin/imlib2_view.c b/src/bin/imlib2_view.c index 12af0f5..e2ad701 100644 --- a/src/bin/imlib2_view.c +++ b/src/bin/imlib2_view.c @@ -13,9 +13,9 @@ Display *disp; static int debug = 0; +static int verbose = 0; static Window win; static Pixmap pm = 0; -static int depth; static int image_width = 0, image_height = 0; static int window_width = 0, window_height = 0; static Imlib_Image bg_im = NULL; @@ -24,6 +24,8 @@ static char opt_cache = 0; static char opt_scale = 0; static double opt_scale_x = 1.; static double opt_scale_y = 1.; +static double opt_sgrab_x = 1.; +static double opt_sgrab_y = 1.; static char opt_progress_granularity = 10; static char opt_progress_print = 0; static int opt_progress_delay = 0; @@ -38,15 +40,16 @@ static int opt_progress_delay = 0; #define HELP \ "Usage:\n" \ - " imlib2_view [OPTIONS] FILE...\n" \ + " imlib2_view [OPTIONS] {FILE | XID}...\n" \ "OPTIONS:\n" \ - " -c : Enable image caching\n" \ - " -d : Enable debug\n" \ - " -g N: Set progress granularity to N%% (default 10(%%))\n" \ - " -l N: Introduce N ms delay in progress callback (default 0)\n" \ - " -p : Print info in progress callback (default no)\n" \ - " -s S: Set scaling factor to S (default 1.0)\n" \ - " -v : Increase verbosity\n" + " -c : Enable image caching\n" \ + " -d : Enable debug\n" \ + " -g N : Set progress granularity to N%% (default 10(%%))\n" \ + " -l N : Introduce N ms delay in progress callback (default 0)\n" \ + " -p : Print info in progress callback (default no)\n" \ + " -s Sx[,Sy] : Set render x/y scaling factors to Sx,Sy (default 1.0)\n" \ + " -S Sx[,Sy] : Set grab x/y scaling factors to Sx,Sy (default 1.0)\n" \ + " -v : Increase verbosity\n" static void usage(void) @@ -117,7 +120,8 @@ progress(Imlib_Image im, char percent, int update_x, int update_y, if (pm) XFreePixmap(disp, pm); - pm = XCreatePixmap(disp, win, window_width, window_height, depth); + pm = XCreatePixmap(disp, win, window_width, window_height, + DefaultDepth(disp, DefaultScreen(disp))); imlib_context_set_drawable(pm); if (bg_im) { @@ -176,18 +180,62 @@ progress(Imlib_Image im, char percent, int update_x, int update_y, return 1; } +static Imlib_Image +load_image(int no, const char *name) +{ + Imlib_Image im; + char *ptr; + Drawable draw; + + Vprintf("Show %d: '%s'\n", no, name); + + image_width = 0; /* Force redraw in progress() */ + + draw = strtoul(name, &ptr, 0); + if (*ptr != '\0') + draw = 0; + + if (draw) + { + Window rr; + int x, y; + unsigned int w, h, bw; + unsigned int wo, ho; + unsigned int depth; + int get_alpha = 1; + + XGetGeometry(disp, draw, &rr, &x, &y, &w, &h, &bw, &depth); + + imlib_context_set_drawable(draw); + + Vprintf("Drawable: %#lx: x,y: %d,%d wxh=%ux%u bw=%u depth=%u\n", + draw, x, y, w, h, bw, depth); + + wo = w * opt_sgrab_x; + ho = h * opt_sgrab_y; + im = imlib_create_scaled_image_from_drawable(None, 0, 0, w, h, wo, ho, + 1, (get_alpha) ? 1 : 0); + + progress(im, 100, 0, 0, wo, ho); + } + else + { + im = imlib_load_image(name); + } + + return im; +} + int main(int argc, char **argv) { int opt; Imlib_Image *im; - char *file; int no, inc; - int verbose; verbose = 0; - while ((opt = getopt(argc, argv, "cdg:l:ps:v")) != -1) + while ((opt = getopt(argc, argv, "cdg:l:ps:S:v")) != -1) { switch (opt) { @@ -208,7 +256,16 @@ main(int argc, char **argv) break; case 's': /* Scale (window size wrt. image size) */ opt_scale = 1; - opt_scale_x = opt_scale_y = atof(optarg); + opt_scale_y = 0.f; + sscanf(optarg, "%lf,%lf", &opt_scale_x, &opt_scale_y); + if (opt_scale_y == 0.f) + opt_scale_y = opt_scale_x; + break; + case 'S': /* Scale on grab */ + opt_sgrab_y = 0.f; + sscanf(optarg, "%lf,%lf", &opt_sgrab_x, &opt_sgrab_y); + if (opt_sgrab_y == 0.f) + opt_sgrab_y = opt_sgrab_x; break; case 'v': verbose += 1; @@ -235,8 +292,6 @@ main(int argc, char **argv) return 1; } - depth = DefaultDepth(disp, DefaultScreen(disp)); - win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 10, 10, 0, 0, 0); XSelectInput(disp, win, KeyPressMask | ButtonPressMask | ButtonReleaseMask | @@ -260,10 +315,7 @@ main(int argc, char **argv) fprintf(stderr, "No loadable image\n"); exit(0); } - file = argv[no]; - Vprintf("Show %d: '%s'\n", no, file); - image_width = 0; - im = imlib_load_image(file); + im = load_image(no, argv[no]); } imlib_context_set_image(im); @@ -403,15 +455,12 @@ main(int argc, char **argv) inc = 1; continue; } - file = argv[no2]; - Vprintf("Show %d: '%s'\n", no2, file); if (no2 == no) break; - image_width = 0; - im2 = imlib_load_image(file); + im2 = load_image(no2, argv[no2]); if (!im2) { - Vprintf("*** Error loading image: %s\n", file); + Vprintf("*** Error loading image: %s\n", argv[no2]); continue; } zoom = 1.0;