Refactor and prepare for future CLI implementation

This commit is contained in:
Essem 2023-03-08 14:41:13 -06:00
parent de308ca795
commit cc7ea2762c
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
17 changed files with 152 additions and 115 deletions

View file

@ -2,24 +2,34 @@ cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW) cmake_policy(SET CMP0091 NEW)
cmake_policy(SET CMP0042 NEW) cmake_policy(SET CMP0042 NEW)
project(image) project(image)
include_directories(${CMAKE_JS_INC})
file(GLOB SOURCE_FILES "natives/*.cc" "natives/*.h") 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") if (CMAKE_JS_VERSION)
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}) include_directories(${CMAKE_JS_INC})
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17) add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC} natives/node/image.cc)
if(MSVC) # todo: change flags for more parity with GCC/clang, I don't know much about MSVC so pull requests are open set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
set(CMAKE_CXX_FLAGS "/Wall /EHsc /GS") target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})
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() 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") add_executable(${PROJECT_NAME} ${SOURCE_FILES} natives/cli/image.cc)
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
endif() 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) find_package(ImageMagick REQUIRED COMPONENTS Magick++ MagickCore)
add_definitions(-DMAGICKCORE_QUANTUM_DEPTH=16) add_definitions(-DMAGICKCORE_QUANTUM_DEPTH=16)
add_definitions(-DMAGICKCORE_HDRI_ENABLE=0) add_definitions(-DMAGICKCORE_HDRI_ENABLE=0)
@ -31,7 +41,7 @@ include_directories(${VIPS_INCLUDE_DIRS})
link_directories(${VIPS_LIBRARY_DIRS}) link_directories(${VIPS_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} ${VIPS_LDFLAGS}) 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 # Generate node.lib
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
endif() endif()

View file

