use new typing and functions for more "safety" (#332)
This commit is contained in:
parent
9ddc32193f
commit
1037e8a9a0
26 changed files with 79 additions and 50 deletions
|
@ -8,8 +8,8 @@ using namespace std;
|
||||||
using namespace vips;
|
using namespace vips;
|
||||||
|
|
||||||
char *Blur(string type, char *BufferData, size_t BufferLength,
|
char *Blur(string type, char *BufferData, size_t BufferLength,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
bool sharp = MAP_GET(Arguments, "sharp", bool);
|
bool sharp = GetArgument<bool>(Arguments, "sharp");
|
||||||
VOption *options = VImage::option()->set("access", "sequential");
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
VImage in =
|
VImage in =
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Blur(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Blur(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -8,11 +8,11 @@ using namespace std;
|
||||||
using namespace vips;
|
using namespace vips;
|
||||||
|
|
||||||
char *Caption(string type, char *BufferData, size_t BufferLength,
|
char *Caption(string type, char *BufferData, size_t BufferLength,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
string caption = MAP_GET(Arguments, "caption", string);
|
string caption = GetArgument<string>(Arguments, "caption");
|
||||||
string font = MAP_GET(Arguments, "font", string);
|
string font = GetArgument<string>(Arguments, "font");
|
||||||
string basePath = MAP_GET(Arguments, "basePath", string);
|
string basePath = GetArgument<string>(Arguments, "basePath");
|
||||||
|
|
||||||
VOption *options = VImage::option()->set("access", "sequential");
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Caption(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Caption(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -8,12 +8,12 @@ using namespace std;
|
||||||
using namespace vips;
|
using namespace vips;
|
||||||
|
|
||||||
char *CaptionTwo(string type, char *BufferData, size_t BufferLength,
|
char *CaptionTwo(string type, char *BufferData, size_t BufferLength,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
bool top = MAP_GET(Arguments, "top", bool);
|
bool top = GetArgument<bool>(Arguments, "top");
|
||||||
string caption = MAP_GET(Arguments, "caption", string);
|
string caption = GetArgument<string>(Arguments, "caption");
|
||||||
string font = MAP_GET(Arguments, "font", string);
|
string font = GetArgument<string>(Arguments, "font");
|
||||||
string basePath = MAP_GET(Arguments, "basePath", string);
|
string basePath = GetArgument<string>(Arguments, "basePath");
|
||||||
|
|
||||||
VOption *options = VImage::option()->set("access", "sequential");
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* CaptionTwo(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* CaptionTwo(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -11,7 +11,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,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
Blob blob;
|
Blob blob;
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Circle(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Circle(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -11,9 +11,9 @@ 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);
|
0.1140, 0.2392, 0.4696, 0.0912);
|
||||||
|
|
||||||
char *Colors(string type, char *BufferData, size_t BufferLength,
|
char *Colors(string type, char *BufferData, size_t BufferLength,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
string color = MAP_GET(Arguments, "color", string);
|
string color = GetArgument<string>(Arguments, "color");
|
||||||
|
|
||||||
VOption *options = VImage::option()->set("access", "sequential");
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Colors(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Colors(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -2,8 +2,34 @@
|
||||||
|
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <map>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
using std::variant;
|
||||||
|
using std::map;
|
||||||
|
|
||||||
|
typedef variant<string, float, bool, int> ArgumentVariant;
|
||||||
|
typedef map<string, ArgumentVariant> ArgumentMap;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T GetArgument(ArgumentMap map, string key) {
|
||||||
|
try {
|
||||||
|
return std::get<T>(map.at(key));
|
||||||
|
} catch (std::bad_variant_access&) {
|
||||||
|
throw "Invalid requested type from variant.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T GetArgumentWithFallback(ArgumentMap map, string key, T fallback) {
|
||||||
|
try {
|
||||||
|
return std::get<T>(map.at(key));
|
||||||
|
} catch (...) { // this is, not great...
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#define MAP_HAS(ARRAY, KEY) (ARRAY.count(KEY) > 0)
|
#define MAP_HAS(ARRAY, KEY) (ARRAY.count(KEY) > 0)
|
||||||
#define MAP_GET(ARRAY, KEY, TYPE) (MAP_HAS(ARRAY, KEY) ? get<TYPE>(ARRAY.at(KEY)) : NULL) // C++ has forced my hand
|
#define MAP_GET(ARRAY, KEY, TYPE) (MAP_HAS(ARRAY, KEY) ? get<TYPE>(ARRAY.at(KEY)) : NULL) // C++ has forced my hand
|
||||||
#define MAP_GET_FALLBACK(ARRAY, KEY, TYPE, FALLBACK) (MAP_HAS(ARRAY, KEY) ? get<TYPE>(ARRAY.at(KEY)) : FALLBACK)
|
#define MAP_GET_FALLBACK(ARRAY, KEY, TYPE, FALLBACK) (MAP_HAS(ARRAY, KEY) ? get<TYPE>(ARRAY.at(KEY)) : FALLBACK)
|
||||||
|
|
|
@ -7,7 +7,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,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
VOption *options = VImage::option()->set("access", "sequential");
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Crop(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Crop(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -6,7 +6,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,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
VOption *options = VImage::option()->set("access", "sequential");
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Deepfry(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Deepfry(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -12,10 +12,10 @@ using namespace std;
|
||||||
using namespace Magick;
|
using namespace Magick;
|
||||||
|
|
||||||
char *Explode(string type, char *BufferData, size_t BufferLength,
|
char *Explode(string type, char *BufferData, size_t BufferLength,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
int amount = MAP_GET(Arguments, "amount", int);
|
int amount = GetArgument<int>(Arguments, "amount");
|
||||||
int delay = MAP_GET_FALLBACK(Arguments, "delay", int, 0);
|
int delay = GetArgumentWithFallback<int>(Arguments, "delay", 0);
|
||||||
|
|
||||||
Blob blob;
|
Blob blob;
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Explode(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Explode(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -8,10 +8,10 @@ using namespace std;
|
||||||
using namespace vips;
|
using namespace vips;
|
||||||
|
|
||||||
char *Flag(string type, char *BufferData, size_t BufferLength,
|
char *Flag(string type, char *BufferData, size_t BufferLength,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
string overlay = MAP_GET(Arguments, "overlay", string);
|
string overlay = GetArgument<string>(Arguments, "overlay");
|
||||||
string basePath = MAP_GET(Arguments, "basePath", string);
|
string basePath = GetArgument<string>(Arguments, "basePath");
|
||||||
|
|
||||||
VOption *options = VImage::option()->set("access", "sequential");
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Flag(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Flag(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -8,9 +8,9 @@ using namespace std;
|
||||||
using namespace vips;
|
using namespace vips;
|
||||||
|
|
||||||
char *Flip(string type, char *BufferData, size_t BufferLength,
|
char *Flip(string type, char *BufferData, size_t BufferLength,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
bool flop = MAP_GET(Arguments, "flop", bool);
|
bool flop = GetArgument<bool>(Arguments, "flop");
|
||||||
|
|
||||||
VImage in = VImage::new_from_buffer(BufferData, BufferLength, "",
|
VImage in = VImage::new_from_buffer(BufferData, BufferLength, "",
|
||||||
type == "gif"
|
type == "gif"
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Flip(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Flip(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -51,7 +51,7 @@
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
std::map<std::string, char* (*)(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize)> FunctionMap = {
|
std::map<std::string, char* (*)(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize)> FunctionMap = {
|
||||||
{"blur", &Blur},
|
{"blur", &Blur},
|
||||||
{"caption", &Caption},
|
{"caption", &Caption},
|
||||||
{"captionTwo", &CaptionTwo},
|
{"captionTwo", &CaptionTwo},
|
||||||
|
@ -117,7 +117,7 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info) {
|
||||||
|
|
||||||
Napi::Array properties = obj.GetPropertyNames();
|
Napi::Array properties = obj.GetPropertyNames();
|
||||||
|
|
||||||
std::map<string, ARG_TYPES> Arguments;
|
ArgumentMap Arguments;
|
||||||
|
|
||||||
for (unsigned int i = 0; i < properties.Length(); i++) {
|
for (unsigned int i = 0; i < properties.Length(); i++) {
|
||||||
string property = properties.Get(uint32_t(i)).As<Napi::String>().Utf8Value();
|
string property = properties.Get(uint32_t(i)).As<Napi::String>().Utf8Value();
|
||||||
|
@ -138,6 +138,9 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info) {
|
||||||
} else {
|
} else {
|
||||||
Arguments[property] = num.FloatValue();
|
Arguments[property] = num.FloatValue();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw "Unimplemented value type passed to image native.";
|
||||||
|
//Arguments[property] = val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,9 @@ using namespace std;
|
||||||
using namespace vips;
|
using namespace vips;
|
||||||
|
|
||||||
char *Uncaption(string type, char *BufferData, size_t BufferLength,
|
char *Uncaption(string type, char *BufferData, size_t BufferLength,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
float tolerance = MAP_GET_FALLBACK(Arguments, "tolerance", float, 0.5);
|
float tolerance = GetArgumentWithFallback<float>(Arguments, "tolerance", 0.5);
|
||||||
|
|
||||||
VOption *options = VImage::option();
|
VOption *options = VImage::option();
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Uncaption(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Uncaption(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
|
@ -7,23 +7,23 @@ using namespace std;
|
||||||
using namespace vips;
|
using namespace vips;
|
||||||
|
|
||||||
char *Watermark(string type, char *BufferData, size_t BufferLength,
|
char *Watermark(string type, char *BufferData, size_t BufferLength,
|
||||||
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
|
|
||||||
string water = MAP_GET(Arguments, "water", string);
|
string water = GetArgument<string>(Arguments, "water");
|
||||||
int gravity = MAP_GET(Arguments, "gravity", int);
|
int gravity = GetArgument<int>(Arguments, "gravity");
|
||||||
|
|
||||||
bool resize = MAP_GET_FALLBACK(Arguments, "resize", bool, false);
|
bool resize = GetArgumentWithFallback<bool>(Arguments, "resize", false);
|
||||||
;
|
|
||||||
float yscale = MAP_GET_FALLBACK(Arguments, "yscale", float, false);
|
|
||||||
|
|
||||||
bool append = MAP_GET_FALLBACK(Arguments, "append", bool, false);
|
float yscale = GetArgumentWithFallback<float>(Arguments, "yscale", false);
|
||||||
|
|
||||||
bool alpha = MAP_GET_FALLBACK(Arguments, "alpha", bool, false);
|
bool append = GetArgumentWithFallback<bool>(Arguments, "append", false);
|
||||||
bool flip = MAP_GET_FALLBACK(Arguments, "flip", bool, false);
|
|
||||||
|
bool alpha = GetArgumentWithFallback<bool>(Arguments, "alpha", false);
|
||||||
|
bool flip = GetArgumentWithFallback<bool>(Arguments, "flip", false);
|
||||||
|
|
||||||
bool mc = MAP_HAS(Arguments, "mc");
|
bool mc = MAP_HAS(Arguments, "mc");
|
||||||
|
|
||||||
string basePath = MAP_GET(Arguments, "basePath", string);
|
string basePath = GetArgument<string>(Arguments, "basePath");
|
||||||
|
|
||||||
VOption *options = VImage::option()->set("access", "sequential");
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
char* Watermark(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
|
char* Watermark(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
Loading…
Add table
Add a link
Reference in a new issue