mrmBot-Matrix/natives/explode.cc

53 lines
1.3 KiB
C++
Raw Normal View History

#include "common.h"
#include <Magick++.h>
#include <iostream>
#include <string>
#include <map>
#include <list>
using namespace std;
using namespace Magick;
char* Explode(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
int amount = stoi(Arguments.at("amount"));
int delay = MAP_HAS(Arguments, "delay") ? stoi(Arguments.at("delay")) : 0;
Blob blob;
list<Image> frames;
list<Image> coalesced;
list<Image> blurred;
try {
readImages(&frames, Blob(BufferData, BufferLength));
} catch (Magick::WarningCoder &warning) {
cerr << "Coder Warning: " << warning.what() << endl;
} catch (Magick::Warning &warning) {
cerr << "Warning: " << warning.what() << endl;
}
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
image.implode(amount);
image.magick(type);
blurred.push_back(image);
}
optimizeTransparency(blurred.begin(), blurred.end());
if (type == "gif") {
for (Image &image : blurred) {
image.quantizeDither(false);
image.quantize();
if (delay != 0) image.animationDelay(delay);
}
}
writeImages(blurred.begin(), blurred.end(), &blob);
*DataSize = blob.length();
return (char*) blob.data();
}