From 89caa43068ca2ebd7085d5f37d89280f9baf11c8 Mon Sep 17 00:00:00 2001 From: Essem Date: Wed, 28 Dec 2022 00:49:38 -0600 Subject: [PATCH] how the hell did I manage to miss squish --- natives/image.cc | 20 ++--------- natives/squish.cc | 89 ++++++++++++++++++++--------------------------- natives/squish.h | 6 ++-- 3 files changed, 45 insertions(+), 70 deletions(-) diff --git a/natives/image.cc b/natives/image.cc index c472cc7..617af5a 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -78,6 +78,7 @@ std::map data = obj.Has("data") ? obj.Get("data").As>() : Napi::Buffer::New(env, 0); buf = FunctionMap.at(command)(&type, data.Data(), data.Length(), Arguments, &length); } else { @@ -164,21 +165,6 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) { return result; } -Napi::Value ProcessImage(const Napi::CallbackInfo &info) { // janky solution for gradual adoption - Napi::Env env = info.Env(); - - string command = info[0].As().Utf8Value(); - - if (MAP_HAS(FunctionMap, command)) { - return NewProcessImage(info, true); - } else if (MAP_HAS(NoInputFunctionMap, command)) { - return NewProcessImage(info, false); - } else { - Napi::Error::New(env, "Invalid command").ThrowAsJavaScriptException(); - return env.Null(); - } -} - Napi::Object Init(Napi::Env env, Napi::Object exports){ #ifdef _WIN32 Magick::InitializeMagick(""); diff --git a/natives/squish.cc b/natives/squish.cc index 4a3ebaf..e536998 100644 --- a/natives/squish.cc +++ b/natives/squish.cc @@ -1,67 +1,54 @@ +#include "common.h" #include -#include #include using namespace std; using namespace vips; -Napi::Value Squish(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *Squish(string *type, char *BufferData, size_t BufferLength, + ArgumentMap Arguments, size_t *DataSize) { - try { - Napi::Object obj = info[1].As(); - Napi::Buffer data = obj.Get("data").As>(); - string type = obj.Get("type").As().Utf8Value(); + VOption *options = VImage::option(); - VOption *options = VImage::option(); + VImage in = + VImage::new_from_buffer( + BufferData, BufferLength, "", + *type == "gif" ? options->set("n", -1)->set("access", "sequential") + : options) + .colourspace(VIPS_INTERPRETATION_sRGB); + if (!in.has_alpha()) + in = in.bandjoin(255); - VImage in = - VImage::new_from_buffer( - data.Data(), data.Length(), "", - type == "gif" ? options->set("n", -1)->set("access", "sequential") - : options) - .colourspace(VIPS_INTERPRETATION_sRGB); - if (!in.has_alpha()) - in = in.bandjoin(255); + int width = in.width(); + int pageHeight = vips_image_get_page_height(in.get_image()); + int nPages = *type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30; + double mult = (2 * M_PI) / nPages; - int width = in.width(); - int pageHeight = vips_image_get_page_height(in.get_image()); - int nPages = type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30; - double mult = (2 * M_PI) / nPages; - - vector img; - for (int i = 0; i < nPages; i++) { - VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; - double newWidth = (sin(i * mult) / 4) + 0.75; - double newHeight = (cos(i * mult) / 4) + 0.75; - VImage resized = - img_frame.resize(newWidth, VImage::option()->set("vscale", newHeight)) - .gravity(VIPS_COMPASS_DIRECTION_CENTRE, width, pageHeight); - img.push_back(resized); - } - VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); - final.set(VIPS_META_PAGE_HEIGHT, pageHeight); - if (type != "gif") { - vector delay(30, 50); - final.set("delay", delay); - } - - void *buf; - size_t length; - final.write_to_buffer(".gif", &buf, &length); - - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", "gif"); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); + vector img; + for (int i = 0; i < nPages; i++) { + VImage img_frame = + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + double newWidth = (sin(i * mult) / 4) + 0.75; + double newHeight = (cos(i * mult) / 4) + 0.75; + VImage resized = + img_frame.resize(newWidth, VImage::option()->set("vscale", newHeight)) + .gravity(VIPS_COMPASS_DIRECTION_CENTRE, width, pageHeight); + img.push_back(resized); } + VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); + final.set(VIPS_META_PAGE_HEIGHT, pageHeight); + if (*type != "gif") { + vector delay(30, 50); + final.set("delay", delay); + } + + void *buf; + final.write_to_buffer(".gif", &buf, DataSize); + + *type = "gif"; vips_error_clear(); vips_thread_shutdown(); - return result; + return (char *)buf; } \ No newline at end of file diff --git a/natives/squish.h b/natives/squish.h index 4ecdfe6..d98def5 100644 --- a/natives/squish.h +++ b/natives/squish.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Squish(const Napi::CallbackInfo& info); +using std::string; + +char* Squish(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file