mirror of
https://github.com/codedread/bitjs
synced 2025-10-03 09:39:16 +02:00
Fix JPG function referencing detached heap. Also, optimize WASM module output.
This commit is contained in:
parent
9dc09e22f4
commit
3ad0fa4473
5 changed files with 12 additions and 9 deletions
|
@ -12,12 +12,15 @@ debug: ${OUT_TARGET}
|
||||||
clean:
|
clean:
|
||||||
rm -rf ${OUT_PATH}/${OUT_NAME}.*
|
rm -rf ${OUT_PATH}/${OUT_NAME}.*
|
||||||
|
|
||||||
${OUT_TARGET}: ${SHIM_SRC}
|
# Debug:
|
||||||
emcc -O3 \
|
# -O0
|
||||||
|
# Optimizations:
|
||||||
|
# -Os --llvm-lto 1
|
||||||
|
${OUT_TARGET}: ${SHIM_SRC} Makefile
|
||||||
|
emcc -Os --llvm-lto 1 \
|
||||||
-s WASM=1 \
|
-s WASM=1 \
|
||||||
-s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap"]' \
|
-s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap"]' \
|
||||||
-s ALLOW_MEMORY_GROWTH=1 \
|
-s ALLOW_MEMORY_GROWTH=1 \
|
||||||
-s ASSERTIONS=1 \
|
|
||||||
-I stb \
|
-I stb \
|
||||||
-I libwebp \
|
-I libwebp \
|
||||||
${LIBWEBP_SRC} \
|
${LIBWEBP_SRC} \
|
||||||
|
|
|
@ -63,7 +63,7 @@ void write_image_to_mem(void* context, void* data, int size) {
|
||||||
|
|
||||||
uint8_t* decode_webp_to_rgba(uint8_t* webp_ptr, size_t size, int* width, int* height) {
|
uint8_t* decode_webp_to_rgba(uint8_t* webp_ptr, size_t size, int* width, int* height) {
|
||||||
if (!webp_ptr) {
|
if (!webp_ptr) {
|
||||||
printf("webp_ptr is NULL");
|
printf("decode_webp_to_rgba() called with NULL webp_ptr");
|
||||||
}
|
}
|
||||||
if (!WebPGetInfo(webp_ptr, size, width, height)) {
|
if (!WebPGetInfo(webp_ptr, size, width, height)) {
|
||||||
fprintf(stderr, "WebPGetInfo() returned an error\n");
|
fprintf(stderr, "WebPGetInfo() returned an error\n");
|
||||||
|
|
File diff suppressed because one or more lines are too long
Binary file not shown.
|
@ -20,8 +20,8 @@ function loadWebPShimApi() {
|
||||||
return loadingPromise = new Promise((resolve, reject) => {
|
return loadingPromise = new Promise((resolve, reject) => {
|
||||||
const scriptEl = document.createElement('script');
|
const scriptEl = document.createElement('script');
|
||||||
scriptEl.onload = () => {
|
scriptEl.onload = () => {
|
||||||
Module.print = str => console.log(str);
|
Module.print = str => console.log(`${Date.now()}: ${str}`);
|
||||||
Module.printErr = str => console.error(str);
|
Module.printErr = str => console.error(`${Date.now()}: ${str}`);
|
||||||
Module.onRuntimeInitialized = () => {
|
Module.onRuntimeInitialized = () => {
|
||||||
api = {
|
api = {
|
||||||
createWASMBuffer: Module.cwrap('create_buffer', 'number', ['number', 'number']),
|
createWASMBuffer: Module.cwrap('create_buffer', 'number', ['number', 'number']),
|
||||||
|
@ -76,13 +76,13 @@ export function convertWebPtoJPG(webpBuffer) {
|
||||||
// Create a buffer of the WebP bytes that we can send into WASM-land.
|
// Create a buffer of the WebP bytes that we can send into WASM-land.
|
||||||
const size = webpBuffer.byteLength;
|
const size = webpBuffer.byteLength;
|
||||||
const webpWASMBuffer = api.createWASMBuffer(size);
|
const webpWASMBuffer = api.createWASMBuffer(size);
|
||||||
api.heap.set(webpBuffer, webpWASMBuffer);
|
api.module.HEAPU8.set(webpBuffer, webpWASMBuffer);
|
||||||
|
|
||||||
// Convert to JPG.
|
// Convert to JPG.
|
||||||
const jpgHandle = api.getJPGHandle(webpWASMBuffer, size);
|
const jpgHandle = api.getJPGHandle(webpWASMBuffer, size);
|
||||||
const numJPGBytes = api.getNumBytesFromHandle(jpgHandle);
|
const numJPGBytes = api.getNumBytesFromHandle(jpgHandle);
|
||||||
const jpgBufPtr = api.getImageBytesFromHandle(jpgHandle);
|
const jpgBufPtr = api.getImageBytesFromHandle(jpgHandle);
|
||||||
const jpgBuffer = api.heap.slice(jpgBufPtr, jpgBufPtr + numJPGBytes - 1);
|
const jpgBuffer = api.module.HEAPU8.slice(jpgBufPtr, jpgBufPtr + numJPGBytes - 1);
|
||||||
|
|
||||||
// Cleanup.
|
// Cleanup.
|
||||||
api.releaseImageHandle(jpgHandle);
|
api.releaseImageHandle(jpgHandle);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue