import * as cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader' function parseImageId(imageId) { // build a url by parsing out the url scheme and frame index from the imageId const firstColonIndex = imageId.indexOf(':'); let url = imageId.substring(firstColonIndex + 1); const frameIndex = url.indexOf('frame='); let frame; if (frameIndex !== -1) { const frameStr = url.substr(frameIndex + 6); frame = parseInt(frameStr, 10); url = url.substr(0, frameIndex - 1); } return { scheme: imageId.substr(0, firstColonIndex), url, frame, }; } function getNumberValues(dataSet, tag, minimumLength) { const values = []; const valueAsString = dataSet.string(tag); if (!valueAsString) { return; } const split = valueAsString.split('\\'); if (minimumLength && split.length < minimumLength) { return; } for (let i = 0; i < split.length; i++) { values.push(parseFloat(split[i])); } return values; } function metaDataProvider(type, imageId) { const parsedImageId = parseImageId(imageId); const dataSet = cornerstoneWADOImageLoader.wadouri.dataSetCacheManager.get(parsedImageId.url); if (!dataSet) { return; } if (type === 'imagePlaneModule') { const imageOrientationPatient = getNumberValues(dataSet, 'x00200037', 6); const imagePositionPatient = getNumberValues(dataSet, 'x00200032', 3); const pixelSpacing = getNumberValues(dataSet, 'x00280030', 2); const imagePixelSpacing = getNumberValues(dataSet, 'x00181164', 2); const estimatedRadiographicMagnificationFactor = getNumberValues(dataSet, 'x00181114', 2); let columnPixelSpacing = null; let rowPixelSpacing = null; if (pixelSpacing) { rowPixelSpacing = pixelSpacing[0]; columnPixelSpacing = pixelSpacing[1]; } else if (imagePixelSpacing && estimatedRadiographicMagnificationFactor) { rowPixelSpacing = imagePixelSpacing[0] / estimatedRadiographicMagnificationFactor[0]; columnPixelSpacing = imagePixelSpacing[1] / estimatedRadiographicMagnificationFactor[1]; } else if (imagePixelSpacing && !estimatedRadiographicMagnificationFactor) { rowPixelSpacing = imagePixelSpacing[0]; columnPixelSpacing = imagePixelSpacing[1]; } let rowCosines = null; let columnCosines = null; if (imageOrientationPatient) { rowCosines = [ parseFloat(imageOrientationPatient[0]), parseFloat(imageOrientationPatient[1]), parseFloat(imageOrientationPatient[2]), ]; columnCosines = [ parseFloat(imageOrientationPatient[3]), parseFloat(imageOrientationPatient[4]), parseFloat(imageOrientationPatient[5]), ]; } return { frameOfReferenceUID: dataSet.string('x00200052'), rows: dataSet.uint16('x00280010'), columns: dataSet.uint16('x00280011'), imageOrientationPatient, rowCosines, columnCosines, imagePositionPatient, sliceThickness: dataSet.floatString('x00180050'), sliceLocation: dataSet.floatString('x00201041'), pixelSpacing, rowPixelSpacing, columnPixelSpacing, }; } if (type === 'imagePixelModule') { return { samplesPerPixel: dataSet.uint16('x00280002'), photometricInterpretation: dataSet.string('x00280004'), rows: dataSet.uint16('x00280010'), columns: dataSet.uint16('x00280011'), bitsAllocated: dataSet.uint16('x00280100'), bitsStored: dataSet.uint16('x00280101'), highBit: dataSet.uint16('x00280102'), pixelRepresentation: dataSet.uint16('x00280103'), planarConfiguration: dataSet.uint16('x00280006'), pixelAspectRatio: dataSet.uint16('x00280034'), smallestPixelValue: null, largestPixelValue: null, redPaletteColorLookupTableDescriptor: dataSet.string('x00281101'), greenPaletteColorLookupTableDescriptor: dataSet.string('x00281102'), bluePaletteColorLookupTableDescriptor: dataSet.string('x00281103'), redPaletteColorLookupTableData: dataSet.string('x00281201'), greenPaletteColorLookupTableData: dataSet.string('x00281202'), bluePaletteColorLookupTableData: dataSet.string('x00281203') } } } export default metaDataProvider;