2021-04-24 22:54:47 +00:00
|
|
|
#include <Magick++.h>
|
|
|
|
|
2022-12-28 06:20:17 +00:00
|
|
|
#include <cstring>
|
2021-10-23 18:58:19 +00:00
|
|
|
#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,
|
2023-03-08 20:41:13 +00:00
|
|
|
[[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());
|
2021-03-06 17:39:54 +00:00
|
|
|
|
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);
|
2021-10-23 18:58:19 +00:00
|
|
|
}
|
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);
|
2021-04-24 22:54:47 +00:00
|
|
|
|
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;
|
2022-01-15 06:04:34 +00:00
|
|
|
}
|