diff --git a/commands/image-editing/squish.js b/commands/image-editing/squish.js new file mode 100644 index 0000000..f3a575c --- /dev/null +++ b/commands/image-editing/squish.js @@ -0,0 +1,11 @@ +import ImageCommand from "../../classes/imageCommand.js"; + +class SquishCommand extends ImageCommand { + static description = "Squishes/stretches an image"; + static aliases = ["squishy", "squash"]; + + static noImage = "You need to provide an image/GIF to squish!"; + static command = "squish"; +} + +export default SquishCommand; diff --git a/natives/image.cc b/natives/image.cc index 83c833a..fcca25a 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -25,9 +25,10 @@ #include "reverse.h" #include "scott.h" #include "snapchat.h" -#include "speed.h" #include "sonic.h" +#include "speed.h" #include "spin.h" +#include "squish.h" #include "tile.h" #include "togif.h" #include "uncanny.h" @@ -74,9 +75,10 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) exports.Set(Napi::String::New(env, "reverse"), Napi::Function::New(env, Reverse)); exports.Set(Napi::String::New(env, "scott"), Napi::Function::New(env, Scott)); exports.Set(Napi::String::New(env, "snapchat"), Napi::Function::New(env, Snapchat)); - exports.Set(Napi::String::New(env, "speed"), Napi::Function::New(env, Speed)); exports.Set(Napi::String::New(env, "sonic"), Napi::Function::New(env, Sonic)); + exports.Set(Napi::String::New(env, "speed"), Napi::Function::New(env, Speed)); exports.Set(Napi::String::New(env, "spin"), Napi::Function::New(env, Spin)); + exports.Set(Napi::String::New(env, "squish"), Napi::Function::New(env, Squish)); exports.Set(Napi::String::New(env, "swirl"), Napi::Function::New(env, Swirl)); exports.Set(Napi::String::New(env, "tile"), Napi::Function::New(env, Tile)); exports.Set(Napi::String::New(env, "togif"), Napi::Function::New(env, ToGif)); diff --git a/natives/squish.cc b/natives/squish.cc new file mode 100644 index 0000000..5b0e573 --- /dev/null +++ b/natives/squish.cc @@ -0,0 +1,67 @@ +#include +#include + +#include + +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[0].As(); + Napi::Buffer data = obj.Get("data").As>(); + string type = obj.Get("type").As().Utf8Value(); + + VOption *options = VImage::option(); + + VImage in = + VImage::new_from_buffer( + data.Data(), data.Length(), "", + type == "gif" ? options->set("n", -1)->set("access", "sequential") + : options) + .colourspace(VIPS_INTERPRETATION_sRGB); + if (!in.has_alpha()) + in = in.bandjoin(255); + + 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; + double mult = (2 * M_PI) / nPages; + + vector img; + for (int i = 0; i < nPages; i++) { + VImage img_frame = + 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 = + 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 delay(30, 50); + final.set("delay", delay); + } + + void *buf; + size_t length; + final.write_to_buffer(".gif", &buf, &length); + + result.Set("data", Napi::Buffer::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(); + } + + vips_error_clear(); + vips_thread_shutdown(); + return result; +} \ No newline at end of file diff --git a/natives/squish.h b/natives/squish.h new file mode 100644 index 0000000..4ecdfe6 --- /dev/null +++ b/natives/squish.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +Napi::Value Squish(const Napi::CallbackInfo& info);