Skip to content

Commit

Permalink
Added reddit, catch any errors in native functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEssem committed Apr 26, 2021
1 parent 6cecd4b commit b7fe04c
Show file tree
Hide file tree
Showing 41 changed files with 1,601 additions and 1,357 deletions.
1 change: 1 addition & 0 deletions Dockerfile.api
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ WORKDIR /home/esmBot/.internal
COPY ./assets/caption.otf /usr/share/fonts/caption.otf
COPY ./assets/caption2.ttf /usr/share/fonts/caption2.ttf
COPY ./assets/hbc.ttf /usr/share/fonts/hbc.ttf
COPY ./assets/reddit.ttf /usr/share/fonts/reddit.ttf
RUN fc-cache -fv

COPY --chown=node:node ./package.json package.json
Expand Down
1 change: 1 addition & 0 deletions Dockerfile.bot
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ WORKDIR /home/esmBot/.internal
COPY ./assets/caption.otf /usr/share/fonts/caption.otf
COPY ./assets/caption2.ttf /usr/share/fonts/caption2.ttf
COPY ./assets/hbc.ttf /usr/share/fonts/hbc.ttf
COPY ./assets/reddit.ttf /usr/share/fonts/reddit.ttf
RUN fc-cache -fv

COPY --chown=node:node ./package.json package.json
Expand Down
Binary file added assets/images/reddit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/reddit.ttf
Binary file not shown.
19 changes: 19 additions & 0 deletions commands/image-editing/reddit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const ImageCommand = require("../../classes/imageCommand.js");
const { random } = require("../../utils/misc.js");
const names = ["esmBot", "me_irl", "dankmemes", "hmmm", "gaming", "wholesome", "chonkers", "memes", "funny", "pcmasterrace", "bellybros"];

class RedditCommand extends ImageCommand {
params(args) {
return {
caption: args.length === 0 ? random(names) : args.join(" ").replaceAll("\n", "").replaceAll(" ", "")
};
}

static description = "Adds a Reddit watermark to an image";
static arguments = ["{text}"];

static noText = "you need to provide some text to add a Reddit watermark!";
static command = "reddit";
}

