From cc7ea2762c06a6eac5cf3a205bb6be133c3ae500 Mon Sep 17 00:00:00 2001 From: Essem Date: Wed, 8 Mar 2023 14:41:13 -0600 Subject: [PATCH] Refactor and prepare for future CLI implementation --- CMakeLists.txt | 40 ++++++++++------- natives/bounce.cc | 2 +- natives/circle.cc | 2 +- natives/cli/image.cc | 26 +++++++++++ natives/common.h | 87 +++++++++++++++++++++++++++++++++++- natives/crop.cc | 2 +- natives/deepfry.cc | 2 +- natives/invert.cc | 2 +- natives/magik.cc | 2 +- natives/{ => node}/image.cc | 88 +------------------------------------ natives/speed.cc | 2 +- natives/spin.cc | 2 +- natives/squish.cc | 2 +- natives/swirl.cc | 2 +- natives/tile.cc | 2 +- natives/togif.cc | 2 +- natives/wall.cc | 2 +- 17 files changed, 152 insertions(+), 115 deletions(-) create mode 100644 natives/cli/image.cc rename natives/{ => node}/image.cc (55%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8617eba..eb8fc66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,24 +2,34 @@ cmake_minimum_required(VERSION 3.15) cmake_policy(SET CMP0091 NEW) cmake_policy(SET CMP0042 NEW) project(image) -include_directories(${CMAKE_JS_INC}) + file(GLOB SOURCE_FILES "natives/*.cc" "natives/*.h") -add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC}) -set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") -target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}) -target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17) -if(MSVC) # todo: change flags for more parity with GCC/clang, I don't know much about MSVC so pull requests are open -set(CMAKE_CXX_FLAGS "/Wall /EHsc /GS") -set(CMAKE_CXX_FLAGS_DEBUG "/Zi") -set(CMAKE_CXX_FLAGS_RELEASE "/Ox") -set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -set(BUILD_SHARED_LIBS TRUE) + +if (CMAKE_JS_VERSION) + include_directories(${CMAKE_JS_INC}) + add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC} natives/node/image.cc) + set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") + target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}) else() -set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror=format-security -Wno-cast-function-type -fexceptions -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -pedantic -D_GLIBCXX_USE_CXX11_ABI=1") -set(CMAKE_CXX_FLAGS_DEBUG "-g") -set(CMAKE_CXX_FLAGS_RELEASE "-O3") + add_executable(${PROJECT_NAME} ${SOURCE_FILES} natives/cli/image.cc) endif() +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17) + +if(MSVC) # todo: change flags for more parity with GCC/clang, I don't know much about MSVC so pull requests are open + set(CMAKE_CXX_FLAGS "/Wall /EHsc /GS") + set(CMAKE_CXX_FLAGS_DEBUG "/Zi") + set(CMAKE_CXX_FLAGS_RELEASE "/Ox") + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + set(BUILD_SHARED_LIBS TRUE) +else() + set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror=format-security -Wno-cast-function-type -fexceptions -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -pedantic -D_GLIBCXX_USE_CXX11_ABI=1") + set(CMAKE_CXX_FLAGS_DEBUG "-g") + set(CMAKE_CXX_FLAGS_RELEASE "-O2") +endif() + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + find_package(ImageMagick REQUIRED COMPONENTS Magick++ MagickCore) add_definitions(-DMAGICKCORE_QUANTUM_DEPTH=16) add_definitions(-DMAGICKCORE_HDRI_ENABLE=0) @@ -31,7 +41,7 @@ include_directories(${VIPS_INCLUDE_DIRS}) link_directories(${VIPS_LIBRARY_DIRS}) target_link_libraries(${PROJECT_NAME} ${VIPS_LDFLAGS}) -if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET) +if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET AND CMAKE_JS_VERSION) # Generate node.lib execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) endif() diff --git a/natives/bounce.cc b/natives/bounce.cc index 7faad55..9f58f72 100644 --- a/natives/bounce.cc +++ b/natives/bounce.cc @@ -8,7 +8,7 @@ using namespace std; using namespace vips; char *Bounce(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option(); VImage in = diff --git a/natives/circle.cc b/natives/circle.cc index 3834be3..0d3e38f 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -12,7 +12,7 @@ using namespace std; using namespace Magick; char *Circle(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { Blob blob; list frames; diff --git a/natives/cli/image.cc b/natives/cli/image.cc new file mode 100644 index 0000000..344d0bb --- /dev/null +++ b/natives/cli/image.cc @@ -0,0 +1,26 @@ +#include +#include + +#include "../common.h" + +void showUsage(char *path) { + std::cout << "Usage: " << path << " operation [--arg=\"param\"] [...]" << std::endl; +} + +int main(int argc, char *argv[]) { + if (argc < 1 || + (argc == 1 && !strcmp(argv[1], "-h"))) { + showUsage(argv[0]); +#ifdef _WIN32 + system("PAUSE"); +#endif + return 1; + } + + char *op = argv[1]; + + //handleArguments(argc, argv); + + std::cout << "This does nothing yet, but it might in the future!" << std::endl; + return 0; +} \ No newline at end of file diff --git a/natives/common.h b/natives/common.h index 2099433..1361be7 100644 --- a/natives/common.h +++ b/natives/common.h @@ -12,6 +12,46 @@ using std::variant; typedef variant ArgumentVariant; typedef map ArgumentMap; +#include "blur.h" +#include "bounce.h" +#include "caption.h" +#include "caption2.h" +#include "circle.h" +#include "colors.h" +#include "crop.h" +#include "deepfry.h" +#include "explode.h" +#include "flag.h" +#include "flip.h" +#include "freeze.h" +#include "gamexplain.h" +#include "globe.h" +#include "homebrew.h" +#include "invert.h" +#include "jpeg.h" +#include "magik.h" +#include "meme.h" +#include "mirror.h" +#include "motivate.h" +#include "reddit.h" +#include "resize.h" +#include "reverse.h" +#include "scott.h" +#include "snapchat.h" +#include "sonic.h" +#include "speed.h" +#include "spin.h" +#include "squish.h" +#include "swirl.h" +#include "tile.h" +#include "togif.h" +#include "uncanny.h" +#include "uncaption.h" +#include "wall.h" +#include "watermark.h" +#include "whisper.h" +#include "zamn.h" + template T GetArgument(ArgumentMap map, string key) { try { @@ -42,4 +82,49 @@ T GetArgumentWithFallback(ArgumentMap map, string key, T fallback) { const std::unordered_map fontPaths{ {"futura", "assets/fonts/caption.otf"}, {"helvetica", "assets/fonts/caption2.ttf"}, - {"roboto", "assets/fonts/reddit.ttf"}}; \ No newline at end of file + {"roboto", "assets/fonts/reddit.ttf"}}; + +const std::map + FunctionMap = {{"blur", &Blur}, + {"bounce", &Bounce}, + {"caption", &Caption}, + {"captionTwo", &CaptionTwo}, + {"circle", &Circle}, + {"colors", &Colors}, + {"crop", &Crop}, + {"deepfry", &Deepfry}, + {"explode", &Explode}, + {"flag", &Flag}, + {"flip", &Flip}, + {"freeze", &Freeze}, + {"gamexplain", Gamexplain}, + {"globe", Globe}, + {"invert", Invert}, + {"jpeg", Jpeg}, + {"magik", Magik}, + {"meme", Meme}, + {"mirror", Mirror}, + {"motivate", Motivate}, + {"reddit", Reddit}, + {"resize", Resize}, + {"reverse", Reverse}, + {"scott", Scott}, + {"snapchat", Snapchat}, + {"speed", &Speed}, + {"spin", Spin}, + {"squish", Squish}, + {"swirl", Swirl}, + {"tile", Tile}, + {"togif", ToGif}, + {"uncanny", Uncanny}, + {"uncaption", &Uncaption}, + {"wall", Wall}, + {"watermark", &Watermark}, + {"whisper", Whisper}, + {"zamn", Zamn}}; + +const std::map + NoInputFunctionMap = {{"homebrew", Homebrew}, {"sonic", Sonic}}; \ No newline at end of file diff --git a/natives/crop.cc b/natives/crop.cc index 07cbad5..d72fa85 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -8,7 +8,7 @@ using namespace std; using namespace vips; char *Crop(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option()->set("access", "sequential"); VImage in = diff --git a/natives/deepfry.cc b/natives/deepfry.cc index dce5b97..aa08d2c 100644 --- a/natives/deepfry.cc +++ b/natives/deepfry.cc @@ -7,7 +7,7 @@ using namespace std; using namespace vips; char *Deepfry(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option()->set("access", "sequential"); VImage in = diff --git a/natives/invert.cc b/natives/invert.cc index 376c066..ae3782b 100644 --- a/natives/invert.cc +++ b/natives/invert.cc @@ -6,7 +6,7 @@ using namespace std; using namespace vips; char *Invert(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option()->set("access", "sequential"); VImage in = diff --git a/natives/magik.cc b/natives/magik.cc index 1cbb1b8..94e6e1c 100644 --- a/natives/magik.cc +++ b/natives/magik.cc @@ -10,7 +10,7 @@ using namespace std; using namespace Magick; char *Magik(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { Blob blob; list frames; diff --git a/natives/image.cc b/natives/node/image.cc similarity index 55% rename from natives/image.cc rename to natives/node/image.cc index b85efb6..3f171c6 100644 --- a/natives/image.cc +++ b/natives/node/image.cc @@ -4,46 +4,7 @@ #include #include -#include "blur.h" -#include "bounce.h" -#include "caption.h" -#include "caption2.h" -#include "circle.h" -#include "colors.h" -#include "common.h" -#include "crop.h" -#include "deepfry.h" -#include "explode.h" -#include "flag.h" -#include "flip.h" -#include "freeze.h" -#include "gamexplain.h" -#include "globe.h" -#include "homebrew.h" -#include "invert.h" -#include "jpeg.h" -#include "magik.h" -#include "meme.h" -#include "mirror.h" -#include "motivate.h" -#include "reddit.h" -#include "resize.h" -#include "reverse.h" -#include "scott.h" -#include "snapchat.h" -#include "sonic.h" -#include "speed.h" -#include "spin.h" -#include "squish.h" -#include "swirl.h" -#include "tile.h" -#include "togif.h" -#include "uncanny.h" -#include "uncaption.h" -#include "wall.h" -#include "watermark.h" -#include "whisper.h" -#include "zamn.h" +#include "../common.h" #ifdef _WIN32 #include @@ -52,51 +13,6 @@ using namespace std; -std::map - FunctionMap = {{"blur", &Blur}, - {"bounce", &Bounce}, - {"caption", &Caption}, - {"captionTwo", &CaptionTwo}, - {"circle", &Circle}, - {"colors", &Colors}, - {"crop", &Crop}, - {"deepfry", &Deepfry}, - {"explode", &Explode}, - {"flag", &Flag}, - {"flip", &Flip}, - {"freeze", &Freeze}, - {"gamexplain", Gamexplain}, - {"globe", Globe}, - {"invert", Invert}, - {"jpeg", Jpeg}, - {"magik", Magik}, - {"meme", Meme}, - {"mirror", Mirror}, - {"motivate", Motivate}, - {"reddit", Reddit}, - {"resize", Resize}, - {"reverse", Reverse}, - {"scott", Scott}, - {"snapchat", Snapchat}, - {"speed", &Speed}, - {"spin", Spin}, - {"squish", Squish}, - {"swirl", Swirl}, - {"tile", Tile}, - {"togif", ToGif}, - {"uncanny", Uncanny}, - {"uncaption", &Uncaption}, - {"wall", Wall}, - {"watermark", &Watermark}, - {"whisper", Whisper}, - {"zamn", Zamn}}; - -std::map - NoInputFunctionMap = {{"homebrew", Homebrew}, {"sonic", Sonic}}; - bool isNapiValueInt(Napi::Env& env, Napi::Value& num) { return env.Global() .Get("Number") @@ -165,7 +81,7 @@ Napi::Value ProcessImage(const Napi::CallbackInfo& info) { result.Set("data", Napi::Buffer::New( env, buf, length, - [](Napi::Env env, void* data) { free(data); })); + []([[maybe_unused]] Napi::Env env, void* data) { free(data); })); result.Set("type", type); } catch (std::exception const& err) { Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); diff --git a/natives/speed.cc b/natives/speed.cc index 080904f..2b0fd14 100644 --- a/natives/speed.cc +++ b/natives/speed.cc @@ -39,7 +39,7 @@ char *vipsRemove(char *data, size_t length, size_t *DataSize, int speed) { return (char *)buf; } -char *Speed(string *type, char *BufferData, size_t BufferLength, +char *Speed([[maybe_unused]] string *type, char *BufferData, size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) { bool slow = GetArgumentWithFallback(Arguments, "slow", false); int speed = GetArgumentWithFallback(Arguments, "speed", 2); diff --git a/natives/spin.cc b/natives/spin.cc index 56c14d9..238ebef 100644 --- a/natives/spin.cc +++ b/natives/spin.cc @@ -9,7 +9,7 @@ using namespace std; using namespace Magick; char *Spin(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { int delay = GetArgumentWithFallback(Arguments, "delay", 0); Blob blob; diff --git a/natives/squish.cc b/natives/squish.cc index 868c163..b7b8a3f 100644 --- a/natives/squish.cc +++ b/natives/squish.cc @@ -8,7 +8,7 @@ using namespace std; using namespace vips; char *Squish(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option(); VImage in = diff --git a/natives/swirl.cc b/natives/swirl.cc index 5a068ff..a61c9f5 100644 --- a/natives/swirl.cc +++ b/natives/swirl.cc @@ -6,7 +6,7 @@ using namespace std; using namespace vips; char *Swirl(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option()->set("access", "sequential"); VImage in = diff --git a/natives/tile.cc b/natives/tile.cc index ee9ab28..973b21f 100644 --- a/natives/tile.cc +++ b/natives/tile.cc @@ -10,7 +10,7 @@ using namespace std; using namespace Magick; char *Tile(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { Blob blob; list frames; diff --git a/natives/togif.cc b/natives/togif.cc index 16ff000..5426654 100644 --- a/natives/togif.cc +++ b/natives/togif.cc @@ -6,7 +6,7 @@ using namespace std; using namespace vips; char *ToGif(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { if (*type == "gif") { *DataSize = BufferLength; char *data = (char *)malloc(BufferLength); diff --git a/natives/wall.cc b/natives/wall.cc index 88e007e..b97e756 100644 --- a/natives/wall.cc +++ b/natives/wall.cc @@ -10,7 +10,7 @@ using namespace std; using namespace Magick; char *Wall(string *type, char *BufferData, size_t BufferLength, - ArgumentMap Arguments, size_t *DataSize) { + [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) { Blob blob; list frames;