ZAMN Image Command (#195)

* Add files via upload

* Add files via upload

* Update image.cc

* Add files via upload
This commit is contained in:
ItsODRJR 2021-12-02 18:05:52 -06:00 committed by GitHub
parent a98601e2e8
commit 98b812e0d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 1 deletions

BIN
assets/images/zamn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 275 KiB

View file

@ -0,0 +1,10 @@
import ImageCommand from "../../classes/imageCommand.js";
class ZamnCommand extends ImageCommand {
static description = "ZAMN! SHES 12?";
static noImage = "You need to provide an image to zamn at!";
static command = "zamn";
}
export default ZamnCommand;

View file

@ -38,6 +38,7 @@
#include "watermark.h"
#include "wdt.h"
#include "whisper.h"
#include "zamn.h"
Napi::Object Init(Napi::Env env, Napi::Object exports)
{
@ -79,7 +80,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
exports.Set(Napi::String::New(env, "watermark"), Napi::Function::New(env, Watermark));
exports.Set(Napi::String::New(env, "wdt"), Napi::Function::New(env, Wdt));
exports.Set(Napi::String::New(env, "whisper"), Napi::Function::New(env, Whisper));
exports.Set(Napi::String::New(env, "zamn"), Napi::Function::New(env, Zamn));
return exports;
}
NODE_API_MODULE(addon, Init);
NODE_API_MODULE(addon, Init);

62
natives/zamn.cc Normal file
View file

@ -0,0 +1,62 @@
#include <Magick++.h>
#include <napi.h>
#include <list>
using namespace std;
using namespace Magick;
Napi::Value Zamn(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
try {
Napi::Object obj = info[0].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay =
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
Blob blob;
list<Image> frames;
list<Image> coalesced;
list<Image> mid;
Image watermark;
readImages(&frames, Blob(data.Data(), data.Length()));
watermark.read("./assets/images/zamn.png");
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
Image watermark_new = watermark;
image.backgroundColor("none");
image.scale(Geometry("303x438!"));
image.extent(Geometry("621x516-310-75"));
watermark_new.composite(image, Magick::CenterGravity,
Magick::OverCompositeOp);
watermark_new.magick(type);
watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay);
mid.push_back(watermark_new);
}
optimizeTransparency(mid.begin(), mid.end());
if (type == "gif") {
for (Image &image : mid) {
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
image.quantize();
}
}
writeImages(mid.begin(), mid.end(), &blob);
Napi::Object result = Napi::Object::New(env);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
blob.length()));
result.Set("type", type);
return result;
} catch (std::exception const &err) {
throw Napi::Error::New(env, err.what());
} catch (...) {
throw Napi::Error::New(env, "Unknown error");
}
}

5
natives/zamn.h Normal file
View file

@ -0,0 +1,5 @@
#pragma once
#include <napi.h>
Napi::Value Zamn(const Napi::CallbackInfo& info);