Added grayscale and sepia

This commit is contained in:
Essem 2021-12-14 01:16:23 -06:00
parent 6494dcdcb4
commit f71f18fc5d
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
8 changed files with 63 additions and 20 deletions

3
.gitignore vendored
View file

@ -38,4 +38,5 @@ todo.txt
.vscode/
migratedb.js
processed.txt
data.sqlite
data.sqlite
.cache

View file

@ -3,7 +3,8 @@ import ImageCommand from "../../classes/imageCommand.js";
class BlurpleCommand extends ImageCommand {
params() {
return {
old: !!this.specialArgs.old
old: !!this.specialArgs.old,
color: "blurple"
};
}
@ -14,8 +15,8 @@ class BlurpleCommand extends ImageCommand {
}];
static noImage = "You need to provide an image to make blurple!";
static command = "blurple";
static command = "colors";
static aliases = ["blurp"];
}
export default BlurpleCommand;
export default BlurpleCommand;

View file

@ -0,0 +1,17 @@
import ImageCommand from "../../classes/imageCommand.js";
class GrayscaleCommand extends ImageCommand {
params() {
return {
color: "grayscale"
};
}
static description = "Adds a grayscale filter";
static noImage = "You need to provide an image to turn grayscale!";
static command = "colors";
static aliases = ["gray", "greyscale", "grey"];
}
export default GrayscaleCommand;

View file

@ -0,0 +1,16 @@
import ImageCommand from "../../classes/imageCommand.js";
class SepiaCommand extends ImageCommand {
params() {
return {
color: "sepia"
};
}
static description = "Adds a sepia filter";
static noImage = "You need to provide an image to add a sepia filter!";
static command = "colors";
}
export default SepiaCommand;

View file

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

View file

@ -7,7 +7,7 @@
using namespace std;
using namespace Magick;
Napi::Value Blurple(const Napi::CallbackInfo &info) {
Napi::Value Colors(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
try {
@ -15,6 +15,7 @@ Napi::Value Blurple(const Napi::CallbackInfo &info) {
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
bool old =
obj.Has("old") ? obj.Get("old").As<Napi::Boolean>().Value() : false;
string color = obj.Get("color").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay =
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
@ -23,7 +24,7 @@ Napi::Value Blurple(const Napi::CallbackInfo &info) {
list<Image> frames;
list<Image> coalesced;
list<Image> blurpled;
list<Image> colored;
try {
readImages(&frames, Blob(data.Data(), data.Length()));
} catch (Magick::WarningCoder &warning) {
@ -34,23 +35,30 @@ Napi::Value Blurple(const Napi::CallbackInfo &info) {
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
image.threshold(49151.25);
image.levelColors(old ? "#7289DA" : "#5865F2", "white");
if (color == "blurple") {
image.threshold(49151.25);
image.levelColors(old ? "#7289DA" : "#5865F2", "white");
} else if (color == "grayscale") {
image.quantizeColorSpace(GRAYColorspace);
image.quantizeColors(256);
} else if (color == "sepia") {
image.sepiaTone(49151.25);
}
image.magick(type);
image.animationDelay(delay == 0 ? image.animationDelay() : delay);
blurpled.push_back(image);
colored.push_back(image);
}
optimizeTransparency(blurpled.begin(), blurpled.end());
optimizeTransparency(colored.begin(), colored.end());
if (type == "gif") {
for (Image &image : blurpled) {
for (Image &image : colored) {
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
image.quantize();
}
}
writeImages(blurpled.begin(), blurpled.end(), &blob);
writeImages(colored.begin(), colored.end(), &blob);
Napi::Object result = Napi::Object::New(env);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
@ -62,4 +70,4 @@ Napi::Value Blurple(const Napi::CallbackInfo &info) {
} catch (...) {
throw Napi::Error::New(env, "Unknown error");
}
}
}

5
natives/colors.h Normal file
View file

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

View file

@ -1,7 +1,7 @@
#include <napi.h>
#include <list>
#include "blur.h"
#include "blurple.h"
#include "colors.h"
#include "caption.h"
#include "caption2.h"
#include "circle.h"
@ -43,7 +43,7 @@
Napi::Object Init(Napi::Env env, Napi::Object exports)
{
exports.Set(Napi::String::New(env, "blur"), Napi::Function::New(env, Blur));
exports.Set(Napi::String::New(env, "blurple"), Napi::Function::New(env, Blurple));
exports.Set(Napi::String::New(env, "colors"), Napi::Function::New(env, Colors));
exports.Set(Napi::String::New(env, "caption"), Napi::Function::New(env, Caption));
exports.Set(Napi::String::New(env, "captionTwo"), Napi::Function::New(env, CaptionTwo));
exports.Set(Napi::String::New(env, "circle"), Napi::Function::New(env, Circle));