mrmBot-Matrix/natives/globe.cc

73 lines
2.6 KiB
C++
Raw Normal View History

#include <vips/vips8>
2020-07-16 14:31:48 +00:00
#include "common.h"
2020-07-16 14:31:48 +00:00
using namespace std;
using namespace vips;
2020-07-16 14:31:48 +00:00
2022-12-28 06:20:17 +00:00
char *Globe(string *type, char *BufferData, size_t BufferLength,
2022-12-26 06:28:56 +00:00
ArgumentMap Arguments, size_t *DataSize) {
string basePath = GetArgument<string>(Arguments, "basePath");
2020-07-16 14:31:48 +00:00
2022-12-26 06:28:56 +00:00
VOption *options = VImage::option();
2020-07-16 14:31:48 +00:00
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)->set("access", "sequential")
: options)
2022-12-26 06:28:56 +00:00
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
2021-02-25 21:09:53 +00:00
2022-12-26 06:28:56 +00:00
int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());
2022-12-28 06:20:17 +00:00
int nPages = *type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30;
2020-07-16 14:31:48 +00:00
2022-12-26 06:28:56 +00:00
double size = min(width, pageHeight);
2020-07-16 14:31:48 +00:00
2022-12-26 06:28:56 +00:00
string diffPath = basePath + "assets/images/globediffuse.png";
VImage diffuse = VImage::new_from_file(diffPath.c_str())
.resize(size / 500.0, VImage::option()->set(
"kernel", VIPS_KERNEL_CUBIC)) /
255;
2020-07-16 14:31:48 +00:00
2022-12-26 06:28:56 +00:00
string specPath = basePath + "assets/images/globespec.png";
VImage specular = VImage::new_from_file(specPath.c_str())
.resize(size / 500.0, VImage::option()->set(
"kernel", VIPS_KERNEL_CUBIC));
2022-12-26 06:28:56 +00:00
string distortPath = basePath + "assets/images/spheremap.png";
VImage distort =
(VImage::new_from_file(distortPath.c_str())
.resize(size / 500.0,
VImage::option()->set("kernel", VIPS_KERNEL_CUBIC)) /
65535) *
size;
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 resized = img_frame.resize(
size / (double)width,
VImage::option()->set("vscale", size / (double)pageHeight));
VImage rolled = img_frame.wrap(
VImage::option()->set("x", width * i / nPages)->set("y", 0));
VImage extracted = rolled.extract_band(0, VImage::option()->set("n", 3));
VImage mapped = extracted.mapim(distort);
VImage composited = mapped * diffuse + specular;
VImage frame = composited.bandjoin(diffuse > 0.0);
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, size);
2022-12-28 06:20:17 +00:00
if (*type != "gif") {
2022-12-26 06:28:56 +00:00
vector<int> delay(30, 50);
final.set("delay", delay);
}
void *buf;
final.write_to_buffer(".gif", &buf, DataSize);
2022-06-28 03:50:53 +00:00
2022-12-26 06:28:56 +00:00
return (char *)buf;
2020-07-16 14:31:48 +00:00
}