Update to newer memory management model

This commit is contained in:
Ingvar Stepanyan
2020-11-02 17:29:02 +00:00
committed by Ingvar Stepanyan
parent 83d5357ac0
commit bb6893e025
10 changed files with 114 additions and 152 deletions

View File

@@ -1,40 +1,24 @@
#include <cstdio>
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <cstdio>
#include "src/wp2/decode.h"
using namespace emscripten;
class RawImage {
public:
val buffer;
int width;
int height;
thread_local const val Uint8ClampedArray = val::global("Uint8ClampedArray");
thread_local const val ImageData = val::global("ImageData");
RawImage(val b, int w, int h) : buffer(b), width(w), height(h) {}
};
WP2::ArgbBuffer *buffer = new WP2::ArgbBuffer(WP2_rgbA_32);
RawImage decode(std::string image_in) {
WP2Status status;
status = WP2::Decode(image_in, buffer);
val decode(std::string image_in) {
WP2::ArgbBuffer buffer(WP2_rgbA_32);
WP2Status status = WP2::Decode(image_in, &buffer);
if (status != WP2_STATUS_OK) {
return RawImage(val::null(), -1, -1);
return val::null();
}
return RawImage(val(typed_memory_view(buffer->width * buffer->height * 4,
(uint8_t *)buffer->GetRow(0))),
buffer->width, buffer->height);
return ImageData.new_(
Uint8ClampedArray.new_(typed_memory_view(buffer.stride * buffer.height, buffer.GetRow8(0))),
buffer.width, buffer.height);
}
void free_result() { delete buffer; }
EMSCRIPTEN_BINDINGS(my_module) {
class_<RawImage>("RawImage")
.property("buffer", &RawImage::buffer)
.property("width", &RawImage::width)
.property("height", &RawImage::height);
function("decode", &decode);
function("free_result", &free_result);
}