From 1ca66919f1e3a2d4a11fac5df90ca8ae9240318b Mon Sep 17 00:00:00 2001 From: speedie Date: Tue, 28 Feb 2023 22:36:33 +0100 Subject: [PATCH] image sizes can now be much, much larger without issue --- libs/img.c | 38 +++++++++++++++++++++++--------------- options.h | 3 ++- spmenu.c | 42 +++++++++++++++++++++++++++--------------- xresources.h | 3 ++- 4 files changed, 54 insertions(+), 32 deletions(-) diff --git a/libs/img.c b/libs/img.c index b75b0be..68d097d 100644 --- a/libs/img.c +++ b/libs/img.c @@ -45,29 +45,35 @@ loadimage(const char *file, int *width, int *height) } void -scaleimage(int width, int height) +scaleimage(int *width, int *height) { int nwidth, nheight; float aspect = 1.0f; - if (width > height) - aspect = (float)imagesize/width; + if (imagewidth > *width) + aspect = (float)(*width)/imagewidth; else - aspect = (float)imagesize/height; + aspect = (float)imagewidth/(*width); - nwidth = width * aspect; - nheight = height * aspect; + nwidth = *width * aspect; + nheight = *height * aspect; - if(nwidth == width && nheight == height) + if(nwidth == *width && nheight == *height) return; - image = imlib_create_cropped_scaled_image(0,0,width,height,nwidth,nheight); + image = imlib_create_cropped_scaled_image(0,0,*width,*height,nwidth,nheight); + imlib_free_image(); - if (!image) + if(!image) return; imlib_context_set_image(image); + + *width = nwidth; + *height = nheight; + + return; } void @@ -80,8 +86,10 @@ loadimagecache(const char *file, int *width, int *height) struct passwd *pw = NULL; /* just load and don't store or try cache */ - if(imagesize > 256) { + if (longestedge > 256) { loadimage(file, width, height); + if (image) + scaleimage(width, height); return; } @@ -97,7 +105,7 @@ loadimagecache(const char *file, int *width, int *height) /* which cache do we try? */ dsize = "normal"; - if (imagesize > 128) + if (longestedge > 128) dsize = "large"; slen = snprintf(NULL, 0, "file://%s", file)+1; @@ -134,11 +142,11 @@ loadimagecache(const char *file, int *width, int *height) loadimage(buf, width, height); - if (image && *width < imagesize && *height < imagesize) { + if (image && *width < imagewidth && *height < imageheight) { imlib_free_image(); image = NULL; - } else if(image && (*width > imagesize || *height > imagesize)) { - scaleimage(*width, *height); + } else if(image && (*width > imagewidth || *height > imageheight)) { + scaleimage(width, height); } /* we are done */ @@ -154,7 +162,7 @@ loadimagecache(const char *file, int *width, int *height) return; } - scaleimage(*width, *height); + scaleimage(width, height); imlib_image_set_format("png"); createifnexist_rec(buf); imlib_save_image(buf); diff --git a/options.h b/options.h index 47015df..8e99697 100644 --- a/options.h +++ b/options.h @@ -16,7 +16,8 @@ static int minwidth = 500; /* Minimum width */ static int centered = 0; /* Whether or not to center spmenu by default */ /* Image options */ -static int imagesize = 86; /* Default image size */ +static int imagewidth = 86; /* Default image width */ +static int imageheight = 86; /* Default image height */ static int imagegaps = 0; /* Image gaps */ static int imageposition = 0; /* Image position (0: Top, 1: Bottom, 2: Center, 3: Top center) */ static int generatecache = 0; /* Generate image cache by default */ diff --git a/spmenu.c b/spmenu.c index c7f3132..4d58163 100644 --- a/spmenu.c +++ b/spmenu.c @@ -168,7 +168,6 @@ static void drawmenu(void); #include "colors.h" /* Include colors */ #include "xresources.h" /* Include .Xresources */ - static char * cistrstr(const char *s, const char *sub); static int (*fstrncmp)(const char *, const char *, size_t) = strncasecmp; static char *(*fstrstr)(const char *, const char *) = cistrstr; @@ -177,6 +176,8 @@ static char *(*fstrstr)(const char *, const char *) = cistrstr; #include "libs/mode.c" #if USEIMAGE +static int longestedge = 0; /* longest edge */ + #include "libs/img.h" #include "libs/img.c" #endif @@ -472,8 +473,8 @@ drawmenu(void) if (lines > 0) { #if USEIMAGE - if (imagesize) - x += imagegaps + imagesize; + if (imagewidth) + x += imagegaps + imagewidth; #endif /* draw grid */ int i = 0; @@ -1423,7 +1424,7 @@ readstdin(void) items[i].image = NULL; } - if (generatecache && imagesize <= 256 && items[i].image && strcmp(items[i].image, limg?limg:"")) { + if (generatecache && longestedge <= 256 && items[i].image && strcmp(items[i].image, limg?limg:"")) { loadimagecache(items[i].image, &w, &h); fprintf(stdout, "-!- Generating thumbnail for: %s\n", items[i].image); } @@ -1441,13 +1442,13 @@ readstdin(void) } #if USEIMAGE if (!limg) - imagesize = 0; + longestedge = imagegaps = 0; #endif inputw = items ? TEXTWM(items[imax].text) : 0; lines = MIN(lines, i); #if USEIMAGE - if(lines * drw->font->h < imagesize) - lines = imagesize/drw->font->h+2; + if(lines * drw->font->h < imagewidth) + lines = imagewidth/drw->font->h+2; #endif } @@ -1499,23 +1500,27 @@ run(void) continue; if (sel && sel->image && strcmp(sel->image, limg ? limg : "")) { - if (imagesize) + if (longestedge) loadimagecache(sel->image, &width, &height); } else if ((!sel || !sel->image) && image) { imlib_free_image(); image = NULL; - } if (image && imagesize) { + } if (image && longestedge) { int leftmargin = imagegaps; if (!imageposition) { /* top mode = 0 */ - imlib_render_image_on_drawable(leftmargin+(imagesize-width)/2, bh+imagegaps); + if (height > width) + width = height; + imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2, bh+imagegaps); } else if (imageposition == 1) { /* bottom mode = 1 */ - imlib_render_image_on_drawable(leftmargin+(imagesize-width)/2, mh-height-imagegaps); + if (height > width) + width = height; + imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2, mh-height-imagegaps); } else if (imageposition == 2) { /* center mode = 2 */ - imlib_render_image_on_drawable(leftmargin+(imagesize-width)/2, (mh-bh-height)/2+bh); + imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2, (mh-bh-height)/2+bh); } else { - int minh = MIN(imagesize, mh-bh-imagegaps*2); - imlib_render_image_on_drawable(leftmargin+(imagesize-width)/2, (minh-height)/2+bh+imagegaps); + int minh = MIN(height, mh-bh-imagegaps*2); + imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2, (minh-height)/2+bh+imagegaps); } @@ -2015,7 +2020,12 @@ main(int argc, char *argv[]) colors[SchemeInput][ColBg] = argv[++i]; colors[SchemePrompt][ColFg] = argv[++i]; } else if (!strcmp(argv[i], "-is")) { /* image size */ - imagesize = atoi(argv[++i]); + char buf[255]; + memset(buf, 0, sizeof(buf)); + memcpy(buf, argv[++i], sizeof(buf)-1); + + if(sscanf(buf, "%dx%d", &imagewidth, &imageheight) == 1) + imageheight = imagewidth; /* spmenu colors */ } else if (!strcmp(argv[i], "-nif")) { /* normal item foreground color */ @@ -2092,6 +2102,8 @@ main(int argc, char *argv[]) else usage(); + longestedge = MAX(imagewidth, imageheight); + if (mode) { selkeys = 1; allowkeys = 1; diff --git a/xresources.h b/xresources.h index cb65037..b8d5582 100644 --- a/xresources.h +++ b/xresources.h @@ -115,7 +115,8 @@ ResourcePref resources[] = { { "hidemode", INTEGER, &hidemode }, { "histnodup", INTEGER, &histnodup }, { "casesensitive", INTEGER, &casesensitive }, - { "imagesize", INTEGER, &imagesize }, + { "imagewidth", INTEGER, &imagewidth }, + { "imageheight", INTEGER, &imageheight }, { "imagegaps", INTEGER, &imagegaps }, { "imageposition", INTEGER, &imageposition }, { "generatecache", INTEGER, &generatecache },