2022-12-12 18:13:03 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
2022-03-09 19:10:10 +00:00
|
|
|
#include <vips/vips8>
|
2020-07-12 15:14:39 +00:00
|
|
|
|
2022-12-28 23:01:50 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
2020-07-12 15:14:39 +00:00
|
|
|
using namespace std;
|
2022-03-09 19:10:10 +00:00
|
|
|
using namespace vips;
|
2020-07-12 15:14:39 +00:00
|
|
|
|
2022-12-28 06:20:17 +00:00
|
|
|
char *Freeze(string *type, char *BufferData, size_t BufferLength,
|
2022-12-12 18:13:03 +00:00
|
|
|
ArgumentMap Arguments, size_t *DataSize) {
|
|
|
|
bool loop = GetArgumentWithFallback<bool>(Arguments, "loop", false);
|
|
|
|
int frame = GetArgumentWithFallback<int>(Arguments, "frame", -1);
|
|
|
|
|
|
|
|
char *fileData = (char *)malloc(BufferLength);
|
|
|
|
memcpy(fileData, BufferData, BufferLength);
|
|
|
|
|
|
|
|
char *match = (char *)"\x21\xFF\x0BNETSCAPE2.0\x03\x01";
|
|
|
|
char *descriptor = (char *)"\x2C\x00\x00\x00\x00";
|
|
|
|
char *lastPos;
|
|
|
|
|
|
|
|
bool none = true;
|
|
|
|
|
|
|
|
if (loop) {
|
|
|
|
char *newData = (char *)malloc(BufferLength + 19);
|
|
|
|
memcpy(newData, fileData, BufferLength);
|
|
|
|
lastPos = (char *)memchr(newData, '\x2C', BufferLength);
|
|
|
|
while (lastPos != NULL) {
|
|
|
|
if (memcmp(lastPos, descriptor, 5) != 0) {
|
|
|
|
lastPos = (char *)memchr(lastPos + 1, '\x2C',
|
|
|
|
(BufferLength - (lastPos - newData)) - 1);
|
|
|
|
continue;
|
2021-05-13 22:11:13 +00:00
|
|
|
}
|
2022-12-12 18:13:03 +00:00
|
|
|
|
|
|
|
memcpy(lastPos + 19, lastPos, (BufferLength - (lastPos - newData)));
|
|
|
|
memcpy(lastPos, match, 16);
|
|
|
|
memcpy(lastPos + 16, "\x00\x00\x00", 3);
|
|
|
|
|
|
|
|
none = false;
|
|
|
|
*DataSize = BufferLength + 19;
|
|
|
|
break;
|
|
|
|
}
|
2022-12-28 23:01:50 +00:00
|
|
|
if (none) *DataSize = BufferLength;
|
2022-12-12 18:13:03 +00:00
|
|
|
|
|
|
|
return newData;
|
|
|
|
} else if (frame >= 0 && !loop) {
|
|
|
|
VOption *options = VImage::option()->set("access", "sequential");
|
|
|
|
|
2022-12-28 23:01:50 +00:00
|
|
|
VImage in = VImage::new_from_buffer(
|
|
|
|
BufferData, BufferLength, "",
|
|
|
|
*type == "gif" ? options->set("n", -1) : options)
|
|
|
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
|
|
|
if (!in.has_alpha()) in = in.bandjoin(255);
|
2022-12-12 18:13:03 +00:00
|
|
|
|
|
|
|
int pageHeight = vips_image_get_page_height(in.get_image());
|
|
|
|
int nPages = vips_image_get_n_pages(in.get_image());
|
|
|
|
int framePos = clamp(frame, 0, (int)nPages);
|
|
|
|
VImage out = in.crop(0, 0, in.width(), pageHeight * (framePos + 1));
|
|
|
|
out.set(VIPS_META_PAGE_HEIGHT, pageHeight);
|
|
|
|
out.set("loop", 1);
|
|
|
|
|
|
|
|
void *buf;
|
2022-12-28 06:20:17 +00:00
|
|
|
out.write_to_buffer(("." + *type).c_str(), &buf, DataSize);
|
2022-12-12 18:13:03 +00:00
|
|
|
|
|
|
|
return (char *)buf;
|
|
|
|
} else {
|
|
|
|
lastPos = (char *)memchr(fileData, '\x21', BufferLength);
|
|
|
|
while (lastPos != NULL) {
|
|
|
|
if (memcmp(lastPos, match, 16) != 0) {
|
|
|
|
lastPos = (char *)memchr(lastPos + 1, '\x21',
|
|
|
|
(BufferLength - (lastPos - fileData)) - 1);
|
|
|
|
continue;
|
2021-05-13 22:11:13 +00:00
|
|
|
}
|
2022-12-12 18:13:03 +00:00
|
|
|
memcpy(lastPos, lastPos + 19, (BufferLength - (lastPos - fileData)) - 19);
|
|
|
|
*DataSize = BufferLength - 19;
|
|
|
|
none = false;
|
|
|
|
break;
|
2021-05-13 22:11:13 +00:00
|
|
|
}
|
2022-12-28 23:01:50 +00:00
|
|
|
if (none) *DataSize = BufferLength;
|
2021-04-26 14:47:03 +00:00
|
|
|
|
2022-12-12 18:13:03 +00:00
|
|
|
return fileData;
|
2021-04-24 22:54:47 +00:00
|
|
|
}
|
2022-01-15 06:04:34 +00:00
|
|
|
}
|