2020-07-22 18:12:38 +00:00
|
|
|
#include <napi.h>
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2022-05-19 03:20:17 +00:00
|
|
|
#include <vips/vips8>
|
2020-07-22 18:12:38 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2022-05-19 03:20:17 +00:00
|
|
|
using namespace vips;
|
2020-07-22 18:12:38 +00:00
|
|
|
|
2021-04-24 22:54:47 +00:00
|
|
|
Napi::Value Reverse(const Napi::CallbackInfo &info) {
|
|
|
|
Napi::Env env = info.Env();
|
2020-09-11 02:04:09 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
try {
|
|
|
|
Napi::Object obj = info[0].As<Napi::Object>();
|
2021-05-11 19:25:02 +00:00
|
|
|
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
|
2021-04-26 14:47:03 +00:00
|
|
|
bool soos =
|
|
|
|
obj.Has("soos") ? obj.Get("soos").As<Napi::Boolean>().Value() : false;
|
|
|
|
|
2022-05-19 03:20:17 +00:00
|
|
|
VOption *options =
|
|
|
|
VImage::option()->set("access", "sequential")->set("n", -1);
|
2021-04-26 14:47:03 +00:00
|
|
|
|
2022-05-19 03:20:17 +00:00
|
|
|
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "", options)
|
|
|
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
|
|
|
|
|
|
|
int width = in.width();
|
|
|
|
int page_height = vips_image_get_page_height(in.get_image());
|
|
|
|
int n_pages = vips_image_get_n_pages(in.get_image());
|
|
|
|
|
|
|
|
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 < n_pages; i++) {
|
|
|
|
VImage img_frame = in.crop(0, i * page_height, width, page_height);
|
|
|
|
split.push_back(img_frame);
|
2021-10-23 18:58:19 +00:00
|
|
|
}
|
2021-04-26 14:47:03 +00:00
|
|
|
|
2022-05-19 03:20:17 +00:00
|
|
|
vector<int> delays = in.get_array_int("delay");
|
2021-04-26 14:47:03 +00:00
|
|
|
if (soos) {
|
2022-05-19 03:20:17 +00:00
|
|
|
vector<VImage> copy = split;
|
|
|
|
vector<int> copy2 = delays;
|
|
|
|
reverse(copy.begin(), copy.end());
|
|
|
|
reverse(copy2.begin(), copy2.end());
|
2021-08-17 04:04:11 +00:00
|
|
|
copy.pop_back();
|
2022-05-19 03:20:17 +00:00
|
|
|
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());
|
2021-04-26 14:47:03 +00:00
|
|
|
} else {
|
2022-05-19 03:20:17 +00:00
|
|
|
reverse(split.begin(), split.end());
|
|
|
|
reverse(delays.begin(), delays.end());
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 03:20:17 +00:00
|
|
|
VImage final = VImage::arrayjoin(split, VImage::option()->set("across", 1));
|
|
|
|
final.set(VIPS_META_PAGE_HEIGHT, page_height);
|
|
|
|
final.set("delay", delays);
|
2021-04-26 14:47:03 +00:00
|
|
|
|
2022-05-19 03:20:17 +00:00
|
|
|
void *buf;
|
|
|
|
size_t length;
|
|
|
|
final.write_to_buffer(".gif", &buf, &length,
|
|
|
|
VImage::option()->set("dither", 0));
|
2021-04-26 14:47:03 +00:00
|
|
|
|
2022-05-19 03:20:17 +00:00
|
|
|
vips_thread_shutdown();
|
2021-04-26 14:47:03 +00:00
|
|
|
|
|
|
|
Napi::Object result = Napi::Object::New(env);
|
2022-05-19 03:20:17 +00:00
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
2021-04-26 14:47:03 +00:00
|
|
|
result.Set("type", "gif");
|
|
|
|
return result;
|
|
|
|
} catch (std::exception const &err) {
|
|
|
|
throw Napi::Error::New(env, err.what());
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (...) {
|
|
|
|
throw Napi::Error::New(env, "Unknown error");
|
2020-07-22 18:12:38 +00:00
|
|
|
}
|
|
|
|
}
|