diff --git a/webassembly/webp.c b/webassembly/webp.c new file mode 100644 index 0000000..108de86 --- /dev/null +++ b/webassembly/webp.c @@ -0,0 +1,44 @@ +#include "emscripten.h" +#include "src/webp/encode.h" +#include + +EMSCRIPTEN_KEEPALIVE +int version() { + return WebPGetEncoderVersion(); +} + +EMSCRIPTEN_KEEPALIVE +uint8_t* create_buffer(int width, int height) { + return malloc(width * height * 4 * sizeof(uint8_t)); +} + +EMSCRIPTEN_KEEPALIVE +void destroy_buffer(uint8_t* p) { + free(p); +} + +int result[2]; +EMSCRIPTEN_KEEPALIVE +void encode(uint8_t* img_in, int width, int height, float quality) { + uint8_t* img_out; + size_t size; + size = WebPEncodeRGBA(img_in, width, height, width * 4, quality, &img_out); + result[0] = (int)img_out; + result[1] = size; +} + +EMSCRIPTEN_KEEPALIVE +void free_result(uint8_t* result) { + WebPFree(result); +} + +EMSCRIPTEN_KEEPALIVE +int get_result_pointer() { + return result[0]; +} + +EMSCRIPTEN_KEEPALIVE +int get_result_size() { + return result[1]; +} +