photomosaics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit f1e77825b2929213aa2cab7cdefe5a188582ea32
parent e54815cd67e4ee484a0f3e87069ab4c0d11a1d8f
Author: Wilson Gheen <wilson@wilsonrgheen.com>
Date:   Sun, 25 Dec 2022 07:07:15 -0600

Move resizing into function

Diffstat:
Mphotomosaics.c | 53+++++++++++++++++++++++++++++++++++++----------------
1 file changed, 37 insertions(+), 16 deletions(-)

diff --git a/photomosaics.c b/photomosaics.c @@ -1,17 +1,50 @@ +#include <locale.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <MagickCore/MagickCore.h> +void resize_image(Image *images, ImageInfo *image_info, float resize_factor, char *new_filename, ExceptionInfo *exception) { + Image *image, *resize_image; + Image *thumbnails = NewImageList(); + while((image=RemoveFirstImageFromList(&images))) { + int new_width = image->columns * resize_factor; + int new_height = image->rows * resize_factor; + resize_image = ResizeImage(image, new_width, new_height, LanczosFilter, exception); + printf("%zu x %zu\n", resize_image->columns, resize_image->rows); + if(!resize_image) + MagickError(exception->severity, exception->reason, exception->description); + AppendImageToList(&thumbnails, resize_image); + DestroyImage(image); + } + + strcpy(thumbnails->filename, new_filename); + WriteImage(image_info, thumbnails, exception); + + DestroyImageList(thumbnails); +} + int main(int argc, char **argv) { ExceptionInfo *exception; - Image *image, *images, *resize_image, *thumbnails; + Image *images; ImageInfo *image_info; - if(argc != 3) { - fprintf(stdout, "Usage: %s old_image new_image\n", argv[0]); + float resize_factor = 0.0; + char *endptr; + if(argc == 4) { + const char *old_locale = setlocale(LC_ALL, NULL); + setlocale(LC_ALL|~LC_NUMERIC, ""); + resize_factor = strtof(argv[3], &endptr); + setlocale(LC_ALL, old_locale); + } + if(argc < 4 || !strncmp(argv[3], endptr, strlen(argv[3])) || resize_factor < 0.0) { + fprintf(stderr, "Usage: %s old_image new_image resize_factor\n", argv[0]); exit(2); } + if(resize_factor > 10.0) { + fprintf(stderr, "resize_factor %.1f is too big. Please don't break my computer.\n", resize_factor); + exit(1); + } MagickCoreGenesis(*argv, MagickTrue); exception = AcquireExceptionInfo(); @@ -22,20 +55,8 @@ int main(int argc, char **argv) { CatchException(exception); if(!images) exit(1); + resize_image(images, image_info, resize_factor, argv[2], exception); - thumbnails = NewImageList(); - while((image=RemoveFirstImageFromList(&images))) { - resize_image = ResizeImage(image, 237, 282, LanczosFilter, exception); - if(!resize_image) - MagickError(exception->severity, exception->reason, exception->description); - AppendImageToList(&thumbnails, resize_image); - DestroyImage(image); - } - - strcpy(thumbnails->filename, argv[2]); - WriteImage(image_info, thumbnails, exception); - - DestroyImageList(thumbnails); DestroyImageInfo(image_info); DestroyExceptionInfo(exception); MagickCoreTerminus();