From a174ef295e8880898afd39c475eab1a82f99ba55 Mon Sep 17 00:00:00 2001 From: Rentib Date: Sun, 20 Oct 2024 14:42:10 +0200 Subject: [PATCH] feat: recreate thumbnail if metadata changed Signed-off-by: Rentib --- src/thumbnail.c | 39 ++++++++++++++++++++++++++++++++++----- src/thumbnail.h | 15 +++++++++------ 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/thumbnail.c b/src/thumbnail.c index 1c1d0d3..aa9d1f5 100644 --- a/src/thumbnail.c +++ b/src/thumbnail.c @@ -113,8 +113,6 @@ bool thumbnail_save(const struct pixmap* thumb, const char* source, uint32_t i; char path[PATH_MAX] = { 0 }; - (void)params; - if (!get_thumb_path(path, source)) { return false; } @@ -127,8 +125,12 @@ bool thumbnail_save(const struct pixmap* thumb, const char* source, return false; } - // TODO: add alpha channel fprintf(fp, "P6\n%zu %zu\n255\n", thumb->width, thumb->height); + /* comment to store params */ + fwrite("#", 1, 1, fp); + fwrite(params, sizeof(struct thumbnail_params), 1, fp); + fwrite("\n", 1, 1, fp); + // TODO: add alpha channel for (i = 0; i < thumb->width * thumb->height; ++i) { uint8_t color[] = { (((thumb->data[i] >> (8 * 2)) & 0xff)), (((thumb->data[i] >> (8 * 1)) & 0xff)), @@ -153,8 +155,7 @@ bool thumbnail_load(struct pixmap* thumb, const char* source, uint32_t i; char path[PATH_MAX] = { 0 }; struct stat attr_img, attr_thumb; - - (void)params; + struct thumbnail_params saved_params; if (!get_thumb_path(path, source) || stat(source, &attr_img) || stat(path, &attr_thumb) || @@ -180,6 +181,34 @@ bool thumbnail_load(struct pixmap* thumb, const char* source, goto fail; } + /* comment to store params */ + fread(header, 1, 1, fp); // '#' + fread(&saved_params, sizeof(struct thumbnail_params), 1, fp); + fread(header, 1, 1, fp); // '\n' + + if (memcmp(params, &saved_params, sizeof(struct thumbnail_params))) { + fprintf(stderr, "Thumbnail params mismatch\n"); + fprintf(stderr, "Expected: %zu %zu %zu %zu %d %d %f\n", + params->thumb_width, params->thumb_height, params->offset_x, + params->offset_y, params->fill, params->antialias, + params->scale); + fprintf(stderr, "Got: %zu %zu %zu %zu %d %d %f\n", + saved_params.thumb_width, saved_params.thumb_height, + saved_params.offset_x, saved_params.offset_y, saved_params.fill, + saved_params.antialias, saved_params.scale); + // print raw memory + fprintf(stderr, "Expected: "); + for (size_t i = 0; i < sizeof(struct thumbnail_params); i++) { + fprintf(stderr, "%02x", ((uint8_t*)params)[i]); + } + fprintf(stderr, "\nGot: "); + for (size_t i = 0; i < sizeof(struct thumbnail_params); i++) { + fprintf(stderr, "%02x", ((uint8_t*)&saved_params)[i]); + } + fprintf(stderr, "\n"); + goto fail; + } + /* NOTE: It might be that we want the pixmap to have exact width and height * and return false if it doesn't match */ if (!pixmap_create(thumb, thumb->width, thumb->height)) { diff --git a/src/thumbnail.h b/src/thumbnail.h index 34a2dce..30ffe1a 100644 --- a/src/thumbnail.h +++ b/src/thumbnail.h @@ -6,12 +6,15 @@ #include "image.h" #include "pixmap.h" -struct thumbnail_params { - size_t thumb_width, thumb_height; - ssize_t offset_x, offset_y; - bool fill; - bool antialias; - float scale; +/** Thumbnail parameters. + * It is packed because we write it and we don't want any padding. + */ +struct __attribute__((packed)) thumbnail_params { + size_t thumb_width, thumb_height; ///< Thumbnail size + ssize_t offset_x, offset_y; ///< Offset to center thumbnail + bool fill; ///< Scale mode (fill/fit) + bool antialias; ///< Use antialiasing + float scale; ///< Scale factor }; /**