mrmBot-Matrix/natives/bounce.cc

51 lines
1.5 KiB
C++
Raw Normal View History

2022-12-28 20:24:22 +00:00
#include <math.h>
#include <vips/vips8>
#include "common.h"
2022-12-28 20:24:22 +00:00
using namespace std;
using namespace vips;
2023-03-09 01:36:39 +00:00
char *Bounce(string type, string *outType, char *BufferData,
size_t BufferLength, [[maybe_unused]] ArgumentMap Arguments,
size_t *DataSize) {
2022-12-28 20:24:22 +00:00
VOption *options = VImage::option();
VImage in =
VImage::new_from_buffer(
BufferData, BufferLength, "",
2023-03-09 01:36:39 +00:00
type == "gif" ? options->set("n", -1)->set("access", "sequential")
: options)
2022-12-28 20:24:22 +00:00
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
2022-12-28 20:24:22 +00:00
int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());
2023-03-09 01:36:39 +00:00
int nPages = type == "gif" ? vips_image_get_n_pages(in.get_image()) : 15;
2022-12-28 20:24:22 +00:00
double mult = M_PI / nPages;
int halfHeight = pageHeight / 2;
vector<VImage> img;
for (int i = 0; i < nPages; i++) {
VImage img_frame =
2023-03-09 01:36:39 +00:00
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
2022-12-28 20:24:22 +00:00
double height = halfHeight * ((abs(sin(i * mult)) * -1) + 1);
VImage embedded =
img_frame.embed(0, height, width, pageHeight + halfHeight);
2022-12-28 20:24:22 +00:00
img.push_back(embedded);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight + halfHeight);
2023-03-09 01:36:39 +00:00
if (type != "gif") {
2022-12-28 20:24:22 +00:00
vector<int> delay(30, 50);
final.set("delay", delay);
}
void *buf;
final.write_to_buffer(".gif", &buf, DataSize);
2023-03-09 01:36:39 +00:00
*outType = "gif";
2022-12-28 20:24:22 +00:00
return (char *)buf;
}