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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | 428x 428x | import type { ByteArray } from 'dicom-parser'; // @ts-ignore import openJphFactory from '@cornerstonejs/codec-openjph/wasmjs'; // @ts-ignore // import openjphWasm from '@cornerstonejs/codec-openjph/wasm'; const openjphWasm = new URL( '@cornerstonejs/codec-openjph/wasm', import.meta.url ); import type { LoaderDecodeOptions } from '../../types'; const local: { codec: unknown; decoder: unknown; decodeConfig: LoaderDecodeOptions; } = { codec: undefined, decoder: undefined, decodeConfig: {}, }; function calculateSizeAtDecompositionLevel( decompositionLevel: number, frameWidth: number, frameHeight: number ) { const result = { width: frameWidth, height: frameHeight }; while (decompositionLevel > 0) { result.width = Math.ceil(result.width / 2); result.height = Math.ceil(result.height / 2); decompositionLevel--; } return result; } export function initialize(decodeConfig?: LoaderDecodeOptions): Promise<void> { local.decodeConfig = decodeConfig; if (local.codec) { return Promise.resolve(); } const openJphModule = openJphFactory({ locateFile: (f) => { if (f.endsWith('.wasm')) { return openjphWasm.toString(); } return f; }, }); return new Promise<void>((resolve, reject) => { openJphModule.then((instance) => { local.codec = instance; local.decoder = new instance.HTJ2KDecoder(); resolve(); }, reject); }); } // https://github.com/chafey/openjpegjs/blob/master/test/browser/index.html async function decodeAsync(compressedImageFrame: ByteArray, imageInfo) { await initialize(); // const decoder = local.decoder; // This is much slower for some reason // @ts-expect-error const decoder = new local.codec.HTJ2KDecoder(); // 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(); const decodeLevel = imageInfo.decodeLevel || 0; decoder.decodeSubResolution(decodeLevel); // decoder.decodeSubResolution(decodeLevel, decodeLayer); // const resolutionAtLevel = decoder.calculateSizeAtDecompositionLevel(decodeLevel); // get information about the decoded image const frameInfo = decoder.getFrameInfo(); // Overwrite width/height if subresolution if (imageInfo.decodeLevel > 0) { const { width, height } = calculateSizeAtDecompositionLevel( imageInfo.decodeLevel, frameInfo.width, frameInfo.height ); frameInfo.width = width; frameInfo.height = height; // console.log(`Decoded sub-resolution of size: ${width} x ${height}`); } // 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); /** * Have to truncate the arraybuffer here to the length of the typed array. Not * sure why the arraybuffer is so huge, maybe this is allocated by the WASM * module? In any case, I think it's too big to postMessage in it's entirety. */ let pixelData = getPixelData(frameInfo, decodedBufferInWASM); const { buffer: b, byteOffset, byteLength } = pixelData; const pixelDataArrayBuffer = b.slice(byteOffset, byteOffset + byteLength); // @ts-ignore pixelData = new pixelData.constructor(pixelDataArrayBuffer); 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; |