diff --git a/natives/blur.cc b/natives/blur.cc index fd281c8..1cef83b 100644 --- a/natives/blur.cc +++ b/natives/blur.cc @@ -1,26 +1,23 @@ -#include +#include "common.h" #include +#include +#include using namespace std; using namespace vips; -Napi::Value Blur(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); - - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - bool sharp = obj.Get("sharp").As().Value(); - string type = obj.Get("type").As().Utf8Value(); - +char* Blur(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { + string caption = Arguments["caption"]; + string font = MAP_GET(Arguments, "font"); + bool sharp = MAP_GET(Arguments, "sharp") == "true"; 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); + VImage::new_from_buffer(BufferData, BufferLength, "", + type == "gif" ? options->set("n", -1) : options) + .colourspace(VIPS_INTERPRETATION_sRGB); + if (!in.has_alpha()) in = in.bandjoin(255); // TODO: find a better way to calculate the intensity for GIFs without @@ -29,18 +26,10 @@ Napi::Value Blur(const Napi::CallbackInfo &info) { : in.gaussblur(15); void *buf; - size_t length; - out.write_to_buffer(("." + type).c_str(), &buf, &length); + out.write_to_buffer(("." + type).c_str(), &buf, DataSize); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } + vips_error_clear(); + vips_thread_shutdown(); - vips_error_clear(); - vips_thread_shutdown(); - return result; + return (char*) buf; } diff --git a/natives/blur.h b/natives/blur.h index a61fec2..447ef20 100644 --- a/natives/blur.h +++ b/natives/blur.h @@ -2,4 +2,7 @@ #include -Napi::Value Blur(const Napi::CallbackInfo& info); +using std::string; +using std::map; + +char* Blur(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/caption.cc b/natives/caption.cc index 2324b43..69c16da 100644 --- a/natives/caption.cc +++ b/natives/caption.cc @@ -1,29 +1,24 @@ #include "common.h" -#include + +#include #include using namespace std; using namespace vips; -Napi::Value Caption(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char* Caption(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - string caption = obj.Get("caption").As().Utf8Value(); - string font = obj.Get("font").As().Utf8Value(); - string type = obj.Get("type").As().Utf8Value(); - string basePath = obj.Get("basePath").As().Utf8Value(); + string caption = Arguments["caption"]; + string font = MAP_GET(Arguments, "font"); 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); + VImage::new_from_buffer(BufferData, BufferLength, "", + type == "gif" ? options->set("n", -1) : options) + .colourspace(VIPS_INTERPRETATION_sRGB); + if (!in.has_alpha()) in = in.bandjoin(255); @@ -33,26 +28,17 @@ Napi::Value Caption(const Napi::CallbackInfo &info) { int nPages = vips_image_get_n_pages(in.get_image()); int textWidth = width - ((width / 25) * 2); - string font_string = (font == "roboto" ? "Roboto Condensed" : font) + ", Twemoji Color Emoji " + - (font != "impact" ? "bold" : "normal") + - " " + to_string(size); + string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " + + (font != "impact" ? "bold" : "normal") + " " + + to_string(size); string captionText = "" + caption + ""; - VImage text; - auto findResult = fontPaths.find(font); - if (findResult != fontPaths.end()) { - text = VImage::text( - ".", VImage::option()->set("fontfile", - (basePath + findResult->second).c_str())); - } - text = VImage::text( - captionText.c_str(), - VImage::option() + VImage text = + VImage::text(captionText.c_str(), VImage::option() ->set("rgba", true) ->set("align", VIPS_ALIGN_CENTRE) ->set("font", font_string.c_str()) - ->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str()) ->set("width", textWidth)); VImage captionImage = ((text == (vector){0, 0, 0, 0}).bandand()) @@ -72,22 +58,14 @@ Napi::Value Caption(const Napi::CallbackInfo &info) { VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, pageHeight + captionImage.height()); - void *buf; - size_t length; + void* buf; final.write_to_buffer( - ("." + type).c_str(), &buf, &length, + ("." + type).c_str(), &buf, DataSize, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - result.Set("data", Napi::Buffer::New(env, (char *)buf, length)); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } + vips_error_clear(); + vips_thread_shutdown(); - vips_error_clear(); - vips_thread_shutdown(); - return result; + return (char*) buf; } diff --git a/natives/caption.h b/natives/caption.h index dbe275e..d5ad71c 100644 --- a/natives/caption.h +++ b/natives/caption.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include -Napi::Value Caption(const Napi::CallbackInfo& info); +char* Caption(std::string type, char* BufferData, size_t BufferLength, std::map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/caption2.cc b/natives/caption2.cc index 7b9dd61..1327065 100644 --- a/natives/caption2.cc +++ b/natives/caption2.cc @@ -1,30 +1,27 @@ #include "common.h" -#include +#include "common.h" +#include +#include #include using namespace std; using namespace vips; -Napi::Value CaptionTwo(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char* CaptionTwo(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - string caption = obj.Get("caption").As().Utf8Value(); - bool top = obj.Get("top").As().Value(); - string font = obj.Get("font").As().Utf8Value(); - string type = obj.Get("type").As().Utf8Value(); - string basePath = obj.Get("basePath").As().Utf8Value(); + bool top = MAP_GET(Arguments, "top") == "true"; + string caption = Arguments["caption"]; + string font = MAP_GET(Arguments, "font"); + string basePath = MAP_GET(Arguments, "basePath"); 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); + VImage::new_from_buffer(BufferData, BufferLength, "", + type == "gif" ? options->set("n", -1) : options) + .colourspace(VIPS_INTERPRETATION_sRGB); + if (!in.has_alpha()) in = in.bandjoin(255); @@ -76,21 +73,13 @@ Napi::Value CaptionTwo(const Napi::CallbackInfo &info) { final.set(VIPS_META_PAGE_HEIGHT, pageHeight + captionImage.height()); void *buf; - size_t length; final.write_to_buffer( - ("." + type).c_str(), &buf, &length, + ("." + type).c_str(), &buf, DataSize, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } - - vips_error_clear(); - vips_thread_shutdown(); - return result; + + vips_error_clear(); + vips_thread_shutdown(); + + return (char*) buf; } diff --git a/natives/caption2.h b/natives/caption2.h index 1edbc2e..a14bd07 100644 --- a/natives/caption2.h +++ b/natives/caption2.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include -Napi::Value CaptionTwo(const Napi::CallbackInfo& info); +char* CaptionTwo(std::string type, char* BufferData, size_t BufferLength, std::map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/circle.cc b/natives/circle.cc index f02c372..5a6897d 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -2,27 +2,22 @@ #include #include +#include +#include #include using namespace std; using namespace Magick; -Napi::Value Circle(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); - - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - string type = obj.Get("type").As().Utf8Value(); - +char* Circle(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { + Blob blob; list frames; list coalesced; list blurred; try { - readImages(&frames, Blob(data.Data(), data.Length())); + readImages(&frames, Blob(BufferData, BufferLength)); } catch (Magick::WarningCoder &warning) { cerr << "Coder Warning: " << warning.what() << endl; } catch (Magick::Warning &warning) { @@ -47,14 +42,7 @@ Napi::Value Circle(const Napi::CallbackInfo &info) { writeImages(blurred.begin(), blurred.end(), &blob); - result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), - blob.length())); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } + *DataSize = blob.length(); - return result; + return (char*) blob.data(); } \ No newline at end of file diff --git a/natives/circle.h b/natives/circle.h index 0dc4be9..21a2cf1 100644 --- a/natives/circle.h +++ b/natives/circle.h @@ -1,5 +1,10 @@ #pragma once #include +#include +#include -Napi::Value Circle(const Napi::CallbackInfo& info); +using std::string; +using std::map; + +char* Circle(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/colors.cc b/natives/colors.cc index dcd4e50..03eed17 100644 --- a/natives/colors.cc +++ b/natives/colors.cc @@ -1,6 +1,8 @@ #include #include +#include +#include using namespace std; using namespace vips; @@ -8,20 +10,14 @@ using namespace vips; VImage sepia = VImage::new_matrixv(3, 3, 0.3588, 0.7044, 0.1368, 0.2990, 0.5870, 0.1140, 0.2392, 0.4696, 0.0912); -Napi::Value Colors(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char* Colors(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - string color = obj.Get("color").As().Utf8Value(); - string type = obj.Get("type").As().Utf8Value(); + string color = Arguments["color"]; VOption *options = VImage::option()->set("access", "sequential"); VImage in = - VImage::new_from_buffer(data.Data(), data.Length(), "", + VImage::new_from_buffer(BufferData, BufferLength, "", type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); @@ -34,18 +30,10 @@ Napi::Value Colors(const Napi::CallbackInfo &info) { } void *buf; - size_t length; - out.write_to_buffer(("." + type).c_str(), &buf, &length); + out.write_to_buffer(("." + type).c_str(), &buf, DataSize); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } + vips_error_clear(); + vips_thread_shutdown(); - vips_error_clear(); - vips_thread_shutdown(); - return result; + return (char*) buf; } diff --git a/natives/colors.h b/natives/colors.h index ef78bcf..75ce917 100644 --- a/natives/colors.h +++ b/natives/colors.h @@ -1,5 +1,10 @@ #pragma once #include +#include +#include -Napi::Value Colors(const Napi::CallbackInfo& info); +using std::map; +using std::string; + +char* Colors(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/common.h b/natives/common.h index 4510065..579e900 100644 --- a/natives/common.h +++ b/natives/common.h @@ -3,6 +3,9 @@ #include #include +#define MAP_HAS(ARRAY, KEY) (ARRAY.count(KEY) > 0) +#define MAP_GET(ARRAY, KEY) (MAP_HAS(ARRAY, KEY) ? ARRAY.at(KEY) : NULL) // C++ has forced my hand + const std::unordered_map fontPaths { {"futura", "assets/fonts/caption.otf"}, {"helvetica", "assets/fonts/caption2.ttf"}, diff --git a/natives/crop.cc b/natives/crop.cc index 548a39c..fb98c46 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -1,25 +1,19 @@ #include - +#include +#include #include using namespace std; using namespace vips; -Napi::Value Crop(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); - - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - string type = obj.Get("type").As().Utf8Value(); +char* Crop(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { 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); + VImage::new_from_buffer(BufferData, BufferLength, "", + type == "gif" ? options->set("n", -1) : options) + .colourspace(VIPS_INTERPRETATION_sRGB); int width = in.width(); int pageHeight = vips_image_get_page_height(in.get_image()); @@ -46,21 +40,13 @@ Napi::Value Crop(const Napi::CallbackInfo &info) { final.set(VIPS_META_PAGE_HEIGHT, finalHeight); void *buf; - size_t length; final.write_to_buffer( - ("." + type).c_str(), &buf, &length, + ("." + type).c_str(), &buf, DataSize, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } + vips_error_clear(); + vips_thread_shutdown(); - vips_error_clear(); - vips_thread_shutdown(); - return result; + return (char*) buf; } \ No newline at end of file diff --git a/natives/crop.h b/natives/crop.h index a84a5b9..ac23dab 100644 --- a/natives/crop.h +++ b/natives/crop.h @@ -1,5 +1,10 @@ #pragma once #include +#include +#include -Napi::Value Crop(const Napi::CallbackInfo& info); +using std::map; +using std::string; + +char* Crop(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/deepfry.cc b/natives/deepfry.cc index c04a3c0..1b4fab9 100644 --- a/natives/deepfry.cc +++ b/natives/deepfry.cc @@ -1,25 +1,18 @@ -#include - +#include #include using namespace std; using namespace vips; -Napi::Value Deepfry(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); - - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - string type = obj.Get("type").As().Utf8Value(); +char* Deepfry(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { 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); + VImage::new_from_buffer(BufferData, BufferLength, "", + type == "gif" ? options->set("n", -1) : options) + .colourspace(VIPS_INTERPRETATION_sRGB); + if (!in.has_alpha()) in = in.bandjoin(255); @@ -59,20 +52,12 @@ Napi::Value Deepfry(const Napi::CallbackInfo &info) { } void *buf; - size_t length; - final.write_to_buffer(("." + type).c_str(), &buf, &length, + final.write_to_buffer(("." + type).c_str(), &buf, DataSize, type == "gif" ? VImage::option()->set("dither", 0) : 0); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } + vips_error_clear(); + vips_thread_shutdown(); - vips_error_clear(); - vips_thread_shutdown(); - return result; + return (char*) buf; } \ No newline at end of file diff --git a/natives/deepfry.h b/natives/deepfry.h index 9eca510..64b89d5 100644 --- a/natives/deepfry.h +++ b/natives/deepfry.h @@ -1,5 +1,10 @@ #pragma once #include +#include +#include -Napi::Value Deepfry(const Napi::CallbackInfo& info); +using std::map; +using std::string; + +char* Deepfry(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/explode.cc b/natives/explode.cc index 3a0323f..06896fd 100644 --- a/natives/explode.cc +++ b/natives/explode.cc @@ -1,22 +1,19 @@ +#include "common.h" + #include -#include #include +#include +#include #include using namespace std; using namespace Magick; -Napi::Value Explode(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); +char* Explode(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - int amount = obj.Get("amount").As().Int32Value(); - string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int amount = stoi(Arguments.at("amount")); + int delay = MAP_HAS(Arguments, "delay") ? stoi(Arguments.at("delay")) : 0; Blob blob; @@ -24,7 +21,7 @@ Napi::Value Explode(const Napi::CallbackInfo &info) { list coalesced; list blurred; try { - readImages(&frames, Blob(data.Data(), data.Length())); + readImages(&frames, Blob(BufferData, BufferLength)); } catch (Magick::WarningCoder &warning) { cerr << "Coder Warning: " << warning.what() << endl; } catch (Magick::Warning &warning) { @@ -50,14 +47,7 @@ Napi::Value Explode(const Napi::CallbackInfo &info) { writeImages(blurred.begin(), blurred.end(), &blob); - Napi::Object result = Napi::Object::New(env); - result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), - blob.length())); - result.Set("type", type); - return result; - } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); - } catch (...) { - throw Napi::Error::New(env, "Unknown error"); - } + *DataSize = blob.length(); + + return (char*) blob.data(); } \ No newline at end of file diff --git a/natives/explode.h b/natives/explode.h index 045b615..aab879d 100644 --- a/natives/explode.h +++ b/natives/explode.h @@ -1,5 +1,10 @@ #pragma once #include +#include +#include -Napi::Value Explode(const Napi::CallbackInfo& info); +using std::map; +using std::string; + +char* Explode(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/flag.cc b/natives/flag.cc index 990a672..f882e22 100644 --- a/natives/flag.cc +++ b/natives/flag.cc @@ -1,27 +1,24 @@ #include #include +#include +#include using namespace std; using namespace vips; -Napi::Value Flag(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char* Flag(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - string overlay = obj.Get("overlay").As().Utf8Value(); - string type = obj.Get("type").As().Utf8Value(); - string basePath = obj.Get("basePath").As().Utf8Value(); + string overlay = Arguments["overlay"]; + string basePath = Arguments["basePath"]; - VOption *options = VImage::option()->set("access", "sequential"); + 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); + VImage::new_from_buffer(BufferData, BufferLength, "", + type == "gif" ? options->set("n", -1) : options) + .colourspace(VIPS_INTERPRETATION_sRGB); + if (!in.has_alpha()) in = in.bandjoin(255); int width = in.width(); @@ -54,20 +51,11 @@ Napi::Value Flag(const Napi::CallbackInfo &info) { final.set(VIPS_META_PAGE_HEIGHT, pageHeight); void *buf; - size_t length; final.write_to_buffer( - ("." + type).c_str(), &buf, &length, + ("." + type).c_str(), &buf, DataSize, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } - - vips_error_clear(); - vips_thread_shutdown(); - return result; + vips_error_clear(); + vips_thread_shutdown(); + return (char*) buf; } \ No newline at end of file diff --git a/natives/flag.h b/natives/flag.h index 5f146f6..c89bc02 100644 --- a/natives/flag.h +++ b/natives/flag.h @@ -1,5 +1,10 @@ #pragma once #include +#include +#include -Napi::Value Flag(const Napi::CallbackInfo& info); +using std::map; +using std::string; + +char* Flag(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/flip.cc b/natives/flip.cc index cb3a973..b117ca6 100644 --- a/natives/flip.cc +++ b/natives/flip.cc @@ -1,23 +1,20 @@ -#include +#include "common.h" + +#include #include using namespace std; using namespace vips; -Napi::Value Flip(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char* Flip(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - bool flop = - obj.Has("flop") ? obj.Get("flop").As().Value() : false; - string type = obj.Get("type").As().Utf8Value(); + bool flop = MAP_GET(Arguments, "flop") == "true"; + + VOption *options = VImage::option()->set("access", "sequential"); VImage in = - VImage::new_from_buffer(data.Data(), data.Length(), "", + VImage::new_from_buffer(BufferData, BufferLength, "", type == "gif" ? VImage::option()->set("n", -1)->set("access", "sequential") : 0) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -41,19 +38,13 @@ Napi::Value Flip(const Napi::CallbackInfo &info) { out = in.flip(VIPS_DIRECTION_VERTICAL); } - void *buf; - size_t length; - out.write_to_buffer(("." + type).c_str(), &buf, &length); + void* buf; + out.write_to_buffer( + ("." + type).c_str(), &buf, DataSize, + type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + : 0); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } - - vips_error_clear(); - vips_thread_shutdown(); - return result; + vips_error_clear(); + vips_thread_shutdown(); + return (char*) buf; } \ No newline at end of file diff --git a/natives/flip.h b/natives/flip.h index 278ffe3..9ee6085 100644 --- a/natives/flip.h +++ b/natives/flip.h @@ -1,5 +1,10 @@ #pragma once #include +#include +#include -Napi::Value Flip(const Napi::CallbackInfo& info); +using std::map; +using std::string; + +char* Flip(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/freeze.cc b/natives/freeze.cc index 0fc7abd..3844bf9 100644 --- a/natives/freeze.cc +++ b/natives/freeze.cc @@ -10,7 +10,7 @@ Napi::Value Freeze(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); bool loop = obj.Has("loop") ? obj.Get("loop").As().Value() : false; diff --git a/natives/gamexplain.cc b/natives/gamexplain.cc index 1799319..65cc372 100644 --- a/natives/gamexplain.cc +++ b/natives/gamexplain.cc @@ -10,7 +10,7 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); diff --git a/natives/globe.cc b/natives/globe.cc index fc53dda..b0abc9a 100644 --- a/natives/globe.cc +++ b/natives/globe.cc @@ -10,7 +10,7 @@ Napi::Value Globe(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); diff --git a/natives/homebrew.cc b/natives/homebrew.cc index eea113b..7d16b3a 100644 --- a/natives/homebrew.cc +++ b/natives/homebrew.cc @@ -10,7 +10,7 @@ Napi::Value Homebrew(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); string caption = obj.Get("caption").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); diff --git a/natives/image.cc b/natives/image.cc index fcca25a..c636ae3 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -1,4 +1,9 @@ +#include "common.h" + #include +#include +#include + #include "blur.h" #include "colors.h" #include "caption.h" @@ -37,58 +42,124 @@ #include "watermark.h" #include "whisper.h" #include "zamn.h" +#include #ifdef _WIN32 #include #endif #include -Napi::Object Init(Napi::Env env, Napi::Object exports) -{ +using namespace std; + +std::map Arguments, size_t* DataSize)> FunctionMap = { + {"caption", &Caption}, + {"caption2", &CaptionTwo}, + {"blur", &Blur}, + {"circle", &Circle}, + {"colors", &Colors}, + {"crop", &Crop}, + {"deepfry", &Deepfry}, + {"explode", &Explode}, + {"flag", &Flag}, + {"flip", &Flip}, + {"watermark", &Watermark}, + {"uncaption", &Uncaption} +}; + +std::map OldFunctionMap = { + {"speed", Speed}, + {"freeze", Freeze}, + {"gamexplain", Gamexplain}, + {"globe", Globe}, + {"homebrew", Homebrew}, + {"invert", Invert}, + {"jpeg", Jpeg}, + {"magik", Magik}, + {"meme", Meme}, + {"mirror", Mirror}, + {"motivate", Motivate}, + {"reddit", Reddit}, + {"resize", Resize}, + {"reverse", Reverse}, + {"scott", Scott}, + {"snapchat", Snapchat}, + {"sonic", Sonic}, + {"spin", Spin}, + {"swirl", Swirl}, + {"tile", Tile}, + {"togif", ToGif}, + {"uncanny", Uncanny}, + {"wall", Wall}, + {"whisper", Whisper}, + {"zamn", Zamn} +}; + +Napi::Value NewProcessImage(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); + + try { + string command = info[0].As().Utf8Value(); + Napi::Object obj = info[1].As(); + Napi::Buffer data = obj.Get("data").As>(); + string type = obj.Get("type").As().Utf8Value(); + + Napi::Array properties = obj.GetPropertyNames(); + + std::map Arguments; + + for (unsigned int i = 0; i < properties.Length(); i++) { + string property = properties.Get(uint32_t(i)).As().Utf8Value(); + + if (property == "data") { + continue; + } + + Arguments[property] = obj.Get(property).ToString().As().Utf8Value(); + } + + size_t length = 0; + char* buf = FunctionMap.at(command)(type, data.Data(), data.Length(), Arguments, &length); + result.Set("data", Napi::Buffer::New(env, buf, length, [](Napi::Env env, void* data) { + free(data); + })); + result.Set("type", type); + } catch (std::exception const &err) { + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); + } catch (...) { + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); + } + + return result; +} + +Napi::Value OldProcessImage(std::string FunctionName, const Napi::CallbackInfo &info) { + return OldFunctionMap.at(FunctionName)(info); +} + +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); + } else if (MAP_HAS(OldFunctionMap, command)) { + return OldProcessImage(command, info); + } else { + Napi::Error::New(env, "Invalid command").ThrowAsJavaScriptException(); + } +} + +Napi::Object Init(Napi::Env env, Napi::Object exports){ #ifdef _WIN32 Magick::InitializeMagick(""); #endif if (vips_init("")) vips_error_exit(NULL); - exports.Set(Napi::String::New(env, "blur"), Napi::Function::New(env, Blur)); - exports.Set(Napi::String::New(env, "colors"), Napi::Function::New(env, Colors)); - exports.Set(Napi::String::New(env, "caption"), Napi::Function::New(env, Caption)); - exports.Set(Napi::String::New(env, "captionTwo"), Napi::Function::New(env, CaptionTwo)); - exports.Set(Napi::String::New(env, "circle"), Napi::Function::New(env, Circle)); - exports.Set(Napi::String::New(env, "crop"), Napi::Function::New(env, Crop)); - exports.Set(Napi::String::New(env, "deepfry"), Napi::Function::New(env, Deepfry)); - exports.Set(Napi::String::New(env, "explode"), Napi::Function::New(env, Explode)); - exports.Set(Napi::String::New(env, "flag"), Napi::Function::New(env, Flag)); - exports.Set(Napi::String::New(env, "flip"), Napi::Function::New(env, Flip)); - exports.Set(Napi::String::New(env, "freeze"), Napi::Function::New(env, Freeze)); - exports.Set(Napi::String::New(env, "gamexplain"), Napi::Function::New(env, Gamexplain)); - exports.Set(Napi::String::New(env, "globe"), Napi::Function::New(env, Globe)); - exports.Set(Napi::String::New(env, "homebrew"), Napi::Function::New(env, Homebrew)); - exports.Set(Napi::String::New(env, "invert"), Napi::Function::New(env, Invert)); - exports.Set(Napi::String::New(env, "jpeg"), Napi::Function::New(env, Jpeg)); - exports.Set(Napi::String::New(env, "magik"), Napi::Function::New(env, Magik)); - exports.Set(Napi::String::New(env, "meme"), Napi::Function::New(env, Meme)); - exports.Set(Napi::String::New(env, "mirror"), Napi::Function::New(env, Mirror)); - exports.Set(Napi::String::New(env, "motivate"), Napi::Function::New(env, Motivate)); - exports.Set(Napi::String::New(env, "reddit"), Napi::Function::New(env, Reddit)); - exports.Set(Napi::String::New(env, "resize"), Napi::Function::New(env, Resize)); - exports.Set(Napi::String::New(env, "reverse"), Napi::Function::New(env, Reverse)); - exports.Set(Napi::String::New(env, "scott"), Napi::Function::New(env, Scott)); - exports.Set(Napi::String::New(env, "snapchat"), Napi::Function::New(env, Snapchat)); - exports.Set(Napi::String::New(env, "sonic"), Napi::Function::New(env, Sonic)); - exports.Set(Napi::String::New(env, "speed"), Napi::Function::New(env, Speed)); - exports.Set(Napi::String::New(env, "spin"), Napi::Function::New(env, Spin)); - exports.Set(Napi::String::New(env, "squish"), Napi::Function::New(env, Squish)); - exports.Set(Napi::String::New(env, "swirl"), Napi::Function::New(env, Swirl)); - exports.Set(Napi::String::New(env, "tile"), Napi::Function::New(env, Tile)); - exports.Set(Napi::String::New(env, "togif"), Napi::Function::New(env, ToGif)); - exports.Set(Napi::String::New(env, "uncanny"), Napi::Function::New(env, Uncanny)); - exports.Set(Napi::String::New(env, "uncaption"), Napi::Function::New(env, Uncaption)); - exports.Set(Napi::String::New(env, "wall"), Napi::Function::New(env, Wall)); - exports.Set(Napi::String::New(env, "watermark"), Napi::Function::New(env, Watermark)); - exports.Set(Napi::String::New(env, "whisper"), Napi::Function::New(env, Whisper)); - exports.Set(Napi::String::New(env, "zamn"), Napi::Function::New(env, Zamn)); - return exports; + exports.Set(Napi::String::New(env, "image"), Napi::Function::New(env, ProcessImage)); // new function handler + + return exports; } NODE_API_MODULE(addon, Init) diff --git a/natives/invert.cc b/natives/invert.cc index 21b678a..4a97802 100644 --- a/natives/invert.cc +++ b/natives/invert.cc @@ -10,7 +10,7 @@ Napi::Value Invert(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); diff --git a/natives/jpeg.cc b/natives/jpeg.cc index 858d2b3..5298c81 100644 --- a/natives/jpeg.cc +++ b/natives/jpeg.cc @@ -10,7 +10,7 @@ Napi::Value Jpeg(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); int quality = obj.Has("quality") ? obj.Get("quality").As().Int32Value() diff --git a/natives/magik.cc b/natives/magik.cc index da36307..f1b0b4c 100644 --- a/natives/magik.cc +++ b/natives/magik.cc @@ -12,7 +12,7 @@ Napi::Value Magik(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); diff --git a/natives/meme.cc b/natives/meme.cc index ed307b8..250581e 100644 --- a/natives/meme.cc +++ b/natives/meme.cc @@ -11,7 +11,7 @@ Napi::Value Meme(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string top = obj.Get("top").As().Utf8Value(); string bottom = obj.Get("bottom").As().Utf8Value(); diff --git a/natives/mirror.cc b/natives/mirror.cc index 8d6b3b2..5467aa1 100644 --- a/natives/mirror.cc +++ b/natives/mirror.cc @@ -10,7 +10,7 @@ Napi::Value Mirror(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); bool vertical = obj.Has("vertical") ? obj.Get("vertical").As().Value() diff --git a/natives/motivate.cc b/natives/motivate.cc index 48834c2..0bce582 100644 --- a/natives/motivate.cc +++ b/natives/motivate.cc @@ -11,7 +11,7 @@ Napi::Value Motivate(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string top_text = obj.Get("top").As().Utf8Value(); string bottom_text = obj.Get("bottom").As().Utf8Value(); diff --git a/natives/reddit.cc b/natives/reddit.cc index 5a33779..7958e16 100644 --- a/natives/reddit.cc +++ b/natives/reddit.cc @@ -10,7 +10,7 @@ Napi::Value Reddit(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string text = obj.Get("caption").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); diff --git a/natives/resize.cc b/natives/resize.cc index 157d32f..6597801 100644 --- a/natives/resize.cc +++ b/natives/resize.cc @@ -10,7 +10,7 @@ Napi::Value Resize(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); bool stretch = obj.Has("stretch") ? obj.Get("stretch").As().Value() diff --git a/natives/reverse.cc b/natives/reverse.cc index 90789a3..c03576f 100644 --- a/natives/reverse.cc +++ b/natives/reverse.cc @@ -10,7 +10,7 @@ Napi::Value Reverse(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); bool soos = obj.Has("soos") ? obj.Get("soos").As().Value() : false; diff --git a/natives/scott.cc b/natives/scott.cc index 523c341..77def5c 100644 --- a/natives/scott.cc +++ b/natives/scott.cc @@ -12,7 +12,7 @@ Napi::Value Scott(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); diff --git a/natives/snapchat.cc b/natives/snapchat.cc index e7e58c7..d896c92 100644 --- a/natives/snapchat.cc +++ b/natives/snapchat.cc @@ -10,7 +10,7 @@ Napi::Value Snapchat(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string caption = obj.Get("caption").As().Utf8Value(); float pos = diff --git a/natives/sonic.cc b/natives/sonic.cc index a797c6d..0f501b3 100644 --- a/natives/sonic.cc +++ b/natives/sonic.cc @@ -10,7 +10,7 @@ Napi::Value Sonic(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); string text = obj.Get("text").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); diff --git a/natives/speed.cc b/natives/speed.cc index 3c0b008..4f4e619 100644 --- a/natives/speed.cc +++ b/natives/speed.cc @@ -45,7 +45,7 @@ Napi::Value Speed(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); bool slow = obj.Has("slow") ? obj.Get("slow").As().Value() : false; @@ -92,7 +92,9 @@ Napi::Value Speed(const Napi::CallbackInfo &info) { removeFrames = true; break; } + memset16(lastPos + 5, new_delay, 1); + lastPos = (char *)memchr(lastPos + 1, '\x00', (data.Length() - (lastPos - fileData)) - 1); ++currentFrame; diff --git a/natives/speed.h b/natives/speed.h index d80b0dc..1a0f97d 100644 --- a/natives/speed.h +++ b/natives/speed.h @@ -2,4 +2,4 @@ #include -Napi::Value Speed(const Napi::CallbackInfo& info); +Napi::Value Speed(const Napi::CallbackInfo &info); \ No newline at end of file diff --git a/natives/spin.cc b/natives/spin.cc index 95e6211..3a7af6e 100644 --- a/natives/spin.cc +++ b/natives/spin.cc @@ -11,7 +11,7 @@ Napi::Value Spin(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); int delay = diff --git a/natives/squish.cc b/natives/squish.cc index 5b0e573..4a3ebaf 100644 --- a/natives/squish.cc +++ b/natives/squish.cc @@ -11,7 +11,7 @@ Napi::Value Squish(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); diff --git a/natives/swirl.cc b/natives/swirl.cc index 6471555..6da83f1 100644 --- a/natives/swirl.cc +++ b/natives/swirl.cc @@ -10,7 +10,7 @@ Napi::Value Swirl(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); diff --git a/natives/tile.cc b/natives/tile.cc index 03f238f..8e97c13 100644 --- a/natives/tile.cc +++ b/natives/tile.cc @@ -12,7 +12,7 @@ Napi::Value Tile(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); diff --git a/natives/togif.cc b/natives/togif.cc index 1cc234e..0fb47c6 100644 --- a/natives/togif.cc +++ b/natives/togif.cc @@ -10,7 +10,7 @@ Napi::Value ToGif(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); diff --git a/natives/uncanny.cc b/natives/uncanny.cc index 758a59f..d6fc3f5 100644 --- a/natives/uncanny.cc +++ b/natives/uncanny.cc @@ -11,7 +11,7 @@ Napi::Value Uncanny(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string caption = obj.Get("caption").As().Utf8Value(); string caption2 = obj.Get("caption2").As().Utf8Value(); diff --git a/natives/uncaption.cc b/natives/uncaption.cc index bb4aa68..10144ca 100644 --- a/natives/uncaption.cc +++ b/natives/uncaption.cc @@ -1,26 +1,21 @@ -#include +#include "common.h" #include +#include using namespace std; using namespace vips; -Napi::Value Uncaption(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char* Uncaption(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - float tolerance = obj.Has("tolerance") - ? obj.Get("tolerance").As().FloatValue() + float tolerance = MAP_HAS(Arguments, "tolerance") + ? stof(Arguments["tolerance"]) : 0.5; - string type = obj.Get("type").As().Utf8Value(); VOption *options = VImage::option(); VImage in = - VImage::new_from_buffer(data.Data(), data.Length(), "", + 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); @@ -50,20 +45,12 @@ Napi::Value Uncaption(const Napi::CallbackInfo &info) { final.set(VIPS_META_PAGE_HEIGHT, newHeight); void *buf; - size_t length; final.write_to_buffer( - ("." + type).c_str(), &buf, &length, + ("." + type).c_str(), &buf, DataSize, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } vips_error_clear(); vips_thread_shutdown(); - return result; + return (char*) buf; } diff --git a/natives/uncaption.h b/natives/uncaption.h index fcfbd0d..fd8e1b6 100644 --- a/natives/uncaption.h +++ b/natives/uncaption.h @@ -1,5 +1,5 @@ #pragma once -#include +#include -Napi::Value Uncaption(const Napi::CallbackInfo& info); +char* Uncaption(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/wall.cc b/natives/wall.cc index b6fab85..f9c1184 100644 --- a/natives/wall.cc +++ b/natives/wall.cc @@ -12,7 +12,7 @@ Napi::Value Wall(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); diff --git a/natives/watermark.cc b/natives/watermark.cc index 3588b52..4ba2f75 100644 --- a/natives/watermark.cc +++ b/natives/watermark.cc @@ -1,40 +1,32 @@ -#include +#include "common.h" #include +#include using namespace std; using namespace vips; -Napi::Value Watermark(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char* Watermark(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize) { - try { - Napi::Object obj = info[0].As(); - Napi::Buffer data = obj.Get("data").As>(); - string water = obj.Get("water").As().Utf8Value(); - int gravity = obj.Get("gravity").As().Int64Value(); - bool resize = obj.Has("resize") - ? obj.Get("resize").As().Value() - : false; - float yscale = obj.Has("yscale") - ? obj.Get("yscale").As().FloatValue() - : false; - bool append = obj.Has("append") - ? obj.Get("append").As().Value() - : false; - bool alpha = - obj.Has("alpha") ? obj.Get("alpha").As().Value() : false; - bool flip = - obj.Has("flip") ? obj.Get("flip").As().Value() : false; - bool mc = obj.Has("mc") ? obj.Get("mc").As().Value() : false; - string basePath = obj.Get("basePath").As().Utf8Value(); - string type = obj.Get("type").As().Utf8Value(); + string water = Arguments["water"]; + int gravity = stoi(Arguments["gravity"]); + + bool resize = MAP_HAS(Arguments, "resize") ? Arguments["resize"] == "true" : false;; + float yscale = MAP_HAS(Arguments, "yscale") ? stof(Arguments["yscale"]) : false; + + bool append = MAP_HAS(Arguments, "append") ? Arguments["append"] == "true" : false; + + bool alpha = MAP_HAS(Arguments, "alpha") ? Arguments["alpha"] == "true" : false; + bool flip = MAP_HAS(Arguments, "flip") ? Arguments["flip"] == "true" : false; + + bool mc = MAP_HAS(Arguments, "mc"); + + string basePath = Arguments["basePath"]; VOption *options = VImage::option()->set("access", "sequential"); VImage in = - VImage::new_from_buffer(data.Data(), data.Length(), "", + VImage::new_from_buffer(BufferData, BufferLength, "", type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -152,20 +144,11 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) { final.set(VIPS_META_PAGE_HEIGHT, pageHeight + addedHeight); void *buf; - size_t length; final.write_to_buffer( - ("." + type).c_str(), &buf, &length, + ("." + type).c_str(), &buf, DataSize, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); - } - vips_error_clear(); vips_thread_shutdown(); - return result; + return (char*) buf; } diff --git a/natives/watermark.h b/natives/watermark.h index e3233b9..5a82641 100644 --- a/natives/watermark.h +++ b/natives/watermark.h @@ -2,4 +2,4 @@ #include -Napi::Value Watermark(const Napi::CallbackInfo& info); +char* Watermark(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/whisper.cc b/natives/whisper.cc index b8b0426..4a5b8e5 100644 --- a/natives/whisper.cc +++ b/natives/whisper.cc @@ -10,7 +10,7 @@ Napi::Value Whisper(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string caption = obj.Get("caption").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); diff --git a/natives/zamn.cc b/natives/zamn.cc index d4e51ab..654bb92 100644 --- a/natives/zamn.cc +++ b/natives/zamn.cc @@ -10,7 +10,7 @@ Napi::Value Zamn(const Napi::CallbackInfo &info) { Napi::Object result = Napi::Object::New(env); try { - Napi::Object obj = info[0].As(); + Napi::Object obj = info[1].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); diff --git a/utils/image-runner.js b/utils/image-runner.js index 23387bc..1a93e7b 100644 --- a/utils/image-runner.js +++ b/utils/image-runner.js @@ -47,7 +47,7 @@ export default function run(object) { } objectWithFixedType.basePath = path.join(path.dirname(fileURLToPath(import.meta.url)), "../"); try { - const result = img[object.cmd](objectWithFixedType); + const result = img.image(object.cmd, objectWithFixedType); const returnObject = { buffer: result.data, fileExtension: result.type