Skip to content

Commit

Permalink
feat: add thumbnail params
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 a05d1de commit 437f249
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 34 deletions.
11 changes: 7 additions & 4 deletions src/gallery.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ static void add_thumbnail(struct image* image)
{
struct thumbnail* entry = malloc(sizeof(*entry));
struct pixmap thumb;
struct thumbnail_params params;

/* TODO: move to config */
bool thumbnails_disk_cache = true;
Expand All @@ -84,11 +85,13 @@ static void add_thumbnail(struct image* image)
entry->width = image->frames[0].pm.width;
entry->height = image->frames[0].pm.height;
entry->image = image;
if (!thumbnails_disk_cache || !thumbnail_load(&thumb, image->source)) {
thumbnail_create(&thumb, image, ctx.thumb_size, ctx.thumb_fill,
ctx.thumb_aa);
thumbnail_params(&params, image, ctx.thumb_size, ctx.thumb_fill,
ctx.thumb_aa);
if (!thumbnails_disk_cache ||
!thumbnail_load(&thumb, image->source, &params)) {
thumbnail_create(&thumb, image, &params);
if (thumbnails_disk_cache) {
thumbnail_save(&thumb, image->source);
thumbnail_save(&thumb, image->source, &params);
}
}
image_thumbnail(image, &thumb);
Expand Down
76 changes: 51 additions & 25 deletions src/thumbnail.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Copyright (C) 2021 Artem Senichev <[email protected]>
// Copyright (C) 2024 Rentib <[email protected]>

#include "thumbnail.h"

#include "image.h"
#include "pixmap.h"

Expand Down Expand Up @@ -50,7 +52,7 @@ static bool make_directories(char* path)
return true;
}

bool get_thumb_path(char* path, const char* source)
static bool get_thumb_path(char* path, const char* source)
{
static char* cache_dir = NULL;
int r;
Expand All @@ -70,12 +72,49 @@ bool get_thumb_path(char* path, const char* source)
return r >= 0 && r < PATH_MAX;
}

bool thumbnail_save(struct pixmap* thumb, const char* source)
void thumbnail_params(struct thumbnail_params* params,
const struct image* image, size_t size, bool fill,
bool antialias)
{
const struct pixmap* full = &image->frames[0].pm;
const float scale_width = 1.0 / ((float)full->width / size);
const float scale_height = 1.0 / ((float)full->height / size);
const float scale =
fill ? max(scale_width, scale_height) : min(scale_width, scale_height);
size_t thumb_width = scale * full->width;
size_t thumb_height = scale * full->height;
ssize_t offset_x, offset_y;

if (fill) {
offset_x = size / 2 - thumb_width / 2;
offset_y = size / 2 - thumb_height / 2;
thumb_width = size;
thumb_height = size;
} else {
offset_x = 0;
offset_y = 0;
}

*params = (struct thumbnail_params) {
.thumb_width = thumb_width,
.thumb_height = thumb_height,
.offset_x = offset_x,
.offset_y = offset_y,
.fill = fill,
.antialias = antialias,
.scale = scale,
};
}

bool thumbnail_save(const struct pixmap* thumb, const char* source,
const struct thumbnail_params* params)
{
FILE* fp;
uint32_t i;
char path[PATH_MAX] = { 0 };

(void)params;

if (!get_thumb_path(path, source)) {
return false;
}
Expand Down Expand Up @@ -107,13 +146,16 @@ bool thumbnail_save(struct pixmap* thumb, const char* source)
* @param path path to load from
* @return true pixmap was loaded
*/
bool thumbnail_load(struct pixmap* thumb, const char* source)
bool thumbnail_load(struct pixmap* thumb, const char* source,
const struct thumbnail_params* params)
{
FILE* fp;
uint32_t i;
char path[PATH_MAX] = { 0 };
struct stat attr_img, attr_thumb;

(void)params;

if (!get_thumb_path(path, source) || stat(source, &attr_img) ||
stat(path, &attr_thumb) ||
difftime(attr_img.st_ctime, attr_thumb.st_ctime) > 0) {
Expand Down Expand Up @@ -162,39 +204,23 @@ bool thumbnail_load(struct pixmap* thumb, const char* source)
}

bool thumbnail_create(struct pixmap* thumb, const struct image* image,
size_t size, bool fill, bool antialias)
const struct thumbnail_params* params)
{
const struct pixmap* full = &image->frames[0].pm;
const float scale_width = 1.0 / ((float)full->width / size);
const float scale_height = 1.0 / ((float)full->height / size);
const float scale =
fill ? max(scale_width, scale_height) : min(scale_width, scale_height);
size_t thumb_width = scale * full->width;
size_t thumb_height = scale * full->height;
ssize_t offset_x, offset_y;
enum pixmap_scale scaler;

if (antialias) {
scaler = (scale > 1.0) ? pixmap_bicubic : pixmap_average;
if (params->antialias) {
scaler = (params->scale > 1.0) ? pixmap_bicubic : pixmap_average;
} else {
scaler = pixmap_nearest;
}

if (fill) {
offset_x = size / 2 - thumb_width / 2;
offset_y = size / 2 - thumb_height / 2;
thumb_width = size;
thumb_height = size;
} else {
offset_x = 0;
offset_y = 0;
}

// create thumbnail
if (!pixmap_create(thumb, thumb_width, thumb_height)) {
if (!pixmap_create(thumb, params->thumb_width, params->thumb_height)) {
return false;
}
pixmap_scale(scaler, full, thumb, offset_x, offset_y, scale, image->alpha);
pixmap_scale(scaler, full, thumb, params->offset_x, params->offset_y,
params->scale, image->alpha);

return true;
}
32 changes: 27 additions & 5 deletions src/thumbnail.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,52 @@
#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;
};

/**
* Create thumbnail from full size image.
* @param thumbnail pixmap to store thumbnail
* Sets up thumbnail parameters.
* @param params thumbnail parameters
* @param image original image
* @param size thumbnail size in pixels
* @param fill thumbnail scale mode (fill/fit)
* @param antialias use antialiasing
*/
void thumbnail_params(struct thumbnail_params* params,
const struct image* image, size_t size, bool fill,
bool antialias);

/**
* Create thumbnail from full size image.
* @param thumbnail pixmap to store thumbnail
* @param image original image
* @param params thumbnail parameters
* @return true if successful
*/
bool thumbnail_create(struct pixmap* thumbnail, const struct image* image,
size_t size, bool fill, bool antialias);
const struct thumbnail_params* params);

/**
* Load thumbnail from disk.
* @param thumbnail pixmap to store thumbnail
* @param source source of image
* @param params thumbnail parameters
* @return true if successful
*/
bool thumbnail_load(struct pixmap* thumbnail, const char* source);
bool thumbnail_load(struct pixmap* thumbnail, const char* source,
const struct thumbnail_params* params);

/**
* Save thumbnail on disk.
* @param thumbnail pixmap with thumbnail
* @param source source of image
* @param params thumbnail parameters
* @return true if successful
*/
bool thumbnail_save(const struct pixmap* thumbnail, const char* source);
bool thumbnail_save(const struct pixmap* thumbnail, const char* source,
const struct thumbnail_params* params);

0 comments on commit 437f249

Please sign in to comment.