Refactor routing in App component to enhance navigation and improve error handling by integrating dynamic routes and updating the NotFound route.
This commit is contained in:
280
node_modules/sharp/src/binding.gyp
generated
vendored
Normal file
280
node_modules/sharp/src/binding.gyp
generated
vendored
Normal file
@@ -0,0 +1,280 @@
|
||||
# Copyright 2013 Lovell Fuller and others.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
{
|
||||
'variables': {
|
||||
'vips_version': '<!(node -p "require(\'../lib/libvips\').minimumLibvipsVersion")',
|
||||
'platform_and_arch': '<!(node -p "require(\'../lib/libvips\').buildPlatformArch()")',
|
||||
'sharp_libvips_version': '<!(node -p "require(\'../package.json\').optionalDependencies[\'@img/sharp-libvips-<(platform_and_arch)\']")',
|
||||
'sharp_libvips_yarn_locator': '<!(node -p "require(\'../lib/libvips\').yarnLocator()")',
|
||||
'sharp_libvips_include_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsIncludeDir()")',
|
||||
'sharp_libvips_cplusplus_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsCPlusPlusDir()")',
|
||||
'sharp_libvips_lib_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsLibDir()")'
|
||||
},
|
||||
'targets': [{
|
||||
'target_name': 'libvips-cpp',
|
||||
'conditions': [
|
||||
['OS == "win"', {
|
||||
# Build libvips C++ binding for Windows due to MSVC std library ABI changes
|
||||
'type': 'shared_library',
|
||||
'defines': [
|
||||
'VIPS_CPLUSPLUS_EXPORTS',
|
||||
'_ALLOW_KEYWORD_MACROS'
|
||||
],
|
||||
'sources': [
|
||||
'<(sharp_libvips_cplusplus_dir)/VConnection.cpp',
|
||||
'<(sharp_libvips_cplusplus_dir)/VError.cpp',
|
||||
'<(sharp_libvips_cplusplus_dir)/VImage.cpp',
|
||||
'<(sharp_libvips_cplusplus_dir)/VInterpolate.cpp',
|
||||
'<(sharp_libvips_cplusplus_dir)/VRegion.cpp'
|
||||
],
|
||||
'include_dirs': [
|
||||
'<(sharp_libvips_include_dir)',
|
||||
'<(sharp_libvips_include_dir)/glib-2.0',
|
||||
'<(sharp_libvips_lib_dir)/glib-2.0/include'
|
||||
],
|
||||
'link_settings': {
|
||||
'library_dirs': [
|
||||
'<(sharp_libvips_lib_dir)'
|
||||
],
|
||||
'libraries': [
|
||||
'libvips.lib'
|
||||
],
|
||||
},
|
||||
'configurations': {
|
||||
'Release': {
|
||||
'msvs_settings': {
|
||||
'VCCLCompilerTool': {
|
||||
'ExceptionHandling': 1,
|
||||
'Optimization': 1,
|
||||
'WholeProgramOptimization': 'true'
|
||||
},
|
||||
'VCLibrarianTool': {
|
||||
'AdditionalOptions': [
|
||||
'/LTCG:INCREMENTAL'
|
||||
]
|
||||
},
|
||||
'VCLinkerTool': {
|
||||
'ImageHasSafeExceptionHandlers': 'false',
|
||||
'OptimizeReferences': 2,
|
||||
'EnableCOMDATFolding': 2,
|
||||
'LinkIncremental': 1,
|
||||
'AdditionalOptions': [
|
||||
'/LTCG:INCREMENTAL'
|
||||
]
|
||||
}
|
||||
},
|
||||
'msvs_disabled_warnings': [
|
||||
4275
|
||||
]
|
||||
}
|
||||
}
|
||||
}, {
|
||||
# Ignore this target for non-Windows
|
||||
'type': 'none'
|
||||
}]
|
||||
]
|
||||
}, {
|
||||
'target_name': 'sharp-<(platform_and_arch)',
|
||||
'defines': [
|
||||
'NAPI_VERSION=9',
|
||||
'NODE_ADDON_API_DISABLE_DEPRECATED',
|
||||
'NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS'
|
||||
],
|
||||
'dependencies': [
|
||||
'<!(node -p "require(\'node-addon-api\').gyp")',
|
||||
'libvips-cpp'
|
||||
],
|
||||
'variables': {
|
||||
'conditions': [
|
||||
['OS != "win"', {
|
||||
'pkg_config_path': '<!(node -p "require(\'../lib/libvips\').pkgConfigPath()")',
|
||||
'use_global_libvips': '<!(node -p "Boolean(require(\'../lib/libvips\').useGlobalLibvips()).toString()")'
|
||||
}, {
|
||||
'pkg_config_path': '',
|
||||
'use_global_libvips': ''
|
||||
}]
|
||||
]
|
||||
},
|
||||
'sources': [
|
||||
'common.cc',
|
||||
'metadata.cc',
|
||||
'stats.cc',
|
||||
'operations.cc',
|
||||
'pipeline.cc',
|
||||
'utilities.cc',
|
||||
'sharp.cc'
|
||||
],
|
||||
'include_dirs': [
|
||||
'<!(node -p "require(\'node-addon-api\').include_dir")',
|
||||
],
|
||||
'conditions': [
|
||||
['use_global_libvips == "true"', {
|
||||
# Use pkg-config for include and lib
|
||||
'include_dirs': ['<!@(PKG_CONFIG_PATH="<(pkg_config_path)" pkg-config --cflags-only-I vips-cpp vips glib-2.0 | sed s\/-I//g)'],
|
||||
'libraries': ['<!@(PKG_CONFIG_PATH="<(pkg_config_path)" pkg-config --libs vips-cpp)'],
|
||||
'defines': [
|
||||
'SHARP_USE_GLOBAL_LIBVIPS'
|
||||
],
|
||||
'conditions': [
|
||||
['OS == "linux"', {
|
||||
'defines': [
|
||||
# Inspect libvips-cpp.so to determine which C++11 ABI version was used and set _GLIBCXX_USE_CXX11_ABI accordingly. This is quite horrible.
|
||||
'_GLIBCXX_USE_CXX11_ABI=<!(if readelf -Ws "$(PKG_CONFIG_PATH="<(pkg_config_path)" pkg-config --variable libdir vips-cpp)/libvips-cpp.so" | c++filt | grep -qF __cxx11;then echo "1";else echo "0";fi)'
|
||||
]
|
||||
}]
|
||||
]
|
||||
}, {
|
||||
# Use pre-built libvips stored locally within node_modules
|
||||
'include_dirs': [
|
||||
'<(sharp_libvips_include_dir)',
|
||||
'<(sharp_libvips_include_dir)/glib-2.0',
|
||||
'<(sharp_libvips_lib_dir)/glib-2.0/include'
|
||||
],
|
||||
'library_dirs': [
|
||||
'<(sharp_libvips_lib_dir)'
|
||||
],
|
||||
'conditions': [
|
||||
['OS == "win"', {
|
||||
'defines': [
|
||||
'_ALLOW_KEYWORD_MACROS',
|
||||
'_FILE_OFFSET_BITS=64'
|
||||
],
|
||||
'link_settings': {
|
||||
'libraries': [
|
||||
'libvips.lib'
|
||||
]
|
||||
}
|
||||
}],
|
||||
['OS == "mac"', {
|
||||
'link_settings': {
|
||||
'libraries': [
|
||||
'libvips-cpp.42.dylib'
|
||||
]
|
||||
},
|
||||
'xcode_settings': {
|
||||
'OTHER_LDFLAGS': [
|
||||
# Ensure runtime linking is relative to sharp.node
|
||||
'-Wl,-rpath,\'@loader_path/../../sharp-libvips-<(platform_and_arch)/lib\'',
|
||||
'-Wl,-rpath,\'@loader_path/../../../sharp-libvips-<(platform_and_arch)/<(sharp_libvips_version)/lib\'',
|
||||
'-Wl,-rpath,\'@loader_path/../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'',
|
||||
'-Wl,-rpath,\'@loader_path/../../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'',
|
||||
'-Wl,-rpath,\'@loader_path/../../../../../@img-sharp-libvips-<(platform_and_arch)-npm-<(sharp_libvips_version)-<(sharp_libvips_yarn_locator)/node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\''
|
||||
]
|
||||
}
|
||||
}],
|
||||
['OS == "linux"', {
|
||||
'defines': [
|
||||
'_GLIBCXX_USE_CXX11_ABI=1'
|
||||
],
|
||||
'link_settings': {
|
||||
'libraries': [
|
||||
'-l:libvips-cpp.so.42'
|
||||
],
|
||||
'ldflags': [
|
||||
'-Wl,-s',
|
||||
'-Wl,--disable-new-dtags',
|
||||
'-Wl,-z,nodelete',
|
||||
'-Wl,-rpath=\'$$ORIGIN/../../sharp-libvips-<(platform_and_arch)/lib\'',
|
||||
'-Wl,-rpath=\'$$ORIGIN/../../../sharp-libvips-<(platform_and_arch)/<(sharp_libvips_version)/lib\'',
|
||||
'-Wl,-rpath=\'$$ORIGIN/../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'',
|
||||
'-Wl,-rpath=\'$$ORIGIN/../../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'',
|
||||
'-Wl,-rpath,\'$$ORIGIN/../../../../../@img-sharp-libvips-<(platform_and_arch)-npm-<(sharp_libvips_version)-<(sharp_libvips_yarn_locator)/node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\''
|
||||
]
|
||||
}
|
||||
}],
|
||||
['OS == "emscripten"', {
|
||||
'product_extension': 'node.js',
|
||||
'link_settings': {
|
||||
'ldflags': [
|
||||
'-fexceptions',
|
||||
'--pre-js=<!(node -p "require.resolve(\'./emscripten/pre.js\')")',
|
||||
'-Oz',
|
||||
'-sALLOW_MEMORY_GROWTH',
|
||||
'-sENVIRONMENT=node',
|
||||
'-sEXPORTED_FUNCTIONS=["emnapiInit", "_vips_shutdown", "_uv_library_shutdown"]',
|
||||
'-sNODERAWFS',
|
||||
'-sTEXTDECODER=0',
|
||||
'-sWASM_ASYNC_COMPILATION=0',
|
||||
'-sWASM_BIGINT'
|
||||
],
|
||||
'libraries': [
|
||||
'<!@(PKG_CONFIG_PATH="<!(node -p "require(\'@img/sharp-libvips-dev-wasm32/lib\')")/pkgconfig" pkg-config --static --libs vips-cpp)'
|
||||
],
|
||||
}
|
||||
}]
|
||||
]
|
||||
}]
|
||||
],
|
||||
'cflags_cc': [
|
||||
'-std=c++0x',
|
||||
'-fexceptions',
|
||||
'-Wall',
|
||||
'-Os'
|
||||
],
|
||||
'xcode_settings': {
|
||||
'CLANG_CXX_LANGUAGE_STANDARD': 'c++11',
|
||||
'MACOSX_DEPLOYMENT_TARGET': '10.13',
|
||||
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
|
||||
'GCC_ENABLE_CPP_RTTI': 'YES',
|
||||
'OTHER_CPLUSPLUSFLAGS': [
|
||||
'-fexceptions',
|
||||
'-Wall',
|
||||
'-Oz'
|
||||
]
|
||||
},
|
||||
'configurations': {
|
||||
'Release': {
|
||||
'conditions': [
|
||||
['target_arch == "arm"', {
|
||||
'cflags_cc': [
|
||||
'-Wno-psabi'
|
||||
]
|
||||
}],
|
||||
['OS == "win"', {
|
||||
'msvs_settings': {
|
||||
'VCCLCompilerTool': {
|
||||
'ExceptionHandling': 1,
|
||||
'Optimization': 1,
|
||||
'WholeProgramOptimization': 'true'
|
||||
},
|
||||
'VCLibrarianTool': {
|
||||
'AdditionalOptions': [
|
||||
'/LTCG:INCREMENTAL'
|
||||
]
|
||||
},
|
||||
'VCLinkerTool': {
|
||||
'ImageHasSafeExceptionHandlers': 'false',
|
||||
'OptimizeReferences': 2,
|
||||
'EnableCOMDATFolding': 2,
|
||||
'LinkIncremental': 1,
|
||||
'AdditionalOptions': [
|
||||
'/LTCG:INCREMENTAL'
|
||||
]
|
||||
}
|
||||
},
|
||||
'msvs_disabled_warnings': [
|
||||
4275
|
||||
]
|
||||
}]
|
||||
]
|
||||
}
|
||||
},
|
||||
}, {
|
||||
'target_name': 'copy-dll',
|
||||
'type': 'none',
|
||||
'dependencies': [
|
||||
'sharp-<(platform_and_arch)'
|
||||
],
|
||||
'conditions': [
|
||||
['OS == "win"', {
|
||||
'copies': [{
|
||||
'destination': 'build/Release',
|
||||
'files': [
|
||||
'<(sharp_libvips_lib_dir)/libvips-42.dll'
|
||||
]
|
||||
}]
|
||||
}]
|
||||
]
|
||||
}]
|
||||
}
|
1091
node_modules/sharp/src/common.cc
generated
vendored
Normal file
1091
node_modules/sharp/src/common.cc
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
393
node_modules/sharp/src/common.h
generated
vendored
Normal file
393
node_modules/sharp/src/common.h
generated
vendored
Normal file
@@ -0,0 +1,393 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef SRC_COMMON_H_
|
||||
#define SRC_COMMON_H_
|
||||
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
|
||||
#include <napi.h>
|
||||
#include <vips/vips8>
|
||||
|
||||
// Verify platform and compiler compatibility
|
||||
|
||||
#if (VIPS_MAJOR_VERSION < 8) || \
|
||||
(VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 15) || \
|
||||
(VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION == 15 && VIPS_MICRO_VERSION < 3)
|
||||
#error "libvips version 8.15.3+ is required - please see https://sharp.pixelplumbing.com/install"
|
||||
#endif
|
||||
|
||||
#if ((!defined(__clang__)) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6)))
|
||||
#error "GCC version 4.6+ is required for C++11 features - please see https://sharp.pixelplumbing.com/install"
|
||||
#endif
|
||||
|
||||
#if (defined(__clang__) && defined(__has_feature))
|
||||
#if (!__has_feature(cxx_range_for))
|
||||
#error "clang version 3.0+ is required for C++11 features - please see https://sharp.pixelplumbing.com/install"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
using vips::VImage;
|
||||
|
||||
namespace sharp {
|
||||
|
||||
struct InputDescriptor { // NOLINT(runtime/indentation_namespace)
|
||||
std::string name;
|
||||
std::string file;
|
||||
char *buffer;
|
||||
VipsFailOn failOn;
|
||||
uint64_t limitInputPixels;
|
||||
bool unlimited;
|
||||
VipsAccess access;
|
||||
size_t bufferLength;
|
||||
bool isBuffer;
|
||||
double density;
|
||||
bool ignoreIcc;
|
||||
VipsBandFormat rawDepth;
|
||||
int rawChannels;
|
||||
int rawWidth;
|
||||
int rawHeight;
|
||||
bool rawPremultiplied;
|
||||
int pages;
|
||||
int page;
|
||||
int level;
|
||||
int subifd;
|
||||
int createChannels;
|
||||
int createWidth;
|
||||
int createHeight;
|
||||
std::vector<double> createBackground;
|
||||
std::string createNoiseType;
|
||||
double createNoiseMean;
|
||||
double createNoiseSigma;
|
||||
std::string textValue;
|
||||
std::string textFont;
|
||||
std::string textFontfile;
|
||||
int textWidth;
|
||||
int textHeight;
|
||||
VipsAlign textAlign;
|
||||
bool textJustify;
|
||||
int textDpi;
|
||||
bool textRgba;
|
||||
int textSpacing;
|
||||
VipsTextWrap textWrap;
|
||||
int textAutofitDpi;
|
||||
|
||||
InputDescriptor():
|
||||
buffer(nullptr),
|
||||
failOn(VIPS_FAIL_ON_WARNING),
|
||||
limitInputPixels(0x3FFF * 0x3FFF),
|
||||
unlimited(false),
|
||||
access(VIPS_ACCESS_RANDOM),
|
||||
bufferLength(0),
|
||||
isBuffer(false),
|
||||
density(72.0),
|
||||
ignoreIcc(false),
|
||||
rawDepth(VIPS_FORMAT_UCHAR),
|
||||
rawChannels(0),
|
||||
rawWidth(0),
|
||||
rawHeight(0),
|
||||
rawPremultiplied(false),
|
||||
pages(1),
|
||||
page(0),
|
||||
level(0),
|
||||
subifd(-1),
|
||||
createChannels(0),
|
||||
createWidth(0),
|
||||
createHeight(0),
|
||||
createBackground{ 0.0, 0.0, 0.0, 255.0 },
|
||||
createNoiseMean(0.0),
|
||||
createNoiseSigma(0.0),
|
||||
textWidth(0),
|
||||
textHeight(0),
|
||||
textAlign(VIPS_ALIGN_LOW),
|
||||
textJustify(false),
|
||||
textDpi(72),
|
||||
textRgba(false),
|
||||
textSpacing(0),
|
||||
textWrap(VIPS_TEXT_WRAP_WORD),
|
||||
textAutofitDpi(0) {}
|
||||
};
|
||||
|
||||
// Convenience methods to access the attributes of a Napi::Object
|
||||
bool HasAttr(Napi::Object obj, std::string attr);
|
||||
std::string AttrAsStr(Napi::Object obj, std::string attr);
|
||||
std::string AttrAsStr(Napi::Object obj, unsigned int const attr);
|
||||
uint32_t AttrAsUint32(Napi::Object obj, std::string attr);
|
||||
int32_t AttrAsInt32(Napi::Object obj, std::string attr);
|
||||
int32_t AttrAsInt32(Napi::Object obj, unsigned int const attr);
|
||||
double AttrAsDouble(Napi::Object obj, std::string attr);
|
||||
double AttrAsDouble(Napi::Object obj, unsigned int const attr);
|
||||
bool AttrAsBool(Napi::Object obj, std::string attr);
|
||||
std::vector<double> AttrAsVectorOfDouble(Napi::Object obj, std::string attr);
|
||||
std::vector<int32_t> AttrAsInt32Vector(Napi::Object obj, std::string attr);
|
||||
template <class T> T AttrAsEnum(Napi::Object obj, std::string attr, GType type) {
|
||||
return static_cast<T>(
|
||||
vips_enum_from_nick(nullptr, type, AttrAsStr(obj, attr).data()));
|
||||
}
|
||||
|
||||
// Create an InputDescriptor instance from a Napi::Object describing an input image
|
||||
InputDescriptor* CreateInputDescriptor(Napi::Object input);
|
||||
|
||||
enum class ImageType {
|
||||
JPEG,
|
||||
PNG,
|
||||
WEBP,
|
||||
JP2,
|
||||
TIFF,
|
||||
GIF,
|
||||
SVG,
|
||||
HEIF,
|
||||
PDF,
|
||||
MAGICK,
|
||||
OPENSLIDE,
|
||||
PPM,
|
||||
FITS,
|
||||
EXR,
|
||||
JXL,
|
||||
VIPS,
|
||||
RAW,
|
||||
UNKNOWN,
|
||||
MISSING
|
||||
};
|
||||
|
||||
enum class Canvas {
|
||||
CROP,
|
||||
EMBED,
|
||||
MAX,
|
||||
MIN,
|
||||
IGNORE_ASPECT
|
||||
};
|
||||
|
||||
// How many tasks are in the queue?
|
||||
extern std::atomic<int> counterQueue;
|
||||
|
||||
// How many tasks are being processed?
|
||||
extern std::atomic<int> counterProcess;
|
||||
|
||||
// Filename extension checkers
|
||||
bool IsJpeg(std::string const &str);
|
||||
bool IsPng(std::string const &str);
|
||||
bool IsWebp(std::string const &str);
|
||||
bool IsJp2(std::string const &str);
|
||||
bool IsGif(std::string const &str);
|
||||
bool IsTiff(std::string const &str);
|
||||
bool IsHeic(std::string const &str);
|
||||
bool IsHeif(std::string const &str);
|
||||
bool IsAvif(std::string const &str);
|
||||
bool IsJxl(std::string const &str);
|
||||
bool IsDz(std::string const &str);
|
||||
bool IsDzZip(std::string const &str);
|
||||
bool IsV(std::string const &str);
|
||||
|
||||
/*
|
||||
Trim space from end of string.
|
||||
*/
|
||||
std::string TrimEnd(std::string const &str);
|
||||
|
||||
/*
|
||||
Provide a string identifier for the given image type.
|
||||
*/
|
||||
std::string ImageTypeId(ImageType const imageType);
|
||||
|
||||
/*
|
||||
Determine image format of a buffer.
|
||||
*/
|
||||
ImageType DetermineImageType(void *buffer, size_t const length);
|
||||
|
||||
/*
|
||||
Determine image format of a file.
|
||||
*/
|
||||
ImageType DetermineImageType(char const *file);
|
||||
|
||||
/*
|
||||
Does this image type support multiple pages?
|
||||
*/
|
||||
bool ImageTypeSupportsPage(ImageType imageType);
|
||||
|
||||
/*
|
||||
Does this image type support removal of safety limits?
|
||||
*/
|
||||
bool ImageTypeSupportsUnlimited(ImageType imageType);
|
||||
|
||||
/*
|
||||
Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data)
|
||||
*/
|
||||
std::tuple<VImage, ImageType> OpenInput(InputDescriptor *descriptor);
|
||||
|
||||
/*
|
||||
Does this image have an embedded profile?
|
||||
*/
|
||||
bool HasProfile(VImage image);
|
||||
|
||||
/*
|
||||
Get copy of embedded profile.
|
||||
*/
|
||||
std::pair<char*, size_t> GetProfile(VImage image);
|
||||
|
||||
/*
|
||||
Set embedded profile.
|
||||
*/
|
||||
VImage SetProfile(VImage image, std::pair<char*, size_t> icc);
|
||||
|
||||
/*
|
||||
Does this image have an alpha channel?
|
||||
Uses colour space interpretation with number of channels to guess this.
|
||||
*/
|
||||
bool HasAlpha(VImage image);
|
||||
|
||||
/*
|
||||
Remove all EXIF-related image fields.
|
||||
*/
|
||||
VImage RemoveExif(VImage image);
|
||||
|
||||
/*
|
||||
Get EXIF Orientation of image, if any.
|
||||
*/
|
||||
int ExifOrientation(VImage image);
|
||||
|
||||
/*
|
||||
Set EXIF Orientation of image.
|
||||
*/
|
||||
VImage SetExifOrientation(VImage image, int const orientation);
|
||||
|
||||
/*
|
||||
Remove EXIF Orientation from image.
|
||||
*/
|
||||
VImage RemoveExifOrientation(VImage image);
|
||||
|
||||
/*
|
||||
Set animation properties if necessary.
|
||||
*/
|
||||
VImage SetAnimationProperties(VImage image, int nPages, int pageHeight, std::vector<int> delay, int loop);
|
||||
|
||||
/*
|
||||
Remove animation properties from image.
|
||||
*/
|
||||
VImage RemoveAnimationProperties(VImage image);
|
||||
|
||||
/*
|
||||
Remove GIF palette from image.
|
||||
*/
|
||||
VImage RemoveGifPalette(VImage image);
|
||||
|
||||
/*
|
||||
Does this image have a non-default density?
|
||||
*/
|
||||
bool HasDensity(VImage image);
|
||||
|
||||
/*
|
||||
Get pixels/mm resolution as pixels/inch density.
|
||||
*/
|
||||
int GetDensity(VImage image);
|
||||
|
||||
/*
|
||||
Set pixels/mm resolution based on a pixels/inch density.
|
||||
*/
|
||||
VImage SetDensity(VImage image, const double density);
|
||||
|
||||
/*
|
||||
Multi-page images can have a page height. Fetch it, and sanity check it.
|
||||
If page-height is not set, it defaults to the image height
|
||||
*/
|
||||
int GetPageHeight(VImage image);
|
||||
|
||||
/*
|
||||
Check the proposed format supports the current dimensions.
|
||||
*/
|
||||
void AssertImageTypeDimensions(VImage image, ImageType const imageType);
|
||||
|
||||
/*
|
||||
Called when a Buffer undergoes GC, required to support mixed runtime libraries in Windows
|
||||
*/
|
||||
extern std::function<void(void*, char*)> FreeCallback;
|
||||
|
||||
/*
|
||||
Called with warnings from the glib-registered "VIPS" domain
|
||||
*/
|
||||
void VipsWarningCallback(char const* log_domain, GLogLevelFlags log_level, char const* message, void* ignore);
|
||||
|
||||
/*
|
||||
Pop the oldest warning message from the queue
|
||||
*/
|
||||
std::string VipsWarningPop();
|
||||
|
||||
/*
|
||||
Attach an event listener for progress updates, used to detect timeout
|
||||
*/
|
||||
void SetTimeout(VImage image, int const timeoutSeconds);
|
||||
|
||||
/*
|
||||
Event listener for progress updates, used to detect timeout
|
||||
*/
|
||||
void VipsProgressCallBack(VipsImage *image, VipsProgress *progress, int *timeoutSeconds);
|
||||
|
||||
/*
|
||||
Calculate the (left, top) coordinates of the output image
|
||||
within the input image, applying the given gravity during an embed.
|
||||
*/
|
||||
std::tuple<int, int> CalculateEmbedPosition(int const inWidth, int const inHeight,
|
||||
int const outWidth, int const outHeight, int const gravity);
|
||||
|
||||
/*
|
||||
Calculate the (left, top) coordinates of the output image
|
||||
within the input image, applying the given gravity.
|
||||
*/
|
||||
std::tuple<int, int> CalculateCrop(int const inWidth, int const inHeight,
|
||||
int const outWidth, int const outHeight, int const gravity);
|
||||
|
||||
/*
|
||||
Calculate the (left, top) coordinates of the output image
|
||||
within the input image, applying the given x and y offsets of the output image.
|
||||
*/
|
||||
std::tuple<int, int> CalculateCrop(int const inWidth, int const inHeight,
|
||||
int const outWidth, int const outHeight, int const x, int const y);
|
||||
|
||||
/*
|
||||
Are pixel values in this image 16-bit integer?
|
||||
*/
|
||||
bool Is16Bit(VipsInterpretation const interpretation);
|
||||
|
||||
/*
|
||||
Return the image alpha maximum. Useful for combining alpha bands. scRGB
|
||||
images are 0 - 1 for image data, but the alpha is 0 - 255.
|
||||
*/
|
||||
double MaximumImageAlpha(VipsInterpretation const interpretation);
|
||||
|
||||
/*
|
||||
Convert RGBA value to another colourspace
|
||||
*/
|
||||
std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba,
|
||||
VipsInterpretation const interpretation, bool premultiply);
|
||||
|
||||
/*
|
||||
Apply the alpha channel to a given colour
|
||||
*/
|
||||
std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour, bool premultiply);
|
||||
|
||||
/*
|
||||
Removes alpha channel, if any.
|
||||
*/
|
||||
VImage RemoveAlpha(VImage image);
|
||||
|
||||
/*
|
||||
Ensures alpha channel, if missing.
|
||||
*/
|
||||
VImage EnsureAlpha(VImage image, double const value);
|
||||
|
||||
/*
|
||||
Calculate the horizontal and vertical shrink factors, taking the canvas mode into account.
|
||||
*/
|
||||
std::pair<double, double> ResolveShrink(int width, int height, int targetWidth, int targetHeight,
|
||||
Canvas canvas, bool withoutEnlargement, bool withoutReduction);
|
||||
|
||||
/*
|
||||
Ensure decoding remains sequential.
|
||||
*/
|
||||
VImage StaySequential(VImage image, bool condition = true);
|
||||
|
||||
} // namespace sharp
|
||||
|
||||
#endif // SRC_COMMON_H_
|
320
node_modules/sharp/src/metadata.cc
generated
vendored
Normal file
320
node_modules/sharp/src/metadata.cc
generated
vendored
Normal file
@@ -0,0 +1,320 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
|
||||
#include <napi.h>
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "common.h"
|
||||
#include "metadata.h"
|
||||
|
||||
static void* readPNGComment(VipsImage *image, const char *field, GValue *value, void *p);
|
||||
|
||||
class MetadataWorker : public Napi::AsyncWorker {
|
||||
public:
|
||||
MetadataWorker(Napi::Function callback, MetadataBaton *baton, Napi::Function debuglog) :
|
||||
Napi::AsyncWorker(callback), baton(baton), debuglog(Napi::Persistent(debuglog)) {}
|
||||
~MetadataWorker() {}
|
||||
|
||||
void Execute() {
|
||||
// Decrement queued task counter
|
||||
sharp::counterQueue--;
|
||||
|
||||
vips::VImage image;
|
||||
sharp::ImageType imageType = sharp::ImageType::UNKNOWN;
|
||||
try {
|
||||
std::tie(image, imageType) = OpenInput(baton->input);
|
||||
} catch (vips::VError const &err) {
|
||||
(baton->err).append(err.what());
|
||||
}
|
||||
if (imageType != sharp::ImageType::UNKNOWN) {
|
||||
// Image type
|
||||
baton->format = sharp::ImageTypeId(imageType);
|
||||
// VipsImage attributes
|
||||
baton->width = image.width();
|
||||
baton->height = image.height();
|
||||
baton->space = vips_enum_nick(VIPS_TYPE_INTERPRETATION, image.interpretation());
|
||||
baton->channels = image.bands();
|
||||
baton->depth = vips_enum_nick(VIPS_TYPE_BAND_FORMAT, image.format());
|
||||
if (sharp::HasDensity(image)) {
|
||||
baton->density = sharp::GetDensity(image);
|
||||
}
|
||||
if (image.get_typeof("jpeg-chroma-subsample") == VIPS_TYPE_REF_STRING) {
|
||||
baton->chromaSubsampling = image.get_string("jpeg-chroma-subsample");
|
||||
}
|
||||
if (image.get_typeof("interlaced") == G_TYPE_INT) {
|
||||
baton->isProgressive = image.get_int("interlaced") == 1;
|
||||
}
|
||||
if (image.get_typeof("palette-bit-depth") == G_TYPE_INT) {
|
||||
baton->paletteBitDepth = image.get_int("palette-bit-depth");
|
||||
}
|
||||
if (image.get_typeof(VIPS_META_N_PAGES) == G_TYPE_INT) {
|
||||
baton->pages = image.get_int(VIPS_META_N_PAGES);
|
||||
}
|
||||
if (image.get_typeof(VIPS_META_PAGE_HEIGHT) == G_TYPE_INT) {
|
||||
baton->pageHeight = image.get_int(VIPS_META_PAGE_HEIGHT);
|
||||
}
|
||||
if (image.get_typeof("loop") == G_TYPE_INT) {
|
||||
baton->loop = image.get_int("loop");
|
||||
}
|
||||
if (image.get_typeof("delay") == VIPS_TYPE_ARRAY_INT) {
|
||||
baton->delay = image.get_array_int("delay");
|
||||
}
|
||||
if (image.get_typeof("heif-primary") == G_TYPE_INT) {
|
||||
baton->pagePrimary = image.get_int("heif-primary");
|
||||
}
|
||||
if (image.get_typeof("heif-compression") == VIPS_TYPE_REF_STRING) {
|
||||
baton->compression = image.get_string("heif-compression");
|
||||
}
|
||||
if (image.get_typeof(VIPS_META_RESOLUTION_UNIT) == VIPS_TYPE_REF_STRING) {
|
||||
baton->resolutionUnit = image.get_string(VIPS_META_RESOLUTION_UNIT);
|
||||
}
|
||||
if (image.get_typeof("magick-format") == VIPS_TYPE_REF_STRING) {
|
||||
baton->formatMagick = image.get_string("magick-format");
|
||||
}
|
||||
if (image.get_typeof("openslide.level-count") == VIPS_TYPE_REF_STRING) {
|
||||
int const levels = std::stoi(image.get_string("openslide.level-count"));
|
||||
for (int l = 0; l < levels; l++) {
|
||||
std::string prefix = "openslide.level[" + std::to_string(l) + "].";
|
||||
int const width = std::stoi(image.get_string((prefix + "width").data()));
|
||||
int const height = std::stoi(image.get_string((prefix + "height").data()));
|
||||
baton->levels.push_back(std::pair<int, int>(width, height));
|
||||
}
|
||||
}
|
||||
if (image.get_typeof(VIPS_META_N_SUBIFDS) == G_TYPE_INT) {
|
||||
baton->subifds = image.get_int(VIPS_META_N_SUBIFDS);
|
||||
}
|
||||
baton->hasProfile = sharp::HasProfile(image);
|
||||
if (image.get_typeof("background") == VIPS_TYPE_ARRAY_DOUBLE) {
|
||||
baton->background = image.get_array_double("background");
|
||||
}
|
||||
// Derived attributes
|
||||
baton->hasAlpha = sharp::HasAlpha(image);
|
||||
baton->orientation = sharp::ExifOrientation(image);
|
||||
// EXIF
|
||||
if (image.get_typeof(VIPS_META_EXIF_NAME) == VIPS_TYPE_BLOB) {
|
||||
size_t exifLength;
|
||||
void const *exif = image.get_blob(VIPS_META_EXIF_NAME, &exifLength);
|
||||
baton->exif = static_cast<char*>(g_malloc(exifLength));
|
||||
memcpy(baton->exif, exif, exifLength);
|
||||
baton->exifLength = exifLength;
|
||||
}
|
||||
// ICC profile
|
||||
if (image.get_typeof(VIPS_META_ICC_NAME) == VIPS_TYPE_BLOB) {
|
||||
size_t iccLength;
|
||||
void const *icc = image.get_blob(VIPS_META_ICC_NAME, &iccLength);
|
||||
baton->icc = static_cast<char*>(g_malloc(iccLength));
|
||||
memcpy(baton->icc, icc, iccLength);
|
||||
baton->iccLength = iccLength;
|
||||
}
|
||||
// IPTC
|
||||
if (image.get_typeof(VIPS_META_IPTC_NAME) == VIPS_TYPE_BLOB) {
|
||||
size_t iptcLength;
|
||||
void const *iptc = image.get_blob(VIPS_META_IPTC_NAME, &iptcLength);
|
||||
baton->iptc = static_cast<char *>(g_malloc(iptcLength));
|
||||
memcpy(baton->iptc, iptc, iptcLength);
|
||||
baton->iptcLength = iptcLength;
|
||||
}
|
||||
// XMP
|
||||
if (image.get_typeof(VIPS_META_XMP_NAME) == VIPS_TYPE_BLOB) {
|
||||
size_t xmpLength;
|
||||
void const *xmp = image.get_blob(VIPS_META_XMP_NAME, &xmpLength);
|
||||
baton->xmp = static_cast<char *>(g_malloc(xmpLength));
|
||||
memcpy(baton->xmp, xmp, xmpLength);
|
||||
baton->xmpLength = xmpLength;
|
||||
}
|
||||
// TIFFTAG_PHOTOSHOP
|
||||
if (image.get_typeof(VIPS_META_PHOTOSHOP_NAME) == VIPS_TYPE_BLOB) {
|
||||
size_t tifftagPhotoshopLength;
|
||||
void const *tifftagPhotoshop = image.get_blob(VIPS_META_PHOTOSHOP_NAME, &tifftagPhotoshopLength);
|
||||
baton->tifftagPhotoshop = static_cast<char *>(g_malloc(tifftagPhotoshopLength));
|
||||
memcpy(baton->tifftagPhotoshop, tifftagPhotoshop, tifftagPhotoshopLength);
|
||||
baton->tifftagPhotoshopLength = tifftagPhotoshopLength;
|
||||
}
|
||||
// PNG comments
|
||||
vips_image_map(image.get_image(), readPNGComment, &baton->comments);
|
||||
}
|
||||
|
||||
// Clean up
|
||||
vips_error_clear();
|
||||
vips_thread_shutdown();
|
||||
}
|
||||
|
||||
void OnOK() {
|
||||
Napi::Env env = Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
// Handle warnings
|
||||
std::string warning = sharp::VipsWarningPop();
|
||||
while (!warning.empty()) {
|
||||
debuglog.Call(Receiver().Value(), { Napi::String::New(env, warning) });
|
||||
warning = sharp::VipsWarningPop();
|
||||
}
|
||||
|
||||
if (baton->err.empty()) {
|
||||
Napi::Object info = Napi::Object::New(env);
|
||||
info.Set("format", baton->format);
|
||||
if (baton->input->bufferLength > 0) {
|
||||
info.Set("size", baton->input->bufferLength);
|
||||
}
|
||||
info.Set("width", baton->width);
|
||||
info.Set("height", baton->height);
|
||||
info.Set("space", baton->space);
|
||||
info.Set("channels", baton->channels);
|
||||
info.Set("depth", baton->depth);
|
||||
if (baton->density > 0) {
|
||||
info.Set("density", baton->density);
|
||||
}
|
||||
if (!baton->chromaSubsampling.empty()) {
|
||||
info.Set("chromaSubsampling", baton->chromaSubsampling);
|
||||
}
|
||||
info.Set("isProgressive", baton->isProgressive);
|
||||
if (baton->paletteBitDepth > 0) {
|
||||
info.Set("paletteBitDepth", baton->paletteBitDepth);
|
||||
}
|
||||
if (baton->pages > 0) {
|
||||
info.Set("pages", baton->pages);
|
||||
}
|
||||
if (baton->pageHeight > 0) {
|
||||
info.Set("pageHeight", baton->pageHeight);
|
||||
}
|
||||
if (baton->loop >= 0) {
|
||||
info.Set("loop", baton->loop);
|
||||
}
|
||||
if (!baton->delay.empty()) {
|
||||
int i = 0;
|
||||
Napi::Array delay = Napi::Array::New(env, static_cast<size_t>(baton->delay.size()));
|
||||
for (int const d : baton->delay) {
|
||||
delay.Set(i++, d);
|
||||
}
|
||||
info.Set("delay", delay);
|
||||
}
|
||||
if (baton->pagePrimary > -1) {
|
||||
info.Set("pagePrimary", baton->pagePrimary);
|
||||
}
|
||||
if (!baton->compression.empty()) {
|
||||
info.Set("compression", baton->compression);
|
||||
}
|
||||
if (!baton->resolutionUnit.empty()) {
|
||||
info.Set("resolutionUnit", baton->resolutionUnit == "in" ? "inch" : baton->resolutionUnit);
|
||||
}
|
||||
if (!baton->formatMagick.empty()) {
|
||||
info.Set("formatMagick", baton->formatMagick);
|
||||
}
|
||||
if (!baton->levels.empty()) {
|
||||
int i = 0;
|
||||
Napi::Array levels = Napi::Array::New(env, static_cast<size_t>(baton->levels.size()));
|
||||
for (std::pair<int, int> const &l : baton->levels) {
|
||||
Napi::Object level = Napi::Object::New(env);
|
||||
level.Set("width", l.first);
|
||||
level.Set("height", l.second);
|
||||
levels.Set(i++, level);
|
||||
}
|
||||
info.Set("levels", levels);
|
||||
}
|
||||
if (baton->subifds > 0) {
|
||||
info.Set("subifds", baton->subifds);
|
||||
}
|
||||
if (!baton->background.empty()) {
|
||||
if (baton->background.size() == 3) {
|
||||
Napi::Object background = Napi::Object::New(env);
|
||||
background.Set("r", baton->background[0]);
|
||||
background.Set("g", baton->background[1]);
|
||||
background.Set("b", baton->background[2]);
|
||||
info.Set("background", background);
|
||||
} else {
|
||||
info.Set("background", baton->background[0]);
|
||||
}
|
||||
}
|
||||
info.Set("hasProfile", baton->hasProfile);
|
||||
info.Set("hasAlpha", baton->hasAlpha);
|
||||
if (baton->orientation > 0) {
|
||||
info.Set("orientation", baton->orientation);
|
||||
}
|
||||
if (baton->exifLength > 0) {
|
||||
info.Set("exif", Napi::Buffer<char>::NewOrCopy(env, baton->exif, baton->exifLength, sharp::FreeCallback));
|
||||
}
|
||||
if (baton->iccLength > 0) {
|
||||
info.Set("icc", Napi::Buffer<char>::NewOrCopy(env, baton->icc, baton->iccLength, sharp::FreeCallback));
|
||||
}
|
||||
if (baton->iptcLength > 0) {
|
||||
info.Set("iptc", Napi::Buffer<char>::NewOrCopy(env, baton->iptc, baton->iptcLength, sharp::FreeCallback));
|
||||
}
|
||||
if (baton->xmpLength > 0) {
|
||||
info.Set("xmp", Napi::Buffer<char>::NewOrCopy(env, baton->xmp, baton->xmpLength, sharp::FreeCallback));
|
||||
}
|
||||
if (baton->tifftagPhotoshopLength > 0) {
|
||||
info.Set("tifftagPhotoshop",
|
||||
Napi::Buffer<char>::NewOrCopy(env, baton->tifftagPhotoshop,
|
||||
baton->tifftagPhotoshopLength, sharp::FreeCallback));
|
||||
}
|
||||
if (baton->comments.size() > 0) {
|
||||
int i = 0;
|
||||
Napi::Array comments = Napi::Array::New(env, baton->comments.size());
|
||||
for (auto &c : baton->comments) {
|
||||
Napi::Object comment = Napi::Object::New(env);
|
||||
comment.Set("keyword", c.first);
|
||||
comment.Set("text", c.second);
|
||||
comments.Set(i++, comment);
|
||||
}
|
||||
info.Set("comments", comments);
|
||||
}
|
||||
Callback().Call(Receiver().Value(), { env.Null(), info });
|
||||
} else {
|
||||
Callback().Call(Receiver().Value(), { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() });
|
||||
}
|
||||
|
||||
delete baton->input;
|
||||
delete baton;
|
||||
}
|
||||
|
||||
private:
|
||||
MetadataBaton* baton;
|
||||
Napi::FunctionReference debuglog;
|
||||
};
|
||||
|
||||
/*
|
||||
metadata(options, callback)
|
||||
*/
|
||||
Napi::Value metadata(const Napi::CallbackInfo& info) {
|
||||
// V8 objects are converted to non-V8 types held in the baton struct
|
||||
MetadataBaton *baton = new MetadataBaton;
|
||||
Napi::Object options = info[size_t(0)].As<Napi::Object>();
|
||||
|
||||
// Input
|
||||
baton->input = sharp::CreateInputDescriptor(options.Get("input").As<Napi::Object>());
|
||||
|
||||
// Function to notify of libvips warnings
|
||||
Napi::Function debuglog = options.Get("debuglog").As<Napi::Function>();
|
||||
|
||||
// Join queue for worker thread
|
||||
Napi::Function callback = info[size_t(1)].As<Napi::Function>();
|
||||
MetadataWorker *worker = new MetadataWorker(callback, baton, debuglog);
|
||||
worker->Receiver().Set("options", options);
|
||||
worker->Queue();
|
||||
|
||||
// Increment queued task counter
|
||||
sharp::counterQueue++;
|
||||
|
||||
return info.Env().Undefined();
|
||||
}
|
||||
|
||||
const char *PNG_COMMENT_START = "png-comment-";
|
||||
const int PNG_COMMENT_START_LEN = strlen(PNG_COMMENT_START);
|
||||
|
||||
static void* readPNGComment(VipsImage *image, const char *field, GValue *value, void *p) {
|
||||
MetadataComments *comments = static_cast<MetadataComments *>(p);
|
||||
|
||||
if (vips_isprefix(PNG_COMMENT_START, field)) {
|
||||
const char *keyword = strchr(field + PNG_COMMENT_START_LEN, '-');
|
||||
const char *str;
|
||||
if (keyword != NULL && !vips_image_get_string(image, field, &str)) {
|
||||
keyword++; // Skip the hyphen
|
||||
comments->push_back(std::make_pair(keyword, str));
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
85
node_modules/sharp/src/metadata.h
generated
vendored
Normal file
85
node_modules/sharp/src/metadata.h
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef SRC_METADATA_H_
|
||||
#define SRC_METADATA_H_
|
||||
|
||||
#include <string>
|
||||
#include <napi.h>
|
||||
|
||||
#include "./common.h"
|
||||
|
||||
typedef std::vector<std::pair<std::string, std::string>> MetadataComments;
|
||||
|
||||
struct MetadataBaton {
|
||||
// Input
|
||||
sharp::InputDescriptor *input;
|
||||
// Output
|
||||
std::string format;
|
||||
int width;
|
||||
int height;
|
||||
std::string space;
|
||||
int channels;
|
||||
std::string depth;
|
||||
int density;
|
||||
std::string chromaSubsampling;
|
||||
bool isProgressive;
|
||||
int paletteBitDepth;
|
||||
int pages;
|
||||
int pageHeight;
|
||||
int loop;
|
||||
std::vector<int> delay;
|
||||
int pagePrimary;
|
||||
std::string compression;
|
||||
std::string resolutionUnit;
|
||||
std::string formatMagick;
|
||||
std::vector<std::pair<int, int>> levels;
|
||||
int subifds;
|
||||
std::vector<double> background;
|
||||
bool hasProfile;
|
||||
bool hasAlpha;
|
||||
int orientation;
|
||||
char *exif;
|
||||
size_t exifLength;
|
||||
char *icc;
|
||||
size_t iccLength;
|
||||
char *iptc;
|
||||
size_t iptcLength;
|
||||
char *xmp;
|
||||
size_t xmpLength;
|
||||
char *tifftagPhotoshop;
|
||||
size_t tifftagPhotoshopLength;
|
||||
MetadataComments comments;
|
||||
std::string err;
|
||||
|
||||
MetadataBaton():
|
||||
input(nullptr),
|
||||
width(0),
|
||||
height(0),
|
||||
channels(0),
|
||||
density(0),
|
||||
isProgressive(false),
|
||||
paletteBitDepth(0),
|
||||
pages(0),
|
||||
pageHeight(0),
|
||||
loop(-1),
|
||||
pagePrimary(-1),
|
||||
subifds(0),
|
||||
hasProfile(false),
|
||||
hasAlpha(false),
|
||||
orientation(0),
|
||||
exif(nullptr),
|
||||
exifLength(0),
|
||||
icc(nullptr),
|
||||
iccLength(0),
|
||||
iptc(nullptr),
|
||||
iptcLength(0),
|
||||
xmp(nullptr),
|
||||
xmpLength(0),
|
||||
tifftagPhotoshop(nullptr),
|
||||
tifftagPhotoshopLength(0) {}
|
||||
};
|
||||
|
||||
Napi::Value metadata(const Napi::CallbackInfo& info);
|
||||
|
||||
#endif // SRC_METADATA_H_
|
475
node_modules/sharp/src/operations.cc
generated
vendored
Normal file
475
node_modules/sharp/src/operations.cc
generated
vendored
Normal file
@@ -0,0 +1,475 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "common.h"
|
||||
#include "operations.h"
|
||||
|
||||
using vips::VImage;
|
||||
using vips::VError;
|
||||
|
||||
namespace sharp {
|
||||
/*
|
||||
* Tint an image using the provided RGB.
|
||||
*/
|
||||
VImage Tint(VImage image, std::vector<double> const tint) {
|
||||
std::vector<double> const tintLab = (VImage::black(1, 1) + tint)
|
||||
.colourspace(VIPS_INTERPRETATION_LAB, VImage::option()->set("source_space", VIPS_INTERPRETATION_sRGB))
|
||||
.getpoint(0, 0);
|
||||
// LAB identity function
|
||||
VImage identityLab = VImage::identity(VImage::option()->set("bands", 3))
|
||||
.colourspace(VIPS_INTERPRETATION_LAB, VImage::option()->set("source_space", VIPS_INTERPRETATION_sRGB));
|
||||
// Scale luminance range, 0.0 to 1.0
|
||||
VImage l = identityLab[0] / 100;
|
||||
// Weighting functions
|
||||
VImage weightL = 1.0 - 4.0 * ((l - 0.5) * (l - 0.5));
|
||||
VImage weightAB = (weightL * tintLab).extract_band(1, VImage::option()->set("n", 2));
|
||||
identityLab = identityLab[0].bandjoin(weightAB);
|
||||
// Convert lookup table to sRGB
|
||||
VImage lut = identityLab.colourspace(VIPS_INTERPRETATION_sRGB,
|
||||
VImage::option()->set("source_space", VIPS_INTERPRETATION_LAB));
|
||||
// Original colourspace
|
||||
VipsInterpretation typeBeforeTint = image.interpretation();
|
||||
if (typeBeforeTint == VIPS_INTERPRETATION_RGB) {
|
||||
typeBeforeTint = VIPS_INTERPRETATION_sRGB;
|
||||
}
|
||||
// Apply lookup table
|
||||
if (HasAlpha(image)) {
|
||||
VImage alpha = image[image.bands() - 1];
|
||||
image = RemoveAlpha(image)
|
||||
.colourspace(VIPS_INTERPRETATION_B_W)
|
||||
.maplut(lut)
|
||||
.colourspace(typeBeforeTint)
|
||||
.bandjoin(alpha);
|
||||
} else {
|
||||
image = image
|
||||
.colourspace(VIPS_INTERPRETATION_B_W)
|
||||
.maplut(lut)
|
||||
.colourspace(typeBeforeTint);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/*
|
||||
* Stretch luminance to cover full dynamic range.
|
||||
*/
|
||||
VImage Normalise(VImage image, int const lower, int const upper) {
|
||||
// Get original colourspace
|
||||
VipsInterpretation typeBeforeNormalize = image.interpretation();
|
||||
if (typeBeforeNormalize == VIPS_INTERPRETATION_RGB) {
|
||||
typeBeforeNormalize = VIPS_INTERPRETATION_sRGB;
|
||||
}
|
||||
// Convert to LAB colourspace
|
||||
VImage lab = image.colourspace(VIPS_INTERPRETATION_LAB);
|
||||
// Extract luminance
|
||||
VImage luminance = lab[0];
|
||||
|
||||
// Find luminance range
|
||||
int const min = lower == 0 ? luminance.min() : luminance.percent(lower);
|
||||
int const max = upper == 100 ? luminance.max() : luminance.percent(upper);
|
||||
|
||||
if (std::abs(max - min) > 1) {
|
||||
// Extract chroma
|
||||
VImage chroma = lab.extract_band(1, VImage::option()->set("n", 2));
|
||||
// Calculate multiplication factor and addition
|
||||
double f = 100.0 / (max - min);
|
||||
double a = -(min * f);
|
||||
// Scale luminance, join to chroma, convert back to original colourspace
|
||||
VImage normalized = luminance.linear(f, a).bandjoin(chroma).colourspace(typeBeforeNormalize);
|
||||
// Attach original alpha channel, if any
|
||||
if (HasAlpha(image)) {
|
||||
// Extract original alpha channel
|
||||
VImage alpha = image[image.bands() - 1];
|
||||
// Join alpha channel to normalised image
|
||||
return normalized.bandjoin(alpha);
|
||||
} else {
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/*
|
||||
* Contrast limiting adapative histogram equalization (CLAHE)
|
||||
*/
|
||||
VImage Clahe(VImage image, int const width, int const height, int const maxSlope) {
|
||||
return image.hist_local(width, height, VImage::option()->set("max_slope", maxSlope));
|
||||
}
|
||||
|
||||
/*
|
||||
* Gamma encoding/decoding
|
||||
*/
|
||||
VImage Gamma(VImage image, double const exponent) {
|
||||
if (HasAlpha(image)) {
|
||||
// Separate alpha channel
|
||||
VImage alpha = image[image.bands() - 1];
|
||||
return RemoveAlpha(image).gamma(VImage::option()->set("exponent", exponent)).bandjoin(alpha);
|
||||
} else {
|
||||
return image.gamma(VImage::option()->set("exponent", exponent));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Flatten image to remove alpha channel
|
||||
*/
|
||||
VImage Flatten(VImage image, std::vector<double> flattenBackground) {
|
||||
double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
|
||||
std::vector<double> background {
|
||||
flattenBackground[0] * multiplier,
|
||||
flattenBackground[1] * multiplier,
|
||||
flattenBackground[2] * multiplier
|
||||
};
|
||||
return image.flatten(VImage::option()->set("background", background));
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce the "negative" of the image.
|
||||
*/
|
||||
VImage Negate(VImage image, bool const negateAlpha) {
|
||||
if (HasAlpha(image) && !negateAlpha) {
|
||||
// Separate alpha channel
|
||||
VImage alpha = image[image.bands() - 1];
|
||||
return RemoveAlpha(image).invert().bandjoin(alpha);
|
||||
} else {
|
||||
return image.invert();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Gaussian blur. Use sigma of -1.0 for fast blur.
|
||||
*/
|
||||
VImage Blur(VImage image, double const sigma, VipsPrecision precision, double const minAmpl) {
|
||||
if (sigma == -1.0) {
|
||||
// Fast, mild blur - averages neighbouring pixels
|
||||
VImage blur = VImage::new_matrixv(3, 3,
|
||||
1.0, 1.0, 1.0,
|
||||
1.0, 1.0, 1.0,
|
||||
1.0, 1.0, 1.0);
|
||||
blur.set("scale", 9.0);
|
||||
return image.conv(blur);
|
||||
} else {
|
||||
// Slower, accurate Gaussian blur
|
||||
return StaySequential(image).gaussblur(sigma, VImage::option()
|
||||
->set("precision", precision)
|
||||
->set("min_ampl", minAmpl));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Convolution with a kernel.
|
||||
*/
|
||||
VImage Convolve(VImage image, int const width, int const height,
|
||||
double const scale, double const offset,
|
||||
std::vector<double> const &kernel_v
|
||||
) {
|
||||
VImage kernel = VImage::new_from_memory(
|
||||
static_cast<void*>(const_cast<double*>(kernel_v.data())),
|
||||
width * height * sizeof(double),
|
||||
width,
|
||||
height,
|
||||
1,
|
||||
VIPS_FORMAT_DOUBLE);
|
||||
kernel.set("scale", scale);
|
||||
kernel.set("offset", offset);
|
||||
|
||||
return image.conv(kernel);
|
||||
}
|
||||
|
||||
/*
|
||||
* Recomb with a Matrix of the given bands/channel size.
|
||||
* Eg. RGB will be a 3x3 matrix.
|
||||
*/
|
||||
VImage Recomb(VImage image, std::vector<double> const& matrix) {
|
||||
double* m = const_cast<double*>(matrix.data());
|
||||
image = image.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||
if (matrix.size() == 9) {
|
||||
return image
|
||||
.recomb(image.bands() == 3
|
||||
? VImage::new_matrix(3, 3, m, 9)
|
||||
: VImage::new_matrixv(4, 4,
|
||||
m[0], m[1], m[2], 0.0,
|
||||
m[3], m[4], m[5], 0.0,
|
||||
m[6], m[7], m[8], 0.0,
|
||||
0.0, 0.0, 0.0, 1.0));
|
||||
} else {
|
||||
return image.recomb(VImage::new_matrix(4, 4, m, 16));
|
||||
}
|
||||
}
|
||||
|
||||
VImage Modulate(VImage image, double const brightness, double const saturation,
|
||||
int const hue, double const lightness) {
|
||||
VipsInterpretation colourspaceBeforeModulate = image.interpretation();
|
||||
if (HasAlpha(image)) {
|
||||
// Separate alpha channel
|
||||
VImage alpha = image[image.bands() - 1];
|
||||
return RemoveAlpha(image)
|
||||
.colourspace(VIPS_INTERPRETATION_LCH)
|
||||
.linear(
|
||||
{ brightness, saturation, 1},
|
||||
{ lightness, 0.0, static_cast<double>(hue) }
|
||||
)
|
||||
.colourspace(colourspaceBeforeModulate)
|
||||
.bandjoin(alpha);
|
||||
} else {
|
||||
return image
|
||||
.colourspace(VIPS_INTERPRETATION_LCH)
|
||||
.linear(
|
||||
{ brightness, saturation, 1 },
|
||||
{ lightness, 0.0, static_cast<double>(hue) }
|
||||
)
|
||||
.colourspace(colourspaceBeforeModulate);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Sharpen flat and jagged areas. Use sigma of -1.0 for fast sharpen.
|
||||
*/
|
||||
VImage Sharpen(VImage image, double const sigma, double const m1, double const m2,
|
||||
double const x1, double const y2, double const y3) {
|
||||
if (sigma == -1.0) {
|
||||
// Fast, mild sharpen
|
||||
VImage sharpen = VImage::new_matrixv(3, 3,
|
||||
-1.0, -1.0, -1.0,
|
||||
-1.0, 32.0, -1.0,
|
||||
-1.0, -1.0, -1.0);
|
||||
sharpen.set("scale", 24.0);
|
||||
return image.conv(sharpen);
|
||||
} else {
|
||||
// Slow, accurate sharpen in LAB colour space, with control over flat vs jagged areas
|
||||
VipsInterpretation colourspaceBeforeSharpen = image.interpretation();
|
||||
if (colourspaceBeforeSharpen == VIPS_INTERPRETATION_RGB) {
|
||||
colourspaceBeforeSharpen = VIPS_INTERPRETATION_sRGB;
|
||||
}
|
||||
return image
|
||||
.sharpen(VImage::option()
|
||||
->set("sigma", sigma)
|
||||
->set("m1", m1)
|
||||
->set("m2", m2)
|
||||
->set("x1", x1)
|
||||
->set("y2", y2)
|
||||
->set("y3", y3))
|
||||
.colourspace(colourspaceBeforeSharpen);
|
||||
}
|
||||
}
|
||||
|
||||
VImage Threshold(VImage image, double const threshold, bool const thresholdGrayscale) {
|
||||
if (!thresholdGrayscale) {
|
||||
return image >= threshold;
|
||||
}
|
||||
return image.colourspace(VIPS_INTERPRETATION_B_W) >= threshold;
|
||||
}
|
||||
|
||||
/*
|
||||
Perform boolean/bitwise operation on image color channels - results in one channel image
|
||||
*/
|
||||
VImage Bandbool(VImage image, VipsOperationBoolean const boolean) {
|
||||
image = image.bandbool(boolean);
|
||||
return image.copy(VImage::option()->set("interpretation", VIPS_INTERPRETATION_B_W));
|
||||
}
|
||||
|
||||
/*
|
||||
Perform bitwise boolean operation between images
|
||||
*/
|
||||
VImage Boolean(VImage image, VImage imageR, VipsOperationBoolean const boolean) {
|
||||
return image.boolean(imageR, boolean);
|
||||
}
|
||||
|
||||
/*
|
||||
Trim an image
|
||||
*/
|
||||
VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt) {
|
||||
if (image.width() < 3 && image.height() < 3) {
|
||||
throw VError("Image to trim must be at least 3x3 pixels");
|
||||
}
|
||||
if (background.size() == 0) {
|
||||
// Top-left pixel provides the default background colour if none is given
|
||||
background = image.extract_area(0, 0, 1, 1)(0, 0);
|
||||
} else if (sharp::Is16Bit(image.interpretation())) {
|
||||
for (size_t i = 0; i < background.size(); i++) {
|
||||
background[i] *= 256.0;
|
||||
}
|
||||
threshold *= 256.0;
|
||||
}
|
||||
std::vector<double> backgroundAlpha({ background.back() });
|
||||
if (HasAlpha(image)) {
|
||||
background.pop_back();
|
||||
} else {
|
||||
background.resize(image.bands());
|
||||
}
|
||||
int left, top, width, height;
|
||||
left = image.find_trim(&top, &width, &height, VImage::option()
|
||||
->set("background", background)
|
||||
->set("line_art", lineArt)
|
||||
->set("threshold", threshold));
|
||||
if (HasAlpha(image)) {
|
||||
// Search alpha channel (A)
|
||||
int leftA, topA, widthA, heightA;
|
||||
VImage alpha = image[image.bands() - 1];
|
||||
leftA = alpha.find_trim(&topA, &widthA, &heightA, VImage::option()
|
||||
->set("background", backgroundAlpha)
|
||||
->set("line_art", lineArt)
|
||||
->set("threshold", threshold));
|
||||
if (widthA > 0 && heightA > 0) {
|
||||
if (width > 0 && height > 0) {
|
||||
// Combined bounding box (B)
|
||||
int const leftB = std::min(left, leftA);
|
||||
int const topB = std::min(top, topA);
|
||||
int const widthB = std::max(left + width, leftA + widthA) - leftB;
|
||||
int const heightB = std::max(top + height, topA + heightA) - topB;
|
||||
return image.extract_area(leftB, topB, widthB, heightB);
|
||||
} else {
|
||||
// Use alpha only
|
||||
return image.extract_area(leftA, topA, widthA, heightA);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (width > 0 && height > 0) {
|
||||
return image.extract_area(left, top, width, height);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate (a * in + b)
|
||||
*/
|
||||
VImage Linear(VImage image, std::vector<double> const a, std::vector<double> const b) {
|
||||
size_t const bands = static_cast<size_t>(image.bands());
|
||||
if (a.size() > bands) {
|
||||
throw VError("Band expansion using linear is unsupported");
|
||||
}
|
||||
bool const uchar = !Is16Bit(image.interpretation());
|
||||
if (HasAlpha(image) && a.size() != bands && (a.size() == 1 || a.size() == bands - 1 || bands - 1 == 1)) {
|
||||
// Separate alpha channel
|
||||
VImage alpha = image[bands - 1];
|
||||
return RemoveAlpha(image).linear(a, b, VImage::option()->set("uchar", uchar)).bandjoin(alpha);
|
||||
} else {
|
||||
return image.linear(a, b, VImage::option()->set("uchar", uchar));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Unflatten
|
||||
*/
|
||||
VImage Unflatten(VImage image) {
|
||||
if (HasAlpha(image)) {
|
||||
VImage alpha = image[image.bands() - 1];
|
||||
VImage noAlpha = RemoveAlpha(image);
|
||||
return noAlpha.bandjoin(alpha & (noAlpha.colourspace(VIPS_INTERPRETATION_B_W) < 255));
|
||||
} else {
|
||||
return image.bandjoin(image.colourspace(VIPS_INTERPRETATION_B_W) < 255);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure the image is in a given colourspace
|
||||
*/
|
||||
VImage EnsureColourspace(VImage image, VipsInterpretation colourspace) {
|
||||
if (colourspace != VIPS_INTERPRETATION_LAST && image.interpretation() != colourspace) {
|
||||
image = image.colourspace(colourspace,
|
||||
VImage::option()->set("source_space", image.interpretation()));
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/*
|
||||
* Split and crop each frame, reassemble, and update pageHeight.
|
||||
*/
|
||||
VImage CropMultiPage(VImage image, int left, int top, int width, int height,
|
||||
int nPages, int *pageHeight) {
|
||||
if (top == 0 && height == *pageHeight) {
|
||||
// Fast path; no need to adjust the height of the multi-page image
|
||||
return image.extract_area(left, 0, width, image.height());
|
||||
} else {
|
||||
std::vector<VImage> pages;
|
||||
pages.reserve(nPages);
|
||||
|
||||
// Split the image into cropped frames
|
||||
image = StaySequential(image);
|
||||
for (int i = 0; i < nPages; i++) {
|
||||
pages.push_back(
|
||||
image.extract_area(left, *pageHeight * i + top, width, height));
|
||||
}
|
||||
|
||||
// Reassemble the frames into a tall, thin image
|
||||
VImage assembled = VImage::arrayjoin(pages,
|
||||
VImage::option()->set("across", 1));
|
||||
|
||||
// Update the page height
|
||||
*pageHeight = height;
|
||||
|
||||
return assembled;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Split into frames, embed each frame, reassemble, and update pageHeight.
|
||||
*/
|
||||
VImage EmbedMultiPage(VImage image, int left, int top, int width, int height,
|
||||
VipsExtend extendWith, std::vector<double> background, int nPages, int *pageHeight) {
|
||||
if (top == 0 && height == *pageHeight) {
|
||||
// Fast path; no need to adjust the height of the multi-page image
|
||||
return image.embed(left, 0, width, image.height(), VImage::option()
|
||||
->set("extend", extendWith)
|
||||
->set("background", background));
|
||||
} else if (left == 0 && width == image.width()) {
|
||||
// Fast path; no need to adjust the width of the multi-page image
|
||||
std::vector<VImage> pages;
|
||||
pages.reserve(nPages);
|
||||
|
||||
// Rearrange the tall image into a vertical grid
|
||||
image = image.grid(*pageHeight, nPages, 1);
|
||||
|
||||
// Do the embed on the wide image
|
||||
image = image.embed(0, top, image.width(), height, VImage::option()
|
||||
->set("extend", extendWith)
|
||||
->set("background", background));
|
||||
|
||||
// Split the wide image into frames
|
||||
for (int i = 0; i < nPages; i++) {
|
||||
pages.push_back(
|
||||
image.extract_area(width * i, 0, width, height));
|
||||
}
|
||||
|
||||
// Reassemble the frames into a tall, thin image
|
||||
VImage assembled = VImage::arrayjoin(pages,
|
||||
VImage::option()->set("across", 1));
|
||||
|
||||
// Update the page height
|
||||
*pageHeight = height;
|
||||
|
||||
return assembled;
|
||||
} else {
|
||||
std::vector<VImage> pages;
|
||||
pages.reserve(nPages);
|
||||
|
||||
// Split the image into frames
|
||||
for (int i = 0; i < nPages; i++) {
|
||||
pages.push_back(
|
||||
image.extract_area(0, *pageHeight * i, image.width(), *pageHeight));
|
||||
}
|
||||
|
||||
// Embed each frame in the target size
|
||||
for (int i = 0; i < nPages; i++) {
|
||||
pages[i] = pages[i].embed(left, top, width, height, VImage::option()
|
||||
->set("extend", extendWith)
|
||||
->set("background", background));
|
||||
}
|
||||
|
||||
// Reassemble the frames into a tall, thin image
|
||||
VImage assembled = VImage::arrayjoin(pages,
|
||||
VImage::option()->set("across", 1));
|
||||
|
||||
// Update the page height
|
||||
*pageHeight = height;
|
||||
|
||||
return assembled;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sharp
|
125
node_modules/sharp/src/operations.h
generated
vendored
Normal file
125
node_modules/sharp/src/operations.h
generated
vendored
Normal file
@@ -0,0 +1,125 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef SRC_OPERATIONS_H_
|
||||
#define SRC_OPERATIONS_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <vips/vips8>
|
||||
|
||||
using vips::VImage;
|
||||
|
||||
namespace sharp {
|
||||
|
||||
/*
|
||||
* Tint an image using the provided RGB.
|
||||
*/
|
||||
VImage Tint(VImage image, std::vector<double> const tint);
|
||||
|
||||
/*
|
||||
* Stretch luminance to cover full dynamic range.
|
||||
*/
|
||||
VImage Normalise(VImage image, int const lower, int const upper);
|
||||
|
||||
/*
|
||||
* Contrast limiting adapative histogram equalization (CLAHE)
|
||||
*/
|
||||
VImage Clahe(VImage image, int const width, int const height, int const maxSlope);
|
||||
|
||||
/*
|
||||
* Gamma encoding/decoding
|
||||
*/
|
||||
VImage Gamma(VImage image, double const exponent);
|
||||
|
||||
/*
|
||||
* Flatten image to remove alpha channel
|
||||
*/
|
||||
VImage Flatten(VImage image, std::vector<double> flattenBackground);
|
||||
|
||||
/*
|
||||
* Produce the "negative" of the image.
|
||||
*/
|
||||
VImage Negate(VImage image, bool const negateAlpha);
|
||||
|
||||
/*
|
||||
* Gaussian blur. Use sigma of -1.0 for fast blur.
|
||||
*/
|
||||
VImage Blur(VImage image, double const sigma, VipsPrecision precision, double const minAmpl);
|
||||
|
||||
/*
|
||||
* Convolution with a kernel.
|
||||
*/
|
||||
VImage Convolve(VImage image, int const width, int const height,
|
||||
double const scale, double const offset, std::vector<double> const &kernel_v);
|
||||
|
||||
/*
|
||||
* Sharpen flat and jagged areas. Use sigma of -1.0 for fast sharpen.
|
||||
*/
|
||||
VImage Sharpen(VImage image, double const sigma, double const m1, double const m2,
|
||||
double const x1, double const y2, double const y3);
|
||||
|
||||
/*
|
||||
Threshold an image
|
||||
*/
|
||||
VImage Threshold(VImage image, double const threshold, bool const thresholdColor);
|
||||
|
||||
/*
|
||||
Perform boolean/bitwise operation on image color channels - results in one channel image
|
||||
*/
|
||||
VImage Bandbool(VImage image, VipsOperationBoolean const boolean);
|
||||
|
||||
/*
|
||||
Perform bitwise boolean operation between images
|
||||
*/
|
||||
VImage Boolean(VImage image, VImage imageR, VipsOperationBoolean const boolean);
|
||||
|
||||
/*
|
||||
Trim an image
|
||||
*/
|
||||
VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt);
|
||||
|
||||
/*
|
||||
* Linear adjustment (a * in + b)
|
||||
*/
|
||||
VImage Linear(VImage image, std::vector<double> const a, std::vector<double> const b);
|
||||
|
||||
/*
|
||||
* Unflatten
|
||||
*/
|
||||
VImage Unflatten(VImage image);
|
||||
|
||||
/*
|
||||
* Recomb with a Matrix of the given bands/channel size.
|
||||
* Eg. RGB will be a 3x3 matrix.
|
||||
*/
|
||||
VImage Recomb(VImage image, std::vector<double> const &matrix);
|
||||
|
||||
/*
|
||||
* Modulate brightness, saturation, hue and lightness
|
||||
*/
|
||||
VImage Modulate(VImage image, double const brightness, double const saturation,
|
||||
int const hue, double const lightness);
|
||||
|
||||
/*
|
||||
* Ensure the image is in a given colourspace
|
||||
*/
|
||||
VImage EnsureColourspace(VImage image, VipsInterpretation colourspace);
|
||||
|
||||
/*
|
||||
* Split and crop each frame, reassemble, and update pageHeight.
|
||||
*/
|
||||
VImage CropMultiPage(VImage image, int left, int top, int width, int height,
|
||||
int nPages, int *pageHeight);
|
||||
|
||||
/*
|
||||
* Split into frames, embed each frame, reassemble, and update pageHeight.
|
||||
*/
|
||||
VImage EmbedMultiPage(VImage image, int left, int top, int width, int height,
|
||||
VipsExtend extendWith, std::vector<double> background, int nPages, int *pageHeight);
|
||||
|
||||
} // namespace sharp
|
||||
|
||||
#endif // SRC_OPERATIONS_H_
|
1758
node_modules/sharp/src/pipeline.cc
generated
vendored
Normal file
1758
node_modules/sharp/src/pipeline.cc
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
393
node_modules/sharp/src/pipeline.h
generated
vendored
Normal file
393
node_modules/sharp/src/pipeline.h
generated
vendored
Normal file
@@ -0,0 +1,393 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef SRC_PIPELINE_H_
|
||||
#define SRC_PIPELINE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <napi.h>
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "./common.h"
|
||||
|
||||
Napi::Value pipeline(const Napi::CallbackInfo& info);
|
||||
|
||||
struct Composite {
|
||||
sharp::InputDescriptor *input;
|
||||
VipsBlendMode mode;
|
||||
int gravity;
|
||||
int left;
|
||||
int top;
|
||||
bool hasOffset;
|
||||
bool tile;
|
||||
bool premultiplied;
|
||||
|
||||
Composite():
|
||||
input(nullptr),
|
||||
mode(VIPS_BLEND_MODE_OVER),
|
||||
gravity(0),
|
||||
left(0),
|
||||
top(0),
|
||||
hasOffset(false),
|
||||
tile(false),
|
||||
premultiplied(false) {}
|
||||
};
|
||||
|
||||
struct PipelineBaton {
|
||||
sharp::InputDescriptor *input;
|
||||
std::string formatOut;
|
||||
std::string fileOut;
|
||||
void *bufferOut;
|
||||
size_t bufferOutLength;
|
||||
int pageHeightOut;
|
||||
int pagesOut;
|
||||
std::vector<Composite *> composite;
|
||||
std::vector<sharp::InputDescriptor *> joinChannelIn;
|
||||
int topOffsetPre;
|
||||
int leftOffsetPre;
|
||||
int widthPre;
|
||||
int heightPre;
|
||||
int topOffsetPost;
|
||||
int leftOffsetPost;
|
||||
int widthPost;
|
||||
int heightPost;
|
||||
int width;
|
||||
int height;
|
||||
int channels;
|
||||
VipsKernel kernel;
|
||||
sharp::Canvas canvas;
|
||||
int position;
|
||||
std::vector<double> resizeBackground;
|
||||
bool hasCropOffset;
|
||||
int cropOffsetLeft;
|
||||
int cropOffsetTop;
|
||||
bool hasAttentionCenter;
|
||||
int attentionX;
|
||||
int attentionY;
|
||||
bool premultiplied;
|
||||
bool tileCentre;
|
||||
bool fastShrinkOnLoad;
|
||||
std::vector<double> tint;
|
||||
bool flatten;
|
||||
std::vector<double> flattenBackground;
|
||||
bool unflatten;
|
||||
bool negate;
|
||||
bool negateAlpha;
|
||||
double blurSigma;
|
||||
VipsPrecision precision;
|
||||
double minAmpl;
|
||||
double brightness;
|
||||
double saturation;
|
||||
int hue;
|
||||
double lightness;
|
||||
int medianSize;
|
||||
double sharpenSigma;
|
||||
double sharpenM1;
|
||||
double sharpenM2;
|
||||
double sharpenX1;
|
||||
double sharpenY2;
|
||||
double sharpenY3;
|
||||
int threshold;
|
||||
bool thresholdGrayscale;
|
||||
std::vector<double> trimBackground;
|
||||
double trimThreshold;
|
||||
bool trimLineArt;
|
||||
int trimOffsetLeft;
|
||||
int trimOffsetTop;
|
||||
std::vector<double> linearA;
|
||||
std::vector<double> linearB;
|
||||
double gamma;
|
||||
double gammaOut;
|
||||
bool greyscale;
|
||||
bool normalise;
|
||||
int normaliseLower;
|
||||
int normaliseUpper;
|
||||
int claheWidth;
|
||||
int claheHeight;
|
||||
int claheMaxSlope;
|
||||
bool useExifOrientation;
|
||||
int angle;
|
||||
double rotationAngle;
|
||||
std::vector<double> rotationBackground;
|
||||
bool rotateBeforePreExtract;
|
||||
bool flip;
|
||||
bool flop;
|
||||
int extendTop;
|
||||
int extendBottom;
|
||||
int extendLeft;
|
||||
int extendRight;
|
||||
std::vector<double> extendBackground;
|
||||
VipsExtend extendWith;
|
||||
bool withoutEnlargement;
|
||||
bool withoutReduction;
|
||||
std::vector<double> affineMatrix;
|
||||
std::vector<double> affineBackground;
|
||||
double affineIdx;
|
||||
double affineIdy;
|
||||
double affineOdx;
|
||||
double affineOdy;
|
||||
std::string affineInterpolator;
|
||||
int jpegQuality;
|
||||
bool jpegProgressive;
|
||||
std::string jpegChromaSubsampling;
|
||||
bool jpegTrellisQuantisation;
|
||||
int jpegQuantisationTable;
|
||||
bool jpegOvershootDeringing;
|
||||
bool jpegOptimiseScans;
|
||||
bool jpegOptimiseCoding;
|
||||
bool pngProgressive;
|
||||
int pngCompressionLevel;
|
||||
bool pngAdaptiveFiltering;
|
||||
bool pngPalette;
|
||||
int pngQuality;
|
||||
int pngEffort;
|
||||
int pngBitdepth;
|
||||
double pngDither;
|
||||
int jp2Quality;
|
||||
bool jp2Lossless;
|
||||
int jp2TileHeight;
|
||||
int jp2TileWidth;
|
||||
std::string jp2ChromaSubsampling;
|
||||
int webpQuality;
|
||||
int webpAlphaQuality;
|
||||
bool webpNearLossless;
|
||||
bool webpLossless;
|
||||
bool webpSmartSubsample;
|
||||
VipsForeignWebpPreset webpPreset;
|
||||
int webpEffort;
|
||||
bool webpMinSize;
|
||||
bool webpMixed;
|
||||
int gifBitdepth;
|
||||
int gifEffort;
|
||||
double gifDither;
|
||||
double gifInterFrameMaxError;
|
||||
double gifInterPaletteMaxError;
|
||||
bool gifReuse;
|
||||
bool gifProgressive;
|
||||
int tiffQuality;
|
||||
VipsForeignTiffCompression tiffCompression;
|
||||
VipsForeignTiffPredictor tiffPredictor;
|
||||
bool tiffPyramid;
|
||||
int tiffBitdepth;
|
||||
bool tiffMiniswhite;
|
||||
bool tiffTile;
|
||||
int tiffTileHeight;
|
||||
int tiffTileWidth;
|
||||
double tiffXres;
|
||||
double tiffYres;
|
||||
VipsForeignTiffResunit tiffResolutionUnit;
|
||||
int heifQuality;
|
||||
VipsForeignHeifCompression heifCompression;
|
||||
int heifEffort;
|
||||
std::string heifChromaSubsampling;
|
||||
bool heifLossless;
|
||||
int heifBitdepth;
|
||||
double jxlDistance;
|
||||
int jxlDecodingTier;
|
||||
int jxlEffort;
|
||||
bool jxlLossless;
|
||||
VipsBandFormat rawDepth;
|
||||
std::string err;
|
||||
int keepMetadata;
|
||||
int withMetadataOrientation;
|
||||
double withMetadataDensity;
|
||||
std::string withIccProfile;
|
||||
std::unordered_map<std::string, std::string> withExif;
|
||||
bool withExifMerge;
|
||||
int timeoutSeconds;
|
||||
std::vector<double> convKernel;
|
||||
int convKernelWidth;
|
||||
int convKernelHeight;
|
||||
double convKernelScale;
|
||||
double convKernelOffset;
|
||||
sharp::InputDescriptor *boolean;
|
||||
VipsOperationBoolean booleanOp;
|
||||
VipsOperationBoolean bandBoolOp;
|
||||
int extractChannel;
|
||||
bool removeAlpha;
|
||||
double ensureAlpha;
|
||||
VipsInterpretation colourspacePipeline;
|
||||
VipsInterpretation colourspace;
|
||||
std::vector<int> delay;
|
||||
int loop;
|
||||
int tileSize;
|
||||
int tileOverlap;
|
||||
VipsForeignDzContainer tileContainer;
|
||||
VipsForeignDzLayout tileLayout;
|
||||
std::string tileFormat;
|
||||
int tileAngle;
|
||||
std::vector<double> tileBackground;
|
||||
int tileSkipBlanks;
|
||||
VipsForeignDzDepth tileDepth;
|
||||
std::string tileId;
|
||||
std::string tileBasename;
|
||||
std::vector<double> recombMatrix;
|
||||
|
||||
PipelineBaton():
|
||||
input(nullptr),
|
||||
bufferOutLength(0),
|
||||
pageHeightOut(0),
|
||||
pagesOut(0),
|
||||
topOffsetPre(-1),
|
||||
topOffsetPost(-1),
|
||||
channels(0),
|
||||
kernel(VIPS_KERNEL_LANCZOS3),
|
||||
canvas(sharp::Canvas::CROP),
|
||||
position(0),
|
||||
resizeBackground{ 0.0, 0.0, 0.0, 255.0 },
|
||||
hasCropOffset(false),
|
||||
cropOffsetLeft(0),
|
||||
cropOffsetTop(0),
|
||||
hasAttentionCenter(false),
|
||||
attentionX(0),
|
||||
attentionY(0),
|
||||
premultiplied(false),
|
||||
tint{ -1.0, 0.0, 0.0, 0.0 },
|
||||
flatten(false),
|
||||
flattenBackground{ 0.0, 0.0, 0.0 },
|
||||
unflatten(false),
|
||||
negate(false),
|
||||
negateAlpha(true),
|
||||
blurSigma(0.0),
|
||||
brightness(1.0),
|
||||
saturation(1.0),
|
||||
hue(0),
|
||||
lightness(0),
|
||||
medianSize(0),
|
||||
sharpenSigma(0.0),
|
||||
sharpenM1(1.0),
|
||||
sharpenM2(2.0),
|
||||
sharpenX1(2.0),
|
||||
sharpenY2(10.0),
|
||||
sharpenY3(20.0),
|
||||
threshold(0),
|
||||
thresholdGrayscale(true),
|
||||
trimBackground{},
|
||||
trimThreshold(-1.0),
|
||||
trimLineArt(false),
|
||||
trimOffsetLeft(0),
|
||||
trimOffsetTop(0),
|
||||
linearA{},
|
||||
linearB{},
|
||||
gamma(0.0),
|
||||
greyscale(false),
|
||||
normalise(false),
|
||||
normaliseLower(1),
|
||||
normaliseUpper(99),
|
||||
claheWidth(0),
|
||||
claheHeight(0),
|
||||
claheMaxSlope(3),
|
||||
useExifOrientation(false),
|
||||
angle(0),
|
||||
rotationAngle(0.0),
|
||||
rotationBackground{ 0.0, 0.0, 0.0, 255.0 },
|
||||
flip(false),
|
||||
flop(false),
|
||||
extendTop(0),
|
||||
extendBottom(0),
|
||||
extendLeft(0),
|
||||
extendRight(0),
|
||||
extendBackground{ 0.0, 0.0, 0.0, 255.0 },
|
||||
extendWith(VIPS_EXTEND_BACKGROUND),
|
||||
withoutEnlargement(false),
|
||||
withoutReduction(false),
|
||||
affineMatrix{ 1.0, 0.0, 0.0, 1.0 },
|
||||
affineBackground{ 0.0, 0.0, 0.0, 255.0 },
|
||||
affineIdx(0),
|
||||
affineIdy(0),
|
||||
affineOdx(0),
|
||||
affineOdy(0),
|
||||
affineInterpolator("bicubic"),
|
||||
jpegQuality(80),
|
||||
jpegProgressive(false),
|
||||
jpegChromaSubsampling("4:2:0"),
|
||||
jpegTrellisQuantisation(false),
|
||||
jpegQuantisationTable(0),
|
||||
jpegOvershootDeringing(false),
|
||||
jpegOptimiseScans(false),
|
||||
jpegOptimiseCoding(true),
|
||||
pngProgressive(false),
|
||||
pngCompressionLevel(6),
|
||||
pngAdaptiveFiltering(false),
|
||||
pngPalette(false),
|
||||
pngQuality(100),
|
||||
pngEffort(7),
|
||||
pngBitdepth(8),
|
||||
pngDither(1.0),
|
||||
jp2Quality(80),
|
||||
jp2Lossless(false),
|
||||
jp2TileHeight(512),
|
||||
jp2TileWidth(512),
|
||||
jp2ChromaSubsampling("4:4:4"),
|
||||
webpQuality(80),
|
||||
webpAlphaQuality(100),
|
||||
webpNearLossless(false),
|
||||
webpLossless(false),
|
||||
webpSmartSubsample(false),
|
||||
webpPreset(VIPS_FOREIGN_WEBP_PRESET_DEFAULT),
|
||||
webpEffort(4),
|
||||
webpMinSize(false),
|
||||
webpMixed(false),
|
||||
gifBitdepth(8),
|
||||
gifEffort(7),
|
||||
gifDither(1.0),
|
||||
gifInterFrameMaxError(0.0),
|
||||
gifInterPaletteMaxError(3.0),
|
||||
gifReuse(true),
|
||||
gifProgressive(false),
|
||||
tiffQuality(80),
|
||||
tiffCompression(VIPS_FOREIGN_TIFF_COMPRESSION_JPEG),
|
||||
tiffPredictor(VIPS_FOREIGN_TIFF_PREDICTOR_HORIZONTAL),
|
||||
tiffPyramid(false),
|
||||
tiffBitdepth(8),
|
||||
tiffMiniswhite(false),
|
||||
tiffTile(false),
|
||||
tiffTileHeight(256),
|
||||
tiffTileWidth(256),
|
||||
tiffXres(1.0),
|
||||
tiffYres(1.0),
|
||||
tiffResolutionUnit(VIPS_FOREIGN_TIFF_RESUNIT_INCH),
|
||||
heifQuality(50),
|
||||
heifCompression(VIPS_FOREIGN_HEIF_COMPRESSION_AV1),
|
||||
heifEffort(4),
|
||||
heifChromaSubsampling("4:4:4"),
|
||||
heifLossless(false),
|
||||
heifBitdepth(8),
|
||||
jxlDistance(1.0),
|
||||
jxlDecodingTier(0),
|
||||
jxlEffort(7),
|
||||
jxlLossless(false),
|
||||
rawDepth(VIPS_FORMAT_UCHAR),
|
||||
keepMetadata(0),
|
||||
withMetadataOrientation(-1),
|
||||
withMetadataDensity(0.0),
|
||||
withExifMerge(true),
|
||||
timeoutSeconds(0),
|
||||
convKernelWidth(0),
|
||||
convKernelHeight(0),
|
||||
convKernelScale(0.0),
|
||||
convKernelOffset(0.0),
|
||||
boolean(nullptr),
|
||||
booleanOp(VIPS_OPERATION_BOOLEAN_LAST),
|
||||
bandBoolOp(VIPS_OPERATION_BOOLEAN_LAST),
|
||||
extractChannel(-1),
|
||||
removeAlpha(false),
|
||||
ensureAlpha(-1.0),
|
||||
colourspacePipeline(VIPS_INTERPRETATION_LAST),
|
||||
colourspace(VIPS_INTERPRETATION_LAST),
|
||||
loop(-1),
|
||||
tileSize(256),
|
||||
tileOverlap(0),
|
||||
tileContainer(VIPS_FOREIGN_DZ_CONTAINER_FS),
|
||||
tileLayout(VIPS_FOREIGN_DZ_LAYOUT_DZ),
|
||||
tileAngle(0),
|
||||
tileBackground{ 255.0, 255.0, 255.0, 255.0 },
|
||||
tileSkipBlanks(-1),
|
||||
tileDepth(VIPS_FOREIGN_DZ_DEPTH_LAST) {}
|
||||
};
|
||||
|
||||
#endif // SRC_PIPELINE_H_
|
40
node_modules/sharp/src/sharp.cc
generated
vendored
Normal file
40
node_modules/sharp/src/sharp.cc
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <mutex> // NOLINT(build/c++11)
|
||||
|
||||
#include <napi.h>
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "common.h"
|
||||
#include "metadata.h"
|
||||
#include "pipeline.h"
|
||||
#include "utilities.h"
|
||||
#include "stats.h"
|
||||
|
||||
Napi::Object init(Napi::Env env, Napi::Object exports) {
|
||||
static std::once_flag sharp_vips_init_once;
|
||||
std::call_once(sharp_vips_init_once, []() {
|
||||
vips_init("sharp");
|
||||
});
|
||||
|
||||
g_log_set_handler("VIPS", static_cast<GLogLevelFlags>(G_LOG_LEVEL_WARNING),
|
||||
static_cast<GLogFunc>(sharp::VipsWarningCallback), nullptr);
|
||||
|
||||
// Methods available to JavaScript
|
||||
exports.Set("metadata", Napi::Function::New(env, metadata));
|
||||
exports.Set("pipeline", Napi::Function::New(env, pipeline));
|
||||
exports.Set("cache", Napi::Function::New(env, cache));
|
||||
exports.Set("concurrency", Napi::Function::New(env, concurrency));
|
||||
exports.Set("counters", Napi::Function::New(env, counters));
|
||||
exports.Set("simd", Napi::Function::New(env, simd));
|
||||
exports.Set("libvipsVersion", Napi::Function::New(env, libvipsVersion));
|
||||
exports.Set("format", Napi::Function::New(env, format));
|
||||
exports.Set("block", Napi::Function::New(env, block));
|
||||
exports.Set("_maxColourDistance", Napi::Function::New(env, _maxColourDistance));
|
||||
exports.Set("_isUsingJemalloc", Napi::Function::New(env, _isUsingJemalloc));
|
||||
exports.Set("stats", Napi::Function::New(env, stats));
|
||||
return exports;
|
||||
}
|
||||
|
||||
NODE_API_MODULE(sharp, init)
|
183
node_modules/sharp/src/stats.cc
generated
vendored
Normal file
183
node_modules/sharp/src/stats.cc
generated
vendored
Normal file
@@ -0,0 +1,183 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#include <napi.h>
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "common.h"
|
||||
#include "stats.h"
|
||||
|
||||
class StatsWorker : public Napi::AsyncWorker {
|
||||
public:
|
||||
StatsWorker(Napi::Function callback, StatsBaton *baton, Napi::Function debuglog) :
|
||||
Napi::AsyncWorker(callback), baton(baton), debuglog(Napi::Persistent(debuglog)) {}
|
||||
~StatsWorker() {}
|
||||
|
||||
const int STAT_MIN_INDEX = 0;
|
||||
const int STAT_MAX_INDEX = 1;
|
||||
const int STAT_SUM_INDEX = 2;
|
||||
const int STAT_SQ_SUM_INDEX = 3;
|
||||
const int STAT_MEAN_INDEX = 4;
|
||||
const int STAT_STDEV_INDEX = 5;
|
||||
const int STAT_MINX_INDEX = 6;
|
||||
const int STAT_MINY_INDEX = 7;
|
||||
const int STAT_MAXX_INDEX = 8;
|
||||
const int STAT_MAXY_INDEX = 9;
|
||||
|
||||
void Execute() {
|
||||
// Decrement queued task counter
|
||||
sharp::counterQueue--;
|
||||
|
||||
vips::VImage image;
|
||||
sharp::ImageType imageType = sharp::ImageType::UNKNOWN;
|
||||
try {
|
||||
std::tie(image, imageType) = OpenInput(baton->input);
|
||||
} catch (vips::VError const &err) {
|
||||
(baton->err).append(err.what());
|
||||
}
|
||||
if (imageType != sharp::ImageType::UNKNOWN) {
|
||||
try {
|
||||
vips::VImage stats = image.stats();
|
||||
int const bands = image.bands();
|
||||
for (int b = 1; b <= bands; b++) {
|
||||
ChannelStats cStats(
|
||||
static_cast<int>(stats.getpoint(STAT_MIN_INDEX, b).front()),
|
||||
static_cast<int>(stats.getpoint(STAT_MAX_INDEX, b).front()),
|
||||
stats.getpoint(STAT_SUM_INDEX, b).front(),
|
||||
stats.getpoint(STAT_SQ_SUM_INDEX, b).front(),
|
||||
stats.getpoint(STAT_MEAN_INDEX, b).front(),
|
||||
stats.getpoint(STAT_STDEV_INDEX, b).front(),
|
||||
static_cast<int>(stats.getpoint(STAT_MINX_INDEX, b).front()),
|
||||
static_cast<int>(stats.getpoint(STAT_MINY_INDEX, b).front()),
|
||||
static_cast<int>(stats.getpoint(STAT_MAXX_INDEX, b).front()),
|
||||
static_cast<int>(stats.getpoint(STAT_MAXY_INDEX, b).front()));
|
||||
baton->channelStats.push_back(cStats);
|
||||
}
|
||||
// Image is not opaque when alpha layer is present and contains a non-mamixa value
|
||||
if (sharp::HasAlpha(image)) {
|
||||
double const minAlpha = static_cast<double>(stats.getpoint(STAT_MIN_INDEX, bands).front());
|
||||
if (minAlpha != sharp::MaximumImageAlpha(image.interpretation())) {
|
||||
baton->isOpaque = false;
|
||||
}
|
||||
}
|
||||
// Convert to greyscale
|
||||
vips::VImage greyscale = image.colourspace(VIPS_INTERPRETATION_B_W)[0];
|
||||
// Estimate entropy via histogram of greyscale value frequency
|
||||
baton->entropy = std::abs(greyscale.hist_find().hist_entropy());
|
||||
// Estimate sharpness via standard deviation of greyscale laplacian
|
||||
if (image.width() > 1 || image.height() > 1) {
|
||||
VImage laplacian = VImage::new_matrixv(3, 3,
|
||||
0.0, 1.0, 0.0,
|
||||
1.0, -4.0, 1.0,
|
||||
0.0, 1.0, 0.0);
|
||||
laplacian.set("scale", 9.0);
|
||||
baton->sharpness = greyscale.conv(laplacian).deviate();
|
||||
}
|
||||
// Most dominant sRGB colour via 4096-bin 3D histogram
|
||||
vips::VImage hist = sharp::RemoveAlpha(image)
|
||||
.colourspace(VIPS_INTERPRETATION_sRGB)
|
||||
.hist_find_ndim(VImage::option()->set("bins", 16));
|
||||
std::complex<double> maxpos = hist.maxpos();
|
||||
int const dx = static_cast<int>(std::real(maxpos));
|
||||
int const dy = static_cast<int>(std::imag(maxpos));
|
||||
std::vector<double> pel = hist(dx, dy);
|
||||
int const dz = std::distance(pel.begin(), std::find(pel.begin(), pel.end(), hist.max()));
|
||||
baton->dominantRed = dx * 16 + 8;
|
||||
baton->dominantGreen = dy * 16 + 8;
|
||||
baton->dominantBlue = dz * 16 + 8;
|
||||
} catch (vips::VError const &err) {
|
||||
(baton->err).append(err.what());
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up
|
||||
vips_error_clear();
|
||||
vips_thread_shutdown();
|
||||
}
|
||||
|
||||
void OnOK() {
|
||||
Napi::Env env = Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
// Handle warnings
|
||||
std::string warning = sharp::VipsWarningPop();
|
||||
while (!warning.empty()) {
|
||||
debuglog.Call(Receiver().Value(), { Napi::String::New(env, warning) });
|
||||
warning = sharp::VipsWarningPop();
|
||||
}
|
||||
|
||||
if (baton->err.empty()) {
|
||||
// Stats Object
|
||||
Napi::Object info = Napi::Object::New(env);
|
||||
Napi::Array channels = Napi::Array::New(env);
|
||||
|
||||
std::vector<ChannelStats>::iterator it;
|
||||
int i = 0;
|
||||
for (it = baton->channelStats.begin(); it < baton->channelStats.end(); it++, i++) {
|
||||
Napi::Object channelStat = Napi::Object::New(env);
|
||||
channelStat.Set("min", it->min);
|
||||
channelStat.Set("max", it->max);
|
||||
channelStat.Set("sum", it->sum);
|
||||
channelStat.Set("squaresSum", it->squaresSum);
|
||||
channelStat.Set("mean", it->mean);
|
||||
channelStat.Set("stdev", it->stdev);
|
||||
channelStat.Set("minX", it->minX);
|
||||
channelStat.Set("minY", it->minY);
|
||||
channelStat.Set("maxX", it->maxX);
|
||||
channelStat.Set("maxY", it->maxY);
|
||||
channels.Set(i, channelStat);
|
||||
}
|
||||
|
||||
info.Set("channels", channels);
|
||||
info.Set("isOpaque", baton->isOpaque);
|
||||
info.Set("entropy", baton->entropy);
|
||||
info.Set("sharpness", baton->sharpness);
|
||||
Napi::Object dominant = Napi::Object::New(env);
|
||||
dominant.Set("r", baton->dominantRed);
|
||||
dominant.Set("g", baton->dominantGreen);
|
||||
dominant.Set("b", baton->dominantBlue);
|
||||
info.Set("dominant", dominant);
|
||||
Callback().Call(Receiver().Value(), { env.Null(), info });
|
||||
} else {
|
||||
Callback().Call(Receiver().Value(), { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() });
|
||||
}
|
||||
|
||||
delete baton->input;
|
||||
delete baton;
|
||||
}
|
||||
|
||||
private:
|
||||
StatsBaton* baton;
|
||||
Napi::FunctionReference debuglog;
|
||||
};
|
||||
|
||||
/*
|
||||
stats(options, callback)
|
||||
*/
|
||||
Napi::Value stats(const Napi::CallbackInfo& info) {
|
||||
// V8 objects are converted to non-V8 types held in the baton struct
|
||||
StatsBaton *baton = new StatsBaton;
|
||||
Napi::Object options = info[size_t(0)].As<Napi::Object>();
|
||||
|
||||
// Input
|
||||
baton->input = sharp::CreateInputDescriptor(options.Get("input").As<Napi::Object>());
|
||||
baton->input->access = VIPS_ACCESS_RANDOM;
|
||||
|
||||
// Function to notify of libvips warnings
|
||||
Napi::Function debuglog = options.Get("debuglog").As<Napi::Function>();
|
||||
|
||||
// Join queue for worker thread
|
||||
Napi::Function callback = info[size_t(1)].As<Napi::Function>();
|
||||
StatsWorker *worker = new StatsWorker(callback, baton, debuglog);
|
||||
worker->Receiver().Set("options", options);
|
||||
worker->Queue();
|
||||
|
||||
// Increment queued task counter
|
||||
sharp::counterQueue++;
|
||||
|
||||
return info.Env().Undefined();
|
||||
}
|
59
node_modules/sharp/src/stats.h
generated
vendored
Normal file
59
node_modules/sharp/src/stats.h
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef SRC_STATS_H_
|
||||
#define SRC_STATS_H_
|
||||
|
||||
#include <string>
|
||||
#include <napi.h>
|
||||
|
||||
#include "./common.h"
|
||||
|
||||
struct ChannelStats {
|
||||
// stats per channel
|
||||
int min;
|
||||
int max;
|
||||
double sum;
|
||||
double squaresSum;
|
||||
double mean;
|
||||
double stdev;
|
||||
int minX;
|
||||
int minY;
|
||||
int maxX;
|
||||
int maxY;
|
||||
|
||||
ChannelStats(int minVal, int maxVal, double sumVal, double squaresSumVal,
|
||||
double meanVal, double stdevVal, int minXVal, int minYVal, int maxXVal, int maxYVal):
|
||||
min(minVal), max(maxVal), sum(sumVal), squaresSum(squaresSumVal),
|
||||
mean(meanVal), stdev(stdevVal), minX(minXVal), minY(minYVal), maxX(maxXVal), maxY(maxYVal) {}
|
||||
};
|
||||
|
||||
struct StatsBaton {
|
||||
// Input
|
||||
sharp::InputDescriptor *input;
|
||||
|
||||
// Output
|
||||
std::vector<ChannelStats> channelStats;
|
||||
bool isOpaque;
|
||||
double entropy;
|
||||
double sharpness;
|
||||
int dominantRed;
|
||||
int dominantGreen;
|
||||
int dominantBlue;
|
||||
|
||||
std::string err;
|
||||
|
||||
StatsBaton():
|
||||
input(nullptr),
|
||||
isOpaque(true),
|
||||
entropy(0.0),
|
||||
sharpness(0.0),
|
||||
dominantRed(0),
|
||||
dominantGreen(0),
|
||||
dominantBlue(0)
|
||||
{}
|
||||
};
|
||||
|
||||
Napi::Value stats(const Napi::CallbackInfo& info);
|
||||
|
||||
#endif // SRC_STATS_H_
|
269
node_modules/sharp/src/utilities.cc
generated
vendored
Normal file
269
node_modules/sharp/src/utilities.cc
generated
vendored
Normal file
@@ -0,0 +1,269 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
|
||||
#include <napi.h>
|
||||
#include <vips/vips8>
|
||||
#include <vips/vector.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "operations.h"
|
||||
#include "utilities.h"
|
||||
|
||||
/*
|
||||
Get and set cache limits
|
||||
*/
|
||||
Napi::Value cache(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
// Set memory limit
|
||||
if (info[size_t(0)].IsNumber()) {
|
||||
vips_cache_set_max_mem(info[size_t(0)].As<Napi::Number>().Int32Value() * 1048576);
|
||||
}
|
||||
// Set file limit
|
||||
if (info[size_t(1)].IsNumber()) {
|
||||
vips_cache_set_max_files(info[size_t(1)].As<Napi::Number>().Int32Value());
|
||||
}
|
||||
// Set items limit
|
||||
if (info[size_t(2)].IsNumber()) {
|
||||
vips_cache_set_max(info[size_t(2)].As<Napi::Number>().Int32Value());
|
||||
}
|
||||
|
||||
// Get memory stats
|
||||
Napi::Object memory = Napi::Object::New(env);
|
||||
memory.Set("current", round(vips_tracked_get_mem() / 1048576));
|
||||
memory.Set("high", round(vips_tracked_get_mem_highwater() / 1048576));
|
||||
memory.Set("max", round(vips_cache_get_max_mem() / 1048576));
|
||||
// Get file stats
|
||||
Napi::Object files = Napi::Object::New(env);
|
||||
files.Set("current", vips_tracked_get_files());
|
||||
files.Set("max", vips_cache_get_max_files());
|
||||
|
||||
// Get item stats
|
||||
Napi::Object items = Napi::Object::New(env);
|
||||
items.Set("current", vips_cache_get_size());
|
||||
items.Set("max", vips_cache_get_max());
|
||||
|
||||
Napi::Object cache = Napi::Object::New(env);
|
||||
cache.Set("memory", memory);
|
||||
cache.Set("files", files);
|
||||
cache.Set("items", items);
|
||||
return cache;
|
||||
}
|
||||
|
||||
/*
|
||||
Get and set size of thread pool
|
||||
*/
|
||||
Napi::Value concurrency(const Napi::CallbackInfo& info) {
|
||||
// Set concurrency
|
||||
if (info[size_t(0)].IsNumber()) {
|
||||
vips_concurrency_set(info[size_t(0)].As<Napi::Number>().Int32Value());
|
||||
}
|
||||
// Get concurrency
|
||||
return Napi::Number::New(info.Env(), vips_concurrency_get());
|
||||
}
|
||||
|
||||
/*
|
||||
Get internal counters (queued tasks, processing tasks)
|
||||
*/
|
||||
Napi::Value counters(const Napi::CallbackInfo& info) {
|
||||
Napi::Object counters = Napi::Object::New(info.Env());
|
||||
counters.Set("queue", static_cast<int>(sharp::counterQueue));
|
||||
counters.Set("process", static_cast<int>(sharp::counterProcess));
|
||||
return counters;
|
||||
}
|
||||
|
||||
/*
|
||||
Get and set use of SIMD vector unit instructions
|
||||
*/
|
||||
Napi::Value simd(const Napi::CallbackInfo& info) {
|
||||
// Set state
|
||||
if (info[size_t(0)].IsBoolean()) {
|
||||
vips_vector_set_enabled(info[size_t(0)].As<Napi::Boolean>().Value());
|
||||
}
|
||||
// Get state
|
||||
return Napi::Boolean::New(info.Env(), vips_vector_isenabled());
|
||||
}
|
||||
|
||||
/*
|
||||
Get libvips version
|
||||
*/
|
||||
Napi::Value libvipsVersion(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::Object version = Napi::Object::New(env);
|
||||
|
||||
char semver[9];
|
||||
std::snprintf(semver, sizeof(semver), "%d.%d.%d", vips_version(0), vips_version(1), vips_version(2));
|
||||
version.Set("semver", Napi::String::New(env, semver));
|
||||
#ifdef SHARP_USE_GLOBAL_LIBVIPS
|
||||
version.Set("isGlobal", Napi::Boolean::New(env, true));
|
||||
#else
|
||||
version.Set("isGlobal", Napi::Boolean::New(env, false));
|
||||
#endif
|
||||
#ifdef __EMSCRIPTEN__
|
||||
version.Set("isWasm", Napi::Boolean::New(env, true));
|
||||
#else
|
||||
version.Set("isWasm", Napi::Boolean::New(env, false));
|
||||
#endif
|
||||
return version;
|
||||
}
|
||||
|
||||
/*
|
||||
Get available input/output file/buffer/stream formats
|
||||
*/
|
||||
Napi::Value format(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::Object format = Napi::Object::New(env);
|
||||
for (std::string const f : {
|
||||
"jpeg", "png", "webp", "tiff", "magick", "openslide", "dz",
|
||||
"ppm", "fits", "gif", "svg", "heif", "pdf", "vips", "jp2k", "jxl"
|
||||
}) {
|
||||
// Input
|
||||
const VipsObjectClass *oc = vips_class_find("VipsOperation", (f + "load").c_str());
|
||||
Napi::Boolean hasInputFile = Napi::Boolean::New(env, oc);
|
||||
Napi::Boolean hasInputBuffer =
|
||||
Napi::Boolean::New(env, vips_type_find("VipsOperation", (f + "load_buffer").c_str()));
|
||||
Napi::Object input = Napi::Object::New(env);
|
||||
input.Set("file", hasInputFile);
|
||||
input.Set("buffer", hasInputBuffer);
|
||||
input.Set("stream", hasInputBuffer);
|
||||
if (hasInputFile) {
|
||||
const VipsForeignClass *fc = VIPS_FOREIGN_CLASS(oc);
|
||||
if (fc->suffs) {
|
||||
Napi::Array fileSuffix = Napi::Array::New(env);
|
||||
const char **suffix = fc->suffs;
|
||||
for (int i = 0; *suffix; i++, suffix++) {
|
||||
fileSuffix.Set(i, Napi::String::New(env, *suffix));
|
||||
}
|
||||
input.Set("fileSuffix", fileSuffix);
|
||||
}
|
||||
}
|
||||
// Output
|
||||
Napi::Boolean hasOutputFile =
|
||||
Napi::Boolean::New(env, vips_type_find("VipsOperation", (f + "save").c_str()));
|
||||
Napi::Boolean hasOutputBuffer =
|
||||
Napi::Boolean::New(env, vips_type_find("VipsOperation", (f + "save_buffer").c_str()));
|
||||
Napi::Object output = Napi::Object::New(env);
|
||||
output.Set("file", hasOutputFile);
|
||||
output.Set("buffer", hasOutputBuffer);
|
||||
output.Set("stream", hasOutputBuffer);
|
||||
// Other attributes
|
||||
Napi::Object container = Napi::Object::New(env);
|
||||
container.Set("id", f);
|
||||
container.Set("input", input);
|
||||
container.Set("output", output);
|
||||
// Add to set of formats
|
||||
format.Set(f, container);
|
||||
}
|
||||
|
||||
// Raw, uncompressed data
|
||||
Napi::Boolean supported = Napi::Boolean::New(env, true);
|
||||
Napi::Boolean unsupported = Napi::Boolean::New(env, false);
|
||||
Napi::Object rawInput = Napi::Object::New(env);
|
||||
rawInput.Set("file", unsupported);
|
||||
rawInput.Set("buffer", supported);
|
||||
rawInput.Set("stream", supported);
|
||||
Napi::Object rawOutput = Napi::Object::New(env);
|
||||
rawOutput.Set("file", unsupported);
|
||||
rawOutput.Set("buffer", supported);
|
||||
rawOutput.Set("stream", supported);
|
||||
Napi::Object raw = Napi::Object::New(env);
|
||||
raw.Set("id", "raw");
|
||||
raw.Set("input", rawInput);
|
||||
raw.Set("output", rawOutput);
|
||||
format.Set("raw", raw);
|
||||
|
||||
return format;
|
||||
}
|
||||
|
||||
/*
|
||||
(Un)block libvips operations at runtime.
|
||||
*/
|
||||
void block(const Napi::CallbackInfo& info) {
|
||||
Napi::Array ops = info[size_t(0)].As<Napi::Array>();
|
||||
bool const state = info[size_t(1)].As<Napi::Boolean>().Value();
|
||||
for (unsigned int i = 0; i < ops.Length(); i++) {
|
||||
vips_operation_block_set(ops.Get(i).As<Napi::String>().Utf8Value().c_str(), state);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Synchronous, internal-only method used by some of the functional tests.
|
||||
Calculates the maximum colour distance using the DE2000 algorithm
|
||||
between two images of the same dimensions and number of channels.
|
||||
*/
|
||||
Napi::Value _maxColourDistance(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
// Open input files
|
||||
VImage image1;
|
||||
sharp::ImageType imageType1 = sharp::DetermineImageType(info[size_t(0)].As<Napi::String>().Utf8Value().data());
|
||||
if (imageType1 != sharp::ImageType::UNKNOWN) {
|
||||
try {
|
||||
image1 = VImage::new_from_file(info[size_t(0)].As<Napi::String>().Utf8Value().c_str());
|
||||
} catch (...) {
|
||||
throw Napi::Error::New(env, "Input file 1 has corrupt header");
|
||||
}
|
||||
} else {
|
||||
throw Napi::Error::New(env, "Input file 1 is of an unsupported image format");
|
||||
}
|
||||
VImage image2;
|
||||
sharp::ImageType imageType2 = sharp::DetermineImageType(info[size_t(1)].As<Napi::String>().Utf8Value().data());
|
||||
if (imageType2 != sharp::ImageType::UNKNOWN) {
|
||||
try {
|
||||
image2 = VImage::new_from_file(info[size_t(1)].As<Napi::String>().Utf8Value().c_str());
|
||||
} catch (...) {
|
||||
throw Napi::Error::New(env, "Input file 2 has corrupt header");
|
||||
}
|
||||
} else {
|
||||
throw Napi::Error::New(env, "Input file 2 is of an unsupported image format");
|
||||
}
|
||||
// Ensure same number of channels
|
||||
if (image1.bands() != image2.bands()) {
|
||||
throw Napi::Error::New(env, "mismatchedBands");
|
||||
}
|
||||
// Ensure same dimensions
|
||||
if (image1.width() != image2.width() || image1.height() != image2.height()) {
|
||||
throw Napi::Error::New(env, "mismatchedDimensions");
|
||||
}
|
||||
|
||||
double maxColourDistance;
|
||||
try {
|
||||
// Premultiply and remove alpha
|
||||
if (sharp::HasAlpha(image1)) {
|
||||
image1 = image1.premultiply().extract_band(1, VImage::option()->set("n", image1.bands() - 1));
|
||||
}
|
||||
if (sharp::HasAlpha(image2)) {
|
||||
image2 = image2.premultiply().extract_band(1, VImage::option()->set("n", image2.bands() - 1));
|
||||
}
|
||||
// Calculate colour distance
|
||||
maxColourDistance = image1.dE00(image2).max();
|
||||
} catch (vips::VError const &err) {
|
||||
throw Napi::Error::New(env, err.what());
|
||||
}
|
||||
|
||||
// Clean up libvips' per-request data and threads
|
||||
vips_error_clear();
|
||||
vips_thread_shutdown();
|
||||
|
||||
return Napi::Number::New(env, maxColourDistance);
|
||||
}
|
||||
|
||||
#if defined(__GNUC__)
|
||||
// mallctl will be resolved by the runtime linker when jemalloc is being used
|
||||
extern "C" {
|
||||
int mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen) __attribute__((weak));
|
||||
}
|
||||
Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
return Napi::Boolean::New(env, mallctl != nullptr);
|
||||
}
|
||||
#else
|
||||
Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
return Napi::Boolean::New(env, false);
|
||||
}
|
||||
#endif
|
19
node_modules/sharp/src/utilities.h
generated
vendored
Normal file
19
node_modules/sharp/src/utilities.h
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright 2013 Lovell Fuller and others.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef SRC_UTILITIES_H_
|
||||
#define SRC_UTILITIES_H_
|
||||
|
||||
#include <napi.h>
|
||||
|
||||
Napi::Value cache(const Napi::CallbackInfo& info);
|
||||
Napi::Value concurrency(const Napi::CallbackInfo& info);
|
||||
Napi::Value counters(const Napi::CallbackInfo& info);
|
||||
Napi::Value simd(const Napi::CallbackInfo& info);
|
||||
Napi::Value libvipsVersion(const Napi::CallbackInfo& info);
|
||||
Napi::Value format(const Napi::CallbackInfo& info);
|
||||
void block(const Napi::CallbackInfo& info);
|
||||
Napi::Value _maxColourDistance(const Napi::CallbackInfo& info);
|
||||
Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info);
|
||||
|
||||
#endif // SRC_UTILITIES_H_
|
Reference in New Issue
Block a user