Skip to content

Commit

Permalink
feat: recreate thumbnail if metadata changed
Browse files Browse the repository at this point in the history
Signed-off-by: Rentib <[email protected]>
  • Loading branch information
Rentib committed Oct 20, 2024
1 parent 437f249 commit ad5f58f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/thumbnail.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)),
Expand All @@ -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) ||
Expand All @@ -180,6 +181,15 @@ 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))) {
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)) {
Expand Down
15 changes: 9 additions & 6 deletions src/thumbnail.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/**
Expand Down

0 comments on commit ad5f58f

Please sign in to comment.