mrmBot-Matrix/natives/explode.cc

57 lines
1.4 KiB
C++
Raw Normal View History

#include <Magick++.h>
2022-11-27 20:52:40 +00:00
#include <cstring>
#include <iostream>
#include <list>
2022-11-27 20:52:40 +00:00
#include <map>
#include <string>
2023-03-09 01:36:39 +00:00
#include "common.h"
using namespace std;
using namespace Magick;
2023-03-09 01:36:39 +00:00
char *Explode(string type, string *outType, char *BufferData,
size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) {
int amount = GetArgument<int>(Arguments, "amount");
int delay = GetArgumentWithFallback<int>(Arguments, "delay", 0);
2022-11-27 20:52:40 +00:00
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);
2023-03-09 01:36:39 +00:00
image.magick(*outType);
2022-11-27 20:52:40 +00:00
blurred.push_back(image);
}
optimizeTransparency(blurred.begin(), blurred.end());
2023-03-09 01:36:39 +00:00
if (*outType == "gif") {
2022-11-27 20:52:40 +00:00
for (Image &image : blurred) {
image.quantizeDither(false);
image.quantize();
if (delay != 0) image.animationDelay(delay);
}
2022-11-27 20:52:40 +00:00
}
2022-11-27 20:52:40 +00:00
writeImages(blurred.begin(), blurred.end(), &blob);
2022-11-27 20:52:40 +00:00
*DataSize = blob.length();
2022-11-27 20:52:40 +00:00
// workaround because the data is tied to the blob
char *data = (char *)malloc(*DataSize);
memcpy(data, blob.data(), *DataSize);
return data;
}