summaryrefslogtreecommitdiff
path: root/src/compress_png.cpp
blob: 068abe65f32e434f09d8d49bb619d7c769aac5b0 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "compress_png.hpp"
#include <mbgl/util/image.hpp>

namespace node_mbgl {

class CompressPNGWorker : public NanAsyncWorker {
public:
    CompressPNGWorker(NanCallback *callback_, v8::Local<v8::Object> buffer_, uint32_t width_,
                      uint32_t height_)
        : NanAsyncWorker(callback_),
          buffer(v8::Persistent<v8::Object>::New(buffer_)),
          data(node::Buffer::Data(buffer_)),
          width(width_),
          height(height_) {
        assert(width * height * 4 == node::Buffer::Length(buffer_));
    }

    ~CompressPNGWorker() {
        buffer.Dispose();
    }

    void Execute() {
        result = mbgl::util::compress_png(width, height, data);
    }

    void HandleOKCallback() {
        NanScope();

        auto img = new std::string(std::move(result));

        v8::Local<v8::Value> argv[] = {
            NanNull(),
            NanNewBufferHandle(
                const_cast<char *>(img->data()),
                img->size(),
                // Retain the std::string until the buffer is deleted.
                [](char *, void *hint) {
                    delete reinterpret_cast<std::string *>(hint);
                },
                img
            )
        };

        callback->Call(2, argv);
    };

private:
    // Retains the buffer while this worker is processing. The user may not modify the buffer.
    v8::Persistent<v8::Object> buffer;
    void *data;
    const uint32_t width;
    const uint32_t height;

    // Stores the compressed PNG.
    std::string result;
};

NAN_METHOD(CompressPNG) {
    NanScope();

    if (args.Length() <= 0 || !args[0]->IsObject()) {
        return NanThrowTypeError("First argument must be the data object");
    }

    uint32_t width = 0;
    uint32_t height = 0;
    v8::Local<v8::Object> buffer;

    auto options = args[0]->ToObject();
    if (options->Has(NanNew("width"))) {
        width = options->Get(NanNew("width"))->Uint32Value();
    }
    if (!width) {
        NanThrowRangeError("Image width must be greater than 0");
    }
    if (options->Has(NanNew("height"))) {
        height = options->Get(NanNew("height"))->Uint32Value();
    }
    if (!height) {
        NanThrowRangeError("Image height must be greater than 0");
    }
    if (options->Has(NanNew("pixels"))) {
        buffer = options->Get(NanNew("pixels")).As<v8::Object>();
    }
    if (!node::Buffer::HasInstance(buffer)) {
        NanThrowTypeError("Pixels must be a Buffer object");
    }
    if (width * height * 4 != node::Buffer::Length(buffer)) {
        NanThrowError("Pixel buffer doesn't match image dimensions");
    }

    if (args.Length() < 2) {
        NanThrowTypeError("Second argument must be a callback function");
    }
    NanCallback *callback = new NanCallback(args[1].As<v8::Function>());

    NanAsyncQueueWorker(new CompressPNGWorker(callback, buffer, width, height));
    NanReturnUndefined();
}

void InitCompressPNG(v8::Handle<v8::Object> target) {
    target->Set(NanNew<v8::String>("compressPNG"),
                NanNew<v8::FunctionTemplate>(CompressPNG)->GetFunction());
}

}