From 1b9a6db73fd9ca55c7a4eb937c58cddad6ad5eca Mon Sep 17 00:00:00 2001 From: Erik Ziegler Date: Tue, 28 Jan 2020 15:01:13 +0100 Subject: [PATCH] Handle Palette Color images where LUT has been returned as InlineBinary (#1350) (#1401) --- .../services/wado/studyInstanceHelpers.js | 78 +++++++++++++------ 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/platform/core/src/studies/services/wado/studyInstanceHelpers.js b/platform/core/src/studies/services/wado/studyInstanceHelpers.js index 69a4bc196..f4928a12f 100644 --- a/platform/core/src/studies/services/wado/studyInstanceHelpers.js +++ b/platform/core/src/studies/services/wado/studyInstanceHelpers.js @@ -337,40 +337,41 @@ async function makeSOPInstance(server, study, instance) { return sopInstance; } +/** + * Convert String to ArrayBuffer + * + * @param {String} str Input String + * @return {ArrayBuffer} Output converted ArrayBuffer + */ +function str2ab(str) { + const strLen = str.length; + const bytes = new Uint8Array(strLen); + + for (let i = 0; i < strLen; i++) { + bytes[i] = str.charCodeAt(i); + } + + return bytes.buffer; +} + function getPaletteColor(server, instance, tag, lutDescriptor) { const numLutEntries = lutDescriptor[0]; const bits = lutDescriptor[2]; - let uri = WADOProxy.convertURL(instance[tag].BulkDataURI, server); - - // TODO: Workaround for dcm4chee behind SSL-terminating proxy returning - // incorrect bulk data URIs - if (server.wadoRoot.indexOf('https') === 0 && !uri.includes('https')) { - uri = uri.replace('http', 'https'); - } - - const config = { - url: server.wadoRoot, //BulkDataURI is absolute, so this isn't used - headers: DICOMWeb.getAuthorizationHeader(server), - }; - const dicomWeb = new api.DICOMwebClient(config); - const options = { - BulkDataURI: uri, - }; - const readUInt16 = (byteArray, position) => { return byteArray[position] + byteArray[position + 1] * 256; }; - const arrayBufferToPaletteColorLUT = result => { - const arraybuffer = result[0]; + const arrayBufferToPaletteColorLUT = arraybuffer => { const byteArray = new Uint8Array(arraybuffer); const lut = []; - for (let i = 0; i < numLutEntries; i++) { - if (bits === 16) { + if (bits === 16) { + for (let i = 0; i < numLutEntries; i++) { lut[i] = readUInt16(byteArray, i * 2); - } else { + } + } else { + for (let i = 0; i < numLutEntries; i++) { lut[i] = byteArray[i]; } } @@ -378,7 +379,38 @@ function getPaletteColor(server, instance, tag, lutDescriptor) { return lut; }; - return dicomWeb.retrieveBulkData(options).then(arrayBufferToPaletteColorLUT); + if (instance[tag].BulkDataURI) { + let uri = WADOProxy.convertURL(instance[tag].BulkDataURI, server); + + // TODO: Workaround for dcm4chee behind SSL-terminating proxy returning + // incorrect bulk data URIs + if (server.wadoRoot.indexOf('https') === 0 && !uri.includes('https')) { + uri = uri.replace('http', 'https'); + } + + const config = { + url: server.wadoRoot, //BulkDataURI is absolute, so this isn't used + headers: DICOMWeb.getAuthorizationHeader(server), + }; + const dicomWeb = new api.DICOMwebClient(config); + const options = { + BulkDataURI: uri, + }; + + return dicomWeb + .retrieveBulkData(options) + .then(result => result[0]) + .then(arrayBufferToPaletteColorLUT); + } else if (instance[tag].InlineBinary) { + const inlineBinaryData = atob(instance[tag].InlineBinary); + const arraybuf = str2ab(inlineBinaryData); + + return arrayBufferToPaletteColorLUT(arraybuf); + } + + throw new Error( + 'Palette Color LUT was not provided as InlineBinary or BulkDataURI' + ); } /**