Port swirl to vips (based on jcupitt's implementation)

This commit is contained in:
Essem 2022-09-11 02:09:44 -05:00
parent 3eae453de3
commit a2493bcc0a
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C

View file

@ -1,62 +1,95 @@
#include <Magick++.h>
#include <napi.h> #include <napi.h>
#include <iostream> #include <vips/vips8>
#include <list>
using namespace std; using namespace std;
using namespace Magick; using namespace vips;
Napi::Value Swirl(const Napi::CallbackInfo &info) { Napi::Value Swirl(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
Napi::Object result = Napi::Object::New(env);
try { try {
Napi::Object obj = info[0].As<Napi::Object>(); Napi::Object obj = info[0].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>(); Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string type = obj.Get("type").As<Napi::String>().Utf8Value(); string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay =
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
Blob blob; VOption *options = VImage::option()->set("access", "sequential");
list<Image> frames; VImage in =
list<Image> coalesced; VImage::new_from_buffer(data.Data(), data.Length(), "",
list<Image> mid; type == "gif" ? options->set("n", -1) : options)
try { .colourspace(VIPS_INTERPRETATION_sRGB);
readImages(&frames, Blob(data.Data(), data.Length())); if (!in.has_alpha())
} catch (Magick::WarningCoder &warning) { in = in.bandjoin(255);
cerr << "Coder Warning: " << warning.what() << endl;
} catch (Magick::Warning &warning) { int pageHeight = vips_image_get_page_height(in.get_image());
cerr << "Warning: " << warning.what() << endl; 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));
int size = min(width, pageHeight) / 2;
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);
} }
coalesceImages(&coalesced, frames.begin(), frames.end()); VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
for (Image &image : coalesced) { void *buf;
image.swirl(180); size_t length;
image.magick(type); final.write_to_buffer(".gif", &buf, &length);
mid.push_back(image);
}
optimizeTransparency(mid.begin(), mid.end()); result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
if (type == "gif") {
for (Image &image : mid) {
image.quantizeDither(false);
image.quantize();
if (delay != 0) image.animationDelay(delay);
}
}
writeImages(mid.begin(), mid.end(), &blob);
Napi::Object result = Napi::Object::New(env);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
blob.length()));
result.Set("type", type); result.Set("type", type);
return result;
} catch (std::exception const &err) { } catch (std::exception const &err) {
throw Napi::Error::New(env, err.what()); Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) { } catch (...) {
throw Napi::Error::New(env, "Unknown error"); Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
} }
vips_error_clear();
vips_thread_shutdown();
return result;
} }