how the hell did I manage to miss squish

This commit is contained in:
Essem 2022-12-28 00:49:38 -06:00
parent 47b2d9fcff
commit 89caa43068
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
3 changed files with 45 additions and 70 deletions

View file

@ -78,6 +78,7 @@ std::map<std::string, char* (*)(string *type, char* BufferData, size_t BufferLen
{"snapchat", Snapchat},
{"speed", &Speed},
{"spin", Spin},
{"squish", Squish},
{"swirl", Swirl},
{"tile", Tile},
{"togif", ToGif},
@ -105,7 +106,7 @@ bool isNapiValueInt(Napi::Env& env, Napi::Value& num) {
.Value();
}
Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) {
Napi::Value ProcessImage(const Napi::CallbackInfo &info) {
Napi::Env env = info.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;
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);
buf = FunctionMap.at(command)(&type, data.Data(), data.Length(), Arguments, &length);
} else {
@ -164,21 +165,6 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) {
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){
#ifdef _WIN32
Magick::InitializeMagick("");

View file

@ -1,26 +1,20 @@
#include "common.h"
#include <math.h>
#include <napi.h>
#include <vips/vips8>
using namespace std;
using namespace vips;
Napi::Value Squish(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Object result = Napi::Object::New(env);
try {
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();
char *Squish(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) {
VOption *options = VImage::option();
VImage in =
VImage::new_from_buffer(
data.Data(), data.Length(), "",
type == "gif" ? options->set("n", -1)->set("access", "sequential")
BufferData, BufferLength, "",
*type == "gif" ? options->set("n", -1)->set("access", "sequential")
: options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha())
@ -28,13 +22,13 @@ Napi::Value Squish(const Napi::CallbackInfo &info) {
int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30;
int nPages = *type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30;
double mult = (2 * M_PI) / nPages;
vector<VImage> img;
for (int i = 0; i < nPages; i++) {
VImage img_frame =
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
*type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
double newWidth = (sin(i * mult) / 4) + 0.75;
double newHeight = (cos(i * mult) / 4) + 0.75;
VImage resized =
@ -44,24 +38,17 @@ Napi::Value Squish(const Napi::CallbackInfo &info) {
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
if (type != "gif") {
if (*type != "gif") {
vector<int> delay(30, 50);
final.set("delay", delay);
}
void *buf;
size_t length;
final.write_to_buffer(".gif", &buf, &length);
final.write_to_buffer(".gif", &buf, DataSize);
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();
}
*type = "gif";
vips_error_clear();
vips_thread_shutdown();
return result;
return (char *)buf;
}

View file

@ -1,5 +1,7 @@
#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);