&togif command (libvips version) (#286)

* &togif command

* use libvips not magick

* include libvips build dir in gitignore
This commit is contained in:
bjcscat 2022-06-25 12:40:52 -05:00 committed by GitHub
parent 3f2de3a168
commit 5072c6ea10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 1 deletions

3
.gitignore vendored
View File

@ -25,6 +25,7 @@ build/
# Dependency directories
node_modules/
jspm_packages/
libvips/
# Optional npm cache directory
.npm
@ -127,4 +128,4 @@ local.settings.json
# Azurite artifacts
__blobstorage__
__queuestorage__
__azurite_db*__.json
__azurite_db*__.json

View File

@ -0,0 +1,11 @@
import ImageCommand from "../../classes/imageCommand.js";
class ToGIFCommand extends ImageCommand {
static description = "Turns an image into a gif";
static aliases = ["tgif", "gifify"];
static noImage = "You need to provide an image to turn into a GIF!";
static command = "togif";
}
export default ToGIFCommand;

View File

@ -29,6 +29,7 @@
#include "sonic.h"
#include "spin.h"
#include "tile.h"
#include "togif.h"
#include "uncaption.h"
#include "wall.h"
#include "watermark.h"
@ -77,6 +78,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
exports.Set(Napi::String::New(env, "spin"), Napi::Function::New(env, Spin));
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));
exports.Set(Napi::String::New(env, "uncaption"), Napi::Function::New(env, Uncaption));
exports.Set(Napi::String::New(env, "wall"), Napi::Function::New(env, Wall));
exports.Set(Napi::String::New(env, "watermark"), Napi::Function::New(env, Watermark));

56
natives/togif.cc Normal file
View File

@ -0,0 +1,56 @@
#include <napi.h>
#include <vips/vips8>
using namespace std;
using namespace vips;
Napi::Value ToGif(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();
if (type == "gif") {
Napi::Object result = Napi::Object::New(env);
result.Set("data", data);
result.Set("type", "gif");
return result;
}
VOption *options = VImage::option()->set("access", "sequential");
VImage in =
VImage::new_from_buffer(data.Data(), data.Length(), "",
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
int pageHeight = vips_image_get_page_height(in.get_image());
vector<VImage> img;
img.push_back(in);
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight + in.height());
void *buf;
size_t length;
final.write_to_buffer(
("." + type).c_str(), &buf, &length, VImage::option()->set("dither", 0));
vips_thread_shutdown();
Napi::Object result = Napi::Object::New(env);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", "gif");
return result;
} catch (std::exception const &err) {
throw Napi::Error::New(env, err.what());
} catch (...) {
throw Napi::Error::New(env, "Unknown error");
}
}

5
natives/togif.h Normal file
View File

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