Fix sticker resizing.
So far, a sticker of size 1028x1028 would have been resized to 128x128, even though the maximum was set to 256x256. This fix introduces a different way of resizing, which means that any dimension of length >256 will result =256. Also, I factored out the maximum size, so maybe we can make this an option in the future.
This commit is contained in:
parent
e26416bc49
commit
cac87f92b8
3 changed files with 31 additions and 13 deletions
37
tgp-2prpl.c
37
tgp-2prpl.c
|
@ -468,6 +468,10 @@ int p2tgl_imgstore_add_with_id (const char* filename) {
|
|||
}
|
||||
|
||||
#ifdef HAVE_LIBWEBP
|
||||
|
||||
static const int MAX_W = 256;
|
||||
static const int MAX_H = 256;
|
||||
|
||||
int p2tgl_imgstore_add_with_id_webp (const char *filename) {
|
||||
|
||||
const uint8_t *data = NULL;
|
||||
|
@ -484,15 +488,28 @@ int p2tgl_imgstore_add_with_id_webp (const char *filename) {
|
|||
g_free ((gchar *)data);
|
||||
return 0;
|
||||
}
|
||||
int H = config.input.height;
|
||||
int W = config.input.width;
|
||||
while (H > 256 || W > 256) {
|
||||
H /= 2;
|
||||
W /= 2;
|
||||
|
||||
config.options.use_scaling = 0;
|
||||
config.options.scaled_width = config.input.width;
|
||||
config.options.scaled_height = config.input.height;
|
||||
if (config.options.scaled_width > MAX_W || config.options.scaled_height > MAX_H) {
|
||||
const float max_scale_width = MAX_W * 1.0f / config.options.scaled_width;
|
||||
const float max_scale_height = MAX_H * 1.0f / config.options.scaled_height;
|
||||
if (max_scale_width < max_scale_height) {
|
||||
/* => the width is most limiting */
|
||||
config.options.scaled_width = MAX_W;
|
||||
/* Can't use ' *= ', because we need to do the multiplication in float
|
||||
* (or double), and only THEN cast back to int. */
|
||||
config.options.scaled_height = (int) (config.options.scaled_height * max_scale_width);
|
||||
} else {
|
||||
/* => the height is most limiting */
|
||||
config.options.scaled_height = MAX_H;
|
||||
/* Can't use ' *= ', because we need to do the multiplication in float
|
||||
* (or double), and only THEN cast back to int. */
|
||||
config.options.scaled_width = (int) (config.options.scaled_width * max_scale_height);
|
||||
}
|
||||
config.options.use_scaling = 1;
|
||||
}
|
||||
config.options.use_scaling = 1;
|
||||
config.options.scaled_width = W;
|
||||
config.options.scaled_height = H;
|
||||
config.output.colorspace = MODE_RGBA;
|
||||
if (! WebPDecode(data, len, &config) == VP8_STATUS_OK) {
|
||||
warning ("error decoding webp: %s", filename);
|
||||
|
@ -511,11 +528,11 @@ int p2tgl_imgstore_add_with_id_webp (const char *filename) {
|
|||
warning ("error encoding webp as png: %s", filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// will be owned by libpurple imgstore, which uses glib functions for managing memory
|
||||
void *pngdub = g_memdup (png, (guint)pnglen);
|
||||
free (png);
|
||||
|
||||
|
||||
int imgStoreId = purple_imgstore_add_with_id (pngdub, pnglen, NULL);
|
||||
return imgStoreId;
|
||||
}
|
||||
|
|
|
@ -79,6 +79,9 @@ PurpleNotifyUserInfo *p2tgl_notify_encrypted_chat_info_new (struct tgl_state *TL
|
|||
|
||||
void p2tgl_blist_alias_buddy (PurpleBuddy *buddy, struct tgl_user *user);
|
||||
int p2tgl_imgstore_add_with_id (const char* filename);
|
||||
int p2tgl_imgstore_add_with_id_webp (const char *filename);
|
||||
void p2tgl_buddy_icons_set_for_user (PurpleAccount *pa, tgl_peer_id_t *id, const char* filename);
|
||||
#ifdef HAVE_LIBWEBP
|
||||
int p2tgl_imgstore_add_with_id_webp (const char *filename);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
@ -22,7 +22,6 @@
|
|||
|
||||
#include "tgp-utils.h"
|
||||
#include "msglog.h"
|
||||
#include "lodepng/lodepng.h"
|
||||
|
||||
#include <purple.h>
|
||||
|
||||
|
@ -143,4 +142,3 @@ int tgp_startswith (const char *str, const char *with) {
|
|||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue