| 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 | 1× 1× 1× 1× | let charLS; function jpegLSDecode (data, isSigned) { // prepare input parameters const dataPtr = charLS._malloc(data.length); charLS.writeArrayToMemory(data, dataPtr); // prepare output parameters const imagePtrPtr = charLS._malloc(4); const imageSizePtr = charLS._malloc(4); const widthPtr = charLS._malloc(4); const heightPtr = charLS._malloc(4); const bitsPerSamplePtr = charLS._malloc(4); const stridePtr = charLS._malloc(4); const allowedLossyErrorPtr = charLS._malloc(4); const componentsPtr = charLS._malloc(4); const interleaveModePtr = charLS._malloc(4); // Decode the image const result = charLS.ccall( 'jpegls_decode', 'number', ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number'], [dataPtr, data.length, imagePtrPtr, imageSizePtr, widthPtr, heightPtr, bitsPerSamplePtr, stridePtr, componentsPtr, allowedLossyErrorPtr, interleaveModePtr] ); // Extract result values into object const image = { result, width: charLS.getValue(widthPtr, 'i32'), height: charLS.getValue(heightPtr, 'i32'), bitsPerSample: charLS.getValue(bitsPerSamplePtr, 'i32'), stride: charLS.getValue(stridePtr, 'i32'), components: charLS.getValue(componentsPtr, 'i32'), allowedLossyError: charLS.getValue(allowedLossyErrorPtr, 'i32'), interleaveMode: charLS.getValue(interleaveModePtr, 'i32'), pixelData: undefined }; // Copy image from emscripten heap into appropriate array buffer type const imagePtr = charLS.getValue(imagePtrPtr, '*'); if (image.bitsPerSample <= 8) { image.pixelData = new Uint8Array(image.width * image.height * image.components); image.pixelData.set(new Uint8Array(charLS.HEAP8.buffer, imagePtr, image.pixelData.length)); } else if (isSigned) { image.pixelData = new Int16Array(image.width * image.height * image.components); image.pixelData.set(new Int16Array(charLS.HEAP16.buffer, imagePtr, image.pixelData.length)); } else { image.pixelData = new Uint16Array(image.width * image.height * image.components); image.pixelData.set(new Uint16Array(charLS.HEAP16.buffer, imagePtr, image.pixelData.length)); } // free memory and return image object charLS._free(dataPtr); charLS._free(imagePtr); charLS._free(imagePtrPtr); charLS._free(imageSizePtr); charLS._free(widthPtr); charLS._free(heightPtr); charLS._free(bitsPerSamplePtr); charLS._free(stridePtr); charLS._free(componentsPtr); charLS._free(interleaveModePtr); return image; } function initializeJPEGLS () { // check to make sure codec is loaded Iif (typeof CharLS === 'undefined') { throw new Error('No JPEG-LS decoder loaded'); } // Try to initialize CharLS // CharLS https://github.com/cornerstonejs/charls Eif (!charLS) { charLS = CharLS(); Iif (!charLS || !charLS._jpegls_decode) { throw new Error('JPEG-LS failed to initialize'); } } } function decodeJPEGLS (imageFrame, pixelData) { initializeJPEGLS(); const image = jpegLSDecode(pixelData, imageFrame.pixelRepresentation === 1); // throw error if not success or too much data if (image.result !== 0 && image.result !== 6) { throw new Error(`JPEG-LS decoder failed to decode frame (error code ${image.result})`); } imageFrame.columns = image.width; imageFrame.rows = image.height; imageFrame.pixelData = image.pixelData; return imageFrame; } export default decodeJPEGLS; export { initializeJPEGLS }; |