-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding.cpp
77 lines (61 loc) · 2.09 KB
/
binding.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdlib.h>
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include <emscripten/wire.h>
#include <emscripten/bind.h>
#include "apngopt.hpp"
using namespace emscripten;
struct Options {
int deflate_method = 1;
int iter = 15;
int min_quality = 0;
int max_quality = 100;
bool disabled_quant = false;
};
struct OptimizerResult {
long bufferPtr;
long size;
};
int main() { }
OptimizerResult optAPNG(long pngBufferPtr, long size, Options options, long callbackPtr) {
unsigned int first, loops, coltype;
std::shared_ptr<APNGOpt> apngOpt = std::make_shared<APNGOpt>(reinterpret_cast<void (*)(float)>(callbackPtr));
std::vector<APNGFrame> frames;
OptimizerResult result = OptimizerResult({ 0, 0 });
int res = apngOpt->load_apng(reinterpret_cast<void *>(pngBufferPtr), size, frames, first, loops);
if (res >= 0)
{
apngOpt->optim_dirty(frames);
apngOpt->optim_duplicates(frames, first);
if(!options.disabled_quant) {
apngOpt->optim_image(frames, coltype, options.min_quality, options.max_quality);
}
else {
apngOpt->optim_downconvert(frames, coltype);
}
long bufferPtr;
result.size = (long) apngOpt->save_apng(bufferPtr, frames, first, loops, coltype, options.deflate_method, options.iter);
result.bufferPtr = bufferPtr;
}
for (size_t j = 0; j < frames.size(); j++)
{
delete[] frames[j].rows;
delete[] frames[j].p;
}
frames.clear();
return result;
}
EMSCRIPTEN_BINDINGS(apng_optimizer_module) {
value_object<Options>("Options")
.field("deflate_method", &Options::deflate_method)
.field("iter", &Options::iter)
.field("min_quality", &Options::min_quality)
.field("max_quality", &Options::max_quality)
.field("disabled_quant", &Options::disabled_quant)
;
value_object<OptimizerResult>("OptimizerResult")
.field("bufferPtr", &OptimizerResult::bufferPtr)
.field("size", &OptimizerResult::size)
;
function("optAPNG", &optAPNG);
}