Convert freeze and speed

This commit is contained in:
Essem 2022-12-12 12:13:03 -06:00
parent 14d5965caa
commit 60cac58043
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
5 changed files with 149 additions and 160 deletions

View file

@ -1,103 +1,96 @@
#include <napi.h>
#include "common.h"
#include <algorithm>
#include <map>
#include <vips/vips8>
using namespace std;
using namespace vips;
Napi::Value Freeze(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Object result = Napi::Object::New(env);
char *Freeze(string type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) {
try {
Napi::Object obj = info[1].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
bool loop =
obj.Has("loop") ? obj.Get("loop").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int frame = obj.Has("frame")
? obj.Get("frame").As<Napi::Number>().Int32Value()
: -1;
bool loop = GetArgumentWithFallback<bool>(Arguments, "loop", false);
int frame = GetArgumentWithFallback<int>(Arguments, "frame", -1);
char *fileData = data.Data();
char *match = (char *)"\x21\xFF\x0BNETSCAPE2.0\x03\x01";
char *descriptor = (char *)"\x2C\x00\x00\x00\x00";
char *lastPos;
char *fileData = (char *)malloc(BufferLength);
memcpy(fileData, BufferData, BufferLength);
bool none = true;
char *match = (char *)"\x21\xFF\x0BNETSCAPE2.0\x03\x01";
char *descriptor = (char *)"\x2C\x00\x00\x00\x00";
char *lastPos;
if (loop) {
char *newData = (char *)malloc(data.Length() + 19);
memcpy(newData, fileData, data.Length());
lastPos = (char *)memchr(newData, '\x2C', data.Length());
while (lastPos != NULL) {
if (memcmp(lastPos, descriptor, 5) != 0) {
lastPos = (char *)memchr(lastPos + 1, '\x2C',
(data.Length() - (lastPos - newData)) - 1);
continue;
}
bool none = true;
memcpy(lastPos + 19, lastPos, (data.Length() - (lastPos - newData)));
memcpy(lastPos, match, 16);
memcpy(lastPos + 16, "\x00\x00\x00", 3);
result.Set("data",
Napi::Buffer<char>::Copy(env, newData, data.Length() + 19));
none = false;
break;
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;
}
if (none)
result.Set("data",
Napi::Buffer<char>::Copy(env, newData, data.Length()));
} else if (frame >= 0 && !loop) {
VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "",
type == "gif" ? options->set("n", -1)
: options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha())
in = in.bandjoin(255);
memcpy(lastPos + 19, lastPos, (BufferLength - (lastPos - newData)));
memcpy(lastPos, match, 16);
memcpy(lastPos + 16, "\x00\x00\x00", 3);
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", loop ? 0 : 1);
void *buf;
size_t length;
out.write_to_buffer(("." + type).c_str(), &buf, &length);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
} else {
lastPos = (char *)memchr(fileData, '\x21', data.Length());
while (lastPos != NULL) {
if (memcmp(lastPos, match, 16) != 0) {
lastPos = (char *)memchr(lastPos + 1, '\x21',
(data.Length() - (lastPos - fileData)) - 1);
continue;
}
memcpy(lastPos, lastPos + 19,
(data.Length() - (lastPos - fileData)) - 19);
result.Set("data",
Napi::Buffer<char>::Copy(env, fileData, data.Length() - 19));
none = false;
break;
}
if (none)
result.Set("data",
Napi::Buffer<char>::Copy(env, fileData, data.Length()));
none = false;
*DataSize = BufferLength + 19;
break;
}
if (none)
*DataSize = BufferLength;
result.Set("type", type);
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
vips_error_clear();
vips_thread_shutdown();
return newData;
} else if (frame >= 0 && !loop) {
VOption *options = VImage::option()->set("access", "sequential");
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);
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;
out.write_to_buffer(("." + type).c_str(), &buf, DataSize);
vips_error_clear();
vips_thread_shutdown();
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;
}
memcpy(lastPos, lastPos + 19, (BufferLength - (lastPos - fileData)) - 19);
*DataSize = BufferLength - 19;
none = false;
break;
}
if (none)
*DataSize = BufferLength;
vips_error_clear();
vips_thread_shutdown();
return fileData;
}
vips_error_clear();
vips_thread_shutdown();
return result;
}

View file

