2021-04-24 22:54:47 +00:00
|
|
|
#include <napi.h>
|
|
|
|
|
2022-09-11 07:09:44 +00:00
|
|
|
#include <vips/vips8>
|
2021-04-24 22:54:47 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2022-09-11 07:09:44 +00:00
|
|
|
using namespace vips;
|
2021-04-24 22:54:47 +00:00
|
|
|
|
|
|
|
Napi::Value Swirl(const Napi::CallbackInfo &info) {
|
|
|
|
Napi::Env env = info.Env();
|
2022-09-11 07:09:44 +00:00
|
|
|
Napi::Object result = Napi::Object::New(env);
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
try {
|
2022-11-27 19:03:07 +00:00
|
|
|
Napi::Object obj = info[1].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
|
|
|
string type = obj.Get("type").As<Napi::String>().Utf8Value();
|
|
|
|
|
2022-09-11 07:09:44 +00:00
|
|
|
VOption *options = VImage::option()->set("access", "sequential");
|
|
|
|
|
|
|
|
VImage in =
|
|
|
|
VImage::new_from_buffer(data.Data(), data.Length(), "",
|
|
|
|
type == "gif" ? options->set("n", -1) : options)
|
|
|
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
|
|
|
if (!in.has_alpha())
|
|
|
|
in = in.bandjoin(255);
|
|
|
|
|
|
|
|
int pageHeight = vips_image_get_page_height(in.get_image());
|
|
|
|
int nPages = vips_image_get_n_pages(in.get_image());
|
|
|
|
int width = in.width();
|
|
|
|
double newWidth = width * 3;
|
|
|
|
double newHeight = pageHeight * 3;
|
|
|
|
vector<double> divSize = {newWidth / 2, newHeight / 2};
|
|
|
|
|
|
|
|
VImage index = VImage::xyz(newWidth, newHeight);
|
|
|
|
VImage center = index - divSize;
|
|
|
|
VImage polar = center
|
|
|
|
.copy(VImage::option()
|
|
|
|
->set("format", VIPS_FORMAT_COMPLEX)
|
|
|
|
->set("bands", 1))
|
|
|
|
.polar()
|
|
|
|
.copy(VImage::option()
|
|
|
|
->set("format", VIPS_FORMAT_FLOAT)
|
|
|
|
->set("bands", 2));
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2022-09-11 07:09:44 +00:00
|
|
|
int size = min(width, pageHeight) / 2;
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2022-09-11 07:09:44 +00:00
|
|
|
VImage test = (1 - polar.extract_band(0) / size);
|
|
|
|
VImage degrees = test.cast(VIPS_FORMAT_FLOAT).pow(2);
|
|
|
|
|
|
|
|
VImage angle = polar.extract_band(1) + degrees * 180;
|
|
|
|
|
|
|
|
VImage distortion = polar.extract_band(0)
|
|
|
|
.bandjoin(angle)
|
|
|
|
.copy(VImage::option()
|
|
|
|
->set("format", VIPS_FORMAT_COMPLEX)
|
|
|
|
->set("bands", 1))
|
|
|
|
.rect()
|
|
|
|
.copy(VImage::option()
|
|
|
|
->set("format", VIPS_FORMAT_FLOAT)
|
|
|
|
->set("bands", 2)) +
|
|
|
|
divSize;
|
|
|
|
|
|
|
|
vector<VImage> img;
|
|
|
|
for (int i = 0; i < nPages; i++) {
|
|
|
|
VImage img_frame =
|
|
|
|
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
|
|
|
|
|
|
|
|
VImage distort =
|
|
|
|
img_frame
|
|
|
|
.gravity(VIPS_COMPASS_DIRECTION_CENTRE, newWidth, newHeight,
|
|
|
|
VImage::option()->set("extend", VIPS_EXTEND_COPY))
|
|
|
|
.mapim(distortion, VImage::option()->set(
|
|
|
|
"interpolate",
|
|
|
|
VInterpolate::new_from_name("bicubic")));
|
|
|
|
VImage frame = distort.crop(width, pageHeight, width, pageHeight);
|
|
|
|
img.push_back(frame);
|
2021-04-24 22:54:47 +00:00
|
|
|
}
|
2022-09-11 07:09:44 +00:00
|
|
|
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
|
|
|
|
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2022-09-11 07:09:44 +00:00
|
|
|
void *buf;
|
|
|
|
size_t length;
|
|
|
|
final.write_to_buffer(".gif", &buf, &length);
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2022-09-11 07:09:44 +00:00
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
2021-04-26 14:47:03 +00:00
|
|
|
result.Set("type", type);
|
|
|
|
} catch (std::exception const &err) {
|
2022-09-11 07:09:44 +00:00
|
|
|
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (...) {
|
2022-09-11 07:09:44 +00:00
|
|
|
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
2022-09-11 07:09:44 +00:00
|
|
|
|
|
|
|
vips_error_clear();
|
|
|
|
vips_thread_shutdown();
|
|
|
|
return result;
|
2021-04-24 22:54:47 +00:00
|
|
|
}
|