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 a174ef2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
39 changes: 34 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,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)) {
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 a174ef2

Please sign in to comment.