@ -1,5 +1,7 @@
#pragma once
#include <napi.h>
#include "common.h"
Napi::Value Freeze(const Napi::CallbackInfo& info);
using std::string;
char* Freeze(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -62,13 +62,13 @@ std::map<std::string, char* (*)(string type, char* BufferData, size_t BufferLeng
{"explode", &Explode},
{"flag", &Flag},
{"flip", &Flip},
{"freeze", &Freeze},
{"speed", &Speed},
{"uncaption", &Uncaption},
{"watermark", &Watermark}
};
std::map<std::string, Napi::Value (*)(const Napi::CallbackInfo &info)> OldFunctionMap = {
{"speed", Speed},
{"freeze", Freeze},
{"gamexplain", Gamexplain},
{"globe", Globe},
{"homebrew", Homebrew},

View file

@ -1,4 +1,7 @@
#include <napi.h>
#include "common.h"
#include <iostream>
#include <map>
#include <vips/vips8>
@ -8,18 +11,18 @@ using namespace vips;
void *memset16(void *m, uint16_t val, size_t count) {
uint16_t *buf = (uint16_t *)m;
while (count--) *buf++ = val;
while (count--)
*buf++ = val;
return m;
}
void vipsRemove(Napi::Env *env, Napi::Object *result, Napi::Buffer<char> data,
int speed) {
char *vipsRemove(char *data, size_t length, size_t *DataSize, int speed) {
VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "",
options->set("n", -1))
VImage in = VImage::new_from_buffer(data, length, "", options->set("n", -1))
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
if (!in.has_alpha())
in = in.bandjoin(255);
int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());
@ -34,84 +37,73 @@ void vipsRemove(Napi::Env *env, Napi::Object *result, Napi::Buffer<char> data,
out.set(VIPS_META_PAGE_HEIGHT, pageHeight);
void *buf;
size_t length;
out.write_to_buffer(".gif", &buf, &length);
out.write_to_buffer(".gif", &buf, DataSize);
result->Set("data", Napi::Buffer<char>::Copy(*env, (char *)buf, length));
return (char *)buf;
}
Napi::Value Speed(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Object result = Napi::Object::New(env);
char *Speed(string type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) {
try {
Napi::Object obj = info[1].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
bool slow =
obj.Has("slow") ? obj.Get("slow").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int speed =
obj.Has("speed") ? obj.Get("speed").As<Napi::Number>().Int32Value() : 2;
bool slow = GetArgumentWithFallback<bool>(Arguments, "slow", false);
int speed = GetArgumentWithFallback<int>(Arguments, "speed", 2);
char *fileData = data.Data();
char *fileData = (char *)malloc(BufferLength);
memcpy(fileData, BufferData, BufferLength);
char *match = (char *)"\x00\x21\xF9\x04";
char *match = (char *)"\x00\x21\xF9\x04";
vector<uint16_t> old_delays;
bool removeFrames = false;
char *lastPos;
vector<uint16_t> old_delays;
bool removeFrames = false;
char *lastPos;
int amount = 0;
int amount = 0;
lastPos = (char *)memchr(fileData, '\x00', data.Length());
while (lastPos != NULL) {
if (memcmp(lastPos, match, 4) != 0) {
lastPos = (char *)memchr(lastPos + 1, '\x00',
(data.Length() - (lastPos - fileData)) - 1);
continue;
}
++amount;
uint16_t old_delay;
memcpy(&old_delay, lastPos + 5, 2);
old_delays.push_back(old_delay);
lastPos = (char *)memchr(fileData, '\x00', BufferLength);
while (lastPos != NULL) {
if (memcmp(lastPos, match, 4) != 0) {
lastPos = (char *)memchr(lastPos + 1, '\x00',
(data.Length() - (lastPos - fileData)) - 1);
(BufferLength - (lastPos - fileData)) - 1);
continue;
}
++amount;
uint16_t old_delay;
memcpy(&old_delay, lastPos + 5, 2);
old_delays.push_back(old_delay);
lastPos = (char *)memchr(lastPos + 1, '\x00',
(BufferLength - (lastPos - fileData)) - 1);
}
int currentFrame = 0;
lastPos = (char *)memchr(fileData, '\x00', BufferLength);
while (lastPos != NULL) {
if (memcmp(lastPos, match, 4) != 0) {
lastPos = (char *)memchr(lastPos + 1, '\x00',
(BufferLength - (lastPos - fileData)) - 1);
continue;
}
uint16_t new_delay = slow ? old_delays[currentFrame] * speed
: old_delays[currentFrame] / speed;
if (!slow && new_delay <= 1) {
removeFrames = true;
break;
}
int currentFrame = 0;
lastPos = (char *)memchr(fileData, '\x00', data.Length());
while (lastPos != NULL) {
if (memcmp(lastPos, match, 4) != 0) {
lastPos = (char *)memchr(lastPos + 1, '\x00',
(data.Length() - (lastPos - fileData)) - 1);
continue;
}
uint16_t new_delay = slow ? old_delays[currentFrame] * speed
: old_delays[currentFrame] / speed;
if (!slow && new_delay <= 1) {
removeFrames = true;
break;
}
memset16(lastPos + 5, new_delay, 1);
memset16(lastPos + 5, new_delay, 1);
lastPos = (char *)memchr(lastPos + 1, '\x00',
(BufferLength - (lastPos - fileData)) - 1);
++currentFrame;
}
lastPos = (char *)memchr(lastPos + 1, '\x00',
(data.Length() - (lastPos - fileData)) - 1);
++currentFrame;
}
result.Set("data", Napi::Buffer<char>::Copy(env, fileData, data.Length()));
if (removeFrames) vipsRemove(&env, &result, data, speed);
result.Set("type", type);
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
if (removeFrames) {
fileData = vipsRemove(BufferData, BufferLength, DataSize, speed);
} else {
*DataSize = BufferLength;
}
vips_error_clear();
vips_thread_shutdown();
return result;
return fileData;
}

View file

@ -1,5 +1,7 @@
#pragma once
#include <napi.h>
#include "common.h"
Napi::Value Speed(const Napi::CallbackInfo &info);
using std::string;
char* Speed(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);