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 210 | 128x 128x 128x 128x 128x 21327x 21327x 21327x 21327x 21327x 10656x 45x 45x 11245x 11245x 45x 11200x 11200x 10656x 10656x 10656x 10656x 10656x 10656x 11245x 11245x 11200x 11200x 45x 10656x 10656x 10656x 36410x 36410x 384x 384x 384x 384x | import { MetadataModules } from '../../enums';
import {
addTypedProvider,
getTyped,
metadataModuleProvider,
type TypedProvider,
} from '../../metaData';
import {
getNaturalizedNumber,
getNaturalizedString,
} from '../getNaturalizedField';
export const FRAME_IMAGE_IDS = MetadataModules.FRAME_IMAGE_IDS;
export const BASE_IMAGE_ID = MetadataModules.BASE_IMAGE_ID;
/** Typed provider signature used by imageId/baseId provider chains. */
export type ImageIdTransformPlugin = TypedProvider;
const BASE_IMAGE_ID_PATH_FILTER_PRIORITY = 9_000;
const BASE_IMAGE_ID_QUERY_FILTER_PRIORITY = 8_000;
const DEFAULT_FRAME_IMAGE_IDS_PRIORITY = 9_000;
function defaultBaseImageIdProvider(_next, imageId: string) {
return imageId;
}
function framePathToBaseFilter(next, imageId: string, data, options) {
const baseImageId =
(next(imageId, data, options) as string | undefined) ?? imageId;
return baseImageId.replace(/\/frames\/\d+(?=(\?|#|$))/i, '');
}
function frameQueryToBaseFilter(next, imageId: string, data, options) {
const baseImageId =
(next(imageId, data, options) as string | undefined) ?? imageId;
// Strip frame query value from either ?frame=N or &frame=N and normalize separators.
return baseImageId
.replace(/([?&])frame=\d+(&?)/i, (_match, separator, hasTrailingAmp) => {
if (separator === '?' && hasTrailingAmp) {
return '?';
}
return '';
})
.replace(/\?$/, '')
.replace(/\?&/, '?')
.replace(/&&+/g, '&');
}
function hasPhotometricInterpretation(
naturalized: Record<string, unknown>
): boolean {
return !!getNaturalizedString(naturalized, 'PhotometricInterpretation');
}
function addFrameQueryParameter(imageId: string, frameNumber: number) {
const separator = imageId.includes('?') ? '&' : '?';
return `${imageId}${separator}frame=${frameNumber}`;
}
function addDicomwebFramePath(
imageId: string,
frameNumber: number
): string | undefined {
const instancesMatch = /\/instances\/[^/]+/i.exec(imageId);
if (!instancesMatch) {
return;
}
const insertIndex = instancesMatch.index + instancesMatch[0].length;
return `${imageId.slice(0, insertIndex)}/frames/${frameNumber}${imageId.slice(insertIndex)}`;
}
export function generateFrameImageIdsFromNaturalized(
baseImageId: string,
naturalized: Record<string, unknown> | null | undefined
): Set<string> | undefined {
Iif (!naturalized) {
return;
}
Iif (!hasPhotometricInterpretation(naturalized)) {
return new Set<string>([baseImageId]);
}
const frameImageIds = new Set<string>();
const numberOfFrames = Math.floor(
getNaturalizedNumber(naturalized, 'NumberOfFrames', 1)
);
Iif (!Number.isFinite(numberOfFrames) || numberOfFrames < 1) {
return;
}
for (let frameNumber = 1; frameNumber <= numberOfFrames; frameNumber++) {
const framePath = addDicomwebFramePath(baseImageId, frameNumber);
if (framePath) {
frameImageIds.add(framePath);
continue;
}
frameImageIds.add(addFrameQueryParameter(baseImageId, frameNumber));
}
return frameImageIds;
}
function defaultFrameImageIdsProvider(next, imageId: string, data, options) {
const naturalized = metadataModuleProvider(
MetadataModules.NATURALIZED,
imageId,
options
) as Record<string, unknown> | null | undefined;
return (
generateFrameImageIdsFromNaturalized(imageId, naturalized) ||
next(imageId, data, options)
);
}
export function registerFrameImageIdsProvider(
provider: ImageIdTransformPlugin,
priority = 0
) {
addTypedProvider(FRAME_IMAGE_IDS, provider, { priority });
}
/**
* Generic query-normalization filter that rewrites a query to its canonical
* base imageId before delegating to the remainder of a typed provider chain.
*
* Can be plugged into any typed type where frame-specific imageIds should
* resolve against base-image keyed cache/state.
*/
export function baseImageIdQueryFilter(next, query: string, data, options) {
const baseImageId = getTyped(MetadataModules.BASE_IMAGE_ID, query, options);
return next(baseImageId, data, options);
}
/**
* Registers a transform filter for `frameImageIds`.
*
* The filter receives the current imageId and accumulated frame-image-id set,
* and may return additional imageIds to merge into that set.
*/
export function registerImageIdTransformFilter(
filter: (
imageId: string,
frameImageIds: Set<string>
) => Iterable<string> | void,
priority = 0
) {
const provider: ImageIdTransformPlugin = (
next,
imageId: string,
data,
options
) => {
const frameImageIds =
(next(imageId, data, options) as Set<string> | undefined) ??
new Set<string>([imageId]);
const produced = filter(imageId, frameImageIds);
if (produced) {
for (const transformedImageId of produced) {
frameImageIds.add(transformedImageId);
}
}
return frameImageIds;
};
registerFrameImageIdsProvider(provider, priority);
}
/**
* Registers the default imageId providers:
* - base imageId canonicalization providers
* - frame imageId expansion providers
*/
export function registerImageIdProviders() {
addTypedProvider(BASE_IMAGE_ID, defaultBaseImageIdProvider, { priority: 0 });
addTypedProvider(BASE_IMAGE_ID, framePathToBaseFilter, {
priority: BASE_IMAGE_ID_PATH_FILTER_PRIORITY,
});
addTypedProvider(BASE_IMAGE_ID, frameQueryToBaseFilter, {
priority: BASE_IMAGE_ID_QUERY_FILTER_PRIORITY,
});
addTypedProvider(FRAME_IMAGE_IDS, defaultFrameImageIdsProvider, {
priority: DEFAULT_FRAME_IMAGE_IDS_PRIORITY,
});
}
/**
* Expands each input imageId to its related frame imageIds and applies
* an update callback once per expanded imageId.
*
* Returns the set of unique imageIds that were updated.
*/
export function bulkUpdateImageIds(
imageIds: Iterable<string>,
updater: (imageId: string) => void
): Set<string> {
const updatedImageIds = new Set<string>();
for (const imageId of imageIds) {
const frameImageIds = getTyped(MetadataModules.FRAME_IMAGE_IDS, imageId);
if (!frameImageIds) {
continue;
}
for (const frameImageId of frameImageIds) {
updater(frameImageId);
updatedImageIds.add(frameImageId);
}
}
return updatedImageIds;
}
|