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> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Freeze(const Napi::CallbackInfo &info) { char *Freeze(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env);
try { bool loop = GetArgumentWithFallback<bool>(Arguments, "loop", false);
Napi::Object obj = info[1].As<Napi::Object>(); int frame = GetArgumentWithFallback<int>(Arguments, "frame", -1);
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;
char *fileData = data.Data(); char *fileData = (char *)malloc(BufferLength);
char *match = (char *)"\x21\xFF\x0BNETSCAPE2.0\x03\x01"; memcpy(fileData, BufferData, BufferLength);
char *descriptor = (char *)"\x2C\x00\x00\x00\x00";
char *lastPos;
bool none = true; char *match = (char *)"\x21\xFF\x0BNETSCAPE2.0\x03\x01";
char *descriptor = (char *)"\x2C\x00\x00\x00\x00";
char *lastPos;
if (loop) { bool none = true;
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;
}
memcpy(lastPos + 19, lastPos, (data.Length() - (lastPos - newData))); if (loop) {
memcpy(lastPos, match, 16); char *newData = (char *)malloc(BufferLength + 19);
memcpy(lastPos + 16, "\x00\x00\x00", 3); memcpy(newData, fileData, BufferLength);
result.Set("data", lastPos = (char *)memchr(newData, '\x2C', BufferLength);
Napi::Buffer<char>::Copy(env, newData, data.Length() + 19)); while (lastPos != NULL) {
none = false; if (memcmp(lastPos, descriptor, 5) != 0) {
break; 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(), "", memcpy(lastPos + 19, lastPos, (BufferLength - (lastPos - newData)));
type == "gif" ? options->set("n", -1) memcpy(lastPos, match, 16);
: options) memcpy(lastPos + 16, "\x00\x00\x00", 3);
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha())
in = in.bandjoin(255);
int pageHeight = vips_image_get_page_height(in.get_image()); none = false;
int nPages = vips_image_get_n_pages(in.get_image()); *DataSize = BufferLength + 19;
int framePos = clamp(frame, 0, (int)nPages); break;
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()));
} }
if (none)
*DataSize = BufferLength;
result.Set("type", type); vips_error_clear();
} catch (std::exception const &err) { vips_thread_shutdown();
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) { return newData;
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } 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 #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}, {"explode", &Explode},
{"flag", &Flag}, {"flag", &Flag},
{"flip", &Flip}, {"flip", &Flip},
{"freeze", &Freeze},
{"speed", &Speed},
{"uncaption", &Uncaption}, {"uncaption", &Uncaption},
{"watermark", &Watermark} {"watermark", &Watermark}
}; };
std::map<std::string, Napi::Value (*)(const Napi::CallbackInfo &info)> OldFunctionMap = { std::map<std::string, Napi::Value (*)(const Napi::CallbackInfo &info)> OldFunctionMap = {
{"speed", Speed},
{"freeze", Freeze},
{"gamexplain", Gamexplain}, {"gamexplain", Gamexplain},
{"globe", Globe}, {"globe", Globe},
{"homebrew", Homebrew}, {"homebrew", Homebrew},

View file

@ -1,4 +1,7 @@
#include <napi.h> #include "common.h"
#include <iostream>
#include <map>
#include <vips/vips8> #include <vips/vips8>
@ -8,18 +11,18 @@ using namespace vips;
void *memset16(void *m, uint16_t val, size_t count) { void *memset16(void *m, uint16_t val, size_t count) {
uint16_t *buf = (uint16_t *)m; uint16_t *buf = (uint16_t *)m;
while (count--) *buf++ = val; while (count--)
*buf++ = val;
return m; return m;
} }
void vipsRemove(Napi::Env *env, Napi::Object *result, Napi::Buffer<char> data, char *vipsRemove(char *data, size_t length, size_t *DataSize, int speed) {
int speed) {
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "", VImage in = VImage::new_from_buffer(data, length, "", options->set("n", -1))
options->set("n", -1))
.colourspace(VIPS_INTERPRETATION_sRGB); .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 width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image()); 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); out.set(VIPS_META_PAGE_HEIGHT, pageHeight);
void *buf; void *buf;
size_t length; out.write_to_buffer(".gif", &buf, DataSize);
out.write_to_buffer(".gif", &buf, &length);
result->Set("data", Napi::Buffer<char>::Copy(*env, (char *)buf, length)); return (char *)buf;
} }
Napi::Value Speed(const Napi::CallbackInfo &info) { char *Speed(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env);
try { bool slow = GetArgumentWithFallback<bool>(Arguments, "slow", false);
Napi::Object obj = info[1].As<Napi::Object>(); int speed = GetArgumentWithFallback<int>(Arguments, "speed", 2);
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;
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; vector<uint16_t> old_delays;
bool removeFrames = false; bool removeFrames = false;
char *lastPos; char *lastPos;
int amount = 0; int amount = 0;
lastPos = (char *)memchr(fileData, '\x00', data.Length()); lastPos = (char *)memchr(fileData, '\x00', BufferLength);
while (lastPos != NULL) { while (lastPos != NULL) {
if (memcmp(lastPos, match, 4) != 0) { 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(lastPos + 1, '\x00', 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; memset16(lastPos + 5, new_delay, 1);
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); lastPos = (char *)memchr(lastPos + 1, '\x00',
(BufferLength - (lastPos - fileData)) - 1);
++currentFrame;
}
lastPos = (char *)memchr(lastPos + 1, '\x00', if (removeFrames) {
(data.Length() - (lastPos - fileData)) - 1); fileData = vipsRemove(BufferData, BufferLength, DataSize, speed);
++currentFrame; } else {
} *DataSize = BufferLength;
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();
} }
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result;
return fileData;
} }

View file

@ -1,5 +1,7 @@
#pragma once #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);