Skip to content

Commit

Permalink
fix: wadouri metadata was not using scaling parameters properly (#159)
Browse files Browse the repository at this point in the history
* fix: scaling for the wadouri scheme

* fix: isPrescaled on the stackViewport

* fix: preScale option in image loaders

* fix: window level for volumeviewport
  • Loading branch information
sedghi authored Aug 3, 2022
1 parent f35654c commit d21aba5
Show file tree
Hide file tree
Showing 21 changed files with 264 additions and 502 deletions.
37 changes: 29 additions & 8 deletions common/reviews/api/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,15 @@ type CPUIImageData = {
scalarData: number[];
scaling: Scaling;
hasPixelSpacing?: boolean;
preScale?: {
scaled?: boolean;
scalingParameters?: {
modality?: string;
rescaleSlope?: number;
rescaleIntercept?: number;
suvbw?: number;
};
};
};

// @public (undocumented)
Expand Down Expand Up @@ -562,9 +571,6 @@ export function getRenderingEngines(): IRenderingEngine[] | undefined;
// @public (undocumented)
function getRuntimeId(context?: unknown, separator?: string, max?: number): string;

// @public (undocumented)
function getScalingParameters(imageId: string): ScalingParameters | undefined;

// @public (undocumented)
export function getShouldUseCPURendering(): boolean;

Expand Down Expand Up @@ -723,6 +729,16 @@ interface IImage {
// (undocumented)
numComps: number;
// (undocumented)
preScale?: {
scaled: boolean;
scalingParameters: {
modality?: string;
rescaleSlope?: number;
rescaleIntercept?: number;
suvbw?: number;
};
};
// (undocumented)
render?: (enabledElement: CPUFallbackEnabledElement, invalidated: boolean) => unknown;
// (undocumented)
rgba: boolean;
Expand Down Expand Up @@ -783,6 +799,16 @@ interface IImageData {
// (undocumented)
origin: Point3;
// (undocumented)
preScale?: {
scaled?: boolean;
scalingParameters?: {
modality?: string;
rescaleSlope?: number;
rescaleIntercept?: number;
suvbw?: number;
};
};
// (undocumented)
scalarData: Float32Array;
// (undocumented)
scaling?: Scaling;
Expand Down Expand Up @@ -1139,8 +1165,6 @@ interface IStackViewport extends IViewport {
// (undocumented)
hasImageURI: (imageURI: string) => boolean;
// (undocumented)
isImagePreScaled(imageId: string): boolean;
// (undocumented)
modality: string;
// (undocumented)
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
Expand Down Expand Up @@ -1689,8 +1713,6 @@ export class StackViewport extends Viewport implements IStackViewport {
// (undocumented)
hasImageURI: (imageURI: string) => boolean;
// (undocumented)
isImagePreScaled(imageId: string): boolean;
// (undocumented)
modality: string;
// (undocumented)
removeAllActors(): void;
Expand Down Expand Up @@ -1877,7 +1899,6 @@ declare namespace utilities {
getSliceRange,
snapFocalPointToSlice,
getImageSliceDataForVolumeViewport,
getScalingParameters,
isImageActor
}
}
Expand Down
29 changes: 28 additions & 1 deletion common/reviews/api/streaming-image-volume-loader.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,15 @@ type CPUIImageData = {
scalarData: number[];
scaling: Scaling;
hasPixelSpacing?: boolean;
preScale?: {
scaled?: boolean;
scalingParameters?: {
modality?: string;
rescaleSlope?: number;
rescaleIntercept?: number;
suvbw?: number;
};
};
};

// @public (undocumented)
Expand Down Expand Up @@ -549,6 +558,15 @@ interface IImage {
minPixelValue: number;
modalityLUT?: CPUFallbackLUT;
numComps: number;
preScale?: {
scaled: boolean;
scalingParameters: {
modality?: string;
rescaleSlope?: number;
rescaleIntercept?: number;
suvbw?: number;
};
};
render?: (
enabledElement: CPUFallbackEnabledElement,
invalidated: boolean
Expand Down Expand Up @@ -593,6 +611,15 @@ interface IImageData {
imageData: vtkImageData;
metadata: { Modality: string };
origin: Point3;
preScale?: {
scaled?: boolean;
scalingParameters?: {
modality?: string;
rescaleSlope?: number;
rescaleIntercept?: number;
suvbw?: number;
};
};
scalarData: Float32Array;
scaling?: Scaling;
spacing: Point3;
Expand Down Expand Up @@ -815,7 +842,6 @@ interface IStackViewport extends IViewport {
getRenderer(): any;
hasImageId: (imageId: string) => boolean;
hasImageURI: (imageURI: string) => boolean;
isImagePreScaled(imageId: string): boolean;
// (undocumented)
modality: string;
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
Expand Down Expand Up @@ -1156,6 +1182,7 @@ export class StreamingImageVolume extends ImageVolume {
type: any;
};
preScale: {
enabled: boolean;
scalingParameters: Types.ScalingParameters;
};
};
Expand Down
28 changes: 27 additions & 1 deletion common/reviews/api/tools.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,15 @@ type CPUIImageData = {
scalarData: number[];
scaling: Scaling;
hasPixelSpacing?: boolean;
preScale?: {
scaled?: boolean;
scalingParameters?: {
modality?: string;
rescaleSlope?: number;
rescaleIntercept?: number;
suvbw?: number;
};
};
};

// @public (undocumented)
Expand Down Expand Up @@ -1867,6 +1876,15 @@ interface IImage {
minPixelValue: number;
modalityLUT?: CPUFallbackLUT;
numComps: number;
preScale?: {
scaled: boolean;
scalingParameters: {
modality?: string;
rescaleSlope?: number;
rescaleIntercept?: number;
suvbw?: number;
};
};
render?: (
enabledElement: CPUFallbackEnabledElement,
invalidated: boolean
Expand Down Expand Up @@ -1911,6 +1929,15 @@ interface IImageData {
imageData: vtkImageData;
metadata: { Modality: string };
origin: Point3;
preScale?: {
scaled?: boolean;
scalingParameters?: {
modality?: string;
rescaleSlope?: number;
rescaleIntercept?: number;
suvbw?: number;
};
};
scalarData: Float32Array;
scaling?: Scaling;
spacing: Point3;
Expand Down Expand Up @@ -2172,7 +2199,6 @@ interface IStackViewport extends IViewport {
getRenderer(): any;
hasImageId: (imageId: string) => boolean;
hasImageURI: (imageURI: string) => boolean;
isImagePreScaled(imageId: string): boolean;
// (undocumented)
modality: string;
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
Expand Down
72 changes: 20 additions & 52 deletions packages/core/src/RenderingEngine/StackViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
VOIRange,
ICamera,
IImage,
ScalingParameters,
IImageData,
CPUIImageData,
PTScaling,
Expand Down Expand Up @@ -61,7 +60,6 @@ import {
StackViewportScrollEventDetail,
VoiModifiedEventDetail,
} from '../types/EventTypes';
import getScalingParameters from '../utilities/getScalingParameters';
import cache from '../cache';
import correctShift from './helpers/cpuFallback/rendering/correctShift';
import { ImageActor } from '../types/IActor';
Expand Down Expand Up @@ -150,11 +148,11 @@ class StackViewport extends Viewport implements IStackViewport {
private useCPURendering: boolean;
private cpuImagePixelData: number[];
private cpuRenderingInvalidated: boolean;
private csImage: IImage;

// TODO: These should not be here and will be nuked
public modality: string; // this is needed for tools
public scaling: Scaling;
private scalingCache: Record<string, ScalingParameters> = {};

/**
* Constructor for the StackViewport class
Expand Down Expand Up @@ -276,11 +274,14 @@ class StackViewport extends Viewport implements IStackViewport {
metadata: { Modality: this.modality },
scaling: this.scaling,
hasPixelSpacing: this.hasPixelSpacing,
preScale: {
...this.csImage.preScale,
},
};
}

private getImageDataCPU(): CPUIImageData | undefined {
const { metadata } = this._cpuFallbackEnabledElement;
const { metadata, image } = this._cpuFallbackEnabledElement;

const spacing = metadata.spacing;

Expand Down Expand Up @@ -314,6 +315,9 @@ class StackViewport extends Viewport implements IStackViewport {
},
scalarData: this.cpuImagePixelData,
hasPixelSpacing: this.hasPixelSpacing,
preScale: {
...this.csImage.preScale,
},
};
}

Expand Down Expand Up @@ -1253,7 +1257,6 @@ class StackViewport extends Viewport implements IStackViewport {
this.currentImageIdIndex = currentImageIdIndex;
this.targetImageIdIndex = currentImageIdIndex;
this.stackInvalidated = true;
this.scalingCache = {};
this.rotationCache = 0;
this.flipVertical = false;
this.flipHorizontal = false;
Expand Down Expand Up @@ -1407,6 +1410,8 @@ class StackViewport extends Viewport implements IStackViewport {
return;
}

this.csImage = image;

const eventDetail: EventTypes.StackNewImageEventDetail = {
image,
imageId,
Expand All @@ -1418,7 +1423,8 @@ class StackViewport extends Viewport implements IStackViewport {
triggerEvent(this.element, Events.STACK_NEW_IMAGE, eventDetail);

const metadata = this._getImageDataMetadata(image) as ImageDataMetaData;
image.isPreScaled = this.isImagePreScaled(imageId);

image.isPreScaled = image.preScale?.scaled;

const viewport = getDefaultViewport(
this.canvas,
Expand Down Expand Up @@ -1497,12 +1503,6 @@ class StackViewport extends Viewport implements IStackViewport {
);
}

const scalingParameters = getScalingParameters(imageId);

if (imageId) {
this.scalingCache[imageId] = scalingParameters;
}

// Todo: Note that eventually all viewport data is converted into Float32Array,
// we use it here for the purpose of scaling for now.
const type = 'Float32Array';
Expand All @@ -1517,7 +1517,7 @@ class StackViewport extends Viewport implements IStackViewport {
length: null,
},
preScale: {
scalingParameters,
enabled: true,
},
useRGBA: false,
};
Expand All @@ -1537,14 +1537,16 @@ class StackViewport extends Viewport implements IStackViewport {
function successCallback(image, imageIdIndex, imageId) {
// Todo: trigger an event to allow applications to hook into END of loading state
// Currently we use loadHandlerManagers for this

// Perform this check after the image has finished loading
// in case the user has already scrolled away to another image.
// In that case, do not render this image.
if (this.currentImageIdIndex !== imageIdIndex) {
return;
}

// cornerstone image
this.csImage = image;

const eventDetail: EventTypes.StackNewImageEventDetail = {
image,
imageId,
Expand Down Expand Up @@ -1587,12 +1589,6 @@ class StackViewport extends Viewport implements IStackViewport {
);
}

const scalingParameters = getScalingParameters(imageId);

if (scalingParameters) {
this.scalingCache[imageId] = scalingParameters;
}

// Todo: Note that eventually all viewport data is converted into Float32Array,
// we use it here for the purpose of scaling for now.
const type = 'Float32Array';
Expand All @@ -1608,7 +1604,7 @@ class StackViewport extends Viewport implements IStackViewport {
length: null,
},
preScale: {
scalingParameters,
enabled: true,
},
useRGBA: false,
};
Expand All @@ -1630,36 +1626,6 @@ class StackViewport extends Viewport implements IStackViewport {
});
}

/**
* Checks if the requests has been sent to the imageLoadPoolManager
* with preScaling options; therefore, the images have been pre-scaled.
* Note: this is more isTheImageThatWasRequestedGonnaBePreScaled, but since
* we are using the same image metadata for adding request options and later
* checking them, we can assume if the scalingParameters are present, the image is pre-scaled
* @param imageId - imageId of the image
* @returns boolean
*/
public isImagePreScaled(imageId: string): boolean {
const scalingParameters = this.scalingCache[imageId];

if (!scalingParameters) {
return false;
}

const { modality, rescaleIntercept, rescaleSlope, suvbw } =
scalingParameters;

if (rescaleSlope !== undefined && rescaleIntercept !== undefined) {
if (modality === 'PT') {
return suvbw !== undefined;
}

return true;
}

return false;
}

/**
* It updates the volume actor with the retrieved cornerstone image.
* It first checks if the new image has the same dimensions, spacings, and
Expand Down Expand Up @@ -1783,7 +1749,9 @@ class StackViewport extends Viewport implements IStackViewport {
: undefined;

// check if the image is already prescaled
const isPreScaled = this.isImagePreScaled(image.imageId);
const isPreScaled =
this.csImage.isPreScaled || this.csImage.preScale?.scaled;

if (imagePixelModule.modality === 'PT' && isPreScaled) {
voiRange = { lower: 0, upper: 5 };
}
Expand Down
Loading

0 comments on commit d21aba5

Please sign in to comment.