diff --git a/natives/blur.cc b/natives/blur.cc index 0324b7f..3ce0257 100644 --- a/natives/blur.cc +++ b/natives/blur.cc @@ -7,14 +7,14 @@ using namespace std; using namespace vips; -char *Blur(string type, char *BufferData, size_t BufferLength, +char *Blur(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { bool sharp = GetArgument(Arguments, "sharp"); VOption *options = VImage::option()->set("access", "sequential"); VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) @@ -26,7 +26,7 @@ char *Blur(string type, char *BufferData, size_t BufferLength, sharp ? in.sharpen(VImage::option()->set("sigma", 3)) : in.gaussblur(15); void *buf; - out.write_to_buffer(("." + type).c_str(), &buf, DataSize); + out.write_to_buffer(("." + *type).c_str(), &buf, DataSize); vips_error_clear(); vips_thread_shutdown(); diff --git a/natives/blur.h b/natives/blur.h index af72efb..1092fc3 100644 --- a/natives/blur.h +++ b/natives/blur.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Blur(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Blur(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/caption.cc b/natives/caption.cc index c5968da..d1afdbf 100644 --- a/natives/caption.cc +++ b/natives/caption.cc @@ -7,7 +7,7 @@ using namespace std; using namespace vips; -char *Caption(string type, char *BufferData, size_t BufferLength, +char *Caption(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { string caption = GetArgument(Arguments, "caption"); @@ -18,7 +18,7 @@ char *Caption(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) @@ -60,7 +60,7 @@ char *Caption(string type, char *BufferData, size_t BufferLength, vector img; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; VImage frame = captionImage.join( img_frame, VIPS_DIRECTION_VERTICAL, VImage::option()->set("background", 0xffffff)->set("expand", true)); @@ -71,8 +71,8 @@ char *Caption(string type, char *BufferData, size_t BufferLength, void *buf; final.write_to_buffer( - ("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); vips_error_clear(); diff --git a/natives/caption.h b/natives/caption.h index a0a822f..1792125 100644 --- a/natives/caption.h +++ b/natives/caption.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Caption(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Caption(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/caption2.cc b/natives/caption2.cc index d4c1dcc..2adb365 100644 --- a/natives/caption2.cc +++ b/natives/caption2.cc @@ -7,7 +7,7 @@ using namespace std; using namespace vips; -char *CaptionTwo(string type, char *BufferData, size_t BufferLength, +char *CaptionTwo(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { bool top = GetArgument(Arguments, "top"); @@ -19,7 +19,7 @@ char *CaptionTwo(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) @@ -60,7 +60,7 @@ char *CaptionTwo(string type, char *BufferData, size_t BufferLength, vector img; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; VImage frame = (top ? captionImage : img_frame) .join(top ? img_frame : captionImage, VIPS_DIRECTION_VERTICAL, @@ -74,8 +74,8 @@ char *CaptionTwo(string type, char *BufferData, size_t BufferLength, void *buf; final.write_to_buffer( - ("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); vips_error_clear(); diff --git a/natives/caption2.h b/natives/caption2.h index 2c9149e..7719f4a 100644 --- a/natives/caption2.h +++ b/natives/caption2.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* CaptionTwo(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* CaptionTwo(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/circle.cc b/natives/circle.cc index aca74ae..d0e0232 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -10,7 +10,7 @@ using namespace std; using namespace Magick; -char *Circle(string type, char *BufferData, size_t BufferLength, +char *Circle(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { Blob blob; @@ -29,13 +29,13 @@ char *Circle(string type, char *BufferData, size_t BufferLength, for (Image &image : coalesced) { image.rotationalBlur(10); - image.magick(type); + image.magick(*type); blurred.push_back(image); } optimizeTransparency(blurred.begin(), blurred.end()); - if (type == "gif") { + if (*type == "gif") { for (Image &image : blurred) { image.quantizeDitherMethod(FloydSteinbergDitherMethod); image.quantize(); diff --git a/natives/circle.h b/natives/circle.h index 5cbce38..cc577e8 100644 --- a/natives/circle.h +++ b/natives/circle.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Circle(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Circle(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/colors.cc b/natives/colors.cc index cd495d5..757d1c3 100644 --- a/natives/colors.cc +++ b/natives/colors.cc @@ -10,7 +10,7 @@ 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); -char *Colors(string type, char *BufferData, size_t BufferLength, +char *Colors(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { string color = GetArgument(Arguments, "color"); @@ -19,7 +19,7 @@ char *Colors(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); VImage out; @@ -31,7 +31,7 @@ char *Colors(string type, char *BufferData, size_t BufferLength, } void *buf; - out.write_to_buffer(("." + type).c_str(), &buf, DataSize); + out.write_to_buffer(("." + *type).c_str(), &buf, DataSize); vips_error_clear(); vips_thread_shutdown(); diff --git a/natives/colors.h b/natives/colors.h index 435cafd..8106b65 100644 --- a/natives/colors.h +++ b/natives/colors.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Colors(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Colors(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/crop.cc b/natives/crop.cc index 16b21a2..4d895d5 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -6,14 +6,14 @@ using namespace std; using namespace vips; -char *Crop(string type, char *BufferData, size_t BufferLength, +char *Crop(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option()->set("access", "sequential"); VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); int width = in.width(); @@ -24,7 +24,7 @@ char *Crop(string type, char *BufferData, size_t BufferLength, int finalHeight = 0; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; int frameWidth = img_frame.width(); int frameHeight = img_frame.height(); bool widthOrHeight = frameWidth / frameHeight >= 1; @@ -42,8 +42,8 @@ char *Crop(string type, char *BufferData, size_t BufferLength, void *buf; final.write_to_buffer( - ("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); vips_error_clear(); diff --git a/natives/crop.h b/natives/crop.h index e497d94..5745da4 100644 --- a/natives/crop.h +++ b/natives/crop.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Crop(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Crop(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/deepfry.cc b/natives/deepfry.cc index 0ed23b2..b6f09e7 100644 --- a/natives/deepfry.cc +++ b/natives/deepfry.cc @@ -5,14 +5,14 @@ using namespace std; using namespace vips; -char *Deepfry(string type, char *BufferData, size_t BufferLength, +char *Deepfry(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option()->set("access", "sequential"); VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) @@ -26,7 +26,7 @@ char *Deepfry(string type, char *BufferData, size_t BufferLength, VImage fried = (in * 1.3 - (255.0 * 1.3 - 255.0)) * 1.5; VImage final; - if (totalHeight > 65500 && type == "gif") { + if (totalHeight > 65500 && *type == "gif") { vector img; for (int i = 0; i < nPages; i++) { VImage img_frame = in.crop(0, i * pageHeight, width, pageHeight); @@ -49,13 +49,13 @@ char *Deepfry(string type, char *BufferData, size_t BufferLength, VImage::option()->set("Q", 1)->set("strip", true)); final = VImage::new_from_buffer(jpgBuf, jpgLength, ""); final.set(VIPS_META_PAGE_HEIGHT, pageHeight); - if (type == "gif") + if (*type == "gif") final.set("delay", fried.get_array_int("delay")); } void *buf; - final.write_to_buffer(("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0) : 0); + final.write_to_buffer(("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0) : 0); vips_error_clear(); vips_thread_shutdown(); diff --git a/natives/deepfry.h b/natives/deepfry.h index fbfffce..dfdfc45 100644 --- a/natives/deepfry.h +++ b/natives/deepfry.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Deepfry(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Deepfry(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/explode.cc b/natives/explode.cc index c1675ff..4d6d42c 100644 --- a/natives/explode.cc +++ b/natives/explode.cc @@ -11,7 +11,7 @@ using namespace std; using namespace Magick; -char *Explode(string type, char *BufferData, size_t BufferLength, +char *Explode(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { int amount = GetArgument(Arguments, "amount"); @@ -33,13 +33,13 @@ char *Explode(string type, char *BufferData, size_t BufferLength, for (Image &image : coalesced) { image.implode(amount); - image.magick(type); + image.magick(*type); blurred.push_back(image); } optimizeTransparency(blurred.begin(), blurred.end()); - if (type == "gif") { + if (*type == "gif") { for (Image &image : blurred) { image.quantizeDither(false); image.quantize(); diff --git a/natives/explode.h b/natives/explode.h index d734d13..6967d91 100644 --- a/natives/explode.h +++ b/natives/explode.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Explode(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Explode(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/flag.cc b/natives/flag.cc index c2d81dc..790da54 100644 --- a/natives/flag.cc +++ b/natives/flag.cc @@ -7,7 +7,7 @@ using namespace std; using namespace vips; -char *Flag(string type, char *BufferData, size_t BufferLength, +char *Flag(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { string overlay = GetArgument(Arguments, "overlay"); @@ -17,7 +17,7 @@ char *Flag(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) @@ -43,7 +43,7 @@ char *Flag(string type, char *BufferData, size_t BufferLength, vector img; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; VImage composited = img_frame.composite2(overlayImage, VIPS_BLEND_MODE_OVER); img.push_back(composited); @@ -54,8 +54,8 @@ char *Flag(string type, char *BufferData, size_t BufferLength, void *buf; final.write_to_buffer( - ("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); vips_error_clear(); diff --git a/natives/flag.h b/natives/flag.h index c1f17e1..c392cae 100644 --- a/natives/flag.h +++ b/natives/flag.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Flag(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Flag(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/flip.cc b/natives/flip.cc index 2fd34ff..c72b584 100644 --- a/natives/flip.cc +++ b/natives/flip.cc @@ -7,13 +7,13 @@ using namespace std; using namespace vips; -char *Flip(string type, char *BufferData, size_t BufferLength, +char *Flip(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { bool flop = GetArgument(Arguments, "flop"); VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" + *type == "gif" ? VImage::option()->set("n", -1)->set( "access", "sequential") : 0) @@ -24,7 +24,7 @@ char *Flip(string type, char *BufferData, size_t BufferLength, VImage out; if (flop) { out = in.flip(VIPS_DIRECTION_HORIZONTAL); - } else if (type == "gif") { + } else if (*type == "gif") { // libvips gif handling is both a blessing and a curse vector img; int pageHeight = vips_image_get_page_height(in.get_image()); @@ -42,8 +42,8 @@ char *Flip(string type, char *BufferData, size_t BufferLength, void *buf; out.write_to_buffer( - ("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); vips_error_clear(); diff --git a/natives/flip.h b/natives/flip.h index 1654adc..ec66b15 100644 --- a/natives/flip.h +++ b/natives/flip.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Flip(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Flip(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/freeze.cc b/natives/freeze.cc index c40d7c2..8e3b1ce 100644 --- a/natives/freeze.cc +++ b/natives/freeze.cc @@ -8,7 +8,7 @@ using namespace std; using namespace vips; -char *Freeze(string type, char *BufferData, size_t BufferLength, +char *Freeze(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { bool loop = GetArgumentWithFallback(Arguments, "loop", false); @@ -54,7 +54,7 @@ char *Freeze(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -67,7 +67,7 @@ char *Freeze(string type, char *BufferData, size_t BufferLength, out.set("loop", 1); void *buf; - out.write_to_buffer(("." + type).c_str(), &buf, DataSize); + out.write_to_buffer(("." + *type).c_str(), &buf, DataSize); vips_error_clear(); vips_thread_shutdown(); diff --git a/natives/freeze.h b/natives/freeze.h index ae3a84d..81ceca5 100644 --- a/natives/freeze.h +++ b/natives/freeze.h @@ -4,4 +4,4 @@ using std::string; -char* Freeze(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Freeze(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/gamexplain.cc b/natives/gamexplain.cc index 7521ee3..e497e09 100644 --- a/natives/gamexplain.cc +++ b/natives/gamexplain.cc @@ -5,7 +5,7 @@ using namespace std; using namespace vips; -char *Gamexplain(string type, char *BufferData, size_t BufferLength, +char *Gamexplain(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { string basePath = GetArgument(Arguments, "basePath"); @@ -14,7 +14,7 @@ char *Gamexplain(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -29,7 +29,7 @@ char *Gamexplain(string type, char *BufferData, size_t BufferLength, vector img; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; VImage resized = img_frame .resize(1181.0 / (double)width, @@ -43,8 +43,8 @@ char *Gamexplain(string type, char *BufferData, size_t BufferLength, void *buf; final.write_to_buffer( - ("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); vips_error_clear(); diff --git a/natives/gamexplain.h b/natives/gamexplain.h index c408c05..45289b9 100644 --- a/natives/gamexplain.h +++ b/natives/gamexplain.h @@ -4,4 +4,4 @@ using std::string; -char* Gamexplain(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Gamexplain(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/globe.cc b/natives/globe.cc index 96a6f62..8735c6c 100644 --- a/natives/globe.cc +++ b/natives/globe.cc @@ -5,7 +5,7 @@ using namespace std; using namespace vips; -char *Globe(string type, char *BufferData, size_t BufferLength, +char *Globe(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { string basePath = GetArgument(Arguments, "basePath"); @@ -15,7 +15,7 @@ char *Globe(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer( BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1)->set("access", "sequential") + *type == "gif" ? options->set("n", -1)->set("access", "sequential") : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) @@ -23,7 +23,7 @@ char *Globe(string type, char *BufferData, size_t BufferLength, 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; + int nPages = *type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30; double size = min(width, pageHeight); @@ -51,7 +51,7 @@ char *Globe(string type, char *BufferData, size_t BufferLength, vector img; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; VImage resized = img_frame.resize( size / (double)width, VImage::option()->set("vscale", size / (double)pageHeight)); @@ -65,7 +65,7 @@ char *Globe(string type, char *BufferData, size_t BufferLength, } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, size); - if (type != "gif") { + if (*type != "gif") { vector delay(30, 50); final.set("delay", delay); } diff --git a/natives/globe.h b/natives/globe.h index 5ad036f..af2e1a1 100644 --- a/natives/globe.h +++ b/natives/globe.h @@ -4,4 +4,4 @@ using std::string; -char* Globe(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Globe(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/image.cc b/natives/image.cc index f326f5b..c472cc7 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -51,7 +51,7 @@ using namespace std; -std::map FunctionMap = { +std::map FunctionMap = { {"blur", &Blur}, {"caption", &Caption}, {"captionTwo", &CaptionTwo}, @@ -67,36 +67,33 @@ std::map NoInputFunctionMap = { - {"homebrew", Homebrew}, - {"sonic", Sonic} -}; - -std::map OldFunctionMap = { - {"magik", Magik}, {"scott", Scott}, {"snapchat", Snapchat}, + {"speed", &Speed}, {"spin", Spin}, {"swirl", Swirl}, {"tile", Tile}, {"togif", ToGif}, {"uncanny", Uncanny}, + {"uncaption", &Uncaption}, {"wall", Wall}, + {"watermark", &Watermark}, {"whisper", Whisper}, {"zamn", Zamn} }; +std::map NoInputFunctionMap = { + {"homebrew", Homebrew}, + {"sonic", Sonic} +}; + bool isNapiValueInt(Napi::Env& env, Napi::Value& num) { return env.Global() .Get("Number") @@ -150,7 +147,7 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) { char* buf; if (input) { Napi::Buffer data = obj.Has("data") ? obj.Get("data").As>() : Napi::Buffer::New(env, 0); - buf = FunctionMap.at(command)(type, data.Data(), data.Length(), Arguments, &length); + buf = FunctionMap.at(command)(&type, data.Data(), data.Length(), Arguments, &length); } else { buf = NoInputFunctionMap.at(command)(&type, Arguments, &length); } @@ -167,10 +164,6 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) { 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(); @@ -180,8 +173,6 @@ Napi::Value ProcessImage(const Napi::CallbackInfo &info) { // janky solution for return NewProcessImage(info, true); } else if (MAP_HAS(NoInputFunctionMap, command)) { return NewProcessImage(info, false); - } else if (MAP_HAS(OldFunctionMap, command)) { - return OldProcessImage(command, info); } else { Napi::Error::New(env, "Invalid command").ThrowAsJavaScriptException(); return env.Null(); @@ -208,11 +199,6 @@ Napi::Object Init(Napi::Env env, Napi::Object exports){ arr[i] = Napi::String::New(env, imap.first); i++; } - for(auto const& imap: OldFunctionMap) { - Napi::HandleScope scope(env); - arr[i] = Napi::String::New(env, imap.first); - i++; - } exports.Set(Napi::String::New(env, "funcs"), arr); diff --git a/natives/invert.cc b/natives/invert.cc index 3e64192..cf7a16b 100644 --- a/natives/invert.cc +++ b/natives/invert.cc @@ -5,14 +5,14 @@ using namespace std; using namespace vips; -char *Invert(string type, char *BufferData, size_t BufferLength, +char *Invert(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option()->set("access", "sequential"); VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -23,7 +23,7 @@ char *Invert(string type, char *BufferData, size_t BufferLength, VImage out = inverted.bandjoin(in.extract_band(3)); void *buf; - out.write_to_buffer(("." + type).c_str(), &buf, DataSize); + out.write_to_buffer(("." + *type).c_str(), &buf, DataSize); vips_error_clear(); vips_thread_shutdown(); diff --git a/natives/invert.h b/natives/invert.h index de6f32a..b55e80f 100644 --- a/natives/invert.h +++ b/natives/invert.h @@ -4,4 +4,4 @@ using std::string; -char* Invert(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Invert(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/jpeg.cc b/natives/jpeg.cc index 46412d6..c79b8fc 100644 --- a/natives/jpeg.cc +++ b/natives/jpeg.cc @@ -5,13 +5,13 @@ using namespace std; using namespace vips; -char *Jpeg(string type, char *BufferData, size_t BufferLength, +char *Jpeg(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { int quality = GetArgumentWithFallback(Arguments, "quality", 0); void *buf; - if (type == "gif") { + if (*type == "gif") { VImage in = VImage::new_from_buffer( BufferData, BufferLength, "", VImage::option()->set("access", "sequential")->set("n", -1)) @@ -53,15 +53,15 @@ char *Jpeg(string type, char *BufferData, size_t BufferLength, final.set("delay", in.get_array_int("delay")); } - final.write_to_buffer(("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0) + final.write_to_buffer(("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0) : 0); } else { VImage in = VImage::new_from_buffer(BufferData, BufferLength, ""); in.write_to_buffer(".jpg", &buf, DataSize, VImage::option()->set("Q", quality)->set("strip", true)); - type = "jpg"; + *type = "jpg"; } vips_error_clear(); diff --git a/natives/jpeg.h b/natives/jpeg.h index 1f75b6b..57c5d7c 100644 --- a/natives/jpeg.h +++ b/natives/jpeg.h @@ -4,4 +4,4 @@ using std::string; -char* Jpeg(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Jpeg(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/magik.cc b/natives/magik.cc index f1b0b4c..ad70677 100644 --- a/natives/magik.cc +++ b/natives/magik.cc @@ -1,62 +1,52 @@ +#include "common.h" #include -#include +#include #include #include using namespace std; using namespace Magick; -Napi::Value Magik(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *Magik(string *type, char *BufferData, size_t BufferLength, + ArgumentMap Arguments, size_t *DataSize) { + Blob blob; + + list frames; + list coalesced; + list blurred; try { - Napi::Object obj = info[1].As(); - Napi::Buffer data = obj.Get("data").As>(); - string type = obj.Get("type").As().Utf8Value(); + readImages(&frames, Blob(BufferData, BufferLength)); + } catch (Magick::WarningCoder &warning) { + cerr << "Coder Warning: " << warning.what() << endl; + } catch (Magick::Warning &warning) { + cerr << "Warning: " << warning.what() << endl; + } + coalesceImages(&coalesced, frames.begin(), frames.end()); - Blob blob; - - list frames; - list coalesced; - list blurred; - try { - readImages(&frames, Blob(data.Data(), data.Length())); - } catch (Magick::WarningCoder &warning) { - cerr << "Coder Warning: " << warning.what() << endl; - } catch (Magick::Warning &warning) { - cerr << "Warning: " << warning.what() << endl; - } - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.scale(Geometry("350x350")); - image.liquidRescale(Geometry("175x175")); - image.liquidRescale(Geometry("350x350")); - image.magick(type); - blurred.push_back(image); - } - - optimizeTransparency(blurred.begin(), blurred.end()); - - if (type == "gif") { - for (Image &image : blurred) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - } - - 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(); + for (Image &image : coalesced) { + image.scale(Geometry("350x350")); + image.liquidRescale(Geometry("175x175")); + image.liquidRescale(Geometry("350x350")); + image.magick(*type); + blurred.push_back(image); } - return result; + optimizeTransparency(blurred.begin(), blurred.end()); + + if (*type == "gif") { + for (Image &image : blurred) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } + } + + writeImages(blurred.begin(), blurred.end(), &blob); + + *DataSize = blob.length(); + + char *data = (char *)malloc(*DataSize); + memcpy(data, blob.data(), *DataSize); + return data; } \ No newline at end of file diff --git a/natives/magik.h b/natives/magik.h index e0700df..af3cf10 100644 --- a/natives/magik.h +++ b/natives/magik.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Magik(const Napi::CallbackInfo& info); +using std::string; + +char* Magik(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/meme.cc b/natives/meme.cc index 9ecf8c7..1a9a758 100644 --- a/natives/meme.cc +++ b/natives/meme.cc @@ -5,7 +5,7 @@ using namespace std; using namespace vips; -char *Meme(string type, char *BufferData, size_t BufferLength, +char *Meme(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { string top = GetArgument(Arguments, "top"); string bottom = GetArgument(Arguments, "bottom"); @@ -16,7 +16,7 @@ char *Meme(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -112,7 +112,7 @@ char *Meme(string type, char *BufferData, size_t BufferLength, vector img; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; if (top != "") { img_frame = img_frame.composite2( topText, VIPS_BLEND_MODE_OVER, @@ -132,8 +132,8 @@ char *Meme(string type, char *BufferData, size_t BufferLength, void *buf; final.write_to_buffer( - ("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); vips_error_clear(); diff --git a/natives/meme.h b/natives/meme.h index f0079f5..e940ded 100644 --- a/natives/meme.h +++ b/natives/meme.h @@ -4,4 +4,4 @@ using std::string; -char* Meme(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Meme(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/mirror.cc b/natives/mirror.cc index 07457ed..96bea65 100644 --- a/natives/mirror.cc +++ b/natives/mirror.cc @@ -5,7 +5,7 @@ using namespace std; using namespace vips; -char *Mirror(string type, char *BufferData, size_t BufferLength, +char *Mirror(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { bool vertical = GetArgumentWithFallback(Arguments, "vertical", false); bool first = GetArgumentWithFallback(Arguments, "first", false); @@ -14,7 +14,7 @@ char *Mirror(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -22,7 +22,7 @@ char *Mirror(string type, char *BufferData, size_t BufferLength, VImage out; if (vertical) { - if (type == "gif") { + if (*type == "gif") { // once again, libvips gif handling is both a blessing and a curse vector img; int pageHeight = vips_image_get_page_height(in.get_image()); @@ -59,7 +59,7 @@ char *Mirror(string type, char *BufferData, size_t BufferLength, } void *buf; - out.write_to_buffer(("." + type).c_str(), &buf, DataSize); + out.write_to_buffer(("." + *type).c_str(), &buf, DataSize); vips_error_clear(); vips_thread_shutdown(); diff --git a/natives/mirror.h b/natives/mirror.h index 4b947cd..8db0b2d 100644 --- a/natives/mirror.h +++ b/natives/mirror.h @@ -4,4 +4,4 @@ using std::string; -char* Mirror(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Mirror(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/motivate.cc b/natives/motivate.cc index aafbc26..e800b10 100644 --- a/natives/motivate.cc +++ b/natives/motivate.cc @@ -5,7 +5,7 @@ using namespace std; using namespace vips; -char *Motivate(string type, char *BufferData, size_t BufferLength, +char *Motivate(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { string top_text = GetArgument(Arguments, "top"); string bottom_text = GetArgument(Arguments, "bottom"); @@ -16,7 +16,7 @@ char *Motivate(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -70,7 +70,7 @@ char *Motivate(string type, char *BufferData, size_t BufferLength, int height; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; int borderSize = max(2, width / 66); int borderSize2 = borderSize * 0.5; @@ -117,8 +117,8 @@ char *Motivate(string type, char *BufferData, size_t BufferLength, final.set(VIPS_META_PAGE_HEIGHT, height); void *buf; - final.write_to_buffer(("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 1) : 0); + final.write_to_buffer(("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 1) : 0); vips_error_clear(); vips_thread_shutdown(); diff --git a/natives/motivate.h b/natives/motivate.h index eccf83a..a23f79e 100644 --- a/natives/motivate.h +++ b/natives/motivate.h @@ -4,4 +4,4 @@ using std::string; -char* Motivate(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Motivate(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/reddit.cc b/natives/reddit.cc index d7a4b80..2391e60 100644 --- a/natives/reddit.cc +++ b/natives/reddit.cc @@ -5,7 +5,7 @@ using namespace std; using namespace vips; -char *Reddit(string type, char *BufferData, size_t BufferLength, +char *Reddit(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { string text = GetArgument(Arguments, "text"); @@ -15,7 +15,7 @@ char *Reddit(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -50,7 +50,7 @@ char *Reddit(string type, char *BufferData, size_t BufferLength, vector img; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; VImage frame = img_frame.join(watermark, VIPS_DIRECTION_VERTICAL, VImage::option()->set("expand", true)); img.push_back(frame); @@ -60,8 +60,8 @@ char *Reddit(string type, char *BufferData, size_t BufferLength, void *buf; final.write_to_buffer( - ("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); vips_error_clear(); diff --git a/natives/reddit.h b/natives/reddit.h index 608bbe4..df3f035 100644 --- a/natives/reddit.h +++ b/natives/reddit.h @@ -4,4 +4,4 @@ using std::string; -char* Reddit(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Reddit(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/resize.cc b/natives/resize.cc index d0fb87e..b1bdf92 100644 --- a/natives/resize.cc +++ b/natives/resize.cc @@ -5,7 +5,7 @@ using namespace std; using namespace vips; -char *Resize(string type, char *BufferData, size_t BufferLength, +char *Resize(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { bool stretch = GetArgumentWithFallback(Arguments, "stretch", false); bool wide = GetArgumentWithFallback(Arguments, "wide", false); @@ -14,7 +14,7 @@ char *Resize(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); VImage out; @@ -37,7 +37,7 @@ char *Resize(string type, char *BufferData, size_t BufferLength, vector img; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; VImage resized = img_frame.resize(0.1).resize( 10, VImage::option()->set("kernel", VIPS_KERNEL_NEAREST)); img.push_back(resized); @@ -48,7 +48,7 @@ char *Resize(string type, char *BufferData, size_t BufferLength, out.set(VIPS_META_PAGE_HEIGHT, finalHeight); void *buf; - out.write_to_buffer(("." + type).c_str(), &buf, DataSize); + out.write_to_buffer(("." + *type).c_str(), &buf, DataSize); vips_error_clear(); vips_thread_shutdown(); diff --git a/natives/resize.h b/natives/resize.h index 5ecb7cd..8c7740e 100644 --- a/natives/resize.h +++ b/natives/resize.h @@ -4,4 +4,4 @@ using std::string; -char* Resize(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Resize(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/reverse.cc b/natives/reverse.cc index 1c507da..41fec49 100644 --- a/natives/reverse.cc +++ b/natives/reverse.cc @@ -6,7 +6,7 @@ using namespace std; using namespace vips; -char *Reverse(string type, char *BufferData, size_t BufferLength, +char *Reverse(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { bool soos = GetArgumentWithFallback(Arguments, "soos", false); @@ -53,7 +53,7 @@ char *Reverse(string type, char *BufferData, size_t BufferLength, final.write_to_buffer(".gif", &buf, DataSize, VImage::option()->set("dither", 0)); - type = "gif"; + *type = "gif"; vips_error_clear(); vips_thread_shutdown(); diff --git a/natives/reverse.h b/natives/reverse.h index 9b2959d..41dbff6 100644 --- a/natives/reverse.h +++ b/natives/reverse.h @@ -4,4 +4,4 @@ using std::string; -char* Reverse(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Reverse(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/scott.cc b/natives/scott.cc index 77def5c..0c71578 100644 --- a/natives/scott.cc +++ b/natives/scott.cc @@ -1,73 +1,63 @@ +#include "common.h" #include -#include +#include #include #include using namespace std; using namespace Magick; -Napi::Value Scott(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *Scott(string *type, char *BufferData, size_t BufferLength, + ArgumentMap Arguments, size_t *DataSize) { + string basePath = GetArgument(Arguments, "basePath"); + Blob blob; + + list frames; + list coalesced; + list mid; + Image watermark; try { - 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(); + readImages(&frames, Blob(BufferData, BufferLength)); + } catch (Magick::WarningCoder &warning) { + cerr << "Coder Warning: " << warning.what() << endl; + } catch (Magick::Warning &warning) { + cerr << "Warning: " << warning.what() << endl; + } + watermark.read(basePath + "assets/images/scott.png"); + coalesceImages(&coalesced, frames.begin(), frames.end()); - Blob blob; - - list frames; - list coalesced; - list mid; - Image watermark; - try { - readImages(&frames, Blob(data.Data(), data.Length())); - } catch (Magick::WarningCoder &warning) { - cerr << "Coder Warning: " << warning.what() << endl; - } catch (Magick::Warning &warning) { - cerr << "Warning: " << warning.what() << endl; - } - watermark.read(basePath + "assets/images/scott.png"); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - Image watermark_new = watermark; - image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod); - image.backgroundColor("none"); - image.scale(Geometry("415x234!")); - double arguments[16] = {0, 0, 129, 187, 415, 0, 517, 182, - 415, 234, 517, 465, 0, 234, 132, 418}; - image.distort(Magick::PerspectiveDistortion, 16, arguments, true); - image.extent(Geometry("864x481"), Magick::CenterGravity); - watermark_new.composite(image, Geometry("-110+83"), - Magick::OverCompositeOp); - watermark_new.magick(type); - watermark_new.animationDelay(image.animationDelay()); - mid.push_back(watermark_new); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - } - - writeImages(mid.begin(), mid.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(); + for (Image &image : coalesced) { + Image watermark_new = watermark; + image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod); + image.backgroundColor("none"); + image.scale(Geometry("415x234!")); + double arguments[16] = {0, 0, 129, 187, 415, 0, 517, 182, + 415, 234, 517, 465, 0, 234, 132, 418}; + image.distort(Magick::PerspectiveDistortion, 16, arguments, true); + image.extent(Geometry("864x481"), Magick::CenterGravity); + watermark_new.composite(image, Geometry("-110+83"), + Magick::OverCompositeOp); + watermark_new.magick(*type); + watermark_new.animationDelay(image.animationDelay()); + mid.push_back(watermark_new); } - return result; + optimizeTransparency(mid.begin(), mid.end()); + + if (*type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } + } + + writeImages(mid.begin(), mid.end(), &blob); + + *DataSize = blob.length(); + + char *data = (char *)malloc(*DataSize); + memcpy(data, blob.data(), *DataSize); + return data; } \ No newline at end of file diff --git a/natives/scott.h b/natives/scott.h index deb0b1c..d5076f0 100644 --- a/natives/scott.h +++ b/natives/scott.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Scott(const Napi::CallbackInfo& info); +using std::string; + +char* Scott(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/snapchat.cc b/natives/snapchat.cc index d896c92..7e9398c 100644 --- a/natives/snapchat.cc +++ b/natives/snapchat.cc @@ -1,92 +1,74 @@ -#include +#include "common.h" #include using namespace std; using namespace vips; -Napi::Value Snapchat(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *Snapchat(string *type, char *BufferData, size_t BufferLength, + ArgumentMap Arguments, size_t *DataSize) { + string caption = GetArgument(Arguments, "caption"); + float pos = GetArgumentWithFallback(Arguments, "pos", 0.5); + string basePath = GetArgument(Arguments, "basePath"); - try { - Napi::Object obj = info[1].As(); - Napi::Buffer data = obj.Get("data").As>(); - string caption = obj.Get("caption").As().Utf8Value(); - float pos = - obj.Has("pos") ? obj.Get("pos").As().FloatValue() : 0.5; - string type = obj.Get("type").As().Utf8Value(); - string basePath = obj.Get("basePath").As().Utf8Value(); + VOption *options = VImage::option()->set("access", "sequential"); - VOption *options = VImage::option()->set("access", "sequential"); + VImage in = + 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); - 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 width = in.width(); + int pageHeight = vips_image_get_page_height(in.get_image()); + int nPages = vips_image_get_n_pages(in.get_image()); + int size = width / 20; + int textWidth = width - ((width / 25) * 2); - int width = in.width(); - int pageHeight = vips_image_get_page_height(in.get_image()); - int nPages = vips_image_get_n_pages(in.get_image()); - int size = width / 20; - int textWidth = width - ((width / 25) * 2); + string font_string = "Helvetica Neue, Twemoji Color Font " + to_string(size); - string font_string = - "Helvetica Neue, Twemoji Color Font " + to_string(size); + VImage textIn = VImage::text( + ".", VImage::option()->set( + "fontfile", (basePath + "assets/fonts/caption2.ttf").c_str())); + textIn = VImage::text( + ("" + caption + + "") + .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)); + int bgHeight = textIn.height() + (width / 25); + textIn = ((textIn == (vector){0, 0, 0, 0}).bandand()) + .ifthenelse({0, 0, 0, 178}, textIn) + .embed((width / 2) - (textIn.width() / 2), + (bgHeight / 2) - (textIn.height() / 2), width, bgHeight, + VImage::option() + ->set("extend", "background") + ->set("background", (vector){0, 0, 0, 178})); - VImage textIn = VImage::text( - ".", VImage::option()->set( - "fontfile", (basePath + "assets/fonts/caption2.ttf").c_str())); - textIn = VImage::text( - ("" + caption + - "") - .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)); - int bgHeight = textIn.height() + (width / 25); - textIn = - ((textIn == (vector){0, 0, 0, 0}).bandand()) - .ifthenelse({0, 0, 0, 178}, textIn) - .embed((width / 2) - (textIn.width() / 2), - (bgHeight / 2) - (textIn.height() / 2), width, bgHeight, - VImage::option() - ->set("extend", "background") - ->set("background", (vector){0, 0, 0, 178})); - - vector img; - for (int i = 0; i < nPages; i++) { - VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; - img_frame = img_frame.composite2( - textIn, VIPS_BLEND_MODE_OVER, - VImage::option()->set("x", 0)->set("y", pageHeight * pos)); - img.push_back(img_frame); - } - VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); - final.set(VIPS_META_PAGE_HEIGHT, pageHeight); - - void *buf; - size_t length; - final.write_to_buffer( - ("." + type).c_str(), &buf, &length, - 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(); + vector img; + for (int i = 0; i < nPages; i++) { + VImage img_frame = + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + img_frame = img_frame.composite2( + textIn, VIPS_BLEND_MODE_OVER, + VImage::option()->set("x", 0)->set("y", pageHeight * pos)); + img.push_back(img_frame); } + VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); + final.set(VIPS_META_PAGE_HEIGHT, pageHeight); + + void *buf; + final.write_to_buffer( + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + : 0); vips_error_clear(); vips_thread_shutdown(); - return result; + return (char *)buf; } \ No newline at end of file diff --git a/natives/snapchat.h b/natives/snapchat.h index d7a22b7..f112ed5 100644 --- a/natives/snapchat.h +++ b/natives/snapchat.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Snapchat(const Napi::CallbackInfo& info); +using std::string; + +char* Snapchat(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/speed.cc b/natives/speed.cc index 1498bb1..806a1d4 100644 --- a/natives/speed.cc +++ b/natives/speed.cc @@ -42,7 +42,7 @@ char *vipsRemove(char *data, size_t length, size_t *DataSize, int speed) { return (char *)buf; } -char *Speed(string type, char *BufferData, size_t BufferLength, +char *Speed(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { bool slow = GetArgumentWithFallback(Arguments, "slow", false); diff --git a/natives/speed.h b/natives/speed.h index 08c5c19..a9097f0 100644 --- a/natives/speed.h +++ b/natives/speed.h @@ -4,4 +4,4 @@ using std::string; -char* Speed(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Speed(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/spin.cc b/natives/spin.cc index 3a7af6e..56c14d9 100644 --- a/natives/spin.cc +++ b/natives/spin.cc @@ -1,80 +1,71 @@ +#include "common.h" #include -#include +#include #include #include using namespace std; using namespace Magick; -Napi::Value Spin(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); +char *Spin(string *type, char *BufferData, size_t BufferLength, + ArgumentMap Arguments, size_t *DataSize) { + int delay = GetArgumentWithFallback(Arguments, "delay", 0); + Blob blob; + + list frames; + list coalesced; + list mid; try { - Napi::Object obj = info[1].As(); - Napi::Buffer data = obj.Get("data").As>(); - string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - - Blob blob; - - list frames; - list coalesced; - list mid; - try { - readImages(&frames, Blob(data.Data(), data.Length())); - } catch (Magick::WarningCoder &warning) { - cerr << "Coder Warning: " << warning.what() << endl; - } catch (Magick::Warning &warning) { - cerr << "Warning: " << warning.what() << endl; - } - coalesceImages(&coalesced, frames.begin(), frames.end()); - - if (type != "gif") { - list::iterator it = coalesced.begin(); - for (int i = 0; i < 29; ++i) { - coalesced.push_back(*it); - } - } - - int i = 0; - for (Image &image : coalesced) { - image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod); - image.scale(Geometry("256x256")); - image.alphaChannel(Magick::SetAlphaChannel); - double rotation[1] = {(double)360 * i / coalesced.size()}; - image.distort(Magick::ScaleRotateTranslateDistortion, 1, rotation); - image.magick("GIF"); - mid.push_back(image); - i++; - } - - for_each(mid.begin(), mid.end(), - gifDisposeMethodImage(Magick::BackgroundDispose)); - - optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) { - for_each(mid.begin(), mid.end(), animationDelayImage(delay)); - } else if (type != "gif") { - for_each(mid.begin(), mid.end(), animationDelayImage(5)); - } - - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - - writeImages(mid.begin(), mid.end(), &blob); - - Napi::Object result = Napi::Object::New(env); - result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), - blob.length())); - result.Set("type", "gif"); - return result; - } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); - } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + readImages(&frames, Blob(BufferData, BufferLength)); + } catch (Magick::WarningCoder &warning) { + cerr << "Coder Warning: " << warning.what() << endl; + } catch (Magick::Warning &warning) { + cerr << "Warning: " << warning.what() << endl; } + coalesceImages(&coalesced, frames.begin(), frames.end()); + + if (*type != "gif") { + list::iterator it = coalesced.begin(); + for (int i = 0; i < 29; ++i) { + coalesced.push_back(*it); + } + } + + int i = 0; + for (Image &image : coalesced) { + image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod); + image.scale(Geometry("256x256")); + image.alphaChannel(Magick::SetAlphaChannel); + double rotation[1] = {(double)360 * i / coalesced.size()}; + image.distort(Magick::ScaleRotateTranslateDistortion, 1, rotation); + image.magick("GIF"); + mid.push_back(image); + i++; + } + + for_each(mid.begin(), mid.end(), + gifDisposeMethodImage(Magick::BackgroundDispose)); + + optimizeTransparency(mid.begin(), mid.end()); + if (delay != 0) { + for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + } else if (*type != "gif") { + for_each(mid.begin(), mid.end(), animationDelayImage(5)); + } + + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } + + writeImages(mid.begin(), mid.end(), &blob); + + *type = "gif"; + *DataSize = blob.length(); + + char *data = (char *)malloc(*DataSize); + memcpy(data, blob.data(), *DataSize); + return data; } diff --git a/natives/spin.h b/natives/spin.h index 317f258..62c28f5 100644 --- a/natives/spin.h +++ b/natives/spin.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Spin(const Napi::CallbackInfo& info); +using std::string; + +char* Spin(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/swirl.cc b/natives/swirl.cc index 6da83f1..02ff822 100644 --- a/natives/swirl.cc +++ b/natives/swirl.cc @@ -1,95 +1,80 @@ -#include +#include "common.h" #include using namespace std; using namespace vips; -Napi::Value Swirl(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *Swirl(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()->set("access", "sequential"); - VOption *options = VImage::option()->set("access", "sequential"); + VImage in = + 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); - 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 divSize = {newWidth / 2, newHeight / 2}; - 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 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)); - 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; - int size = min(width, pageHeight) / 2; + VImage test = (1 - polar.extract_band(0) / size); + VImage degrees = test.cast(VIPS_FORMAT_FLOAT).pow(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 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; - 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 img; + for (int i = 0; i < nPages; i++) { + VImage img_frame = + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; - vector 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); - } - VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); - final.set(VIPS_META_PAGE_HEIGHT, pageHeight); - - 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", type); - } catch (std::exception const &err) { - Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); - } catch (...) { - Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); + 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); } + VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); + final.set(VIPS_META_PAGE_HEIGHT, pageHeight); + + void *buf; + final.write_to_buffer(".gif", &buf, DataSize); vips_error_clear(); vips_thread_shutdown(); - return result; + return (char *)buf; } \ No newline at end of file diff --git a/natives/swirl.h b/natives/swirl.h index 5c63855..ec46cea 100644 --- a/natives/swirl.h +++ b/natives/swirl.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Swirl(const Napi::CallbackInfo& info); +using std::string; + +char* Swirl(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/tile.cc b/natives/tile.cc index 8e97c13..93510a9 100644 --- a/natives/tile.cc +++ b/natives/tile.cc @@ -1,75 +1,65 @@ +#include "common.h" #include -#include +#include #include #include using namespace std; using namespace Magick; -Napi::Value Tile(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *Tile(string *type, char *BufferData, size_t BufferLength, + ArgumentMap Arguments, size_t *DataSize) { + Blob blob; + + list frames; + list coalesced; + list mid; try { - Napi::Object obj = info[1].As(); - Napi::Buffer data = obj.Get("data").As>(); - string type = obj.Get("type").As().Utf8Value(); + readImages(&frames, Blob(BufferData, BufferLength)); + } catch (Magick::WarningCoder &warning) { + cerr << "Coder Warning: " << warning.what() << endl; + } catch (Magick::Warning &warning) { + cerr << "Warning: " << warning.what() << endl; + } + coalesceImages(&coalesced, frames.begin(), frames.end()); - Blob blob; - - list frames; - list coalesced; - list mid; - try { - readImages(&frames, Blob(data.Data(), data.Length())); - } catch (Magick::WarningCoder &warning) { - cerr << "Coder Warning: " << warning.what() << endl; - } catch (Magick::Warning &warning) { - cerr << "Warning: " << warning.what() << endl; + for (Image &image : coalesced) { + list duplicated; + Image appended; + list montage; + Image frame; + image.magick(*type); + for (int i = 0; i < 5; ++i) { + duplicated.push_back(image); } - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - list duplicated; - Image appended; - list montage; - Image frame; - image.magick(type); - for (int i = 0; i < 5; ++i) { - duplicated.push_back(image); - } - appendImages(&appended, duplicated.begin(), duplicated.end()); - appended.repage(); - for (int i = 0; i < 5; ++i) { - montage.push_back(appended); - } - appendImages(&frame, montage.begin(), montage.end(), true); - frame.repage(); - frame.scale(Geometry("800x800>")); - frame.animationDelay(image.animationDelay()); - mid.push_back(frame); + appendImages(&appended, duplicated.begin(), duplicated.end()); + appended.repage(); + for (int i = 0; i < 5; ++i) { + montage.push_back(appended); } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - } - - writeImages(mid.begin(), mid.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(); + appendImages(&frame, montage.begin(), montage.end(), true); + frame.repage(); + frame.scale(Geometry("800x800>")); + frame.animationDelay(image.animationDelay()); + mid.push_back(frame); } - return result; + optimizeTransparency(mid.begin(), mid.end()); + + if (*type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } + } + + writeImages(mid.begin(), mid.end(), &blob); + + *DataSize = blob.length(); + + char *data = (char *)malloc(*DataSize); + memcpy(data, blob.data(), *DataSize); + return data; } \ No newline at end of file diff --git a/natives/tile.h b/natives/tile.h index 36dd2a0..666e2cb 100644 --- a/natives/tile.h +++ b/natives/tile.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Tile(const Napi::CallbackInfo& info); +using std::string; + +char* Tile(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/togif.cc b/natives/togif.cc index 0fb47c6..f9e4891 100644 --- a/natives/togif.cc +++ b/natives/togif.cc @@ -1,43 +1,32 @@ -#include +#include "common.h" #include using namespace std; using namespace vips; -Napi::Value ToGif(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *ToGif(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(); + if (*type == "gif") { + vips_error_clear(); + vips_thread_shutdown(); + *DataSize = BufferLength; + return BufferData; + } else { + VOption *options = VImage::option()->set("access", "sequential"); - if (type == "gif") { - result.Set("data", data); - result.Set("type", "gif"); - } else { - VOption *options = VImage::option()->set("access", "sequential"); + VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", + *type == "webp" ? options->set("n", -1) + : options); - VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "", - type == "webp" ? options->set("n", -1) - : options); + void *buf; + in.write_to_buffer(".gif", &buf, DataSize); - void *buf; - size_t length; - in.write_to_buffer(".gif", &buf, &length); + *type = "gif"; - 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(); + vips_error_clear(); + vips_thread_shutdown(); + return (char *)buf; } - - vips_error_clear(); - vips_thread_shutdown(); - return result; } \ No newline at end of file diff --git a/natives/togif.h b/natives/togif.h index e03210c..c97a201 100644 --- a/natives/togif.h +++ b/natives/togif.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value ToGif(const Napi::CallbackInfo& info); +using std::string; + +char* ToGif(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/uncanny.cc b/natives/uncanny.cc index d6fc3f5..453c5de 100644 --- a/natives/uncanny.cc +++ b/natives/uncanny.cc @@ -1,120 +1,104 @@ #include "common.h" -#include #include using namespace std; using namespace vips; -Napi::Value Uncanny(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *Uncanny(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 caption = obj.Get("caption").As().Utf8Value(); - string caption2 = obj.Get("caption2").As().Utf8Value(); - string font = obj.Get("font").As().Utf8Value(); - string type = obj.Get("type").As().Utf8Value(); - string path = obj.Get("path").As().Utf8Value(); - string basePath = obj.Get("basePath").As().Utf8Value(); + string caption = GetArgument(Arguments, "caption"); + string caption2 = GetArgument(Arguments, "caption2"); + string font = GetArgument(Arguments, "font"); + string path = GetArgument(Arguments, "path"); + string basePath = GetArgument(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) - .extract_band(0, VImage::option()->set("n", 3)); + VImage in = + VImage::new_from_buffer(BufferData, BufferLength, "", + *type == "gif" ? options->set("n", -1) : options) + .colourspace(VIPS_INTERPRETATION_sRGB) + .extract_band(0, VImage::option()->set("n", 3)); - VImage base = VImage::black(1280, 720, VImage::option()->set("bands", 3)); + VImage base = VImage::black(1280, 720, VImage::option()->set("bands", 3)); - string font_string = (font == "roboto" ? "Roboto Condensed" : font) + ", Twemoji Color Font " + - (font != "impact" ? "bold" : "normal") + " 72"; + string font_string = (font == "roboto" ? "Roboto Condensed" : font) + + ", Twemoji Color Font " + + (font != "impact" ? "bold" : "normal") + " 72"; - string captionText = "" + - caption + ""; - string caption2Text = - "" + caption2 + ""; + string captionText = + "" + caption + ""; + string caption2Text = + "" + caption2 + ""; - auto findResult = fontPaths.find(font); - if (findResult != fontPaths.end()) { - VImage::text( - ".", VImage::option()->set("fontfile", - (basePath + findResult->second).c_str())); - } - - 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", 588) - ->set("height", 90)); - VImage captionImage = - text.extract_band(0, VImage::option()->set("n", 3)) - .gravity(VIPS_COMPASS_DIRECTION_CENTRE, 640, text.height() + 40, - VImage::option()->set("extend", "black")); - - VImage text2 = VImage::text( - caption2Text.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", 588) - ->set("height", 90)); - VImage caption2Image = - text2.extract_band(0, VImage::option()->set("n", 3)) - .gravity(VIPS_COMPASS_DIRECTION_CENTRE, 640, text.height() + 40, - VImage::option()->set("extend", "black")); - - base = base.insert(captionImage, 0, 0).insert(caption2Image, 640, 0); - - int width = in.width(); - int pageHeight = vips_image_get_page_height(in.get_image()); - int nPages = vips_image_get_n_pages(in.get_image()); - - VImage uncanny = VImage::new_from_file((basePath + path).c_str()); - - base = base.insert(uncanny, 0, 130); - - vector img; - for (int i = 0; i < nPages; i++) { - VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; - VImage resized = img_frame.resize(690.0 / (double)width); - if (resized.height() > 590) { - double vscale = 590.0 / (double)resized.height(); - resized = - resized.resize(vscale, VImage::option()->set("vscale", vscale)); - } - VImage composited = base.insert(resized, 935 - (resized.width() / 2), - 425 - (resized.height() / 2)); - img.push_back(composited); - } - VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); - final.set(VIPS_META_PAGE_HEIGHT, 720); - - void *buf; - size_t length; - final.write_to_buffer(("." + type).c_str(), &buf, &length, - type == "gif" ? VImage::option()->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(); + auto findResult = fontPaths.find(font); + if (findResult != fontPaths.end()) { + VImage::text(".", VImage::option()->set( + "fontfile", (basePath + findResult->second).c_str())); } + 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", 588) + ->set("height", 90)); + VImage captionImage = + text.extract_band(0, VImage::option()->set("n", 3)) + .gravity(VIPS_COMPASS_DIRECTION_CENTRE, 640, text.height() + 40, + VImage::option()->set("extend", "black")); + + VImage text2 = VImage::text( + caption2Text.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", 588) + ->set("height", 90)); + VImage caption2Image = + text2.extract_band(0, VImage::option()->set("n", 3)) + .gravity(VIPS_COMPASS_DIRECTION_CENTRE, 640, text.height() + 40, + VImage::option()->set("extend", "black")); + + base = base.insert(captionImage, 0, 0).insert(caption2Image, 640, 0); + + int width = in.width(); + int pageHeight = vips_image_get_page_height(in.get_image()); + int nPages = vips_image_get_n_pages(in.get_image()); + + VImage uncanny = VImage::new_from_file((basePath + path).c_str()); + + base = base.insert(uncanny, 0, 130); + + vector img; + for (int i = 0; i < nPages; i++) { + VImage img_frame = + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage resized = img_frame.resize(690.0 / (double)width); + if (resized.height() > 590) { + double vscale = 590.0 / (double)resized.height(); + resized = resized.resize(vscale, VImage::option()->set("vscale", vscale)); + } + VImage composited = base.insert(resized, 935 - (resized.width() / 2), + 425 - (resized.height() / 2)); + img.push_back(composited); + } + VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); + final.set(VIPS_META_PAGE_HEIGHT, 720); + + void *buf; + final.write_to_buffer(("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("reoptimise", 1) + : 0); + vips_error_clear(); vips_thread_shutdown(); - return result; + return (char *)buf; } \ No newline at end of file diff --git a/natives/uncanny.h b/natives/uncanny.h index 933bd16..6ae0cd5 100644 --- a/natives/uncanny.h +++ b/natives/uncanny.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Uncanny(const Napi::CallbackInfo& info); +using std::string; + +char* Uncanny(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/uncaption.cc b/natives/uncaption.cc index 30785c2..6b127e3 100644 --- a/natives/uncaption.cc +++ b/natives/uncaption.cc @@ -6,7 +6,7 @@ using namespace std; using namespace vips; -char *Uncaption(string type, char *BufferData, size_t BufferLength, +char *Uncaption(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { float tolerance = GetArgumentWithFallback(Arguments, "tolerance", 0.5); @@ -16,7 +16,7 @@ char *Uncaption(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer( BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1)->set("access", "sequential") + *type == "gif" ? options->set("n", -1)->set("access", "sequential") : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) @@ -47,8 +47,8 @@ char *Uncaption(string type, char *BufferData, size_t BufferLength, void *buf; final.write_to_buffer( - ("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); vips_error_clear(); diff --git a/natives/uncaption.h b/natives/uncaption.h index b4bc5f3..0dfa0a2 100644 --- a/natives/uncaption.h +++ b/natives/uncaption.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Uncaption(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Uncaption(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/wall.cc b/natives/wall.cc index f9c1184..359850e 100644 --- a/natives/wall.cc +++ b/natives/wall.cc @@ -1,68 +1,58 @@ +#include "common.h" #include -#include +#include #include #include using namespace std; using namespace Magick; -Napi::Value Wall(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *Wall(string *type, char *BufferData, size_t BufferLength, + ArgumentMap Arguments, size_t *DataSize) { + Blob blob; + + list frames; + list coalesced; + list mid; try { - Napi::Object obj = info[1].As(); - Napi::Buffer data = obj.Get("data").As>(); - string type = obj.Get("type").As().Utf8Value(); + readImages(&frames, Blob(BufferData, BufferLength)); + } catch (Magick::WarningCoder &warning) { + cerr << "Coder Warning: " << warning.what() << endl; + } catch (Magick::Warning &warning) { + cerr << "Warning: " << warning.what() << endl; + } + coalesceImages(&coalesced, frames.begin(), frames.end()); - Blob blob; - - list frames; - list coalesced; - list mid; - try { - readImages(&frames, Blob(data.Data(), data.Length())); - } catch (Magick::WarningCoder &warning) { - cerr << "Coder Warning: " << warning.what() << endl; - } catch (Magick::Warning &warning) { - cerr << "Warning: " << warning.what() << endl; - } - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.resize(Geometry("128x128")); - image.virtualPixelMethod(Magick::TileVirtualPixelMethod); - image.matteColor("none"); - image.backgroundColor("none"); - image.scale(Geometry("512x512")); - double arguments[16] = {0, 0, 57, 42, 0, 128, 63, 130, - 128, 0, 140, 60, 128, 128, 140, 140}; - image.distort(Magick::PerspectiveDistortion, 16, arguments); - image.scale(Geometry("800x800>")); - image.magick(type); - mid.push_back(image); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - } - - writeImages(mid.begin(), mid.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(); + for (Image &image : coalesced) { + image.resize(Geometry("128x128")); + image.virtualPixelMethod(Magick::TileVirtualPixelMethod); + image.matteColor("none"); + image.backgroundColor("none"); + image.scale(Geometry("512x512")); + double arguments[16] = {0, 0, 57, 42, 0, 128, 63, 130, + 128, 0, 140, 60, 128, 128, 140, 140}; + image.distort(Magick::PerspectiveDistortion, 16, arguments); + image.scale(Geometry("800x800>")); + image.magick(*type); + mid.push_back(image); } - return result; + optimizeTransparency(mid.begin(), mid.end()); + + if (*type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } + } + + writeImages(mid.begin(), mid.end(), &blob); + + *DataSize = blob.length(); + + char *data = (char *)malloc(*DataSize); + memcpy(data, blob.data(), *DataSize); + return data; } \ No newline at end of file diff --git a/natives/wall.h b/natives/wall.h index 09e75af..9fcc884 100644 --- a/natives/wall.h +++ b/natives/wall.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Wall(const Napi::CallbackInfo& info); +using std::string; + +char* Wall(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/watermark.cc b/natives/watermark.cc index 9df1822..dd5ae0d 100644 --- a/natives/watermark.cc +++ b/natives/watermark.cc @@ -6,7 +6,7 @@ using namespace std; using namespace vips; -char *Watermark(string type, char *BufferData, size_t BufferLength, +char *Watermark(string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { string water = GetArgument(Arguments, "water"); @@ -29,7 +29,7 @@ char *Watermark(string type, char *BufferData, size_t BufferLength, VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", - type == "gif" ? options->set("n", -1) : options) + *type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -93,7 +93,7 @@ char *Watermark(string type, char *BufferData, size_t BufferLength, VImage frame; for (int i = 0; i < nPages; i++) { VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; if (append) { VImage appended = img_frame.join(watermark, VIPS_DIRECTION_VERTICAL, VImage::option()->set("expand", true)); @@ -123,8 +123,8 @@ char *Watermark(string type, char *BufferData, size_t BufferLength, bg = frameAlpha.new_from_image({0, 0, 0}).copy(VImage::option()->set( "interpretation", VIPS_INTERPRETATION_sRGB)); frame = bg.bandjoin(frameAlpha); - if (type == "jpg" || type == "jpeg") { - type = "png"; + if (*type == "jpg" || *type == "jpeg") { + *type = "png"; } } VImage content = @@ -147,8 +147,8 @@ char *Watermark(string type, char *BufferData, size_t BufferLength, void *buf; final.write_to_buffer( - ("." + type).c_str(), &buf, DataSize, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); vips_error_clear(); diff --git a/natives/watermark.h b/natives/watermark.h index 0fa82e8..dd8ad76 100644 --- a/natives/watermark.h +++ b/natives/watermark.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Watermark(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file +char* Watermark(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/whisper.cc b/natives/whisper.cc index 4a5b8e5..4d18944 100644 --- a/natives/whisper.cc +++ b/natives/whisper.cc @@ -1,105 +1,88 @@ -#include +#include "common.h" #include using namespace std; using namespace vips; -Napi::Value Whisper(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *Whisper(string *type, char *BufferData, size_t BufferLength, + ArgumentMap Arguments, size_t *DataSize) { + string caption = GetArgument(Arguments, "caption"); + string basePath = GetArgument(Arguments, "basePath"); - try { - 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(); - string basePath = obj.Get("basePath").As().Utf8Value(); + VOption *options = VImage::option()->set("access", "sequential"); - VOption *options = VImage::option()->set("access", "sequential"); + VImage in = + 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); - 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 width = in.width(); + int pageHeight = vips_image_get_page_height(in.get_image()); + int nPages = vips_image_get_n_pages(in.get_image()); + int size = width / 6; + int dividedWidth = width / 175; + int rad = 1; - int width = in.width(); - int pageHeight = vips_image_get_page_height(in.get_image()); - int nPages = vips_image_get_n_pages(in.get_image()); - int size = width / 6; - int dividedWidth = width / 175; - int rad = 1; + string font_string = "Upright, Twemoji Color Font " + to_string(size); - string font_string = "Upright, Twemoji Color Font " + to_string(size); - - VImage mask; - if (dividedWidth >= 1) { - mask = VImage::black(dividedWidth * 2 + 1, dividedWidth * 2 + 1) + 128; - mask.draw_circle({255}, dividedWidth, dividedWidth, dividedWidth, - VImage::option()->set("fill", true)); - } else { - mask = VImage::black(rad * 2 + 1, rad * 2 + 1) + 128; - mask.draw_circle({255}, rad, rad, rad, - VImage::option()->set("fill", true)); - } - - VImage textIn = VImage::text( - ".", VImage::option()->set( - "fontfile", (basePath + "assets/fonts/whisper.otf").c_str())); - textIn = VImage::text( - ("" + caption + "").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", width)); - - textIn = textIn.embed(rad + 10, rad + 10, (textIn.width() + 2 * rad) + 20, - (textIn.height() + 2 * rad) + 20); - - VImage outline = - textIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE) - .gaussblur(0.5, VImage::option()->set("min_ampl", 0.1)); - outline = (outline == (vector){0, 0, 0, 0}); - VImage invert = outline.extract_band(3).invert(); - outline = - outline.extract_band(0, VImage::option()->set("n", outline.bands() - 1)) - .bandjoin(invert); - VImage textImg = outline.composite2(textIn, VIPS_BLEND_MODE_OVER); - - vector img; - for (int i = 0; i < nPages; i++) { - VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; - img_frame = img_frame.composite2( - textImg, VIPS_BLEND_MODE_OVER, - VImage::option() - ->set("x", (width / 2) - (textImg.width() / 2)) - ->set("y", (pageHeight / 2) - (textImg.height() / 2))); - img.push_back(img_frame); - } - VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); - final.set(VIPS_META_PAGE_HEIGHT, pageHeight); - - void *buf; - size_t length; - final.write_to_buffer( - ("." + type).c_str(), &buf, &length, - 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(); + VImage mask; + if (dividedWidth >= 1) { + mask = VImage::black(dividedWidth * 2 + 1, dividedWidth * 2 + 1) + 128; + mask.draw_circle({255}, dividedWidth, dividedWidth, dividedWidth, + VImage::option()->set("fill", true)); + } else { + mask = VImage::black(rad * 2 + 1, rad * 2 + 1) + 128; + mask.draw_circle({255}, rad, rad, rad, VImage::option()->set("fill", true)); } + VImage textIn = VImage::text( + ".", VImage::option()->set( + "fontfile", (basePath + "assets/fonts/whisper.otf").c_str())); + textIn = VImage::text( + ("" + caption + "").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", width)); + + textIn = textIn.embed(rad + 10, rad + 10, (textIn.width() + 2 * rad) + 20, + (textIn.height() + 2 * rad) + 20); + + VImage outline = textIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE) + .gaussblur(0.5, VImage::option()->set("min_ampl", 0.1)); + outline = (outline == (vector){0, 0, 0, 0}); + VImage invert = outline.extract_band(3).invert(); + outline = + outline.extract_band(0, VImage::option()->set("n", outline.bands() - 1)) + .bandjoin(invert); + VImage textImg = outline.composite2(textIn, VIPS_BLEND_MODE_OVER); + + vector img; + for (int i = 0; i < nPages; i++) { + VImage img_frame = + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + img_frame = img_frame.composite2( + textImg, VIPS_BLEND_MODE_OVER, + VImage::option() + ->set("x", (width / 2) - (textImg.width() / 2)) + ->set("y", (pageHeight / 2) - (textImg.height() / 2))); + img.push_back(img_frame); + } + VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); + final.set(VIPS_META_PAGE_HEIGHT, pageHeight); + + void *buf; + final.write_to_buffer( + ("." + *type).c_str(), &buf, DataSize, + *type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + : 0); + vips_error_clear(); vips_thread_shutdown(); - return result; + return (char *)buf; } \ No newline at end of file diff --git a/natives/whisper.h b/natives/whisper.h index 1b97c70..a6e8b80 100644 --- a/natives/whisper.h +++ b/natives/whisper.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Whisper(const Napi::CallbackInfo& info); +using std::string; + +char* Whisper(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/zamn.cc b/natives/zamn.cc index 654bb92..ed274b2 100644 --- a/natives/zamn.cc +++ b/natives/zamn.cc @@ -1,62 +1,50 @@ -#include +#include "common.h" #include using namespace std; using namespace vips; -Napi::Value Zamn(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); - Napi::Object result = Napi::Object::New(env); +char *Zamn(string *type, char *BufferData, size_t BufferLength, + ArgumentMap Arguments, size_t *DataSize) { + string basePath = GetArgument(Arguments, "basePath"); - try { - 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(); + VOption *options = VImage::option()->set("access", "sequential"); - VOption *options = VImage::option()->set("access", "sequential"); + VImage in = + 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); - 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 width = in.width(); + int pageHeight = vips_image_get_page_height(in.get_image()); + int nPages = vips_image_get_n_pages(in.get_image()); - int width = in.width(); - int pageHeight = vips_image_get_page_height(in.get_image()); - int nPages = vips_image_get_n_pages(in.get_image()); + string assetPath = basePath + "assets/images/zamn.png"; + VImage tmpl = VImage::new_from_file(assetPath.c_str()); - string assetPath = basePath + "assets/images/zamn.png"; - VImage tmpl = VImage::new_from_file(assetPath.c_str()); - - vector img; - for (int i = 0; i < nPages; i++) { - VImage img_frame = - type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; - VImage composited = tmpl.insert( - img_frame.extract_band(0, VImage::option()->set("n", 3)).bandjoin(255).resize( - 303.0 / (double)width, - VImage::option()->set("vscale", 438.0 / (double)pageHeight)), - 310, 76); - img.push_back(composited); - } - VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); - final.set(VIPS_META_PAGE_HEIGHT, 516); - - void *buf; - size_t length; - final.write_to_buffer(("." + type).c_str(), &buf, &length); - - 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(); + vector img; + for (int i = 0; i < nPages; i++) { + VImage img_frame = + *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage composited = + tmpl.insert(img_frame.extract_band(0, VImage::option()->set("n", 3)) + .bandjoin(255) + .resize(303.0 / (double)width, + VImage::option()->set( + "vscale", 438.0 / (double)pageHeight)), + 310, 76); + img.push_back(composited); } + VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); + final.set(VIPS_META_PAGE_HEIGHT, 516); + + void *buf; + final.write_to_buffer(("." + *type).c_str(), &buf, DataSize); vips_error_clear(); vips_thread_shutdown(); - return result; + return (char *)buf; } \ No newline at end of file diff --git a/natives/zamn.h b/natives/zamn.h index 4bdc016..6581e73 100644 --- a/natives/zamn.h +++ b/natives/zamn.h @@ -1,5 +1,7 @@ #pragma once -#include +#include "common.h" -Napi::Value Zamn(const Napi::CallbackInfo& info); +using std::string; + +char* Zamn(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/utils/tempimages.js b/utils/tempimages.js index 5c3b354..0753ff8 100644 --- a/utils/tempimages.js +++ b/utils/tempimages.js @@ -6,7 +6,7 @@ let dirSizeCache; export async function upload(client, result, context, interaction = false) { const filename = `${Math.random().toString(36).substring(2, 15)}.${result.name.split(".")[1]}`; await writeFile(`${process.env.TEMPDIR}/${filename}`, result.contents); - const imageURL = `${process.env.TMP_DOMAIN || "https://tmp.projectlounge.pw"}/${filename}`; + const imageURL = `${process.env.TMP_DOMAIN || "https://tmp.esmbot.net"}/${filename}`; const payload = { embeds: [{ color: 16711680,