Added deepfry, make jpeg work on GIFs

This commit is contained in:
Essem 2021-06-29 17:26:22 -05:00
parent 4672e8d6ad
commit 2da505a778
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
43 changed files with 162 additions and 157 deletions

View file

@ -0,0 +1,11 @@
const ImageCommand = require("../../classes/imageCommand.js");
class DeepfryCommand extends ImageCommand {
static description = "Deep-fries an image";
static aliases = ["fry", "jpeg2", "nuke"];
static noImage = "You need to provide an image to fry!";
static command = "deepfry";
}
module.exports = DeepfryCommand;

View file

@ -25,7 +25,7 @@ Napi::Value Blur(const Napi::CallbackInfo &info) {
coalesceImages(&coalesced, frames.begin(), frames.end());
if (sharp) {
for_each(coalesced.begin(), coalesced.end(), sharpenImage(10, 3));
for_each(coalesced.begin(), coalesced.end(), sharpenImage(0, 3));
} else {
for_each(coalesced.begin(), coalesced.end(), blurImage(15));
}

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_BLUR_H_
#define ESMBOT_NATIVES_BLUR_H_
#pragma once
#include <napi.h>
Napi::Value Blur(const Napi::CallbackInfo& info);
#endif

View file

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

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_CAPTION_H_
#define ESMBOT_NATIVES_CAPTION_H_
#pragma once
#include <napi.h>
Napi::Value Caption(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_CAPTIONTWO_H_
#define ESMBOT_NATIVES_CAPTIONTWO_H_
#pragma once
#include <napi.h>
Napi::Value CaptionTwo(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_CIRCLE_H_
#define ESMBOT_NATIVES_CIRCLE_H_
#pragma once
#include <napi.h>
Napi::Value Circle(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_CROP_H_
#define ESMBOT_NATIVES_CROP_H_
#pragma once
#include <napi.h>
Napi::Value Crop(const Napi::CallbackInfo& info);
#endif

61
natives/deepfry.cc Normal file
View file

@ -0,0 +1,61 @@
#include <Magick++.h>
#include <napi.h>
#include <list>
#include <iostream>
using namespace std;
using namespace Magick;
Napi::Value Deepfry(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> blurred;
readImages(&frames, Blob(data.Data(), data.Length()));
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
Blob temp;
image.level(QuantumRange / 2, QuantumRange / 2);
image.quality(1);
image.magick("JPEG");
image.write(&temp);
Image newImage(temp);
newImage.magick(type);
newImage.animationDelay(delay == 0 ? image.animationDelay() : delay);
blurred.push_back(newImage);
}
optimizeTransparency(blurred.begin(), blurred.end());
if (type == "gif") {
for (Image &image : blurred) {
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
image.quantize();
}
}
writeImages(blurred.begin(), blurred.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/deepfry.h Normal file
View file

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

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_EXPLODE_H_
#define ESMBOT_NATIVES_EXPLODE_H_
#pragma once
#include <napi.h>
Napi::Value Explode(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_FLAG_H_
#define ESMBOT_NATIVES_FLAG_H_
#pragma once
#include <napi.h>
Napi::Value Flag(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_FLIP_H_
#define ESMBOT_NATIVES_FLIP_H_
#pragma once
#include <napi.h>
Napi::Value Flip(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_FREEZE_H_
#define ESMBOT_NATIVES_FREEZE_H_
#pragma once
#include <napi.h>
Napi::Value Freeze(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_GAMEXPLAIN_H_
#define ESMBOT_NATIVES_GAMEXPLAIN_H_
#pragma once
#include <napi.h>
Napi::Value Gamexplain(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_GLOBE_H_
#define ESMBOT_NATIVES_GLOBE_H_
#pragma once
#include <napi.h>
Napi::Value Globe(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_HOMEBREW_H_
#define ESMBOT_NATIVES_HOMEBREW_H_
#pragma once
#include <napi.h>
Napi::Value Homebrew(const Napi::CallbackInfo& info);
#endif

View file

@ -6,6 +6,7 @@
#include "caption2.h"
#include "circle.h"
#include "crop.h"
#include "deepfry.h"
#include "explode.h"
#include "flag.h"
#include "flip.h"
@ -45,6 +46,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
exports.Set(Napi::String::New(env, "captionTwo"), Napi::Function::New(env, CaptionTwo));
exports.Set(Napi::String::New(env, "circle"), Napi::Function::New(env, Circle));
exports.Set(Napi::String::New(env, "crop"), Napi::Function::New(env, Crop));
exports.Set(Napi::String::New(env, "deepfry"), Napi::Function::New(env, Deepfry));
exports.Set(Napi::String::New(env, "explode"), Napi::Function::New(env, Explode));
exports.Set(Napi::String::New(env, "flag"), Napi::Function::New(env, Flag));
exports.Set(Napi::String::New(env, "flip"), Napi::Function::New(env, Flip));

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_INVERT_H_
#define ESMBOT_NATIVES_INVERT_H_
#pragma once
#include <napi.h>
Napi::Value Invert(const Napi::CallbackInfo& info);
#endif

View file

@ -12,19 +12,56 @@ Napi::Value Jpeg(const Napi::CallbackInfo &info) {
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;
Image image;
image.read(Blob(data.Data(), data.Length()));
image.quality(1);
image.magick("JPEG");
image.write(&blob);
Napi::Object result = Napi::Object::New(env);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
blob.length()));
result.Set("type", "jpg");
if (type == "gif") {
list<Image> frames;
list<Image> coalesced;
list<Image> jpeged;
readImages(&frames, Blob(data.Data(), data.Length()));
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
Blob temp;
image.quality(1);
image.magick("JPEG");
image.write(&temp);
Image newImage(temp);
newImage.magick(type);
newImage.animationDelay(delay == 0 ? image.animationDelay() : delay);
jpeged.push_back(newImage);
}
optimizeTransparency(jpeged.begin(), jpeged.end());
for (Image &image : jpeged) {
image.quantizeDither(false);
image.quantize();
}
writeImages(jpeged.begin(), jpeged.end(), &blob);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
blob.length()));
result.Set("type", type);
} else {
Image image;
image.read(Blob(data.Data(), data.Length()));
image.quality(1);
image.magick("JPEG");
image.write(&blob);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
blob.length()));
result.Set("type", "jpg");
}
return result;
} catch (std::exception const &err) {
throw Napi::Error::New(env, err.what());

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_JPEG_H_
#define ESMBOT_NATIVES_JPEG_H_
#pragma once
#include <napi.h>
Napi::Value Jpeg(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_LEAK_H_
#define ESMBOT_NATIVES_LEAK_H_
#pragma once
#include <napi.h>
Napi::Value Leak(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_MAGIK_H_
#define ESMBOT_NATIVES_MAGIK_H_
#pragma once
#include <napi.h>
Napi::Value Magik(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_MEME_H_
#define ESMBOT_NATIVES_MEME_H_
#pragma once
#include <napi.h>
Napi::Value Meme(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_MIRROR_H_
#define ESMBOT_NATIVES_MIRROR_H_
#pragma once
#include <napi.h>
Napi::Value Mirror(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_MOTIVATE_H_
#define ESMBOT_NATIVES_MOTIVATE_H_
#pragma once
#include <napi.h>
Napi::Value Motivate(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_REDDIT_H_
#define ESMBOT_NATIVES_REDDIT_H_
#pragma once
#include <napi.h>
Napi::Value Reddit(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_RESIZE_H_
#define ESMBOT_NATIVES_RESIZE_H_
#pragma once
#include <napi.h>
Napi::Value Resize(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_RETRO_H_
#define ESMBOT_NATIVES_RETRO_H_
#pragma once
#include <napi.h>
Napi::Value Retro(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_REVERSE_H_
#define ESMBOT_NATIVES_REVERSE_H_
#pragma once
#include <napi.h>
Napi::Value Reverse(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_SCOTT_H_
#define ESMBOT_NATIVES_SCOTT_H_
#pragma once
#include <napi.h>
Napi::Value Scott(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_SNAPCHAT_H_
#define ESMBOT_NATIVES_SNAPCHAT_H_
#pragma once
#include <napi.h>
Napi::Value Snapchat(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_SONIC_H_
#define ESMBOT_NATIVES_SONIC_H_
#pragma once
#include <napi.h>
Napi::Value Sonic(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_SPEED_H_
#define ESMBOT_NATIVES_SPEED_H_
#pragma once
#include <napi.h>
Napi::Value Speed(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_SPIN_H_
#define ESMBOT_NATIVES_SPIN_H_
#pragma once
#include <napi.h>
Napi::Value Spin(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_SWIRL_H_
#define ESMBOT_NATIVES_SWIRL_H_
#pragma once
#include <napi.h>
Napi::Value Swirl(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_TILE_H_
#define ESMBOT_NATIVES_TILE_H_
#pragma once
#include <napi.h>
Napi::Value Tile(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_TRUMP_H_
#define ESMBOT_NATIVES_TRUMP_H_
#pragma once
#include <napi.h>
Napi::Value Trump(const Napi::CallbackInfo& info);
#endif

View file

@ -1,9 +1,7 @@
#include <Magick++.h>
#include <napi.h>
#include <iostream>
#include <list>
#include <string_view>
using namespace std;
using namespace Magick;
@ -32,7 +30,6 @@ Napi::Value Uncaption(const Napi::CallbackInfo &info) {
ssize_t row;
for (row = 0; row < rows; ++row) {
ColorGray color = firstImage.pixelColor(0, row);
cout << color.shade() << "\n";
if (color.shade() < 0.95) {
break;
}

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_UNCAPTION_H_
#define ESMBOT_NATIVES_UNCAPTION_H_
#pragma once
#include <napi.h>
Napi::Value Uncaption(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_WALL_H_
#define ESMBOT_NATIVES_WALL_H_
#pragma once
#include <napi.h>
Napi::Value Wall(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_WATERMARK_H_
#define ESMBOT_NATIVES_WATERMARK_H_
#pragma once
#include <napi.h>
Napi::Value Watermark(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +1,5 @@
#ifndef ESMBOT_NATIVES_WDT_H_
#define ESMBOT_NATIVES_WDT_H_
#pragma once
#include <napi.h>
Napi::Value Wdt(const Napi::CallbackInfo& info);
#endif