1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-03 09:39:16 +02:00

Correct error, by creating a Uint8Array, in JPG converter function

This commit is contained in:
codedread 2020-03-16 12:04:04 -07:00
parent 3ad0fa4473
commit c85f5ae2cb

View file

@ -56,9 +56,13 @@ export function convertWebPtoPNG(webpBuffer) {
// Convert to PNG.
const pngHandle = api.getPNGHandle(webpWASMBuffer, size);
const numBytes = api.getNumBytesFromHandle(pngHandle);
if (!pngHandle) {
api.destroyWASMBuffer(webpWASMBuffer);
return null;
}
const numPNGBytes = api.getNumBytesFromHandle(pngHandle);
const pngBufPtr = api.getImageBytesFromHandle(pngHandle);
let pngBuffer = api.module.HEAPU8.slice(pngBufPtr, pngBufPtr + numBytes - 1);
let pngBuffer = api.module.HEAPU8.slice(pngBufPtr, pngBufPtr + numPNGBytes - 1);
// Cleanup.
api.releaseImageHandle(pngHandle);
@ -74,12 +78,17 @@ export function convertWebPtoPNG(webpBuffer) {
export function convertWebPtoJPG(webpBuffer) {
return loadWebPShimApi().then((api) => {
// Create a buffer of the WebP bytes that we can send into WASM-land.
const size = webpBuffer.byteLength;
const webpArray = new Uint8Array(webpBuffer);
const size = webpArray.byteLength;
const webpWASMBuffer = api.createWASMBuffer(size);
api.module.HEAPU8.set(webpBuffer, webpWASMBuffer);
api.module.HEAPU8.set(webpArray, webpWASMBuffer);
// Convert to JPG.
const jpgHandle = api.getJPGHandle(webpWASMBuffer, size);
if (!jpgHandle) {
api.destroyWASMBuffer(webpWASMBuffer);
return null;
}
const numJPGBytes = api.getNumBytesFromHandle(jpgHandle);
const jpgBufPtr = api.getImageBytesFromHandle(jpgHandle);
const jpgBuffer = api.module.HEAPU8.slice(jpgBufPtr, jpgBufPtr + numJPGBytes - 1);