module.exports = RedditCommand;
70 changes: 37 additions & 33 deletions natives/blur.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,47 @@ using namespace Magick;
Napi::Value Blur(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();

Napi::Object obj = info[0].As<Napi::Object>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
bool sharp = obj.Get("sharp").As<Napi::Boolean>().Value();
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;
readImages(&frames, path);
coalesceImages(&coalesced, frames.begin(), frames.end());

if (sharp) {
for_each(coalesced.begin(), coalesced.end(), sharpenImage(10, 3));
} else {
for_each(coalesced.begin(), coalesced.end(), blurImage(15));
}
try {
Napi::Object obj = info[0].As<Napi::Object>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
bool sharp = obj.Get("sharp").As<Napi::Boolean>().Value();
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;
readImages(&frames, path);
coalesceImages(&coalesced, frames.begin(), frames.end());

if (sharp) {
for_each(coalesced.begin(), coalesced.end(), sharpenImage(10, 3));
} else {
for_each(coalesced.begin(), coalesced.end(), blurImage(15));
}

for_each(coalesced.begin(), coalesced.end(), magickImage(type));
for_each(coalesced.begin(), coalesced.end(), magickImage(type));

optimizeTransparency(coalesced.begin(), coalesced.end());
optimizeTransparency(coalesced.begin(), coalesced.end());

if (type == "gif") {
for (Image &image : coalesced) {
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
image.quantize();
if (delay != 0) image.animationDelay(delay);
if (type == "gif") {
for (Image &image : coalesced) {
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
image.quantize();
if (delay != 0) image.animationDelay(delay);
}
}
}

writeImages(coalesced.begin(), coalesced.end(), &blob);
writeImages(coalesced.begin(), coalesced.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;
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());
}
}
70 changes: 37 additions & 33 deletions natives/blurple.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,46 @@ using namespace Magick;
Napi::Value Blurple(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();

Napi::Object obj = info[0].As<Napi::Object>();
string path = obj.Get("path").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;

Blob blob;

list<Image> frames;
list<Image> coalesced;
list<Image> blurpled;
readImages(&frames, path);
coalesceImages(&coalesced, frames.begin(), frames.end());

for (Image &image : coalesced) {
image.threshold(49151.25);
image.levelColors("#7289DA", "white");
image.magick(type);
image.animationDelay(delay == 0 ? image.animationDelay() : delay);
blurpled.push_back(image);
}
try {
Napi::Object obj = info[0].As<Napi::Object>();
string path = obj.Get("path").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;

Blob blob;

list<Image> frames;
list<Image> coalesced;
list<Image> blurpled;
readImages(&frames, path);
coalesceImages(&coalesced, frames.begin(), frames.end());

for (Image &image : coalesced) {
image.threshold(49151.25);
image.levelColors("#7289DA", "white");
image.magick(type);
image.animationDelay(delay == 0 ? image.animationDelay() : delay);
blurpled.push_back(image);
}

optimizeTransparency(blurpled.begin(), blurpled.end());
optimizeTransparency(blurpled.begin(), blurpled.end());

if (type == "gif") {
for (Image &image : blurpled) {
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
image.quantize();
if (type == "gif") {
for (Image &image : blurpled) {
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
image.quantize();
}
}
}

writeImages(blurpled.begin(), blurpled.end(), &blob);
writeImages(blurpled.begin(), blurpled.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;
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());
}
}
100 changes: 52 additions & 48 deletions natives/caption.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,66 @@ using namespace Magick;
Napi::Value Caption(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();

Napi::Object obj = info[0].As<Napi::Object>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string caption = obj.Get("caption").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;
try {
Napi::Object obj = info[0].As<Napi::Object>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string caption = obj.Get("caption").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;

Blob blob;
Blob blob;

list<Image> frames;
list<Image> coalesced;
list<Image> captioned;
readImages(&frames, path);
list<Image> frames;
list<Image> coalesced;
list<Image> captioned;
readImages(&frames, path);

size_t width = frames.front().baseColumns();
string query(to_string(width - ((width / 25) * 2)) + "x");
Image caption_image(Geometry(query), Color("white"));
caption_image.fillColor("black");
caption_image.alpha(true);
caption_image.font("Futura");
caption_image.fontPointsize(width / 13);
caption_image.textGravity(Magick::CenterGravity);
caption_image.read("pango:" + caption);
caption_image.extent(Geometry(width, caption_image.rows() + (width / 13)),
Magick::CenterGravity);
size_t width = frames.front().baseColumns();
string query(to_string(width - ((width / 25) * 2)) + "x");
Image caption_image(Geometry(query), Color("white"));
caption_image.fillColor("black");
caption_image.alpha(true);
caption_image.font("Futura");
caption_image.fontPointsize(width / 13);
caption_image.textGravity(Magick::CenterGravity);
caption_image.read("pango:" + caption);
caption_image.extent(Geometry(width, caption_image.rows() + (width / 13)),
Magick::CenterGravity);

coalesceImages(&coalesced, frames.begin(), frames.end());
coalesceImages(&coalesced, frames.begin(), frames.end());

for (Image &image : coalesced) {
Image appended;
list<Image> images;
image.backgroundColor("white");
images.push_back(caption_image);
images.push_back(image);
appendImages(&appended, images.begin(), images.end(), true);
appended.repage();
appended.magick(type);
appended.animationDelay(delay == 0 ? image.animationDelay() : delay);
appended.gifDisposeMethod(Magick::BackgroundDispose);
captioned.push_back(appended);
}
for (Image &image : coalesced) {
Image appended;
list<Image> images;
image.backgroundColor("white");
images.push_back(caption_image);
images.push_back(image);
appendImages(&appended, images.begin(), images.end(), true);
appended.repage();
appended.magick(type);
appended.animationDelay(delay == 0 ? image.animationDelay() : delay);
appended.gifDisposeMethod(Magick::BackgroundDispose);
captioned.push_back(appended);
}

optimizeTransparency(captioned.begin(), captioned.end());
optimizeTransparency(captioned.begin(), captioned.end());

if (type == "gif") {
for (Image &image : captioned) {
image.quantizeDither(false);
image.quantize();
if (type == "gif") {
for (Image &image : captioned) {
image.quantizeDither(false);
image.quantize();
}
}
}

writeImages(captioned.begin(), captioned.end(), &blob);
writeImages(captioned.begin(), captioned.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;
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());
}
}
Loading

0 comments on commit b7fe04c

Please sign in to comment.