diff --git a/doc/tools.md b/doc/tools.md index 78506b9d..4afc5d0b 100644 --- a/doc/tools.md +++ b/doc/tools.md @@ -65,6 +65,7 @@ Options: (default: 0 100) -crop .. crop picture with the given rectangle -resize ........ resize picture (*after* any cropping) +-resize_mode .. one of: up_only, down_only, always (default) -mt .................... use multi-threading if available -low_memory ............ reduce memory usage (slower encoding) -map ............. print map of extra info diff --git a/examples/cwebp.c b/examples/cwebp.c index 716a1117..deccffc2 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -515,6 +515,37 @@ static int WriteWebPWithMetadata(FILE* const out, return (fwrite(webp, webp_size, 1, out) == 1); } +//------------------------------------------------------------------------------ +// Resize + +enum { + RESIZE_MODE_DOWN_ONLY, + RESIZE_MODE_UP_ONLY, + RESIZE_MODE_ALWAYS, + RESIZE_MODE_DEFAULT = RESIZE_MODE_ALWAYS +}; + +static void ApplyResizeMode(const int resize_mode, + const WebPPicture* const pic, + int* const resize_w, int* const resize_h) { + const int src_w = pic->width; + const int src_h = pic->height; + const int dst_w = *resize_w; + const int dst_h = *resize_h; + + if (resize_mode == RESIZE_MODE_DOWN_ONLY) { + if ((dst_w == 0 && src_h <= dst_h) || + (dst_h == 0 && src_w <= dst_w) || + (src_w <= dst_w && src_h <= dst_h)) { + *resize_w = *resize_h = 0; + } + } else if (resize_mode == RESIZE_MODE_UP_ONLY) { + if (src_w >= dst_w && src_h >= dst_h) { + *resize_w = *resize_h = 0; + } + } +} + //------------------------------------------------------------------------------ static int ProgressReport(int percent, const WebPPicture* const picture) { @@ -583,6 +614,8 @@ static void HelpLong(void) { " (default: 0 100)\n"); printf(" -crop .. crop picture with the given rectangle\n"); printf(" -resize ........ resize picture (*after* any cropping)\n"); + printf(" -resize_mode .. one of: up_only, down_only," + " always (default)\n"); printf(" -mt .................... use multi-threading if available\n"); printf(" -low_memory ............ reduce memory usage (slower encoding)\n"); printf(" -map ............. print map of extra info\n"); @@ -670,6 +703,7 @@ int main(int argc, const char* argv[]) { uint32_t background_color = 0xffffffu; int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0; int resize_w = 0, resize_h = 0; + int resize_mode = RESIZE_MODE_DEFAULT; int lossless_preset = 6; int use_lossless_preset = -1; // -1=unset, 0=don't use, 1=use it int show_progress = 0; @@ -837,6 +871,18 @@ int main(int argc, const char* argv[]) { } else if (!strcmp(argv[c], "-resize") && c + 2 < argc) { resize_w = ExUtilGetInt(argv[++c], 0, &parse_error); resize_h = ExUtilGetInt(argv[++c], 0, &parse_error); + } else if (!strcmp(argv[c], "-resize_mode") && c + 1 < argc) { + ++c; + if (!strcmp(argv[c], "down_only")) { + resize_mode = RESIZE_MODE_DOWN_ONLY; + } else if (!strcmp(argv[c], "up_only")) { + resize_mode = RESIZE_MODE_UP_ONLY; + } else if (!strcmp(argv[c], "always")) { + resize_mode = RESIZE_MODE_ALWAYS; + } else { + fprintf(stderr, "Error! Unrecognized resize mode: %s\n", argv[c]); + goto Error; + } #ifndef WEBP_DLL } else if (!strcmp(argv[c], "-noasm")) { VP8GetCPUInfo = NULL; @@ -1057,6 +1103,7 @@ int main(int argc, const char* argv[]) { goto Error; } } + ApplyResizeMode(resize_mode, &picture, &resize_w, &resize_h); if ((resize_w | resize_h) > 0) { WebPPicture picture_no_alpha; if (config.exact) { diff --git a/man/cwebp.1 b/man/cwebp.1 index b4bf4f9d..d8164048 100644 --- a/man/cwebp.1 +++ b/man/cwebp.1 @@ -1,5 +1,5 @@ .\" Hey, EMACS: -*- nroff -*- -.TH CWEBP 1 "September 17, 2024" +.TH CWEBP 1 "April 10, 2025" .SH NAME cwebp \- compress an image file to a WebP file .SH SYNOPSIS @@ -102,6 +102,14 @@ If either (but not both) of the \fBwidth\fP or \fBheight\fP parameters is 0, the value will be calculated preserving the aspect\-ratio. Note: scaling is applied \fIafter\fP cropping. .TP +.BI \-resize_mode " string +Specify the behavior of the \fB\-resize\fP option. Possible values are: +\fBdown_only\fP, \fBup_only\fP, \fBalways\fP (default). \fBdown_only\fP will +use the values specified by \fB\-resize\fP if \fIeither\fP the input width or +height are larger than the given dimensions. Similarly, \fBup_only\fP will only +resize if \fIeither\fP the input width or height are smaller than the given +dimensions. +.TP .B \-mt Use multi\-threading for encoding, if possible. .TP