how the hell did I manage to miss squish
This commit is contained in:
parent
47b2d9fcff
commit
89caa43068
3 changed files with 45 additions and 70 deletions
|
@ -78,6 +78,7 @@ std::map<std::string, char* (*)(string *type, char* BufferData, size_t BufferLen
|
||||||
{"snapchat", Snapchat},
|
{"snapchat", Snapchat},
|
||||||
{"speed", &Speed},
|
{"speed", &Speed},
|
||||||
{"spin", Spin},
|
{"spin", Spin},
|
||||||
|
{"squish", Squish},
|
||||||
{"swirl", Swirl},
|
{"swirl", Swirl},
|
||||||
{"tile", Tile},
|
{"tile", Tile},
|
||||||
{"togif", ToGif},
|
{"togif", ToGif},
|
||||||
|
@ -105,7 +106,7 @@ bool isNapiValueInt(Napi::Env& env, Napi::Value& num) {
|
||||||
.Value();
|
.Value();
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) {
|
Napi::Value ProcessImage(const Napi::CallbackInfo &info) {
|
||||||
Napi::Env env = info.Env();
|
Napi::Env env = info.Env();
|
||||||
Napi::Object result = Napi::Object::New(env);
|
Napi::Object result = Napi::Object::New(env);
|
||||||
|
|
||||||
|
@ -145,7 +146,7 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) {
|
||||||
|
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
char* buf;
|
char* buf;
|
||||||
if (input) {
|
if (obj.Has("data")) {
|
||||||
Napi::Buffer<char> data = obj.Has("data") ? obj.Get("data").As<Napi::Buffer<char>>() : Napi::Buffer<char>::New(env, 0);
|
Napi::Buffer<char> data = obj.Has("data") ? obj.Get("data").As<Napi::Buffer<char>>() : Napi::Buffer<char>::New(env, 0);
|
||||||
buf = FunctionMap.at(command)(&type, data.Data(), data.Length(), Arguments, &length);
|
buf = FunctionMap.at(command)(&type, data.Data(), data.Length(), Arguments, &length);
|
||||||
} else {
|
} else {
|
||||||
|
@ -164,21 +165,6 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Value ProcessImage(const Napi::CallbackInfo &info) { // janky solution for gradual adoption
|
|
||||||
Napi::Env env = info.Env();
|
|
||||||
|
|
||||||
string command = info[0].As<Napi::String>().Utf8Value();
|
|
||||||
|
|
||||||
if (MAP_HAS(FunctionMap, command)) {
|
|
||||||
return NewProcessImage(info, true);
|
|
||||||
} else if (MAP_HAS(NoInputFunctionMap, command)) {
|
|
||||||
return NewProcessImage(info, false);
|
|
||||||
} else {
|
|
||||||
Napi::Error::New(env, "Invalid command").ThrowAsJavaScriptException();
|
|
||||||
return env.Null();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Napi::Object Init(Napi::Env env, Napi::Object exports){
|
Napi::Object Init(Napi::Env env, Napi::Object exports){
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
Magick::InitializeMagick("");
|
Magick::InitializeMagick("");
|
||||||
|
|
|
@ -1,67 +1,54 @@
|
||||||
|
#include "common.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <napi.h>
|
|
||||||
|
|
||||||
#include <vips/vips8>
|
#include <vips/vips8>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace vips;
|
using namespace vips;
|
||||||
|
|
||||||
Napi::Value Squish(const Napi::CallbackInfo &info) {
|
char *Squish(string *type, char *BufferData, size_t BufferLength,
|
||||||
Napi::Env env = info.Env();
|
ArgumentMap Arguments, size_t *DataSize) {
|
||||||
Napi::Object result = Napi::Object::New(env);
|
|
||||||
|
|
||||||
try {
|
VOption *options = VImage::option();
|
||||||
Napi::Object obj = info[1].As<Napi::Object>();
|
|
||||||
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
|
|
||||||
string type = obj.Get("type").As<Napi::String>().Utf8Value();
|
|
||||||
|
|
||||||
VOption *options = VImage::option();
|
VImage in =
|
||||||
|
VImage::new_from_buffer(
|
||||||
|
BufferData, BufferLength, "",
|
||||||
|
*type == "gif" ? options->set("n", -1)->set("access", "sequential")
|
||||||
|
: options)
|
||||||
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||||
|
if (!in.has_alpha())
|
||||||
|
in = in.bandjoin(255);
|
||||||
|
|
||||||
VImage in =
|
int width = in.width();
|
||||||
VImage::new_from_buffer(
|
int pageHeight = vips_image_get_page_height(in.get_image());
|
||||||
data.Data(), data.Length(), "",
|
int nPages = *type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30;
|
||||||
type == "gif" ? options->set("n", -1)->set("access", "sequential")
|
double mult = (2 * M_PI) / nPages;
|
||||||
: options)
|
|
||||||
.colourspace(VIPS_INTERPRETATION_sRGB);
|
|
||||||
if (!in.has_alpha())
|
|
||||||
in = in.bandjoin(255);
|
|
||||||
|
|
||||||
int width = in.width();
|
vector<VImage> img;
|
||||||
int pageHeight = vips_image_get_page_height(in.get_image());
|
for (int i = 0; i < nPages; i++) {
|
||||||
int nPages = type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30;
|
VImage img_frame =
|
||||||
double mult = (2 * M_PI) / nPages;
|
*type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
|
||||||
|
double newWidth = (sin(i * mult) / 4) + 0.75;
|
||||||
vector<VImage> img;
|
double newHeight = (cos(i * mult) / 4) + 0.75;
|
||||||
for (int i = 0; i < nPages; i++) {
|
VImage resized =
|
||||||
VImage img_frame =
|
img_frame.resize(newWidth, VImage::option()->set("vscale", newHeight))
|
||||||
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
|
.gravity(VIPS_COMPASS_DIRECTION_CENTRE, width, pageHeight);
|
||||||
double newWidth = (sin(i * mult) / 4) + 0.75;
|
img.push_back(resized);
|
||||||
double newHeight = (cos(i * mult) / 4) + 0.75;
|
|
||||||
VImage resized =
|
|
||||||
img_frame.resize(newWidth, VImage::option()->set("vscale", newHeight))
|
|
||||||
.gravity(VIPS_COMPASS_DIRECTION_CENTRE, width, pageHeight);
|
|
||||||
img.push_back(resized);
|
|
||||||
}
|
|
||||||
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
|
|
||||||
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
|
|
||||||
if (type != "gif") {
|
|
||||||
vector<int> delay(30, 50);
|
|
||||||
final.set("delay", delay);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *buf;
|
|
||||||
size_t length;
|
|
||||||
final.write_to_buffer(".gif", &buf, &length);
|
|
||||||
|
|
||||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
|
||||||
result.Set("type", "gif");
|
|
||||||
} catch (std::exception const &err) {
|
|
||||||
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
|
|
||||||
} catch (...) {
|
|
||||||
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
|
|
||||||
}
|
}
|
||||||
|
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
|
||||||
|
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
|
||||||
|
if (*type != "gif") {
|
||||||
|
vector<int> delay(30, 50);
|
||||||
|
final.set("delay", delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *buf;
|
||||||
|
final.write_to_buffer(".gif", &buf, DataSize);
|
||||||
|
|
||||||
|
*type = "gif";
|
||||||
|
|
||||||
vips_error_clear();
|
vips_error_clear();
|
||||||
vips_thread_shutdown();
|
vips_thread_shutdown();
|
||||||
return result;
|
return (char *)buf;
|
||||||
}
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <napi.h>
|
#include "common.h"
|
||||||
|
|
||||||
Napi::Value Squish(const Napi::CallbackInfo& info);
|
using std::string;
|
||||||
|
|
||||||
|
char* Squish(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
|
Loading…
Reference in a new issue