@ -8,7 +8,7 @@ using namespace std;
using namespace vips; using namespace vips;
char *Bounce(string *type, char *BufferData, size_t BufferLength, 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(); VOption *options = VImage::option();
VImage in = VImage in =

View file

@ -12,7 +12,7 @@ using namespace std;
using namespace Magick; using namespace Magick;
char *Circle(string *type, char *BufferData, size_t BufferLength, char *Circle(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) { [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) {
Blob blob; Blob blob;
list<Image> frames; list<Image> frames;

26
natives/cli/image.cc Normal file
View file

@ -0,0 +1,26 @@
#include <cstring>
#include <iostream>
#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;
}

View file

@ -12,6 +12,46 @@ using std::variant;
typedef variant<string, float, bool, int> ArgumentVariant; typedef variant<string, float, bool, int> ArgumentVariant;
typedef map<string, ArgumentVariant> ArgumentMap; typedef map<string, ArgumentVariant> 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 <typename T> template <typename T>
T GetArgument(ArgumentMap map, string key) { T GetArgument(ArgumentMap map, string key) {
try { try {
@ -42,4 +82,49 @@ T GetArgumentWithFallback(ArgumentMap map, string key, T fallback) {
const std::unordered_map<std::string, std::string> fontPaths{ const std::unordered_map<std::string, std::string> fontPaths{
{"futura", "assets/fonts/caption.otf"}, {"futura", "assets/fonts/caption.otf"},
{"helvetica", "assets/fonts/caption2.ttf"}, {"helvetica", "assets/fonts/caption2.ttf"},
{"roboto", "assets/fonts/reddit.ttf"}}; {"roboto", "assets/fonts/reddit.ttf"}};
const std::map<std::string,
char* (*)(string* type, char* BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t* DataSize)>
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<std::string,
char* (*)(string* type, ArgumentMap Arguments, size_t* DataSize)>
NoInputFunctionMap = {{"homebrew", Homebrew}, {"sonic", Sonic}};

View file

@ -8,7 +8,7 @@ using namespace std;
using namespace vips; using namespace vips;
char *Crop(string *type, char *BufferData, size_t BufferLength, char *Crop(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) { [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) {
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =

View file

@ -7,7 +7,7 @@ using namespace std;
using namespace vips; using namespace vips;
char *Deepfry(string *type, char *BufferData, size_t BufferLength, char *Deepfry(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) { [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) {
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =

View file

@ -6,7 +6,7 @@ using namespace std;
using namespace vips; using namespace vips;
char *Invert(string *type, char *BufferData, size_t BufferLength, char *Invert(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) { [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) {
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =

View file

@ -10,7 +10,7 @@ using namespace std;
using namespace Magick; using namespace Magick;
char *Magik(string *type, char *BufferData, size_t BufferLength, char *Magik(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) { [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) {
Blob blob; Blob blob;
list<Image> frames; list<Image> frames;

View file

@ -4,46 +4,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include "blur.h" #include "../common.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"
#ifdef _WIN32 #ifdef _WIN32
#include <Magick++.h> #include <Magick++.h>
@ -52,51 +13,6 @@
using namespace std; using namespace std;
std::map<std::string,
char* (*)(string* type, char* BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t* DataSize)>
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<std::string,
char* (*)(string* type, ArgumentMap Arguments, size_t* DataSize)>
NoInputFunctionMap = {{"homebrew", Homebrew}, {"sonic", Sonic}};
bool isNapiValueInt(Napi::Env& env, Napi::Value& num) { bool isNapiValueInt(Napi::Env& env, Napi::Value& num) {
return env.Global() return env.Global()
.Get("Number") .Get("Number")
@ -165,7 +81,7 @@ Napi::Value ProcessImage(const Napi::CallbackInfo& info) {
result.Set("data", Napi::Buffer<char>::New( result.Set("data", Napi::Buffer<char>::New(
env, buf, length, env, buf, length,
[](Napi::Env env, void* data) { free(data); })); []([[maybe_unused]] Napi::Env env, void* data) { free(data); }));
result.Set("type", type); result.Set("type", type);
} catch (std::exception const& err) { } catch (std::exception const& err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();

View file

@ -39,7 +39,7 @@ char *vipsRemove(char *data, size_t length, size_t *DataSize, int speed) {
return (char *)buf; 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) { ArgumentMap Arguments, size_t *DataSize) {
bool slow = GetArgumentWithFallback<bool>(Arguments, "slow", false); bool slow = GetArgumentWithFallback<bool>(Arguments, "slow", false);
int speed = GetArgumentWithFallback<int>(Arguments, "speed", 2); int speed = GetArgumentWithFallback<int>(Arguments, "speed", 2);

View file

@ -9,7 +9,7 @@ using namespace std;
using namespace Magick; using namespace Magick;
char *Spin(string *type, char *BufferData, size_t BufferLength, char *Spin(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) { [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) {
int delay = GetArgumentWithFallback<int>(Arguments, "delay", 0); int delay = GetArgumentWithFallback<int>(Arguments, "delay", 0);
Blob blob; Blob blob;

View file

@ -8,7 +8,7 @@ using namespace std;
using namespace vips; using namespace vips;
char *Squish(string *type, char *BufferData, size_t BufferLength, 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(); VOption *options = VImage::option();
VImage in = VImage in =

View file

@ -6,7 +6,7 @@ using namespace std;
using namespace vips; using namespace vips;
char *Swirl(string *type, char *BufferData, size_t BufferLength, 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"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =

View file

@ -10,7 +10,7 @@ using namespace std;
using namespace Magick; using namespace Magick;
char *Tile(string *type, char *BufferData, size_t BufferLength, char *Tile(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) { [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) {
Blob blob; Blob blob;
list<Image> frames; list<Image> frames;

View file

@ -6,7 +6,7 @@ using namespace std;
using namespace vips; using namespace vips;
char *ToGif(string *type, char *BufferData, size_t BufferLength, char *ToGif(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) { [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) {
if (*type == "gif") { if (*type == "gif") {
*DataSize = BufferLength; *DataSize = BufferLength;
char *data = (char *)malloc(BufferLength); char *data = (char *)malloc(BufferLength);

View file

@ -10,7 +10,7 @@ using namespace std;
using namespace Magick; using namespace Magick;
char *Wall(string *type, char *BufferData, size_t BufferLength, char *Wall(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) { [[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) {
Blob blob; Blob blob;
list<Image> frames; list<Image> frames;