mrmBot-Matrix/natives/reddit.cc

66 lines
2.4 KiB
C++
Raw Normal View History

#include <vips/vips8>
#include "common.h"
using namespace std;
using namespace vips;
2022-12-28 06:20:17 +00:00
char *Reddit(string *type, char *BufferData, size_t BufferLength,
2022-12-26 06:28:56 +00:00
ArgumentMap Arguments, size_t *DataSize) {
string text = GetArgument<string>(Arguments, "text");
string basePath = GetArgument<string>(Arguments, "basePath");
2022-12-26 06:28:56 +00:00
VOption *options = VImage::option()->set("access", "sequential");
2022-12-26 06:28:56 +00:00
VImage in =
VImage::new_from_buffer(BufferData, BufferLength, "",
2022-12-28 06:20:17 +00:00
*type == "gif" ? options->set("n", -1) : options)
2022-12-26 06:28:56 +00:00
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
2022-12-26 06:28:56 +00:00
string assetPath = basePath + "assets/images/reddit.png";
VImage tmpl = VImage::new_from_file(assetPath.c_str());
2022-12-26 06:28:56 +00:00
int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = vips_image_get_n_pages(in.get_image());
2022-12-26 06:28:56 +00:00
string captionText = "<span foreground=\"white\">" + text + "</span>";
2022-12-26 06:28:56 +00:00
VImage textImage = VImage::text(
".", VImage::option()->set(
"fontfile", (basePath + "assets/fonts/reddit.ttf").c_str()));
textImage = VImage::text(
captionText.c_str(),
VImage::option()
->set("rgba", true)
->set("font", "Roboto, Twemoji Color Font 62")
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("align", VIPS_ALIGN_LOW));
2022-12-26 06:28:56 +00:00
VImage composited =
tmpl.composite2(textImage, VIPS_BLEND_MODE_OVER,
VImage::option()->set("x", 375)->set(
"y", (tmpl.height() - textImage.height()) - 64));
VImage watermark =
composited.resize((double)width / (double)composited.width());
2022-12-26 06:28:56 +00:00
vector<VImage> img;
for (int i = 0; i < nPages; i++) {
VImage img_frame =
2022-12-28 06:20:17 +00:00
*type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
2022-12-26 06:28:56 +00:00
VImage frame = img_frame.join(watermark, VIPS_DIRECTION_VERTICAL,
VImage::option()->set("expand", true));
img.push_back(frame);
}
2022-12-26 06:28:56 +00:00
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight + watermark.height());
void *buf;
final.write_to_buffer(
2022-12-28 06:20:17 +00:00
("." + *type).c_str(), &buf, DataSize,
*type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1)
: 0);
2022-06-28 03:50:53 +00:00
2022-12-26 06:28:56 +00:00
return (char *)buf;
}