mrmBot-Matrix/natives/reverse.cc

59 lines
1.8 KiB
C++
Raw Normal View History

2022-12-26 06:28:56 +00:00
#include <algorithm>
#include <vips/vips8>
2020-07-22 18:12:38 +00:00
#include "common.h"
2020-07-22 18:12:38 +00:00
using namespace std;
using namespace vips;
2020-07-22 18:12:38 +00:00
2023-03-09 01:36:39 +00:00
char *Reverse(string type, string *outType, char *BufferData,
size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) {
2022-12-26 06:28:56 +00:00
bool soos = GetArgumentWithFallback<bool>(Arguments, "soos", false);
2022-12-26 06:28:56 +00:00
VOption *options =
VImage::option()->set("access", "sequential")->set("n", -1);
2022-12-26 06:28:56 +00:00
VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", options)
.colourspace(VIPS_INTERPRETATION_sRGB);
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
vector<VImage> split;
// todo: find a better way of getting individual frames (or at least getting
// the frames in reverse order)
for (int i = 0; i < nPages; i++) {
VImage img_frame = in.crop(0, i * pageHeight, width, pageHeight);
split.push_back(img_frame);
}
2022-12-26 06:28:56 +00:00
vector<int> delays = in.get_array_int("delay");
if (soos) {
vector<VImage> copy = split;
vector<int> copy2 = delays;
reverse(copy.begin(), copy.end());
reverse(copy2.begin(), copy2.end());
copy.pop_back();
copy2.pop_back();
copy.erase(copy.begin());
copy2.erase(copy2.begin());
split.insert(split.end(), copy.begin(), copy.end());
delays.insert(delays.end(), copy2.begin(), copy2.end());
} else {
reverse(split.begin(), split.end());
reverse(delays.begin(), delays.end());
}
2022-12-26 06:28:56 +00:00
VImage final = VImage::arrayjoin(split, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
final.set("delay", delays);
2022-12-26 06:28:56 +00:00
void *buf;
final.write_to_buffer(".gif", &buf, DataSize,
VImage::option()->set("dither", 0));
2023-03-09 01:36:39 +00:00
*outType = "gif";
2022-06-28 03:50:53 +00:00
2022-12-26 06:28:56 +00:00
return (char *)buf;
2020-07-22 18:12:38 +00:00
}