diff --git a/tgp-2prpl.c b/tgp-2prpl.c index 9c39b91..39932f0 100644 --- a/tgp-2prpl.c +++ b/tgp-2prpl.c @@ -231,13 +231,35 @@ int p2tgl_imgstore_add_with_id (const char* filename) { return id; } +int p2tgl_imgstore_add_with_id_raw (const unsigned char *raw_rgba, unsigned width, unsigned height) { + /* Heavily inspired by: https://github.com/EionRobb/pidgin-opensteamworks/blob/master/libsteamworks.cpp#L113 */ + const unsigned char tga_header[] = { + /* No ID; no color map; uncompressed true color */ + 0,0,2, + /* No color map metadata */ + 0,0,0,0,0, + /* No offsets */ + 0,0,0,0, + /* Dimensions */ + width&0xFF,(width/256)&0xFF,height&0xFF,(height/256)&0xFF, + /* 32 bits per pixel */ + 32, + /* "Origin in upper left-hand corner" */ + 32}; + // Will be owned by libpurple imgstore, which uses glib functions for managing memory + const unsigned tga_len = sizeof(tga_header) + width * height * 4; + unsigned char *tga = g_malloc(tga_len); + memcpy(tga, tga_header, sizeof(tga_header)); + memcpy(tga + sizeof(tga_header), raw_rgba, width * height * 4); + return purple_imgstore_add_with_id (tga, tga_len, NULL); +} + #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; size_t len; GError *err = NULL; diff --git a/tgp-2prpl.h b/tgp-2prpl.h index 32a165c..8b3f19a 100644 --- a/tgp-2prpl.h +++ b/tgp-2prpl.h @@ -46,6 +46,7 @@ PurpleNotifyUserInfo *p2tgl_notify_encrypted_chat_info_new (struct tgl_state *TL int p2tgl_imgstore_add_with_id (const char* filename); void p2tgl_buddy_icons_set_for_user (PurpleAccount *pa, tgl_peer_id_t id, const char* filename); +int p2tgl_imgstore_add_with_id_raw (const unsigned char *raw_rgba, unsigned width, unsigned height); #ifdef HAVE_LIBWEBP int p2tgl_imgstore_add_with_id_webp (const char *filename); #endif