From 428216caffe8e04b1959b7171ad2de9ebb540c50 Mon Sep 17 00:00:00 2001 From: foswret Date: Tue, 12 May 2026 19:02:50 -0500 Subject: switch from pkg-config to pkgconf, pull in MagickWand library, move image-related functions to image.h, create resize_image(), get_image_width() and get_image_height --- src/calp.c | 10 +-- src/date.o | Bin 4464 -> 4472 bytes src/draw.c | 54 +-------------- src/draw.h | 4 -- src/image.c | 222 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/image.h | 13 ++++ src/image.o | Bin 0 -> 38440 bytes 7 files changed, 242 insertions(+), 61 deletions(-) create mode 100644 src/image.c create mode 100644 src/image.h create mode 100644 src/image.o (limited to 'src') diff --git a/src/calp.c b/src/calp.c index 2d155cc..3f81aff 100644 --- a/src/calp.c +++ b/src/calp.c @@ -2,12 +2,14 @@ #include #include +#include #include #include #include "draw.h" #include "date.h" #include "color.h" +#include "image.h" // Metric #define A4_WIDTH 210 @@ -51,7 +53,7 @@ int main (void) { days_in_month[1]++; } - int verbose_output = 1; + int verbose_output = 0; // TBD: Add ability to choose filename @@ -109,7 +111,6 @@ int main (void) { month.num_of_days = days_in_month[i]; month.month_position = i; month.minimum_rows = calculate_minimum_rows(&month, &dim); - printf("month.minimum_rows: %d\n", month.minimum_rows); dim.header_width = dim.paper_width - (dim.margin * 2.0); dim.header_height = dim.paper_height - (dim.margin * 3.0) - (month.minimum_rows * dim.day_height); @@ -123,13 +124,12 @@ int main (void) { set_color(ch, BLACK); draw_month_title(ch, &dim, "Sans", 40, months[i]); - printf("header width: %f\n", dim.header_width); - printf("header height: %f\n", dim.header_height); - char path[20]; snprintf(path, 20, "assets/%d.png", i + 1); + prepare_month_art(path, &dim); + if (verbose_output) { printf("Drawing month \'%s\'\n", months[i]); } diff --git a/src/date.o b/src/date.o index 5fdd91f..9af799f 100644 Binary files a/src/date.o and b/src/date.o differ diff --git a/src/draw.c b/src/draw.c index bdc62af..e475b32 100644 --- a/src/draw.c +++ b/src/draw.c @@ -203,8 +203,8 @@ int print_dimensions(struct dimensions *d) { printf("month_top_corner_y: %f\n", d->month_top_corner_y); printf("day width: %.2f\n", d->day_width); printf("day height: %.2f\n", d->day_height); - printf("header width: %.2f\n", d->header_width); - printf("header height: %.2f\n", d->header_height); + //printf("header width: %.2f\n", d->header_width); + //printf("header height: %.2f\n", d->header_height); return 0; } @@ -359,56 +359,6 @@ int draw_month_title(cairo_t *c, struct dimensions *d, char *font_family, int fo } - - -// Draw an image at a specified point -int draw_image(cairo_t *c, char *path, double x, double y) { - cairo_surface_t *image; - - // Checks if the file exists. If it doesn't, warn the user that it was not found. - // A file not existing will not terminate the program, but simply not render the image. - FILE *file; - if((file = fopen(path, "r")) != NULL) - { - // file exists - fclose(file); - } - else - { - printf("Warning: image \'%s\' does not exist.\n", path); - return 1; - } - - image = cairo_image_surface_create_from_png(path); - cairo_set_source_surface(c, image, x, y); - cairo_paint(c); - - cairo_surface_destroy (image); - return 0; -} - - - - -// Draw month art -int draw_month_art(cairo_t *c, struct dimensions *d, char *path) { - cairo_save(c); - double width = d->header_width; - // TODO: Make this relative to text height - double height = d->header_height - (d->margin * 3.0); - - double cursor_x = d->margin; - double cursor_y = d->margin; - cairo_rectangle (c, d->margin, d->margin, width, height); - cairo_clip(c); - - - draw_image(c, path, cursor_x, cursor_y); - cairo_restore(c); - return 0; -} - - //int draw_weekday_names(cairo_t *c, struct dimensions *d) { // //} diff --git a/src/draw.h b/src/draw.h index 40ca609..51855bf 100644 --- a/src/draw.h +++ b/src/draw.h @@ -51,8 +51,4 @@ PangoRectangle get_logical_extents (cairo_t *c, char *font_family, int font_size int calculate_minimum_rows(struct month_info *m, struct dimensions *d); -int draw_image(cairo_t *c, char *path, double x, double y); - -int draw_month_art(cairo_t *c, struct dimensions *d, char *path); - int construct_day_box(cairo_t *cd, int month, int day, int week_day); diff --git a/src/image.c b/src/image.c new file mode 100644 index 0000000..4295ab1 --- /dev/null +++ b/src/image.c @@ -0,0 +1,222 @@ +#include +#include +#include +#include +#include +#include "draw.h" + + +// Used for MagickWand +#define ThrowWandException(wand) \ +{ \ + char \ + *description; \ + \ + ExceptionType \ + severity; \ + \ + description=MagickGetException(wand,&severity); \ + (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \ + description=(char *) MagickRelinquishMemory(description); \ + exit(-1); \ +} + + + +// Draw an image at a specific point +int draw_image(cairo_t *c, char *path, double x, double y) { + cairo_surface_t *image; + + // Checks if the file exists. If it doesn't, warn the user that it was not found. + // A file not existing will not terminate the program, but simply not render the image. + FILE *file; + if((file = fopen(path, "r")) != NULL) + { + // file exists + fclose(file); + } + else + { + printf("Warning: image \'%s\' does not exist.\n", path); + return 1; + } + + image = cairo_image_surface_create_from_png(path); + cairo_set_source_surface(c, image, x, y); + cairo_paint(c); + + cairo_surface_destroy (image); + return 0; +} + + + + + + +unsigned long get_image_width(char *path) { + MagickBooleanType status; + MagickWand *wand; + + MagickWandGenesis(); + wand = NewMagickWand(); + status = MagickReadImage(wand, path); + + if (status == MagickFalse) { + ThrowWandException(wand); + } + + unsigned long width = MagickGetImageWidth(wand); + + wand = DestroyMagickWand(wand); + MagickWandTerminus(); + return width; +} + + + +unsigned long get_image_height(char *path) { + MagickBooleanType status; + MagickWand *wand; + + MagickWandGenesis(); + wand = NewMagickWand(); + status = MagickReadImage(wand, path); + + if (status == MagickFalse) { + ThrowWandException(wand); + } + + unsigned long height = MagickGetImageHeight(wand); + + wand = DestroyMagickWand(wand); + MagickWandTerminus(); + return height; +} + + + + + +// Resize an image to a specific width and height. +// Optionally create a new file to preserve the source image. +int resize_image(char *path, double width, double height, bool create_new) { + MagickBooleanType status; + MagickWand *wand; + + MagickWandGenesis(); + wand = NewMagickWand(); + status = MagickReadImage(wand, path); + + if (status == MagickFalse) { + ThrowWandException(wand); + } + + MagickResetIterator(wand); + + MagickResizeImage(wand, width, height, LanczosFilter, 1.0); + + if (create_new == true) { + char new_path[30]; + snprintf(new_path, sizeof(new_path), "%s.tmp", path); + status = MagickWriteImages(wand, new_path, MagickTrue); + } else { + status = MagickWriteImages(wand, path, MagickTrue); + } + + if (status == MagickFalse) { + ThrowWandException(wand); + } + wand = DestroyMagickWand(wand); + MagickWandTerminus(); + return 0; +} + + +// Create a cropped version of an image to a certain dimensions and position. +// Optionally create a new file to preserve the source image. +int crop_image(char *path, double width, double height, double x, double y, bool create_new) { + MagickBooleanType status; + MagickWand *wand; + + MagickWandGenesis(); + wand = NewMagickWand(); + status = MagickReadImage(wand, path); + + if (status == MagickFalse) { + ThrowWandException(wand); + } + + MagickResetIterator(wand); + + MagickCropImage(wand, width, height, x, y); + + if (create_new == true) { + char new_path[30]; + snprintf(new_path, sizeof(new_path), "%s.tmp", path); + status = MagickWriteImages(wand, new_path, MagickTrue); + } else { + status = MagickWriteImages(wand, path, MagickTrue); + } + + if (status == MagickFalse) { + ThrowWandException(wand); + } + wand = DestroyMagickWand(wand); + MagickWandTerminus(); + return 0; +} + + +int prepare_month_art(char *path, struct dimensions *d) { + if (!d->header_width || !d->header_height) { + printf("Error: header_width and header_height unknown.\n"); + return 1; + } + + double desired_ratio = d->header_width / d->header_height; + + unsigned long width = get_image_width(path); + unsigned long height = get_image_height(path); + //double original_ratio = width / height; + + + // Landscape + if (desired_ratio > 1.0) { + } + + // Portrait + if (desired_ratio < 1.0) { + } + + + + printf("%lu x %lu\n", width, height); + + + return 0; + +} + + + + +// Draw month art +int draw_month_art(cairo_t *c, struct dimensions *d, char *path) { + cairo_save(c); + double width = d->header_width; + // TODO: Make this relative to text height + double height = d->header_height - (d->margin * 3.0); + + double cursor_x = d->margin; + double cursor_y = d->margin; + cairo_rectangle (c, d->margin, d->margin, width, height); + cairo_clip(c); + + + draw_image(c, path, cursor_x, cursor_y); + cairo_restore(c); + return 0; +} + + diff --git a/src/image.h b/src/image.h new file mode 100644 index 0000000..117efaa --- /dev/null +++ b/src/image.h @@ -0,0 +1,13 @@ +int draw_image(cairo_t *c, char *path, double x, double y); + +int draw_month_art(cairo_t *c, struct dimensions *d, char *path); + +int resize_image(char *path, double width, double height, bool create_new); + +int crop_image(char *path, double width, double height, double x, double y, bool create_new); + +unsigned long get_image_width(char *path); + +unsigned long get_image_height(char *path); + +int prepare_month_art(char *path, struct dimensions *d); diff --git a/src/image.o b/src/image.o new file mode 100644 index 0000000..107752b Binary files /dev/null and b/src/image.o differ -- cgit v1.2.3