11import computeImageDataIncrements from './computeImageDataIncrements.js' ;
22import computeIndex from './computeIndex.js' ;
3+ import cornerstone from 'cornerstone-core' ;
34
45// insert the slice at the z index location.
5- export default function insertSlice ( imageData , pixels , index ) {
6+ export default function insertSlice ( imageData , index , image ) {
7+ const pixels = image . getPixelData ( ) ;
8+ const scalingParameters = _calculateScalingParametersForModality ( image ) ;
9+
610 const datasetDefinition = imageData . get ( 'extent' , 'spacing' , 'origin' ) ;
711 const scalars = imageData . getPointData ( ) . getScalars ( ) ;
812 const increments = computeImageDataIncrements ( imageData , 1 ) ; // TODO number of components.
@@ -20,8 +24,113 @@ export default function insertSlice(imageData, pixels, index) {
2024 increments ,
2125 indexXYZ
2226 ) ;
23- scalarData [ destIdx ] = pixels [ pixelIndex ++ ] ;
27+ const pixel = pixels [ pixelIndex ] ;
28+
29+ scalarData [ destIdx ] = _getModalityPixelsOrSUV ( pixel , scalingParameters ) ;
30+
31+ pixelIndex ++ ;
2432 }
2533 }
2634 imageData . modified ( ) ;
2735}
36+
37+ function _calculateScalingParametersForModality ( image ) {
38+ const patientStudyModule = cornerstone . metaData . get (
39+ 'patientStudyModule' ,
40+ image . imageId
41+ ) ;
42+ const seriesModule = cornerstone . metaData . get (
43+ 'generalSeriesModule' ,
44+ image . imageId
45+ ) ;
46+
47+ if ( ! patientStudyModule ) {
48+ throw new Error ( 'patientStudyModule metadata is required' ) ;
49+ }
50+
51+ if ( ! seriesModule ) {
52+ throw new Error ( 'seriesModule metadata is required' ) ;
53+ }
54+
55+ const modality = seriesModule . modality ;
56+
57+ const scalingParameters = {
58+ slope : image . slope ,
59+ intercept : image . intercept ,
60+ modality,
61+ } ;
62+
63+ if ( modality === 'PT' ) {
64+ const patientWeight = patientStudyModule . patientWeight ; // In kg
65+
66+ if ( ! patientWeight ) {
67+ throw new Error (
68+ 'patientWeight must be present in patientStudyModule for modality PT'
69+ ) ;
70+ }
71+
72+ const petSequenceModule = cornerstone . metaData . get (
73+ 'petIsotopeModule' ,
74+ image . imageId
75+ ) ;
76+
77+ if ( ! petSequenceModule ) {
78+ throw new Error ( 'petSequenceModule metadata is required' ) ;
79+ }
80+
81+ // TODO:
82+ // - Update this to match the SUV logic provided here:
83+ // https://github.com/salimkanoun/fijiPlugins/blob/master/Pet_Ct_Viewer/src/SUVDialog.java
84+ // - Test with PET datasets from various providers to ensure SUV is correct
85+ const radiopharmaceuticalInfo = petSequenceModule . radiopharmaceuticalInfo ;
86+ const startTime = radiopharmaceuticalInfo . radiopharmaceuticalStartTime ;
87+ const totalDose = radiopharmaceuticalInfo . radionuclideTotalDose ;
88+ const halfLife = radiopharmaceuticalInfo . radionuclideHalfLife ;
89+ const seriesAcquisitionTime = seriesModule . seriesTime ;
90+
91+ if ( ! startTime || ! totalDose || ! halfLife || ! seriesAcquisitionTime ) {
92+ throw new Error (
93+ 'The required radiopharmaceutical information was not present.'
94+ ) ;
95+ }
96+
97+ const acquisitionTimeInSeconds =
98+ fracToDec ( seriesAcquisitionTime . fractionalSeconds || 0 ) +
99+ seriesAcquisitionTime . seconds +
100+ seriesAcquisitionTime . minutes * 60 +
101+ seriesAcquisitionTime . hours * 60 * 60 ;
102+ const injectionStartTimeInSeconds =
103+ fracToDec ( startTime . fractionalSeconds ) +
104+ startTime . seconds +
105+ startTime . minutes * 60 +
106+ startTime . hours * 60 * 60 ;
107+ const durationInSeconds =
108+ acquisitionTimeInSeconds - injectionStartTimeInSeconds ;
109+ const correctedDose =
110+ totalDose * Math . exp ( ( - durationInSeconds * Math . log ( 2 ) ) / halfLife ) ;
111+
112+ scalingParameters . patientWeight = patientWeight ;
113+ scalingParameters . correctedDose = correctedDose ;
114+ }
115+
116+ return scalingParameters ;
117+ }
118+
119+ function _getModalityPixelsOrSUV ( pixel , scalingParameters ) {
120+ if ( scalingParameters . modality === 'PT' ) {
121+ const {
122+ slope,
123+ intercept,
124+ patientWeight,
125+ correctedDose,
126+ } = scalingParameters ;
127+
128+ const modalityPixelValue = pixel * slope + intercept ;
129+ const suv = ( 1000 * modalityPixelValue * patientWeight ) / correctedDose ;
130+ return suv ;
131+ }
132+
133+ const { slope, intercept } = scalingParameters ;
134+
135+ return pixel * slope + intercept ;
136+ }
0 commit comments