mrmBot-Matrix/natives/spin.cc

73 lines
1.9 KiB
C++
Raw Normal View History

#include <Magick++.h>
2022-12-28 06:20:17 +00:00
#include <cstring>
#include <iostream>
2020-07-23 00:54:58 +00:00
#include <list>
2023-03-09 01:36:39 +00:00
#include "common.h"
2020-07-23 00:54:58 +00:00
using namespace std;
using namespace Magick;
2023-03-09 01:36:39 +00:00
char *Spin(string type, string *outType, char *BufferData, size_t BufferLength,
[[maybe_unused]] ArgumentMap Arguments, size_t *DataSize) {
2022-12-28 06:20:17 +00:00
int delay = GetArgumentWithFallback<int>(Arguments, "delay", 0);
2020-07-23 00:54:58 +00:00
2022-12-28 06:20:17 +00:00
Blob blob;
2020-07-23 00:54:58 +00:00
2022-12-28 06:20:17 +00:00
list<Image> frames;
list<Image> coalesced;
list<Image> mid;
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());
2023-03-09 01:36:39 +00:00
if (type != "gif") {
2022-12-28 06:20:17 +00:00
list<Image>::iterator it = coalesced.begin();
for (int i = 0; i < 29; ++i) {
coalesced.push_back(*it);
}
2022-12-28 06:20:17 +00:00
}
2021-02-25 21:09:53 +00:00
2022-12-28 06:20:17 +00:00
int i = 0;
for (Image &image : coalesced) {
image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod);
image.scale(Geometry("256x256"));
image.alphaChannel(Magick::SetAlphaChannel);
double rotation[1] = {(double)360 * i / coalesced.size()};
image.distort(Magick::ScaleRotateTranslateDistortion, 1, rotation);
image.magick("GIF");
mid.push_back(image);
i++;
}
2020-07-23 00:54:58 +00:00
2022-12-28 06:20:17 +00:00
for_each(mid.begin(), mid.end(),
gifDisposeMethodImage(Magick::BackgroundDispose));
2020-07-23 00:54:58 +00:00
2022-12-28 06:20:17 +00:00
optimizeTransparency(mid.begin(), mid.end());
if (delay != 0) {
for_each(mid.begin(), mid.end(), animationDelayImage(delay));
2023-03-09 01:36:39 +00:00
} else if (type != "gif") {
2022-12-28 06:20:17 +00:00
for_each(mid.begin(), mid.end(), animationDelayImage(5));
}
2020-07-23 00:54:58 +00:00
2022-12-28 06:20:17 +00:00
for (Image &image : mid) {
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
image.quantize();
}
2020-07-23 00:54:58 +00:00
2022-12-28 06:20:17 +00:00
writeImages(mid.begin(), mid.end(), &blob);
2023-03-09 01:36:39 +00:00
*outType = "gif";
2022-12-28 06:20:17 +00:00
*DataSize = blob.length();
2020-07-23 00:54:58 +00:00
2022-12-28 06:20:17 +00:00
char *data = (char *)malloc(*DataSize);
memcpy(data, blob.data(), *DataSize);
return data;
}