diff --git a/.clang-format b/.clang-format index ea91c9ec..a372fa79 100644 --- a/.clang-format +++ b/.clang-format @@ -1 +1,14 @@ -BasedOnStyle: google +BasedOnStyle: microsoft +IndentWidth: 2 +ContinuationIndentWidth: 2 +Cpp11BracedListStyle: true +AlignArrayOfStructures: Left +BreakAfterReturnType: Automatic +BreakBeforeBraces: Attach +NamespaceIndentation: All +AllowShortBlocksOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: WithoutElse +AllowShortLambdasOnASingleLine: Inline +AllowShortFunctionsOnASingleLine: All +AllowShortLoopsOnASingleLine: true +IndentCaseLabels: true diff --git a/natives/blur.cc b/natives/blur.cc index 6677dfac..628e2f8c 100644 --- a/natives/blur.cc +++ b/natives/blur.cc @@ -7,22 +7,19 @@ using namespace std; using namespace vips; -ArgumentMap Blur(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Blur(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { bool sharp = GetArgument(arguments, "sharp"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)); - VImage out = - sharp ? in.sharpen(VImage::option()->set("sigma", 3)) : in.gaussblur(5); + VImage out = sharp ? in.sharpen(VImage::option()->set("sigma", 3)) : in.gaussblur(5); SetupTimeoutCallback(out, shouldKill); - char* buf; + char *buf; size_t dataSize = 0; - out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; output["size"] = dataSize; diff --git a/natives/bounce.cc b/natives/bounce.cc index 16a72b49..124002b6 100644 --- a/natives/bounce.cc +++ b/natives/bounce.cc @@ -7,13 +7,10 @@ using namespace std; using namespace vips; -ArgumentMap Bounce(const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, bool* shouldKill) -{ - VImage in = - VImage::new_from_buffer( - bufferdata, bufferLength, "", - GetInputOptions(type, true, true)) - .colourspace(VIPS_INTERPRETATION_sRGB); +ArgumentMap Bounce(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, bool *shouldKill) { + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, true)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); int width = in.width(); @@ -42,11 +39,9 @@ ArgumentMap Bounce(const string& type, string& outType, const char* bufferdata, vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - multiPage ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = multiPage ? in.crop(0, i * pageHeight, width, pageHeight) : in; double height = halfHeight * (-sin(i * mult) + 1); - VImage embedded = - img_frame.embed(0, height, width, pageHeight + halfHeight); + VImage embedded = img_frame.embed(0, height, width, pageHeight + halfHeight); img.push_back(embedded); } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); @@ -60,7 +55,7 @@ ArgumentMap Bounce(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize); + final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize); if (outType != "webp") outType = "gif"; diff --git a/natives/caption.cc b/natives/caption.cc index 9112fc17..c0f8e81a 100644 --- a/natives/caption.cc +++ b/natives/caption.cc @@ -6,16 +6,14 @@ using namespace std; using namespace vips; -ArgumentMap Caption(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Caption(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string caption = GetArgument(arguments, "caption"); string font = GetArgument(arguments, "font"); string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -25,37 +23,31 @@ ArgumentMap Caption(const string& type, string& outType, const char* bufferdata, int nPages = type == "avif" ? 1 : vips_image_get_n_pages(in.get_image()); int textWidth = width - ((width / 25) * 2); - string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " + - (font != "impact" ? "bold" : "normal") + " " + - to_string(size); + string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " + (font != "impact" ? "bold" : "normal") + + " " + to_string(size); string captionText = "" + caption + ""; LoadFonts(basePath); auto findResult = fontPaths.find(font); - 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", findResult != fontPaths.end() - ? (basePath + findResult->second).c_str() - : NULL) - ->set("width", textWidth)); + 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", findResult != fontPaths.end() ? (basePath + findResult->second).c_str() : NULL) + ->set("width", textWidth)); VImage captionImage = - ((text == zeroVec).bandand()) - .ifthenelse(255, text) - .gravity(VIPS_COMPASS_DIRECTION_CENTRE, width, text.height() + size, - VImage::option()->set("extend", "white")); + ((text == zeroVec).bandand()) + .ifthenelse(255, text) + .gravity(VIPS_COMPASS_DIRECTION_CENTRE, width, text.height() + size, VImage::option()->set("extend", "white")); vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? 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)); + VImage img_frame = nPages > 1 ? 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)); img.push_back(frame); } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); @@ -65,11 +57,8 @@ ArgumentMap Caption(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/caption2.cc b/natives/caption2.cc index 61407e70..7728a82c 100644 --- a/natives/caption2.cc +++ b/natives/caption2.cc @@ -7,17 +7,15 @@ using namespace std; using namespace vips; -ArgumentMap CaptionTwo(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap CaptionTwo(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { bool top = GetArgument(arguments, "top"); string caption = GetArgument(arguments, "caption"); string font = GetArgument(arguments, "font"); string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -27,39 +25,31 @@ ArgumentMap CaptionTwo(const string& type, string& outType, const char* bufferda int nPages = type == "avif" ? 1 : vips_image_get_n_pages(in.get_image()); int textWidth = width - ((width / 25) * 2); - string font_string = - (font == "roboto" ? "Roboto Condensed" : font) + " " + to_string(size); + string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " + to_string(size); string captionText = "" + caption + ""; LoadFonts(basePath); auto findResult = fontPaths.find(font); - VImage text = VImage::text( - captionText.c_str(), - VImage::option() - ->set("rgba", true) - ->set("font", font_string.c_str()) - ->set("fontfile", findResult != fontPaths.end() - ? (basePath + findResult->second).c_str() - : NULL) - ->set("align", VIPS_ALIGN_LOW) - ->set("width", textWidth)); + VImage text = + VImage::text(captionText.c_str(), + VImage::option() + ->set("rgba", true) + ->set("font", font_string.c_str()) + ->set("fontfile", findResult != fontPaths.end() ? (basePath + findResult->second).c_str() : NULL) + ->set("align", VIPS_ALIGN_LOW) + ->set("width", textWidth)); VImage captionImage = - ((text == zeroVec).bandand()) - .ifthenelse(255, text) - .embed(width / 25, width / 25, width, text.height() + size, - VImage::option()->set("extend", "white")); + ((text == zeroVec).bandand()) + .ifthenelse(255, text) + .embed(width / 25, width / 25, width, text.height() + size, VImage::option()->set("extend", "white")); vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; - VImage frame = - (top ? captionImage : img_frame) - .join(top ? img_frame : captionImage, VIPS_DIRECTION_VERTICAL, - VImage::option() - ->set("background", 0xffffff) - ->set("expand", true)); + VImage img_frame = nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage frame = (top ? captionImage : img_frame) + .join(top ? img_frame : captionImage, VIPS_DIRECTION_VERTICAL, + VImage::option()->set("background", 0xffffff)->set("expand", true)); img.push_back(frame); } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); @@ -69,11 +59,8 @@ ArgumentMap CaptionTwo(const string& type, string& outType, const char* bufferda char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/circle.cc b/natives/circle.cc index b907e064..0c88eedb 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -29,13 +29,10 @@ VImage rectangularMap(int width, int height) { return index; } -ArgumentMap Circle(const string& type, string& outType, - const char* bufferdata, size_t bufferLength, - [[maybe_unused]] ArgumentMap arguments, bool* shouldKill) { +ArgumentMap Circle(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, bool *shouldKill) { - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, false, false)); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, false, false)); int width = in.width(); int pageHeight = vips_image_get_page_height(in.get_image()); @@ -54,19 +51,16 @@ ArgumentMap Circle(const string& type, string& outType, VImage rectIndex = rectangularMap(width, pageHeight); VImage polarIndex = polarMap(width, pageHeight); - VImage gaussmat = - VImage::gaussmat(5, 0.2, VImage::option()->set("separable", true)).rot90(); + VImage gaussmat = VImage::gaussmat(5, 0.2, VImage::option()->set("separable", true)).rot90(); vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; - VImage result = img_frame.mapim(rectIndex).replicate(1, 3) - .conv(gaussmat, - VImage::option()->set("precision", VIPS_PRECISION_INTEGER)) - .crop(0, pageHeight, width, pageHeight) - .mapim(polarIndex, - VImage::option()->set("extend", VIPS_EXTEND_MIRROR)); + VImage img_frame = nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage result = img_frame.mapim(rectIndex) + .replicate(1, 3) + .conv(gaussmat, VImage::option()->set("precision", VIPS_PRECISION_INTEGER)) + .crop(0, pageHeight, width, pageHeight) + .mapim(polarIndex, VImage::option()->set("extend", VIPS_EXTEND_MIRROR)); img.push_back(result); } @@ -74,10 +68,10 @@ ArgumentMap Circle(const string& type, string& outType, SetupTimeoutCallback(out, shouldKill); - char* buf; + char *buf; size_t dataSize = 0; - out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), - &dataSize, outType == "gif" ? VImage::option()->set("dither", 0) : 0); + out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0) : 0); ArgumentMap output; output["buf"] = buf; output["size"] = dataSize; diff --git a/natives/colors.cc b/natives/colors.cc index ade85b62..e4a73b74 100644 --- a/natives/colors.cc +++ b/natives/colors.cc @@ -7,17 +7,14 @@ using namespace std; 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); +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); -ArgumentMap Colors(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Colors(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string color = GetArgument(arguments, "color"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); VImage out; @@ -31,7 +28,7 @@ ArgumentMap Colors(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; diff --git a/natives/commands.h b/natives/commands.h index bbdf1741..fc4aa3df 100644 --- a/natives/commands.h +++ b/natives/commands.h @@ -2,8 +2,11 @@ // Vultu: These are both bad, but I wanted to clean up code at least a little using std::string; -#define declare_input_func(NAME) ArgumentMap NAME(const string& type, string& outType, const char* bufferData, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -#define declare_noinput_func(NAME) ArgumentMap NAME(const string& type, string& outType, ArgumentMap arguments, bool* shouldKill) +#define declare_input_func(NAME) \ + ArgumentMap NAME(const string &type, string &outType, const char *bufferData, size_t bufferLength, \ + ArgumentMap arguments, bool *shouldKill) +#define declare_noinput_func(NAME) \ + ArgumentMap NAME(const string &type, string &outType, ArgumentMap arguments, bool *shouldKill) // Declare our Input Functions declare_input_func(Blur); diff --git a/natives/common.cc b/natives/common.cc index 6d728c74..0bdfc03f 100644 --- a/natives/common.cc +++ b/natives/common.cc @@ -2,23 +2,16 @@ #include "common.h" -void LoadFonts(string basePath) -{ +void LoadFonts(string basePath) { // manually loading fonts to workaround some font issues with libvips - if (!FcConfigAppFontAddDir( - NULL, (const FcChar8*)(basePath + "assets/fonts/").c_str())) { - std::cerr - << "Unable to load local font files from directory, falling back to " - "global fonts (which may be inaccurate!)" - << std::endl; + if (!FcConfigAppFontAddDir(NULL, (const FcChar8 *)(basePath + "assets/fonts/").c_str())) { + std::cerr << "Unable to load local font files from directory, falling back to " + "global fonts (which may be inaccurate!)" + << std::endl; } - if (!FcConfigParseAndLoad( - FcConfigGetCurrent(), - (const FcChar8*)(basePath + "assets/fonts/fontconfig.xml").c_str(), - true)) { - std::cerr - << "Unable to load local fontconfig, some fonts may be inaccurate!" - << std::endl; + if (!FcConfigParseAndLoad(FcConfigGetCurrent(), (const FcChar8 *)(basePath + "assets/fonts/fontconfig.xml").c_str(), + true)) { + std::cerr << "Unable to load local fontconfig, some fonts may be inaccurate!" << std::endl; } } @@ -40,9 +33,9 @@ vips::VImage NormalizeVips(vips::VImage in, int *width, int *pageHeight, int nPa return out; } -vips::VOption* GetInputOptions(string type, bool sequential, bool sequentialIfAnim) { +vips::VOption *GetInputOptions(string type, bool sequential, bool sequentialIfAnim) { bool anim = type == "gif" || type == "webp"; - vips::VOption* options = vips::VImage::option(); + vips::VOption *options = vips::VImage::option(); if (anim) { options->set("n", -1); @@ -78,8 +71,6 @@ void SetupTimeoutCallback(vips::VImage image, bool *shouldKill) { } uint32_t readUint32LE(unsigned char *buffer) { - return static_cast(buffer[0]) | - (static_cast(buffer[1]) << 8) | - (static_cast(buffer[2]) << 16) | - (static_cast(buffer[3]) << 24); + return static_cast(buffer[0]) | (static_cast(buffer[1]) << 8) | + (static_cast(buffer[2]) << 16) | (static_cast(buffer[3]) << 24); } diff --git a/natives/common.h b/natives/common.h index 6283154c..81d95361 100644 --- a/natives/common.h +++ b/natives/common.h @@ -1,10 +1,10 @@ #pragma once -#include #include +#include -#include #include +#include #include #include #include @@ -15,7 +15,7 @@ using std::map; using std::string; using std::variant; -typedef variant ArgumentVariant; +typedef variant ArgumentVariant; typedef map ArgumentMap; void SetupTimeoutCallback(vips::VImage image, bool *shouldKill); @@ -31,20 +31,16 @@ uint32_t readUint32LE(unsigned char *buffer); void LoadFonts(string basePath); vips::VImage NormalizeVips(vips::VImage in, int *width, int *pageHeight, int nPages); -vips::VOption* GetInputOptions(string type, bool sequential, bool sequentialIfAnim); +vips::VOption *GetInputOptions(string type, bool sequential, bool sequentialIfAnim); #define MapContainsKey(MAP, KEY) (MAP.find(KEY) != MAP.end()) -template -T GetArgument(ArgumentMap map, string key) { - if (!MapContainsKey(map, key)) - throw "Invalid requested type from variant."; +template T GetArgument(ArgumentMap map, string key) { + if (!MapContainsKey(map, key)) throw "Invalid requested type from variant."; return std::get(map.at(key)); } -template -T GetArgumentWithFallback(ArgumentMap map, string key, T fallback) { - if (!MapContainsKey(map, key)) - return fallback; +template T GetArgumentWithFallback(ArgumentMap map, string key, T fallback) { + if (!MapContainsKey(map, key)) return fallback; return std::get(map.at(key)); } @@ -52,65 +48,68 @@ const std::vector zeroVec = {0, 0, 0, 0}; const std::vector zeroVecOneAlpha = {0, 0, 0, 1}; const std::unordered_map fontPaths{ - {"futura", "assets/fonts/caption.otf"}, - {"helvetica", "assets/fonts/caption2.ttf"}, - {"roboto", "assets/fonts/reddit.ttf"}, - {"ubuntu", "assets/fonts/Ubuntu.ttf"}}; + {"futura", "assets/fonts/caption.otf" }, + {"helvetica", "assets/fonts/caption2.ttf"}, + {"roboto", "assets/fonts/reddit.ttf" }, + {"ubuntu", "assets/fonts/Ubuntu.ttf" } +}; -const std::map - FunctionMap = {{"blur", &Blur}, - {"bounce", &Bounce}, - {"caption", &Caption}, - {"captionTwo", &CaptionTwo}, - {"circle", &Circle}, - {"colors", &Colors}, - {"crop", &Crop}, - {"deepfry", &Deepfry}, - {"distort", &Distort}, - {"fade", &Fade}, - {"flag", &Flag}, - {"flip", &Flip}, - {"freeze", &Freeze}, - {"gamexplain", &Gamexplain}, - {"globe", &Globe}, - {"invert", &Invert}, - {"jpeg", &Jpeg}, +const std::map + FunctionMap = { + {"blur", &Blur }, + {"bounce", &Bounce }, + {"caption", &Caption }, + {"captionTwo", &CaptionTwo}, + {"circle", &Circle }, + {"colors", &Colors }, + {"crop", &Crop }, + {"deepfry", &Deepfry }, + {"distort", &Distort }, + {"fade", &Fade }, + {"flag", &Flag }, + {"flip", &Flip }, + {"freeze", &Freeze }, + {"gamexplain", &Gamexplain}, + {"globe", &Globe }, + {"invert", &Invert }, + {"jpeg", &Jpeg }, #ifdef MAGICK_ENABLED - {"magik", &Magik}, + {"magik", &Magik }, #endif - {"meme", &Meme}, - {"mirror", &Mirror}, - {"motivate", &Motivate}, + {"meme", &Meme }, + {"mirror", &Mirror }, + {"motivate", &Motivate }, #ifdef ZXING_ENABLED - {"qrread", &QrRead}, + {"qrread", &QrRead }, #endif - {"reddit", &Reddit}, - {"resize", &Resize}, - {"reverse", &Reverse}, - {"scott", &Scott}, - {"snapchat", &Snapchat}, - {"speed", &Speed}, - {"spin", &Spin}, - {"spotify", &Spotify}, - {"squish", &Squish}, - {"swirl", &Swirl}, - {"tile", &Tile}, - {"togif", &ToGif}, - {"uncanny", &Uncanny}, - {"uncaption", &Uncaption}, + {"reddit", &Reddit }, + {"resize", &Resize }, + {"reverse", &Reverse }, + {"scott", &Scott }, + {"snapchat", &Snapchat }, + {"speed", &Speed }, + {"spin", &Spin }, + {"spotify", &Spotify }, + {"squish", &Squish }, + {"swirl", &Swirl }, + {"tile", &Tile }, + {"togif", &ToGif }, + {"uncanny", &Uncanny }, + {"uncaption", &Uncaption }, #if MAGICK_ENABLED - {"wall", &Wall}, + {"wall", &Wall }, #endif - {"watermark", &Watermark}, - {"whisper", &Whisper}}; + {"watermark", &Watermark }, + {"whisper", &Whisper } +}; const std::map - NoInputFunctionMap = {{"homebrew", &Homebrew}, + ArgumentMap (*)(const string &type, string &outType, ArgumentMap arguments, bool *shouldKill)> + NoInputFunctionMap = { + {"homebrew", &Homebrew}, #if ZXING_ENABLED {"qrcreate", &QrCreate}, #endif - {"sonic", &Sonic}}; + {"sonic", &Sonic } +}; diff --git a/natives/crop.cc b/natives/crop.cc index 8d32cd02..88fec82a 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -7,11 +7,9 @@ using namespace std; using namespace vips; -ArgumentMap Crop(const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, bool* shouldKill) -{ - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)); +ArgumentMap Crop(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, bool *shouldKill) { + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)); int width = in.width(); int pageHeight = vips_image_get_page_height(in.get_image()); @@ -20,16 +18,13 @@ ArgumentMap Crop(const string& type, string& outType, const char* bufferdata, si vector img; int finalHeight = 0; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; int frameWidth = img_frame.width(); int frameHeight = img_frame.height(); bool widthOrHeight = frameWidth / frameHeight >= 1; int size = widthOrHeight ? frameHeight : frameWidth; // img_frame.crop(frameWidth - size, frameHeight - size, size, size); - VImage result = img_frame.smartcrop( - size, size, - VImage::option()->set("interesting", VIPS_INTERESTING_CENTRE)); + VImage result = img_frame.smartcrop(size, size, VImage::option()->set("interesting", VIPS_INTERESTING_CENTRE)); finalHeight = size; img.push_back(result); } @@ -41,11 +36,8 @@ ArgumentMap Crop(const string& type, string& outType, const char* bufferdata, si char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/deepfry.cc b/natives/deepfry.cc index 37a67421..131d8def 100644 --- a/natives/deepfry.cc +++ b/natives/deepfry.cc @@ -6,11 +6,9 @@ using namespace std; using namespace vips; -ArgumentMap Deepfry(const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, bool* shouldKill) -{ - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)); +ArgumentMap Deepfry(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, bool *shouldKill) { + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)); int width = in.width(); int pageHeight = vips_image_get_page_height(in.get_image()); @@ -26,9 +24,7 @@ ArgumentMap Deepfry(const string& type, string& outType, const char* bufferdata, VImage img_frame = in.crop(0, i * pageHeight, width, pageHeight); void *jpgBuf; size_t jpgLength; - img_frame.write_to_buffer( - ".jpg", &jpgBuf, &jpgLength, - VImage::option()->set("Q", 1)->set("strip", true)); + img_frame.write_to_buffer(".jpg", &jpgBuf, &jpgLength, VImage::option()->set("Q", 1)->set("strip", true)); VImage jpeged = VImage::new_from_buffer(jpgBuf, jpgLength, ""); jpeged.set(VIPS_META_PAGE_HEIGHT, pageHeight); jpeged.set("delay", in.get_array_int("delay")); @@ -39,8 +35,7 @@ ArgumentMap Deepfry(const string& type, string& outType, const char* bufferdata, } else { void *jpgBuf; size_t jpgLength; - fried.write_to_buffer(".jpg", &jpgBuf, &jpgLength, - VImage::option()->set("Q", 1)->set("strip", true)); + fried.write_to_buffer(".jpg", &jpgBuf, &jpgLength, VImage::option()->set("Q", 1)->set("strip", true)); final = VImage::new_from_buffer(jpgBuf, jpgLength, ""); final.set(VIPS_META_PAGE_HEIGHT, pageHeight); if (nPages > 1) final.set("delay", fried.get_array_int("delay")); @@ -50,9 +45,8 @@ ArgumentMap Deepfry(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" ? VImage::option()->set("dither", 0) : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/distort.cc b/natives/distort.cc index 1788d56b..f9d3c980 100644 --- a/natives/distort.cc +++ b/natives/distort.cc @@ -5,15 +5,12 @@ using namespace std; using namespace vips; -ArgumentMap Distort(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Distort(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string mapName = GetArgument(arguments, "mapName"); string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer( - bufferdata, bufferLength, "", - GetInputOptions(type, true, true)); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, true)); int width = in.width(); int pageHeight = vips_image_get_page_height(in.get_image()); @@ -21,18 +18,15 @@ ArgumentMap Distort(const string& type, string& outType, const char* bufferdata, string distortPath = basePath + "assets/images/" + mapName; VImage distort = - (VImage::new_from_file(distortPath.c_str()) - .resize(width / 500.0, VImage::option() - ->set("vscale", pageHeight / 500.0) - ->set("kernel", VIPS_KERNEL_CUBIC)) / - 65535); + (VImage::new_from_file(distortPath.c_str()) + .resize(width / 500.0, VImage::option()->set("vscale", pageHeight / 500.0)->set("kernel", VIPS_KERNEL_CUBIC)) / + 65535); VImage distortImage = (distort[0] * width).bandjoin(distort[1] * pageHeight); vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; VImage mapped = img_frame.mapim(distortImage); img.push_back(mapped); } @@ -43,7 +37,7 @@ ArgumentMap Distort(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; diff --git a/natives/fade.cc b/natives/fade.cc index bcab364f..c5cbe737 100644 --- a/natives/fade.cc +++ b/natives/fade.cc @@ -7,12 +7,11 @@ using namespace std; using namespace vips; -ArgumentMap Fade(const string& type, string& outType, const char* bufferdata, - size_t bufferLength, ArgumentMap arguments, bool* shouldKill) { +ArgumentMap Fade(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { bool alpha = GetArgumentWithFallback(arguments, "alpha", false); - VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, true)) + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, true)) .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -42,16 +41,13 @@ ArgumentMap Fade(const string& type, string& outType, const char* bufferdata, vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - multiPage ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = multiPage ? in.crop(0, i * pageHeight, width, pageHeight) : in; double mult = (double)i / (nPages - 1); - VImage faded = img_frame.extract_band( - 0, VImage::option()->set("n", img_frame.bands() - 1)); + VImage faded = img_frame.extract_band(0, VImage::option()->set("n", img_frame.bands() - 1)); if (outType == "gif") { faded *= mult; } else { - faded = - faded.bandjoin(img_frame.extract_band(img_frame.bands() - 1) * mult); + faded = faded.bandjoin(img_frame.extract_band(img_frame.bands() - 1) * mult); } img.push_back(faded); } @@ -65,10 +61,9 @@ ArgumentMap Fade(const string& type, string& outType, const char* bufferdata, SetupTimeoutCallback(final, shouldKill); - char* buf; + char *buf; size_t dataSize = 0; - final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", - reinterpret_cast(&buf), &dataSize); + final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; diff --git a/natives/flag.cc b/natives/flag.cc index 7cbdd499..553c1c45 100644 --- a/natives/flag.cc +++ b/natives/flag.cc @@ -5,15 +5,13 @@ using namespace std; using namespace vips; -ArgumentMap Flag(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Flag(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string overlay = GetArgument(arguments, "overlay"); string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); @@ -23,10 +21,9 @@ ArgumentMap Flag(const string& type, string& outType, const char* bufferdata, si string assetPath = basePath + overlay; VImage overlayInput = VImage::new_from_file(assetPath.c_str()); - VImage overlayImage = overlayInput.resize( - (double)width / (double)overlayInput.width(), - VImage::option()->set( - "vscale", (double)pageHeight / (double)overlayInput.height())); + VImage overlayImage = + overlayInput.resize((double)width / (double)overlayInput.width(), + VImage::option()->set("vscale", (double)pageHeight / (double)overlayInput.height())); if (!overlayImage.has_alpha()) { overlayImage = overlayImage.bandjoin(127); } else { @@ -40,11 +37,8 @@ ArgumentMap Flag(const string& type, string& outType, const char* bufferdata, si char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/flip.cc b/natives/flip.cc index 03f342c2..3e2bad46 100644 --- a/natives/flip.cc +++ b/natives/flip.cc @@ -6,12 +6,11 @@ using namespace std; using namespace vips; -ArgumentMap Flip(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Flip(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { bool flop = GetArgumentWithFallback(arguments, "flop", false); - VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, true)); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, true)); int nPages = type == "avif" ? 1 : vips_image_get_n_pages(in.get_image()); @@ -37,11 +36,8 @@ ArgumentMap Flip(const string& type, string& outType, const char* bufferdata, si char *buf; size_t dataSize = 0; - out.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/freeze.cc b/natives/freeze.cc index f9f7d16a..f4c6336f 100644 --- a/natives/freeze.cc +++ b/natives/freeze.cc @@ -7,10 +7,9 @@ using namespace std; using namespace vips; -char *vipsTrim(const char *data, size_t length, size_t& dataSize, int frame, string suffix, string outType, bool *shouldKill) { - VImage in = - VImage::new_from_buffer(data, length, "", - GetInputOptions(suffix, true, false)); +char *vipsTrim(const char *data, size_t length, size_t &dataSize, int frame, string suffix, string outType, + bool *shouldKill) { + VImage in = VImage::new_from_buffer(data, length, "", GetInputOptions(suffix, true, false)); int pageHeight = vips_image_get_page_height(in.get_image()); int nPages = vips_image_get_n_pages(in.get_image()); @@ -22,13 +21,13 @@ char *vipsTrim(const char *data, size_t length, size_t& dataSize, int frame, str SetupTimeoutCallback(out, shouldKill); char *buf; - out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); return buf; } -ArgumentMap Freeze(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Freeze(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { bool loop = GetArgumentWithFallback(arguments, "loop", false); int frame = GetArgumentWithFallback(arguments, "frame", -1); @@ -36,23 +35,22 @@ ArgumentMap Freeze(const string& type, string& outType, const char* bufferdata, size_t dataSize = 0; if (type == "gif") { - char *fileData = reinterpret_cast(malloc(bufferLength)); + char *fileData = reinterpret_cast(malloc(bufferLength)); memcpy(fileData, bufferdata, bufferLength); - char *match = const_cast("\x21\xFF\x0BNETSCAPE2.0\x03\x01"); - char *descriptor = const_cast("\x2C\x00\x00\x00\x00"); + char *match = const_cast("\x21\xFF\x0BNETSCAPE2.0\x03\x01"); + char *descriptor = const_cast("\x2C\x00\x00\x00\x00"); char *lastPos; bool none = true; if (loop) { - char *newData = reinterpret_cast(malloc(bufferLength + 19)); + char *newData = reinterpret_cast(malloc(bufferLength + 19)); memcpy(newData, fileData, bufferLength); - lastPos = reinterpret_cast(memchr(newData, '\x2C', bufferLength)); + lastPos = reinterpret_cast(memchr(newData, '\x2C', bufferLength)); while (lastPos != NULL) { if (memcmp(lastPos, descriptor, 5) != 0) { - lastPos = reinterpret_cast(memchr(lastPos + 1, '\x2C', - (bufferLength - (lastPos - newData)) - 1)); + lastPos = reinterpret_cast(memchr(lastPos + 1, '\x2C', (bufferLength - (lastPos - newData)) - 1)); continue; } @@ -71,11 +69,10 @@ ArgumentMap Freeze(const string& type, string& outType, const char* bufferdata, char *buf = vipsTrim(bufferdata, bufferLength, dataSize, frame, type, outType, shouldKill); output["buf"] = buf; } else { - lastPos = reinterpret_cast(memchr(fileData, '\x21', bufferLength)); + lastPos = reinterpret_cast(memchr(fileData, '\x21', bufferLength)); while (lastPos != NULL) { if (memcmp(lastPos, match, 16) != 0) { - lastPos = reinterpret_cast(memchr(lastPos + 1, '\x21', - (bufferLength - (lastPos - fileData)) - 1)); + lastPos = reinterpret_cast(memchr(lastPos + 1, '\x21', (bufferLength - (lastPos - fileData)) - 1)); continue; } memcpy(lastPos, lastPos + 19, (bufferLength - (lastPos - fileData)) - 19); @@ -94,14 +91,14 @@ ArgumentMap Freeze(const string& type, string& outType, const char* bufferdata, output["buf"] = buf; output["size"] = dataSize; } else { - char *fileData = reinterpret_cast(malloc(bufferLength)); + char *fileData = reinterpret_cast(malloc(bufferLength)); memcpy(fileData, bufferdata, bufferLength); size_t position = 12; while (position + 8 <= bufferLength) { - const char* fourCC = &fileData[position]; - uint32_t chunkSize = readUint32LE(reinterpret_cast(fileData) + position + 4); + const char *fourCC = &fileData[position]; + uint32_t chunkSize = readUint32LE(reinterpret_cast(fileData) + position + 4); if (memcmp(fourCC, "ANIM", 4) == 0) { size_t dataStart = position + 8; @@ -117,7 +114,7 @@ ArgumentMap Freeze(const string& type, string& outType, const char* bufferdata, output["size"] = bufferLength; } } else { - char *data = reinterpret_cast(malloc(bufferLength)); + char *data = reinterpret_cast(malloc(bufferLength)); memcpy(data, bufferdata, bufferLength); output["buf"] = data; output["size"] = bufferLength; diff --git a/natives/gamexplain.cc b/natives/gamexplain.cc index 3f3402a1..75f26f10 100644 --- a/natives/gamexplain.cc +++ b/natives/gamexplain.cc @@ -5,14 +5,12 @@ using namespace std; using namespace vips; -ArgumentMap Gamexplain(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Gamexplain(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); string assetPath = basePath + "assets/images/gamexplain.png"; @@ -24,13 +22,10 @@ ArgumentMap Gamexplain(const string& type, string& outType, const char* bufferda vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; VImage resized = - img_frame - .resize(1181.0 / (double)width, - VImage::option()->set("vscale", 571.0 / (double)pageHeight)) - .embed(10, 92, 1200, 675, VImage::option()->set("extend", "white")); + img_frame.resize(1181.0 / (double)width, VImage::option()->set("vscale", 571.0 / (double)pageHeight)) + .embed(10, 92, 1200, 675, VImage::option()->set("extend", "white")); VImage composited = resized.composite2(tmpl, VIPS_BLEND_MODE_OVER); img.push_back(composited); } @@ -41,11 +36,8 @@ ArgumentMap Gamexplain(const string& type, string& outType, const char* bufferda char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/generic/image.cc b/natives/generic/image.cc index a844ec93..dfd78c7d 100644 --- a/natives/generic/image.cc +++ b/natives/generic/image.cc @@ -38,7 +38,7 @@ image_result *image(const char *command, const char *args, const char *data, siz nlohmann::json parsedArgs = nlohmann::json::parse(args); ArgumentMap Arguments; - for (auto& pair : parsedArgs.items()) { + for (auto &pair : parsedArgs.items()) { auto key = pair.key(); if (key == "data") { continue; @@ -67,14 +67,16 @@ image_result *image(const char *command, const char *args, const char *data, siz outMap = FunctionMap.at(command)(type, outType, data, length, Arguments, NULL); } else { // Vultu: I don't think we will ever be here, but just in case we need a descriptive error string cmd(command); - throw "Error: \"FunctionMap\" does not contain \"" + cmd + "\", which was requested because \"length\" parameter was not 0."; + throw "Error: \"FunctionMap\" does not contain \"" + cmd + + "\", which was requested because \"length\" parameter was not 0."; } } else { if (MapContainsKey(NoInputFunctionMap, command)) { outMap = NoInputFunctionMap.at(command)(type, outType, Arguments, NULL); } else { string cmd(command); - throw "Error: \"NoInputFunctionMap\" does not contain \"" + cmd + "\", which was requested because \"length\" parameter was 0."; + throw "Error: \"NoInputFunctionMap\" does not contain \"" + cmd + + "\", which was requested because \"length\" parameter was 0."; } } @@ -90,21 +92,19 @@ image_result *image(const char *command, const char *args, const char *data, siz return out; } -void img_free(void *ptr, [[maybe_unused]] void *ctx) { - g_free(ptr); -} +void img_free(void *ptr, [[maybe_unused]] void *ctx) { g_free(ptr); } char *get_funcs() { nlohmann::json funcs = nlohmann::json::array(); - for (auto const& imap : FunctionMap) { + for (auto const &imap : FunctionMap) { funcs.push_back(imap.first); } - for (auto const& imap : NoInputFunctionMap) { + for (auto const &imap : NoInputFunctionMap) { funcs.push_back(imap.first); } string dumped = funcs.dump(); size_t length = dumped.length(); - char* out = new char[length + 1]; + char *out = new char[length + 1]; out[length] = 0; dumped.copy(out, length); return out; diff --git a/natives/globe.cc b/natives/globe.cc index 4123b37f..a7345122 100644 --- a/natives/globe.cc +++ b/natives/globe.cc @@ -5,15 +5,12 @@ using namespace std; using namespace vips; -ArgumentMap Globe(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Globe(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer( - bufferdata, bufferLength, "", - GetInputOptions(type, true, true)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, true)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); int width = in.width(); @@ -40,32 +37,26 @@ ArgumentMap Globe(const string& type, string& outType, const char* bufferdata, s double size = min(width, pageHeight); string diffPath = basePath + "assets/images/globediffuse.png"; - VImage diffuse = VImage::new_from_file(diffPath.c_str()) - .resize(size / 500.0, VImage::option()->set( - "kernel", VIPS_KERNEL_CUBIC)) / - 255; + VImage diffuse = + VImage::new_from_file(diffPath.c_str()).resize(size / 500.0, VImage::option()->set("kernel", VIPS_KERNEL_CUBIC)) / + 255; string specPath = basePath + "assets/images/globespec.png"; - VImage specular = VImage::new_from_file(specPath.c_str()) - .resize(size / 500.0, VImage::option()->set( - "kernel", VIPS_KERNEL_CUBIC)); + VImage specular = + VImage::new_from_file(specPath.c_str()).resize(size / 500.0, VImage::option()->set("kernel", VIPS_KERNEL_CUBIC)); string distortPath = basePath + "assets/images/spheremap.png"; - VImage distort = - (VImage::new_from_file(distortPath.c_str()) - .resize(size / 500.0, - VImage::option()->set("kernel", VIPS_KERNEL_CUBIC)) / - 65535) * - size; + VImage distort = (VImage::new_from_file(distortPath.c_str()) + .resize(size / 500.0, VImage::option()->set("kernel", VIPS_KERNEL_CUBIC)) / + 65535) * + size; vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - multiPage ? in.crop(0, i * pageHeight, width, pageHeight) : in; - VImage mapped = img_frame - .wrap(VImage::option()->set("x", width * i / nPages)->set("y", 0)) - .extract_band(0, VImage::option()->set("n", 3)) - .mapim(distort); + VImage img_frame = multiPage ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage mapped = img_frame.wrap(VImage::option()->set("x", width * i / nPages)->set("y", 0)) + .extract_band(0, VImage::option()->set("n", 3)) + .mapim(distort); VImage frame = (mapped * diffuse + specular).bandjoin(diffuse > 0.0); img.push_back(frame); } @@ -80,7 +71,7 @@ ArgumentMap Globe(const string& type, string& outType, const char* bufferdata, s char *buf; size_t dataSize = 0; - final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize); + final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize); if (outType != "webp") outType = "gif"; diff --git a/natives/homebrew.cc b/natives/homebrew.cc index a2fa3dea..f0c84c60 100644 --- a/natives/homebrew.cc +++ b/natives/homebrew.cc @@ -5,8 +5,7 @@ using namespace std; using namespace vips; -ArgumentMap Homebrew([[maybe_unused]] const string& type, string& outType, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Homebrew([[maybe_unused]] const string &type, string &outType, ArgumentMap arguments, bool *shouldKill) { string caption = GetArgument(arguments, "caption"); string basePath = GetArgument(arguments, "basePath"); @@ -14,25 +13,22 @@ ArgumentMap Homebrew([[maybe_unused]] const string& type, string& outType, Argum VImage bg = VImage::new_from_file(assetPath.c_str()); LoadFonts(basePath); - VImage text = VImage::text( - ("" + caption + "") - .c_str(), - VImage::option() - ->set("rgba", true) - ->set("align", VIPS_ALIGN_CENTRE) - ->set("font", "PF Square Sans Pro 96") - ->set("fontfile", (basePath + "assets/fonts/hbc.ttf").c_str())); - - VImage out = bg.composite2(text, VIPS_BLEND_MODE_OVER, + VImage text = VImage::text(("" + caption + "").c_str(), VImage::option() - ->set("x", 400 - (text.width() / 2)) - ->set("y", 300 - (text.height() / 2) - 8)); + ->set("rgba", true) + ->set("align", VIPS_ALIGN_CENTRE) + ->set("font", "PF Square Sans Pro 96") + ->set("fontfile", (basePath + "assets/fonts/hbc.ttf").c_str())); + + VImage out = + bg.composite2(text, VIPS_BLEND_MODE_OVER, + VImage::option()->set("x", 400 - (text.width() / 2))->set("y", 300 - (text.height() / 2) - 8)); SetupTimeoutCallback(out, shouldKill); char *buf; size_t dataSize = 0; - out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; diff --git a/natives/invert.cc b/natives/invert.cc index 08dad97e..fe6ef463 100644 --- a/natives/invert.cc +++ b/natives/invert.cc @@ -5,16 +5,13 @@ using namespace std; using namespace vips; -ArgumentMap Invert(const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, bool* shouldKill) -{ - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)); +ArgumentMap Invert(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, bool *shouldKill) { + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)); bool hasAlpha = in.has_alpha(); - VImage noAlpha = - hasAlpha ? in.extract_band(0, VImage::option()->set("n", in.bands() - 1)) : in; + VImage noAlpha = hasAlpha ? in.extract_band(0, VImage::option()->set("n", in.bands() - 1)) : in; VImage inverted = noAlpha.invert(); VImage out = hasAlpha ? inverted.bandjoin(in.extract_band(in.bands() - 1)) : inverted; @@ -22,7 +19,7 @@ ArgumentMap Invert(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; diff --git a/natives/jpeg.cc b/natives/jpeg.cc index be01cd3c..34c430ad 100644 --- a/natives/jpeg.cc +++ b/natives/jpeg.cc @@ -5,16 +5,14 @@ using namespace std; using namespace vips; -ArgumentMap Jpeg(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Jpeg(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { int quality = GetArgumentWithFallback(arguments, "quality", 1); char *buf; size_t dataSize = 0; - VImage in = VImage::new_from_buffer( - bufferdata, bufferLength, "", - GetInputOptions(type, true, false)); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)); int nPages = type == "avif" ? 1 : vips_image_get_n_pages(in.get_image()); @@ -31,9 +29,7 @@ ArgumentMap Jpeg(const string& type, string& outType, const char* bufferdata, si VImage img_frame = in.crop(0, i * pageHeight, width, pageHeight); void *jpgBuf; size_t jpgLength; - img_frame.write_to_buffer( - ".jpg", &jpgBuf, &jpgLength, - VImage::option()->set("Q", quality)->set("strip", true)); + img_frame.write_to_buffer(".jpg", &jpgBuf, &jpgLength, VImage::option()->set("Q", quality)->set("strip", true)); VImage jpeged = VImage::new_from_buffer(jpgBuf, jpgLength, ""); jpeged.set(VIPS_META_PAGE_HEIGHT, pageHeight); jpeged.set("delay", in.get_array_int("delay")); @@ -44,29 +40,23 @@ ArgumentMap Jpeg(const string& type, string& outType, const char* bufferdata, si } else { void *jpgBuf; size_t jpgLength; - in.write_to_buffer( - ".jpg", &jpgBuf, &jpgLength, - VImage::option()->set("Q", quality)->set("strip", true)); + in.write_to_buffer(".jpg", &jpgBuf, &jpgLength, VImage::option()->set("Q", quality)->set("strip", true)); final = VImage::new_from_buffer(jpgBuf, jpgLength, ""); final.set(VIPS_META_PAGE_HEIGHT, pageHeight); final.set("delay", in.get_array_int("delay")); } - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" ? VImage::option()->set("dither", 0) : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0) : 0); } else { void *jpgBuf; - in.write_to_buffer(".jpg", &jpgBuf, &dataSize, - VImage::option()->set("Q", quality)->set("strip", true)); + in.write_to_buffer(".jpg", &jpgBuf, &dataSize, VImage::option()->set("Q", quality)->set("strip", true)); if (outType == "gif") { - VImage gifIn = VImage::new_from_buffer(reinterpret_cast(jpgBuf), dataSize, ""); - gifIn.write_to_buffer( - ".gif", reinterpret_cast(&buf), &dataSize, - VImage::option()->set("strip", true)); + VImage gifIn = VImage::new_from_buffer(reinterpret_cast(jpgBuf), dataSize, ""); + gifIn.write_to_buffer(".gif", reinterpret_cast(&buf), &dataSize, VImage::option()->set("strip", true)); } else { outType = "jpg"; - buf = reinterpret_cast(jpgBuf); + buf = reinterpret_cast(jpgBuf); } } diff --git a/natives/magik.cc b/natives/magik.cc index b7d26cb2..0fe0630f 100644 --- a/natives/magik.cc +++ b/natives/magik.cc @@ -10,8 +10,8 @@ using namespace std; using namespace Magick; -ArgumentMap Magik([[maybe_unused]] const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, [[maybe_unused]] bool* shouldKill) -{ +ArgumentMap Magik([[maybe_unused]] const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, [[maybe_unused]] bool *shouldKill) { Blob blob; list frames; @@ -47,7 +47,7 @@ ArgumentMap Magik([[maybe_unused]] const string& type, string& outType, const ch size_t dataSize = blob.length(); - char *data = reinterpret_cast(malloc(dataSize)); + char *data = reinterpret_cast(malloc(dataSize)); memcpy(data, blob.data(), dataSize); ArgumentMap output; diff --git a/natives/meme.cc b/natives/meme.cc index 7880c69a..efc2880d 100644 --- a/natives/meme.cc +++ b/natives/meme.cc @@ -5,36 +5,31 @@ using namespace std; using namespace vips; -VImage genText(string text, string font, const char *fontfile, int width, - VImage mask, int radius) { +VImage genText(string text, string font, const char *fontfile, int width, VImage mask, int radius) { VOption *options = VImage::option() - ->set("rgba", true) - ->set("align", VIPS_ALIGN_CENTRE) - ->set("font", font.c_str()) - ->set("width", width); - VImage in = VImage::text( - ("" + text + "").c_str(), - strncmp(fontfile, "\0", 1) != 0 ? options->set("fontfile", fontfile) : options); + ->set("rgba", true) + ->set("align", VIPS_ALIGN_CENTRE) + ->set("font", font.c_str()) + ->set("width", width); + VImage in = VImage::text(("" + text + "").c_str(), + strncmp(fontfile, "\0", 1) != 0 ? options->set("fontfile", fontfile) : options); - in = in.embed(radius, radius * 2, in.width() + 2 * radius, - (in.height() + 2 * radius) + (radius * 2)); + in = in.embed(radius, radius * 2, in.width() + 2 * radius, (in.height() + 2 * radius) + (radius * 2)); VImage newText = in.convsep(mask); VImage outline = newText.cast(VIPS_FORMAT_UCHAR) * zeroVecOneAlpha; return outline.composite2(in, VIPS_BLEND_MODE_OVER); } -ArgumentMap Meme(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Meme(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string top = GetArgument(arguments, "topText"); string bottom = GetArgument(arguments, "bottomText"); string font = GetArgument(arguments, "font"); string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); int width = in.width(); @@ -43,59 +38,41 @@ ArgumentMap Meme(const string& type, string& outType, const char* bufferdata, si double size = (double)width / 9; double radius = size / 18; - string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " + - (font != "impact" ? "bold" : "normal") + " " + - to_string(size); + string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " + (font != "impact" ? "bold" : "normal") + + " " + to_string(size); - VImage mask = VImage::gaussmat(radius / 2, 0.1, - VImage::option()->set("separable", true)) * - 8; + VImage mask = VImage::gaussmat(radius / 2, 0.1, VImage::option()->set("separable", true)) * 8; auto findResult = fontPaths.find(font); - string fontResult = - findResult != fontPaths.end() ? basePath + findResult->second : ""; + string fontResult = findResult != fontPaths.end() ? basePath + findResult->second : ""; LoadFonts(basePath); - VImage combinedText = - VImage::black(width, pageHeight, VImage::option()->set("bands", 3)) - .bandjoin(0) - .copy(VImage::option()->set("interpretation", - VIPS_INTERPRETATION_sRGB)); + VImage combinedText = VImage::black(width, pageHeight, VImage::option()->set("bands", 3)) + .bandjoin(0) + .copy(VImage::option()->set("interpretation", VIPS_INTERPRETATION_sRGB)); if (top != "") { - VImage topText = - genText(top, font_string, fontResult.c_str(), width, mask, radius); - combinedText = combinedText.composite( - topText, VIPS_BLEND_MODE_OVER, - VImage::option() - ->set("x", (width / 2) - (topText.width() / 2)) - ->set("y", 0)); + VImage topText = genText(top, font_string, fontResult.c_str(), width, mask, radius); + combinedText = combinedText.composite(topText, VIPS_BLEND_MODE_OVER, + VImage::option()->set("x", (width / 2) - (topText.width() / 2))->set("y", 0)); } if (bottom != "") { - VImage bottomText = - genText(bottom, font_string, fontResult.c_str(), width, mask, radius); + VImage bottomText = genText(bottom, font_string, fontResult.c_str(), width, mask, radius); combinedText = combinedText.composite( - bottomText, VIPS_BLEND_MODE_OVER, - VImage::option() - ->set("x", (width / 2) - (bottomText.width() / 2)) - ->set("y", pageHeight - bottomText.height())); + bottomText, VIPS_BLEND_MODE_OVER, + VImage::option()->set("x", (width / 2) - (bottomText.width() / 2))->set("y", pageHeight - bottomText.height())); } - VImage replicated = combinedText - .copy(VImage::option()->set("interpretation", - VIPS_INTERPRETATION_sRGB)) - .replicate(1, nPages); + VImage replicated = + combinedText.copy(VImage::option()->set("interpretation", VIPS_INTERPRETATION_sRGB)).replicate(1, nPages); VImage final = in.composite(replicated, VIPS_BLEND_MODE_OVER); SetupTimeoutCallback(final, shouldKill); char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/mirror.cc b/natives/mirror.cc index 1bce9d3f..4067443c 100644 --- a/natives/mirror.cc +++ b/natives/mirror.cc @@ -5,14 +5,12 @@ using namespace std; using namespace vips; -ArgumentMap Mirror(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Mirror(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { bool vertical = GetArgumentWithFallback(arguments, "vertical", false); bool first = GetArgumentWithFallback(arguments, "first", false); - VImage in = VImage::new_from_buffer( - bufferdata, bufferLength, "", - GetInputOptions(type, false, false)); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, false, false)); int nPages = type == "avif" ? 1 : vips_image_get_n_pages(in.get_image()); @@ -28,9 +26,8 @@ ArgumentMap Mirror(const string& type, string& outType, const char* bufferdata, int x = (i * pageHeight) + (first ? 0 : (pageHeight / 2)); VImage cropped = in.crop(0, x, in.width(), pageHeight / 2); VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL); - VImage final = VImage::arrayjoin( - {first ? cropped : flipped, first ? flipped : cropped}, - VImage::option()->set("across", 1)); + VImage final = + VImage::arrayjoin({first ? cropped : flipped, first ? flipped : cropped}, VImage::option()->set("across", 1)); img.push_back(final); } out = VImage::arrayjoin(img, VImage::option()->set("across", 1)); @@ -39,14 +36,12 @@ ArgumentMap Mirror(const string& type, string& outType, const char* bufferdata, if (first) { VImage cropped = in.extract_area(0, 0, in.width(), in.height() / 2); VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL); - out = VImage::arrayjoin({cropped, flipped}, - VImage::option()->set("across", 1)); + out = VImage::arrayjoin({cropped, flipped}, VImage::option()->set("across", 1)); } else { int size = in.height() / 2; VImage cropped = in.extract_area(0, size, in.width(), size); VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL); - out = VImage::arrayjoin({flipped, cropped}, - VImage::option()->set("across", 1)); + out = VImage::arrayjoin({flipped, cropped}, VImage::option()->set("across", 1)); } } } else { @@ -66,7 +61,7 @@ ArgumentMap Mirror(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; diff --git a/natives/motivate.cc b/natives/motivate.cc index aa79da3a..f8d65747 100644 --- a/natives/motivate.cc +++ b/natives/motivate.cc @@ -5,17 +5,15 @@ using namespace std; using namespace vips; -ArgumentMap Motivate(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Motivate(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string top_text = GetArgument(arguments, "topText"); string bottom_text = GetArgument(arguments, "bottomText"); string font = GetArgument(arguments, "font"); string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); int width = in.width(); @@ -26,42 +24,35 @@ ArgumentMap Motivate(const string& type, string& outType, const char* bufferdata string font_string = font == "roboto" ? "Roboto Condensed" : font; auto findResult = fontPaths.find(font); - string fontResult = - findResult != fontPaths.end() ? basePath + findResult->second : ""; + string fontResult = findResult != fontPaths.end() ? basePath + findResult->second : ""; LoadFonts(basePath); VImage topImage; if (top_text != "") { - string topText = "" + - top_text + ""; - - VOption *topTextOptions = - VImage::option() - ->set("rgba", true) - ->set("align", VIPS_ALIGN_CENTRE) - ->set("width", textWidth) - ->set("font", (font_string + " " + to_string(size)).c_str()); + string topText = "" + top_text + ""; + + VOption *topTextOptions = VImage::option() + ->set("rgba", true) + ->set("align", VIPS_ALIGN_CENTRE) + ->set("width", textWidth) + ->set("font", (font_string + " " + to_string(size)).c_str()); if (fontResult != "") { - topTextOptions = topTextOptions->set( - "fontfile", (basePath + findResult->second).c_str()); + topTextOptions = topTextOptions->set("fontfile", (basePath + findResult->second).c_str()); } topImage = VImage::text(topText.c_str(), topTextOptions); } VImage bottomImage; if (bottom_text != "") { - string bottomText = "" + - bottom_text + ""; - - VOption *bottomTextOptions = - VImage::option() - ->set("rgba", true) - ->set("align", VIPS_ALIGN_CENTRE) - ->set("width", textWidth) - ->set("font", (font_string + " " + to_string(size * 0.4)).c_str()); + string bottomText = "" + bottom_text + ""; + + VOption *bottomTextOptions = VImage::option() + ->set("rgba", true) + ->set("align", VIPS_ALIGN_CENTRE) + ->set("width", textWidth) + ->set("font", (font_string + " " + to_string(size * 0.4)).c_str()); if (fontResult != "") { - bottomTextOptions = bottomTextOptions->set( - "fontfile", (basePath + findResult->second).c_str()); + bottomTextOptions = bottomTextOptions->set("fontfile", (basePath + findResult->second).c_str()); } bottomImage = VImage::text(bottomText.c_str(), bottomTextOptions); } @@ -69,59 +60,47 @@ ArgumentMap Motivate(const string& type, string& outType, const char* bufferdata vector img; int height = 0; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; int borderSize = max(2, width / 66); int borderSize2 = borderSize * 0.5; - VImage bordered = - img_frame.embed(borderSize, borderSize, width + (borderSize * 2), - pageHeight + (borderSize * 2), - VImage::option()->set("extend", "black")); - VImage bordered2 = bordered.embed(borderSize2, borderSize2, - bordered.width() + (borderSize2 * 2), - bordered.height() + (borderSize2 * 2), - VImage::option()->set("extend", "white")); + VImage bordered = img_frame.embed(borderSize, borderSize, width + (borderSize * 2), pageHeight + (borderSize * 2), + VImage::option()->set("extend", "black")); + VImage bordered2 = bordered.embed(borderSize2, borderSize2, bordered.width() + (borderSize2 * 2), + bordered.height() + (borderSize2 * 2), VImage::option()->set("extend", "white")); int addition = width / 8; int sideAddition = pageHeight * 0.4; - VImage bordered3 = bordered2.embed( - sideAddition / 2, addition / 2, bordered2.width() + sideAddition, - bordered2.height() + addition, - VImage::option()->set("extend", "black")); + VImage bordered3 = bordered2.embed(sideAddition / 2, addition / 2, bordered2.width() + sideAddition, + bordered2.height() + addition, VImage::option()->set("extend", "black")); VImage frame; if (top_text != "") { - frame = bordered3.join( - topImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(), - topImage.height() + (size / 4), - VImage::option()->set("extend", "black")), - VIPS_DIRECTION_VERTICAL, - VImage::option()->set("background", 0x000000)->set("expand", true)); + frame = + bordered3.join(topImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(), topImage.height() + (size / 4), + VImage::option()->set("extend", "black")), + VIPS_DIRECTION_VERTICAL, VImage::option()->set("background", 0x000000)->set("expand", true)); } if (bottom_text != "") { if (top_text == "") frame = bordered3; - frame = frame.join( - bottomImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(), - bottomImage.height() + (size / 4), - VImage::option()->set("extend", "black")), - VIPS_DIRECTION_VERTICAL, - VImage::option()->set("background", 0x000000)->set("expand", true)); + frame = + frame.join(bottomImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(), + bottomImage.height() + (size / 4), VImage::option()->set("extend", "black")), + VIPS_DIRECTION_VERTICAL, VImage::option()->set("background", 0x000000)->set("expand", true)); } height = frame.height(); img.push_back(frame); } - VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)) - .extract_band(0, VImage::option()->set("n", 3)); + VImage final = + VImage::arrayjoin(img, VImage::option()->set("across", 1)).extract_band(0, VImage::option()->set("n", 3)); final.set(VIPS_META_PAGE_HEIGHT, height); SetupTimeoutCallback(final, shouldKill); char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" ? VImage::option()->set("dither", 1) : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/node/image.cc b/natives/node/image.cc index 8cf1add9..089a6dfe 100644 --- a/natives/node/image.cc +++ b/natives/node/image.cc @@ -21,25 +21,17 @@ namespace backward { using namespace std; -bool isNapiValueInt(Napi::Env& env, Napi::Value& num) { - return env.Global() - .Get("Number") - .ToObject() - .Get("isInteger") - .As() - .Call({num}) - .ToBoolean() - .Value(); +bool isNapiValueInt(Napi::Env &env, Napi::Value &num) { + return env.Global().Get("Number").ToObject().Get("isInteger").As().Call({num}).ToBoolean().Value(); } -Napi::Value ProcessImage(const Napi::CallbackInfo& info) { +Napi::Value ProcessImage(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); string command = info[0].As().Utf8Value(); Napi::Object obj = info[1].As(); Napi::Object input = info[2].As(); - string type = - input.Has("type") ? input.Get("type").As().Utf8Value() : "png"; + string type = input.Has("type") ? input.Get("type").As().Utf8Value() : "png"; Napi::Function callback = info[3].As(); Napi::Array properties = obj.GetPropertyNames(); @@ -47,8 +39,7 @@ Napi::Value ProcessImage(const Napi::CallbackInfo& info) { ArgumentMap Arguments; for (unsigned int i = 0; i < properties.Length(); i++) { - string property = - properties.Get(uint32_t(i)).As().Utf8Value(); + string property = properties.Get(uint32_t(i)).As().Utf8Value(); auto val = obj.Get(property); if (val.IsBoolean()) { @@ -57,7 +48,8 @@ Napi::Value ProcessImage(const Napi::CallbackInfo& info) { Arguments[property] = val.ToString().As().Utf8Value(); } else if (val.IsNumber()) { auto num = val.ToNumber(); - if (isNapiValueInt(env, num) && property != "yscale" && property != "tolerance" && property != "pos") { // dumb hack + if (isNapiValueInt(env, num) && property != "yscale" && property != "tolerance" && + property != "pos") { // dumb hack Arguments[property] = num.Int32Value(); } else { Arguments[property] = num.FloatValue(); @@ -77,13 +69,13 @@ Napi::Value ProcessImage(const Napi::CallbackInfo& info) { bufSize = data.ByteLength(); } - ImageAsyncWorker* asyncWorker = new ImageAsyncWorker(callback, command, Arguments, type, bufData, bufSize); + ImageAsyncWorker *asyncWorker = new ImageAsyncWorker(callback, command, Arguments, type, bufData, bufSize); asyncWorker->Queue(); return Napi::BigInt::From(env, reinterpret_cast(asyncWorker)); } void *checkTypes(GType type, Napi::Object *formats) { - VipsObjectClass *c = VIPS_OBJECT_CLASS(g_type_class_ref(type)); + VipsObjectClass *c = VIPS_OBJECT_CLASS(g_type_class_ref(type)); if (strcmp(c->nickname, "jpegload")) formats->Set("image/jpeg", true); if (strcmp(c->nickname, "pngload")) formats->Set("image/png", true); @@ -91,10 +83,10 @@ void *checkTypes(GType type, Napi::Object *formats) { if (strcmp(c->nickname, "webpload")) formats->Set("image/webp", true); if (strcmp(c->nickname, "heifload")) formats->Set("image/avif", true); - return NULL; + return NULL; } -Napi::Value ImgInit(const Napi::CallbackInfo& info) { +Napi::Value ImgInit(const Napi::CallbackInfo &info) { #if defined(WIN32) && defined(MAGICK_ENABLED) Magick::InitializeMagick(""); #endif @@ -115,18 +107,17 @@ Napi::Value ImgInit(const Napi::CallbackInfo& info) { } Napi::Object Init(Napi::Env env, Napi::Object exports) { - exports.Set(Napi::String::New(env, "image"), - Napi::Function::New(env, ProcessImage)); + exports.Set(Napi::String::New(env, "image"), Napi::Function::New(env, ProcessImage)); exports.Set(Napi::String::New(env, "imageInit"), Napi::Function::New(env, ImgInit)); Napi::Array arr = Napi::Array::New(env); size_t i = 0; - for (auto const& imap : FunctionMap) { + for (auto const &imap : FunctionMap) { Napi::HandleScope scope(env); arr[i] = Napi::String::New(env, imap.first); i++; } - for (auto const& imap : NoInputFunctionMap) { + for (auto const &imap : NoInputFunctionMap) { Napi::HandleScope scope(env); arr[i] = Napi::String::New(env, imap.first); i++; diff --git a/natives/node/worker.cc b/natives/node/worker.cc index ba133024..9e2bd5e9 100644 --- a/natives/node/worker.cc +++ b/natives/node/worker.cc @@ -3,22 +3,22 @@ using namespace std; -ImageAsyncWorker::ImageAsyncWorker(Function& callback, string command, ArgumentMap inArgs, string type, const char *bufData, size_t bufSize) - : AsyncWorker(callback), command(command), inArgs(inArgs), type(type), bufData(bufData), bufSize(bufSize){} +ImageAsyncWorker::ImageAsyncWorker(Function &callback, string command, ArgumentMap inArgs, string type, + const char *bufData, size_t bufSize) + : AsyncWorker(callback), command(command), inArgs(inArgs), type(type), bufData(bufData), bufSize(bufSize) {} void ImageAsyncWorker::Execute() { outType = GetArgumentWithFallback(inArgs, "togif", false) ? "gif" : type; shouldKill = false; if (bufSize != 0) { - outArgs = FunctionMap.at(command)(type, outType, bufData, bufSize, - inArgs, &shouldKill); + outArgs = FunctionMap.at(command)(type, outType, bufData, bufSize, inArgs, &shouldKill); } else { outArgs = NoInputFunctionMap.at(command)(type, outType, inArgs, &shouldKill); } } -void ImageAsyncWorker::OnError(const Error& e) { +void ImageAsyncWorker::OnError(const Error &e) { vips_error_clear(); vips_thread_shutdown(); if (shouldKill) { @@ -34,7 +34,7 @@ void ImageAsyncWorker::OnOK() { Buffer nodeBuf = Buffer::New(Env(), 0); size_t outSize = GetArgument(outArgs, "size"); if (outSize > 0) { - char* buf = GetArgument(outArgs, "buf"); + char *buf = GetArgument(outArgs, "buf"); nodeBuf = Buffer::Copy(Env(), buf, outSize); g_free(buf); } diff --git a/natives/node/worker.h b/natives/node/worker.h index ae1da6d0..7481696f 100644 --- a/natives/node/worker.h +++ b/natives/node/worker.h @@ -1,30 +1,31 @@ #pragma once -#include #include "../common.h" +#include using namespace Napi; class ImageAsyncWorker : public AsyncWorker { - public: - ImageAsyncWorker(Function& callback, string command, ArgumentMap inArgs, string type, const char *bufData, size_t bufSize); - virtual ~ImageAsyncWorker(){}; +public: + ImageAsyncWorker(Function &callback, string command, ArgumentMap inArgs, string type, const char *bufData, + size_t bufSize); + virtual ~ImageAsyncWorker() {}; void Execute(); - void OnError(const Error& e); + void OnError(const Error &e); void OnOK(); void SetKill() { shouldKill = true; } - private: - string command; - ArgumentMap inArgs; - string type; +private: + string command; + ArgumentMap inArgs; + string type; - const char *bufData; - size_t bufSize; + const char *bufData; + size_t bufSize; - ArgumentMap outArgs; - string outType; + ArgumentMap outArgs; + string outType; - bool shouldKill; + bool shouldKill; }; diff --git a/natives/qr.cc b/natives/qr.cc index 639ebfe7..0fede497 100644 --- a/natives/qr.cc +++ b/natives/qr.cc @@ -10,10 +10,12 @@ #include "common.h" -ArgumentMap QrCreate([[maybe_unused]] const string& type, string& outType, ArgumentMap arguments, [[maybe_unused]] bool* shouldKill) { +ArgumentMap QrCreate([[maybe_unused]] const string &type, string &outType, ArgumentMap arguments, + [[maybe_unused]] bool *shouldKill) { string text = GetArgument(arguments, "text"); - auto writer = ZXing::MultiFormatWriter(ZXing::BarcodeFormat::QRCode).setMargin(1).setEncoding(ZXing::CharacterSet::UTF8); + auto writer = + ZXing::MultiFormatWriter(ZXing::BarcodeFormat::QRCode).setMargin(1).setEncoding(ZXing::CharacterSet::UTF8); #if ZXING_VERSION_MAJOR >= 2 ZXing::BitMatrix matrix = writer.encode(text, 0, 0); @@ -22,12 +24,13 @@ ArgumentMap QrCreate([[maybe_unused]] const string& type, string& outType, Argum #endif ZXing::Matrix bitmap = ZXing::ToMatrix(matrix); - vips::VImage img = vips::VImage::new_from_memory(const_cast(bitmap.data()), bitmap.size(), bitmap.width(), bitmap.height(), 1, VIPS_FORMAT_UCHAR) - .resize(4, vips::VImage::option()->set("kernel", VIPS_KERNEL_NEAREST)); + vips::VImage img = vips::VImage::new_from_memory(const_cast(bitmap.data()), bitmap.size(), + bitmap.width(), bitmap.height(), 1, VIPS_FORMAT_UCHAR) + .resize(4, vips::VImage::option()->set("kernel", VIPS_KERNEL_NEAREST)); char *buf; size_t dataSize = 0; - img.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + img.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; @@ -36,21 +39,22 @@ ArgumentMap QrCreate([[maybe_unused]] const string& type, string& outType, Argum return output; } -ArgumentMap QrRead([[maybe_unused]] const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, [[maybe_unused]] bool* shouldKill) { +ArgumentMap QrRead([[maybe_unused]] const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, [[maybe_unused]] bool *shouldKill) { vips::VOption *options = vips::VImage::option()->set("access", "sequential"); - vips::VImage in = - vips::VImage::new_from_buffer(bufferdata, bufferLength, "", options) - .colourspace(VIPS_INTERPRETATION_B_W).extract_band(0, vips::VImage::option()->set("n", 1)); + vips::VImage in = vips::VImage::new_from_buffer(bufferdata, bufferLength, "", options) + .colourspace(VIPS_INTERPRETATION_B_W) + .extract_band(0, vips::VImage::option()->set("n", 1)); vips_image_wio_input(in.get_image()); #if ZXING_VERSION_MAJOR >= 2 - #if ZXING_VERSION_MINOR >= 2 - ZXing::ReaderOptions opts; - #else - ZXing::DecodeHints opts; - #endif +#if ZXING_VERSION_MINOR >= 2 + ZXing::ReaderOptions opts; +#else + ZXing::DecodeHints opts; +#endif #else ZXing::DecodeHints opts; #endif @@ -75,7 +79,7 @@ ArgumentMap QrRead([[maybe_unused]] const string& type, string& outType, const c #endif size_t dataSize = resultText.length(); - char *data = reinterpret_cast(malloc(dataSize)); + char *data = reinterpret_cast(malloc(dataSize)); memcpy(data, resultText.c_str(), dataSize); output["buf"] = data; diff --git a/natives/reddit.cc b/natives/reddit.cc index ac994b68..947abdf6 100644 --- a/natives/reddit.cc +++ b/natives/reddit.cc @@ -5,15 +5,13 @@ using namespace std; using namespace vips; -ArgumentMap Reddit(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Reddit(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string text = GetArgument(arguments, "caption"); string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); string assetPath = basePath + "assets/images/reddit.png"; @@ -26,27 +24,22 @@ ArgumentMap Reddit(const string& type, string& outType, const char* bufferdata, string captionText = "" + text + ""; LoadFonts(basePath); - VImage textImage = VImage::text( - captionText.c_str(), - VImage::option() - ->set("rgba", true) - ->set("font", "Roboto 62") - ->set("fontfile", (basePath + "assets/fonts/reddit.ttf").c_str()) - ->set("align", VIPS_ALIGN_LOW)); + VImage textImage = + VImage::text(captionText.c_str(), VImage::option() + ->set("rgba", true) + ->set("font", "Roboto 62") + ->set("fontfile", (basePath + "assets/fonts/reddit.ttf").c_str()) + ->set("align", VIPS_ALIGN_LOW)); VImage composited = - tmpl.composite2(textImage, VIPS_BLEND_MODE_OVER, - VImage::option()->set("x", 375)->set( - "y", (tmpl.height() - textImage.height()) - 64)); - VImage watermark = - composited.resize((double)width / (double)composited.width()); + tmpl.composite2(textImage, VIPS_BLEND_MODE_OVER, + VImage::option()->set("x", 375)->set("y", (tmpl.height() - textImage.height()) - 64)); + VImage watermark = composited.resize((double)width / (double)composited.width()); vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; - VImage frame = img_frame.join(watermark, VIPS_DIRECTION_VERTICAL, - VImage::option()->set("expand", true)); + VImage img_frame = nPages > 1 ? 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); } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); @@ -56,11 +49,8 @@ ArgumentMap Reddit(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/resize.cc b/natives/resize.cc index 95b91386..28de1d4d 100644 --- a/natives/resize.cc +++ b/natives/resize.cc @@ -5,15 +5,13 @@ using namespace std; using namespace vips; -ArgumentMap Resize(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Resize(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { bool stretch = GetArgumentWithFallback(arguments, "stretch", false); bool wide = GetArgumentWithFallback(arguments, "wide", false); int wideAmount = GetArgumentWithFallback(arguments, "amount", 19); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)); VImage out; @@ -34,9 +32,7 @@ ArgumentMap Resize(const string& type, string& outType, const char* bufferdata, int finalHeight = 0; if (stretch) { - out = - in.resize(512.0 / (double)width, - VImage::option()->set("vscale", 512.0 / (double)pageHeight)); + out = in.resize(512.0 / (double)width, VImage::option()->set("vscale", 512.0 / (double)pageHeight)); finalHeight = 512; } else if (wide) { out = in.resize(wideAmount, VImage::option()->set("vscale", 1)); @@ -45,10 +41,8 @@ ArgumentMap Resize(const string& type, string& outType, const char* bufferdata, // Pain. Pain. Pain. Pain. Pain. vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; - VImage resized = img_frame.resize(0.1).resize( - 10, VImage::option()->set("kernel", VIPS_KERNEL_NEAREST)); + VImage img_frame = nPages > 1 ? 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); finalHeight = resized.height(); } @@ -60,7 +54,7 @@ ArgumentMap Resize(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; diff --git a/natives/reverse.cc b/natives/reverse.cc index 27017c6b..85cf47a5 100644 --- a/natives/reverse.cc +++ b/natives/reverse.cc @@ -5,8 +5,8 @@ using namespace std; using namespace vips; -ArgumentMap Reverse([[maybe_unused]] const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Reverse([[maybe_unused]] const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { bool soos = GetArgumentWithFallback(arguments, "soos", false); VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, false, false)); @@ -29,7 +29,7 @@ ArgumentMap Reverse([[maybe_unused]] const string& type, string& outType, const // this command is useless with single-page images if (nPages < 2) { size_t dataSize = bufferLength; - char *data = reinterpret_cast(malloc(bufferLength)); + char *data = reinterpret_cast(malloc(bufferLength)); memcpy(data, bufferdata, bufferLength); ArgumentMap output; @@ -73,7 +73,7 @@ ArgumentMap Reverse([[maybe_unused]] const string& type, string& outType, const char *buf; size_t dataSize = 0; - final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize, + final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize, outType == "gif" ? VImage::option()->set("dither", 0) : 0); ArgumentMap output; diff --git a/natives/scott.cc b/natives/scott.cc index 7f5a35fa..bf13c33d 100644 --- a/natives/scott.cc +++ b/natives/scott.cc @@ -5,14 +5,12 @@ using namespace std; using namespace vips; -ArgumentMap Scott(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Scott(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); int width = in.width(); @@ -36,19 +34,13 @@ ArgumentMap Scott(const string& type, string& outType, const char* bufferdata, s string distortPath = basePath + "assets/images/scottmap.png"; VImage distort = VImage::new_from_file(distortPath.c_str()); - VImage distortImage = - ((distort[1] / 255) * 414).bandjoin((distort[0] / 255) * 233); + VImage distortImage = ((distort[1] / 255) * 414).bandjoin((distort[0] / 255) * 233); vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; - VImage resized = img_frame.resize( - 415 / (double)width, - VImage::option()->set("vscale", 234 / (double)pageHeight)); - VImage mapped = resized.mapim(distortImage) - .extract_band(0, VImage::option()->set("n", 3)) - .bandjoin(distort[2]); + VImage img_frame = nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage resized = img_frame.resize(415 / (double)width, VImage::option()->set("vscale", 234 / (double)pageHeight)); + VImage mapped = resized.mapim(distortImage).extract_band(0, VImage::option()->set("n", 3)).bandjoin(distort[2]); VImage offset = mapped.embed(127, 181, 864, 481); VImage composited = bg.composite2(offset, VIPS_BLEND_MODE_OVER); img.push_back(composited); @@ -60,9 +52,8 @@ ArgumentMap Scott(const string& type, string& outType, const char* bufferdata, s char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" ? VImage::option()->set("dither", 1) : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/snapchat.cc b/natives/snapchat.cc index 5aa63ffc..73125a82 100644 --- a/natives/snapchat.cc +++ b/natives/snapchat.cc @@ -7,16 +7,14 @@ using namespace vips; const vector zeroVec178 = {0, 0, 0, 178}; -ArgumentMap Snapchat(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Snapchat(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string caption = GetArgument(arguments, "caption"); float pos = GetArgumentWithFallback(arguments, "pos", 0.565); string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); int width = in.width(); @@ -28,41 +26,31 @@ ArgumentMap Snapchat(const string& type, string& outType, const char* bufferdata string font_string = "Helvetica Neue " + to_string(size); LoadFonts(basePath); - VImage 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/caption2.ttf").c_str()) - ->set("width", textWidth)); + VImage 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/caption2.ttf").c_str()) + ->set("width", textWidth)); int bgHeight = textIn.height() + (width / 25); textIn = ((textIn == zeroVec).bandand()) - .ifthenelse(zeroVec178, textIn) - .embed((width / 2) - (textIn.width() / 2), - (bgHeight / 2) - (textIn.height() / 2), width, bgHeight, - VImage::option() - ->set("extend", "background") - ->set("background", zeroVec178)); + .ifthenelse(zeroVec178, textIn) + .embed((width / 2) - (textIn.width() / 2), (bgHeight / 2) - (textIn.height() / 2), width, bgHeight, + VImage::option()->set("extend", "background")->set("background", zeroVec178)); int yPos = (pageHeight - textIn.height()) * pos; VImage replicated = textIn.embed(0, yPos, width, pageHeight) - .copy(VImage::option()->set("interpretation", - VIPS_INTERPRETATION_sRGB)) - .replicate(1, nPages); + .copy(VImage::option()->set("interpretation", VIPS_INTERPRETATION_sRGB)) + .replicate(1, nPages); VImage final = in.composite(replicated, VIPS_BLEND_MODE_OVER); SetupTimeoutCallback(final, shouldKill); char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/sonic.cc b/natives/sonic.cc index 268bdc8f..d24bcca1 100644 --- a/natives/sonic.cc +++ b/natives/sonic.cc @@ -5,8 +5,7 @@ using namespace std; using namespace vips; -ArgumentMap Sonic([[maybe_unused]] const string& type, string& outType, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Sonic([[maybe_unused]] const string &type, string &outType, ArgumentMap arguments, bool *shouldKill) { string text = GetArgument(arguments, "text"); string basePath = GetArgument(arguments, "basePath"); @@ -15,24 +14,21 @@ ArgumentMap Sonic([[maybe_unused]] const string& type, string& outType, Argument LoadFonts(basePath); VImage textImage = - VImage::text( - ("" + text + "").c_str(), - VImage::option() - ->set("rgba", true) - ->set("align", VIPS_ALIGN_CENTRE) - ->set("font", "Verdana") - ->set("width", 542) - ->set("height", 390)) - .gravity(VIPS_COMPASS_DIRECTION_CENTRE, 542, 390); - - VImage out = bg.composite2(textImage, VIPS_BLEND_MODE_OVER, - VImage::option()->set("x", 391)->set("y", 84)); + VImage::text(("" + text + "").c_str(), VImage::option() + ->set("rgba", true) + ->set("align", VIPS_ALIGN_CENTRE) + ->set("font", "Verdana") + ->set("width", 542) + ->set("height", 390)) + .gravity(VIPS_COMPASS_DIRECTION_CENTRE, 542, 390); + + VImage out = bg.composite2(textImage, VIPS_BLEND_MODE_OVER, VImage::option()->set("x", 391)->set("y", 84)); SetupTimeoutCallback(out, shouldKill); char *buf; size_t dataSize = 0; - out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + out.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; diff --git a/natives/speed.cc b/natives/speed.cc index 24f229ca..b29977c7 100644 --- a/natives/speed.cc +++ b/natives/speed.cc @@ -1,5 +1,5 @@ -#include #include +#include #include #include "common.h" @@ -14,7 +14,7 @@ void *memset16(void *m, uint16_t val, size_t count) { return m; } -char *vipsRemove(const char *data, size_t length, size_t& dataSize, int speed, string suffix, bool *shouldKill) { +char *vipsRemove(const char *data, size_t length, size_t &dataSize, int speed, string suffix, bool *shouldKill) { VOption *options = VImage::option()->set("access", "sequential"); VImage in = VImage::new_from_buffer(data, length, "", options->set("n", -1)); @@ -34,53 +34,49 @@ char *vipsRemove(const char *data, size_t length, size_t& dataSize, int speed, s SetupTimeoutCallback(out, shouldKill); char *buf; - out.write_to_buffer(suffix.c_str(), reinterpret_cast(&buf), &dataSize); + out.write_to_buffer(suffix.c_str(), reinterpret_cast(&buf), &dataSize); return buf; } -ArgumentMap Speed([[maybe_unused]] const string& type, [[maybe_unused]] string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Speed([[maybe_unused]] const string &type, [[maybe_unused]] string &outType, const char *bufferdata, + size_t bufferLength, ArgumentMap arguments, bool *shouldKill) { bool slow = GetArgumentWithFallback(arguments, "slow", false); int speed = GetArgumentWithFallback(arguments, "speed", 2); ArgumentMap output; size_t dataSize = 0; - char *fileData = reinterpret_cast(malloc(bufferLength)); + char *fileData = reinterpret_cast(malloc(bufferLength)); memcpy(fileData, bufferdata, bufferLength); if (type == "gif") { - char *match = const_cast("\x00\x21\xF9\x04"); + char *match = const_cast("\x00\x21\xF9\x04"); vector old_delays; bool removeFrames = false; char *lastPos; - lastPos = reinterpret_cast(memchr(fileData, '\x00', bufferLength)); + lastPos = reinterpret_cast(memchr(fileData, '\x00', bufferLength)); while (lastPos != NULL) { if (memcmp(lastPos, match, 4) != 0) { - lastPos = reinterpret_cast(memchr(lastPos + 1, '\x00', - (bufferLength - (lastPos - fileData)) - 1)); + lastPos = reinterpret_cast(memchr(lastPos + 1, '\x00', (bufferLength - (lastPos - fileData)) - 1)); continue; } uint16_t old_delay; memcpy(&old_delay, lastPos + 5, 2); old_delays.push_back(old_delay); - lastPos = reinterpret_cast(memchr(lastPos + 1, '\x00', - (bufferLength - (lastPos - fileData)) - 1)); + lastPos = reinterpret_cast(memchr(lastPos + 1, '\x00', (bufferLength - (lastPos - fileData)) - 1)); } int currentFrame = 0; - lastPos = reinterpret_cast(memchr(fileData, '\x00', bufferLength)); + lastPos = reinterpret_cast(memchr(fileData, '\x00', bufferLength)); while (lastPos != NULL) { if (memcmp(lastPos, match, 4) != 0) { - lastPos = reinterpret_cast(memchr(lastPos + 1, '\x00', - (bufferLength - (lastPos - fileData)) - 1)); + lastPos = reinterpret_cast(memchr(lastPos + 1, '\x00', (bufferLength - (lastPos - fileData)) - 1)); continue; } - uint16_t new_delay = slow ? old_delays[currentFrame] * speed - : old_delays[currentFrame] / speed; + uint16_t new_delay = slow ? old_delays[currentFrame] * speed : old_delays[currentFrame] / speed; if (!slow && new_delay <= 1) { removeFrames = true; break; @@ -88,8 +84,7 @@ ArgumentMap Speed([[maybe_unused]] const string& type, [[maybe_unused]] string& memset16(lastPos + 5, new_delay, 1); - lastPos = reinterpret_cast(memchr(lastPos + 1, '\x00', - (bufferLength - (lastPos - fileData)) - 1)); + lastPos = reinterpret_cast(memchr(lastPos + 1, '\x00', (bufferLength - (lastPos - fileData)) - 1)); ++currentFrame; } @@ -103,12 +98,12 @@ ArgumentMap Speed([[maybe_unused]] const string& type, [[maybe_unused]] string& bool removeFrames = false; while (position + 8 <= bufferLength) { - const char* fourCC = &fileData[position]; - uint32_t chunkSize = readUint32LE(reinterpret_cast(fileData) + position + 4); + const char *fourCC = &fileData[position]; + uint32_t chunkSize = readUint32LE(reinterpret_cast(fileData) + position + 4); if (memcmp(fourCC, "ANMF", 4) == 0) { size_t dataStart = position + 8; - uint32_t duration = readUint32LE(reinterpret_cast(fileData) + dataStart + 12) & 0x00FFFFFF; + uint32_t duration = readUint32LE(reinterpret_cast(fileData) + dataStart + 12) & 0x00FFFFFF; uint32_t newDuration = slow ? duration * speed : duration / speed; if (!slow && newDuration <= 10) { removeFrames = true; diff --git a/natives/spin.cc b/natives/spin.cc index 825ae48d..9a4efb39 100644 --- a/natives/spin.cc +++ b/natives/spin.cc @@ -5,10 +5,9 @@ using namespace std; using namespace vips; -ArgumentMap Spin(const string& type, string& outType, const char* bufferdata, - size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, bool* shouldKill) { - VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, true)); +ArgumentMap Spin(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, bool *shouldKill) { + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, true)); int width = in.width(); int pageHeight = vips_image_get_page_height(in.get_image()); @@ -33,14 +32,11 @@ ArgumentMap Spin(const string& type, string& outType, const char* bufferdata, vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - multiPage ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = multiPage ? in.crop(0, i * pageHeight, width, pageHeight) : in; double rotation = (double)360 * i / nPages; - VImage rotated = img_frame.similarity( - VImage::option()->set("angle", rotation)); - VImage embedded = rotated.embed( - (width / 2) - (rotated.width() / 2), - (pageHeight / 2) - (rotated.height() / 2), width, pageHeight); + VImage rotated = img_frame.similarity(VImage::option()->set("angle", rotation)); + VImage embedded = + rotated.embed((width / 2) - (rotated.width() / 2), (pageHeight / 2) - (rotated.height() / 2), width, pageHeight); img.push_back(embedded); } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); @@ -52,9 +48,9 @@ ArgumentMap Spin(const string& type, string& outType, const char* bufferdata, SetupTimeoutCallback(final, shouldKill); - char* buf; + char *buf; size_t dataSize = 0; - final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize); + final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize); if (outType != "webp") outType = "gif"; diff --git a/natives/spotify.cc b/natives/spotify.cc index dd98b7ea..81b52a1d 100644 --- a/natives/spotify.cc +++ b/natives/spotify.cc @@ -5,15 +5,13 @@ using namespace std; using namespace vips; -ArgumentMap Spotify(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Spotify(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string text = GetArgument(arguments, "caption"); string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); string assetPath = basePath + "assets/images/spotify.png"; @@ -26,28 +24,22 @@ ArgumentMap Spotify(const string& type, string& outType, const char* bufferdata, string captionText = "" + text + ""; LoadFonts(basePath); - VImage textImage = VImage::text( - captionText.c_str(), - VImage::option() - ->set("rgba", true) - ->set("font", "Circular Bold 78") - ->set("fontfile", (basePath + "assets/fonts/Circular.ttf").c_str()) - ->set("align", VIPS_ALIGN_CENTRE)); - - VImage composited = tmpl.composite2( - textImage, VIPS_BLEND_MODE_OVER, - VImage::option() - ->set("x", (tmpl.width() / 2) - (textImage.width() / 2)) - ->set("y", 195)); - VImage watermark = - composited.resize((double)width / (double)composited.width()); + VImage textImage = + VImage::text(captionText.c_str(), VImage::option() + ->set("rgba", true) + ->set("font", "Circular Bold 78") + ->set("fontfile", (basePath + "assets/fonts/Circular.ttf").c_str()) + ->set("align", VIPS_ALIGN_CENTRE)); + + VImage composited = + tmpl.composite2(textImage, VIPS_BLEND_MODE_OVER, + VImage::option()->set("x", (tmpl.width() / 2) - (textImage.width() / 2))->set("y", 195)); + VImage watermark = composited.resize((double)width / (double)composited.width()); vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; - VImage frame = watermark.join(img_frame, VIPS_DIRECTION_VERTICAL, - VImage::option()->set("expand", true)); + VImage img_frame = nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage frame = watermark.join(img_frame, VIPS_DIRECTION_VERTICAL, VImage::option()->set("expand", true)); img.push_back(frame); } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); @@ -57,11 +49,8 @@ ArgumentMap Spotify(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/squish.cc b/natives/squish.cc index 351f2f50..91d9fd8c 100644 --- a/natives/squish.cc +++ b/natives/squish.cc @@ -7,12 +7,9 @@ using namespace std; using namespace vips; -ArgumentMap Squish(const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, bool* shouldKill) -{ - VImage in = - VImage::new_from_buffer( - bufferdata, bufferLength, "", - GetInputOptions(type, true, true)); +ArgumentMap Squish(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, bool *shouldKill) { + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, true)); int width = in.width(); int pageHeight = vips_image_get_page_height(in.get_image()); @@ -39,13 +36,11 @@ ArgumentMap Squish(const string& type, string& outType, const char* bufferdata, vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - multiPage ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = multiPage ? in.crop(0, i * pageHeight, width, pageHeight) : in; double newWidth = (sin(i * mult) / 4) + 0.75; double newHeight = (cos(i * mult) / 4) + 0.75; - VImage resized = - img_frame.resize(newWidth, VImage::option()->set("vscale", newHeight)) - .gravity(VIPS_COMPASS_DIRECTION_CENTRE, width, pageHeight); + VImage resized = img_frame.resize(newWidth, VImage::option()->set("vscale", newHeight)) + .gravity(VIPS_COMPASS_DIRECTION_CENTRE, width, pageHeight); img.push_back(resized); } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); @@ -59,7 +54,7 @@ ArgumentMap Squish(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize); + final.write_to_buffer(outType == "webp" ? ".webp" : ".gif", reinterpret_cast(&buf), &dataSize); if (outType != "webp") outType = "gif"; diff --git a/natives/swirl.cc b/natives/swirl.cc index 0e031047..689a6ada 100644 --- a/natives/swirl.cc +++ b/natives/swirl.cc @@ -5,11 +5,9 @@ using namespace std; using namespace vips; -ArgumentMap Swirl(const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, bool* shouldKill) -{ - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, false, false)); +ArgumentMap Swirl(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, bool *shouldKill) { + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, false, false)); int pageHeight = vips_image_get_page_height(in.get_image()); int nPages = type == "avif" ? 1 : vips_image_get_n_pages(in.get_image()); @@ -20,14 +18,9 @@ ArgumentMap Swirl(const string& type, string& outType, const char* bufferdata, s 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 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; @@ -37,28 +30,20 @@ ArgumentMap Swirl(const string& type, string& outType, const char* bufferdata, s 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)) + + .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 = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = nPages > 1 ? 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"))); + 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); } @@ -69,7 +54,7 @@ ArgumentMap Swirl(const string& type, string& outType, const char* bufferdata, s char *buf; size_t dataSize = 0; - final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize); ArgumentMap output; output["buf"] = buf; diff --git a/natives/tile.cc b/natives/tile.cc index 161a9220..94a5dcf0 100644 --- a/natives/tile.cc +++ b/natives/tile.cc @@ -5,16 +5,14 @@ using namespace std; using namespace vips; -ArgumentMap Tile(const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, bool* shouldKill) -{ - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, false, false)); +ArgumentMap Tile(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, bool *shouldKill) { + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, false, false)); int width = in.width(); int pageHeight = vips_image_get_page_height(in.get_image()); int nPages = type == "avif" ? 1 : vips_image_get_n_pages(in.get_image()); - + try { in = NormalizeVips(in, &width, &pageHeight, nPages); } catch (int e) { @@ -29,8 +27,7 @@ ArgumentMap Tile(const string& type, string& outType, const char* bufferdata, si vector img; int finalHeight = 0; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; VImage replicated = img_frame.replicate(5, 5); double scale = 800.0 / replicated.height(); if (scale > 1) scale = 800.0 / replicated.width(); @@ -45,9 +42,8 @@ ArgumentMap Tile(const string& type, string& outType, const char* bufferdata, si char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" ? VImage::option()->set("reoptimise", 1) : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/togif.cc b/natives/togif.cc index fa4f83ae..1e37e591 100644 --- a/natives/togif.cc +++ b/natives/togif.cc @@ -5,10 +5,10 @@ using namespace std; using namespace vips; -ArgumentMap ToGif(const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap ToGif(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, bool *shouldKill) { if (type == "gif") { - char *data = reinterpret_cast(malloc(bufferLength)); + char *data = reinterpret_cast(malloc(bufferLength)); memcpy(data, bufferdata, bufferLength); ArgumentMap output; @@ -20,15 +20,13 @@ ArgumentMap ToGif(const string& type, string& outType, const char* bufferdata, s } 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(bufferdata, bufferLength, "", type == "webp" ? options->set("n", -1) : options); SetupTimeoutCallback(in, shouldKill); char *buf; size_t dataSize = 0; - in.write_to_buffer(".gif", reinterpret_cast(&buf), &dataSize); + in.write_to_buffer(".gif", reinterpret_cast(&buf), &dataSize); outType = "gif"; ArgumentMap output; diff --git a/natives/uncanny.cc b/natives/uncanny.cc index 624e49b9..97114dbc 100644 --- a/natives/uncanny.cc +++ b/natives/uncanny.cc @@ -5,68 +5,57 @@ using namespace std; using namespace vips; -ArgumentMap Uncanny(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Uncanny(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { 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"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB) - .extract_band(0, VImage::option()->set("n", 3)); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB) + .extract_band(0, VImage::option()->set("n", 3)); VImage base = VImage::black(1280, 720, VImage::option()->set("bands", 3)); - string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " + - (font != "impact" ? "bold" : "normal") + " 72"; + string font_string = + (font == "roboto" ? "Roboto Condensed" : font) + " " + (font != "impact" ? "bold" : "normal") + " 72"; - string captionText = - "" + caption + ""; - string caption2Text = - "" + caption2 + ""; + string captionText = "" + caption + ""; + string caption2Text = "" + caption2 + ""; auto findResult = fontPaths.find(font); - string fontResult = - findResult != fontPaths.end() ? basePath + findResult->second : ""; + string fontResult = findResult != fontPaths.end() ? basePath + findResult->second : ""; LoadFonts(basePath); VOption *textOptions = VImage::option() - ->set("rgba", true) - ->set("align", VIPS_ALIGN_CENTRE) - ->set("font", font_string.c_str()) - ->set("width", 588) - ->set("height", 90); + ->set("rgba", true) + ->set("align", VIPS_ALIGN_CENTRE) + ->set("font", font_string.c_str()) + ->set("width", 588) + ->set("height", 90); if (fontResult != "") { - textOptions = textOptions->set( - "fontfile", fontResult.c_str()); + textOptions = textOptions->set("fontfile", fontResult.c_str()); } - VImage text = VImage::text(captionText.c_str(), - textOptions); + VImage text = VImage::text(captionText.c_str(), textOptions); 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")); + text.extract_band(0, VImage::option()->set("n", 3)) + .gravity(VIPS_COMPASS_DIRECTION_CENTRE, 640, text.height() + 40, VImage::option()->set("extend", "black")); VOption *textOptions2 = VImage::option() - ->set("rgba", true) - ->set("align", VIPS_ALIGN_CENTRE) - ->set("font", font_string.c_str()) - ->set("width", 588) - ->set("height", 90); + ->set("rgba", true) + ->set("align", VIPS_ALIGN_CENTRE) + ->set("font", font_string.c_str()) + ->set("width", 588) + ->set("height", 90); if (fontResult != "") { - textOptions2 = textOptions2->set( - "fontfile", fontResult.c_str()); + textOptions2 = textOptions2->set("fontfile", fontResult.c_str()); } - VImage text2 = VImage::text(caption2Text.c_str(), - textOptions2); + VImage text2 = VImage::text(caption2Text.c_str(), textOptions2); 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")); + 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); @@ -80,15 +69,13 @@ ArgumentMap Uncanny(const string& type, string& outType, const char* bufferdata, vector img; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = nPages > 1 ? 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)); + 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)); @@ -98,9 +85,8 @@ ArgumentMap Uncanny(const string& type, string& outType, const char* bufferdata, char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" ? VImage::option()->set("reoptimise", 1) : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/uncaption.cc b/natives/uncaption.cc index 646b33b8..ed1d63cd 100644 --- a/natives/uncaption.cc +++ b/natives/uncaption.cc @@ -6,22 +6,17 @@ using namespace std; using namespace vips; -ArgumentMap Uncaption(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Uncaption(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { float tolerance = GetArgumentWithFallback(arguments, "tolerance", 0.5); - VImage in = - VImage::new_from_buffer( - bufferdata, bufferLength, "", - GetInputOptions(type, true, true)); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, true)); int width = in.width(); int pageHeight = vips_image_get_page_height(in.get_image()); int nPages = type == "avif" ? 1 : vips_image_get_n_pages(in.get_image()); - VImage first = - in.crop(0, 0, 3, pageHeight).colourspace(VIPS_INTERPRETATION_B_W) > - (255 * tolerance); + VImage first = in.crop(0, 0, 3, pageHeight).colourspace(VIPS_INTERPRETATION_B_W) > (255 * tolerance); int top, captionWidth, captionHeight; first.find_trim(&top, &captionWidth, &captionHeight); @@ -42,11 +37,8 @@ ArgumentMap Uncaption(const string& type, string& outType, const char* bufferdat char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/wall.cc b/natives/wall.cc index 8be70d54..7e6920c8 100644 --- a/natives/wall.cc +++ b/natives/wall.cc @@ -10,8 +10,8 @@ using namespace std; using namespace Magick; -ArgumentMap Wall([[maybe_unused]] const string& type, string& outType, const char* bufferdata, size_t bufferLength, [[maybe_unused]] ArgumentMap arguments, [[maybe_unused]] bool* shouldKill) -{ +ArgumentMap Wall([[maybe_unused]] const string &type, string &outType, const char *bufferdata, size_t bufferLength, + [[maybe_unused]] ArgumentMap arguments, [[maybe_unused]] bool *shouldKill) { Blob blob; list frames; @@ -32,8 +32,7 @@ ArgumentMap Wall([[maybe_unused]] const string& type, string& outType, const cha 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}; + 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(outType); @@ -53,7 +52,7 @@ ArgumentMap Wall([[maybe_unused]] const string& type, string& outType, const cha size_t dataSize = blob.length(); - char *data = reinterpret_cast(malloc(dataSize)); + char *data = reinterpret_cast(malloc(dataSize)); memcpy(data, blob.data(), dataSize); ArgumentMap output; diff --git a/natives/watermark.cc b/natives/watermark.cc index 2149161e..fd806c9b 100644 --- a/natives/watermark.cc +++ b/natives/watermark.cc @@ -6,8 +6,8 @@ using namespace std; using namespace vips; -ArgumentMap Watermark(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Watermark(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string water = GetArgument(arguments, "water"); int gravity = GetArgument(arguments, "gravity"); @@ -24,10 +24,8 @@ ArgumentMap Watermark(const string& type, string& outType, const char* bufferdat string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); string merged = basePath + water; @@ -49,13 +47,11 @@ ArgumentMap Watermark(const string& type, string& outType, const char* bufferdat if (append) { watermark = watermark.resize((double)width / (double)watermark.width()); } else if (yscale) { - watermark = watermark.resize( - (double)width / (double)watermark.width(), - VImage::option()->set("vscale", (double)(pageHeight * yscale) / - (double)watermark.height())); - } else { watermark = - watermark.resize((double)pageHeight / (double)watermark.height()); + watermark.resize((double)width / (double)watermark.width(), + VImage::option()->set("vscale", (double)(pageHeight * yscale) / (double)watermark.height())); + } else { + watermark = watermark.resize((double)pageHeight / (double)watermark.height()); } } @@ -99,52 +95,39 @@ ArgumentMap Watermark(const string& type, string& outType, const char* bufferdat VImage bg; VImage frame; for (int i = 0; i < nPages; i++) { - VImage img_frame = - nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; + VImage img_frame = nPages > 1 ? in.crop(0, i * pageHeight, width, pageHeight) : in; if (append) { - VImage appended = img_frame.join(watermark, VIPS_DIRECTION_VERTICAL, - VImage::option()->set("expand", true)); + VImage appended = img_frame.join(watermark, VIPS_DIRECTION_VERTICAL, VImage::option()->set("expand", true)); addedHeight = watermark.height(); img.push_back(appended); } else if (mc) { - VImage padded = - img_frame.embed(0, 0, width, pageHeight + 15, - VImage::option()->set("background", 0xffffff)); - VImage composited = - padded.composite2(watermark, VIPS_BLEND_MODE_OVER, - VImage::option() - ->set("x", width - 190) - ->set("y", padded.height() - 22)); + VImage padded = img_frame.embed(0, 0, width, pageHeight + 15, VImage::option()->set("background", 0xffffff)); + VImage composited = padded.composite2(watermark, VIPS_BLEND_MODE_OVER, + VImage::option()->set("x", width - 190)->set("y", padded.height() - 22)); addedHeight = 15; img.push_back(composited); } else { VImage composited; if (alpha) { if (i == 0) { - contentAlpha = watermark.extract_band(0).embed( - x, y, width, pageHeight, - VImage::option()->set("extend", "white")); - frameAlpha = watermark.extract_band(1).embed( - x, y, width, pageHeight, - VImage::option()->set("extend", "black")); - bg = frameAlpha.new_from_image({0, 0, 0}).copy(VImage::option()->set( - "interpretation", VIPS_INTERPRETATION_sRGB)); + contentAlpha = + watermark.extract_band(0).embed(x, y, width, pageHeight, VImage::option()->set("extend", "white")); + frameAlpha = + watermark.extract_band(1).embed(x, y, width, pageHeight, VImage::option()->set("extend", "black")); + bg = frameAlpha.new_from_image({0, 0, 0}).copy( + VImage::option()->set("interpretation", VIPS_INTERPRETATION_sRGB)); frame = bg.bandjoin(frameAlpha); if (outType == "jpg" || outType == "jpeg") { outType = "png"; } } VImage content = - img_frame.extract_band(0, VImage::option()->set("n", 3)) - .bandjoin(contentAlpha & img_frame.extract_band(3)); + img_frame.extract_band(0, VImage::option()->set("n", 3)).bandjoin(contentAlpha & img_frame.extract_band(3)); - composited = - content.composite2(frame, VIPS_BLEND_MODE_OVER, - VImage::option()->set("x", x)->set("y", y)); + composited = content.composite2(frame, VIPS_BLEND_MODE_OVER, VImage::option()->set("x", x)->set("y", y)); } else { composited = - img_frame.composite2(watermark, VIPS_BLEND_MODE_OVER, - VImage::option()->set("x", x)->set("y", y)); + img_frame.composite2(watermark, VIPS_BLEND_MODE_OVER, VImage::option()->set("x", x)->set("y", y)); } img.push_back(composited); } @@ -157,11 +140,8 @@ ArgumentMap Watermark(const string& type, string& outType, const char* bufferdat char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf; diff --git a/natives/whisper.cc b/natives/whisper.cc index 84a747eb..2d547e3d 100644 --- a/natives/whisper.cc +++ b/natives/whisper.cc @@ -5,15 +5,13 @@ using namespace std; using namespace vips; -ArgumentMap Whisper(const string& type, string& outType, const char* bufferdata, size_t bufferLength, ArgumentMap arguments, bool* shouldKill) -{ +ArgumentMap Whisper(const string &type, string &outType, const char *bufferdata, size_t bufferLength, + ArgumentMap arguments, bool *shouldKill) { string caption = GetArgument(arguments, "caption"); string basePath = GetArgument(arguments, "basePath"); - VImage in = - VImage::new_from_buffer(bufferdata, bufferLength, "", - GetInputOptions(type, true, false)) - .colourspace(VIPS_INTERPRETATION_sRGB); + VImage in = VImage::new_from_buffer(bufferdata, bufferLength, "", GetInputOptions(type, true, false)) + .colourspace(VIPS_INTERPRETATION_sRGB); if (!in.has_alpha()) in = in.bandjoin(255); int width = in.width(); @@ -24,45 +22,35 @@ ArgumentMap Whisper(const string& type, string& outType, const char* bufferdata, string font_string = "Upright " + to_string(size); - VImage mask = - VImage::gaussmat(rad / 2, 0.1, VImage::option()->set("separable", true)) * - 8; + VImage mask = VImage::gaussmat(rad / 2, 0.1, VImage::option()->set("separable", true)) * 8; LoadFonts(basePath); - VImage 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/whisper.otf").c_str()) - ->set("width", width)); + VImage 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/whisper.otf").c_str()) + ->set("width", width)); - textIn = textIn.embed(rad, rad, textIn.width() + 2 * rad, - textIn.height() + 2 * rad); + textIn = textIn.embed(rad, rad, textIn.width() + 2 * rad, textIn.height() + 2 * rad); VImage newText = textIn.convsep(mask); VImage outline = newText.cast(VIPS_FORMAT_UCHAR) * zeroVecOneAlpha; VImage composited = outline.composite2(textIn, VIPS_BLEND_MODE_OVER); - VImage textImg = composited.embed( - (width / 2) - (composited.width() / 2), - (pageHeight / 2) - (composited.height() / 2), width, pageHeight); + VImage textImg = composited.embed((width / 2) - (composited.width() / 2), + (pageHeight / 2) - (composited.height() / 2), width, pageHeight); - VImage replicated = textImg - .copy(VImage::option()->set("interpretation", - VIPS_INTERPRETATION_sRGB)) - .replicate(1, nPages); + VImage replicated = + textImg.copy(VImage::option()->set("interpretation", VIPS_INTERPRETATION_sRGB)).replicate(1, nPages); VImage final = in.composite(replicated, VIPS_BLEND_MODE_OVER); SetupTimeoutCallback(final, shouldKill); char *buf; size_t dataSize = 0; - final.write_to_buffer( - ("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, - outType == "gif" - ? VImage::option()->set("dither", 0)->set("reoptimise", 1) - : 0); + final.write_to_buffer(("." + outType).c_str(), reinterpret_cast(&buf), &dataSize, + outType == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); ArgumentMap output; output["buf"] = buf;