Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | 428x 428x | // https://emscripten.org/docs/api_reference/module.html import type { J2KDecoder, OpenJpegModule, } from '@cornerstonejs/codec-openjpeg/dist/openjpegwasm_decode'; // @ts-ignore import openJpegFactory from '@cornerstonejs/codec-openjpeg/decodewasmjs'; // Webpack asset/resource copies this to our output folder // TODO: At some point maybe we can use this instead. // This is closer to what Webpack 5 wants but it doesn't seem to work now // const wasm = new URL('./blah.wasm', import.meta.url) // @ts-ignore // import openjpegWasm from '@cornerstonejs/codec-openjpeg/decodewasm'; const openjpegWasm = new URL( '@cornerstonejs/codec-openjpeg/decodewasm', import.meta.url ); import type { Types } from '@cornerstonejs/core'; import type { WebWorkerDecodeConfig } from '../../types'; const local: { codec: OpenJpegModule; decoder: J2KDecoder; decodeConfig: WebWorkerDecodeConfig; } = { codec: undefined, decoder: undefined, decodeConfig: {} as WebWorkerDecodeConfig, }; export function initialize( decodeConfig?: WebWorkerDecodeConfig ): Promise<void> { local.decodeConfig = decodeConfig; if (local.codec) { return Promise.resolve(); } const openJpegModule = openJpegFactory({ locateFile: (f) => { if (f.endsWith('.wasm')) { return openjpegWasm.toString(); } return f; }, }); return new Promise((resolve, reject) => { openJpegModule.then((instance) => { local.codec = instance; local.decoder = new instance.J2KDecoder(); resolve(); }, reject); }); } // https://github.com/chafey/openjpegjs/blob/master/test/browser/index.html async function decodeAsync( compressedImageFrame, imageInfo ): Promise<Types.IImageFrame> { await initialize(); const decoder = local.decoder; // get pointer to the source/encoded bit stream buffer in WASM memory // that can hold the encoded bitstream const encodedBufferInWASM = decoder.getEncodedBuffer( compressedImageFrame.length ); // copy the encoded bitstream into WASM memory buffer encodedBufferInWASM.set(compressedImageFrame); // decode it decoder.decode(); // decoder.decodeSubResolution(decodeLevel, decodeLayer); // const resolutionAtLevel = decoder.calculateSizeAtDecompositionLevel(decodeLevel); // get information about the decoded image const frameInfo = decoder.getFrameInfo(); // get the decoded pixels const decodedBufferInWASM = decoder.getDecodedBuffer(); const imageFrame = new Uint8Array(decodedBufferInWASM.length); imageFrame.set(decodedBufferInWASM); const imageOffset = `x: ${decoder.getImageOffset().x}, y: ${ decoder.getImageOffset().y }`; const numDecompositions = decoder.getNumDecompositions(); const numLayers = decoder.getNumLayers(); const progessionOrder = ['unknown', 'LRCP', 'RLCP', 'RPCL', 'PCRL', 'CPRL'][ decoder.getProgressionOrder() + 1 ]; const reversible = decoder.getIsReversible(); const blockDimensions = `${decoder.getBlockDimensions().width} x ${ decoder.getBlockDimensions().height }`; const tileSize = `${decoder.getTileSize().width} x ${ decoder.getTileSize().height }`; const tileOffset = `${decoder.getTileOffset().x}, ${ decoder.getTileOffset().y }`; const colorTransform = decoder.getColorSpace(); const decodedSize = `${decodedBufferInWASM.length.toLocaleString()} bytes`; const compressionRatio = `${( decodedBufferInWASM.length / encodedBufferInWASM.length ).toFixed(2)}:1`; const encodedImageInfo = { columns: frameInfo.width, rows: frameInfo.height, bitsPerPixel: frameInfo.bitsPerSample, signed: frameInfo.isSigned, bytesPerPixel: imageInfo.bytesPerPixel, componentsPerPixel: frameInfo.componentCount, }; const pixelData = getPixelData(frameInfo, decodedBufferInWASM); const encodeOptions = { imageOffset, numDecompositions, numLayers, progessionOrder, reversible, blockDimensions, tileSize, tileOffset, colorTransform, decodedSize, compressionRatio, }; return { ...imageInfo, pixelData, imageInfo: encodedImageInfo, encodeOptions, ...encodeOptions, ...encodedImageInfo, }; } function getPixelData(frameInfo, decodedBuffer) { if (frameInfo.bitsPerSample > 8) { if (frameInfo.isSigned) { return new Int16Array( decodedBuffer.buffer, decodedBuffer.byteOffset, decodedBuffer.byteLength / 2 ); } return new Uint16Array( decodedBuffer.buffer, decodedBuffer.byteOffset, decodedBuffer.byteLength / 2 ); } if (frameInfo.isSigned) { return new Int8Array( decodedBuffer.buffer, decodedBuffer.byteOffset, decodedBuffer.byteLength ); } return new Uint8Array( decodedBuffer.buffer, decodedBuffer.byteOffset, decodedBuffer.byteLength ); } export default decodeAsync; |