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 | import { vec3 } from 'gl-matrix';
import { MetadataModules } from '../enums';
import type { Point3 } from '../types';
import * as metaData from '../metaData';
import isVideoTransferSyntax from './isVideoTransferSyntax';
export interface VideoImageMetadata {
bitsAllocated: number;
numberOfComponents: number;
origin: Point3;
rows: number;
columns: number;
direction: number[];
dimensions: [number, number, number];
spacing: [number, number, number];
hasPixelSpacing: boolean;
numVoxels: number;
imagePlaneModule: Record<string, unknown>;
}
export interface LoadedVideoStreamMetadata {
renderedUrl: string;
modality?: string;
metadata: VideoImageMetadata;
cineRate?: number;
numberOfFrames?: number;
}
export function getVideoImageDataMetadata(imageId: string): VideoImageMetadata {
const imagePlaneModule = metaData.get(MetadataModules.IMAGE_PLANE, imageId);
let rowCosines = imagePlaneModule.rowCosines as Point3;
let columnCosines = imagePlaneModule.columnCosines as Point3;
const usingDefaultValues = imagePlaneModule.usingDefaultValues;
if (usingDefaultValues || rowCosines == null || columnCosines == null) {
rowCosines = [1, 0, 0];
columnCosines = [0, 1, 0];
}
const rowCosineVec = vec3.fromValues(
rowCosines[0],
rowCosines[1],
rowCosines[2]
);
const colCosineVec = vec3.fromValues(
columnCosines[0],
columnCosines[1],
columnCosines[2]
);
const scanAxisNormal = vec3.create();
vec3.cross(scanAxisNormal, rowCosineVec, colCosineVec);
const rows = imagePlaneModule.rows;
const columns = imagePlaneModule.columns;
const origin = (imagePlaneModule.imagePositionPatient || [0, 0, 0]) as Point3;
const xSpacing = imagePlaneModule.columnPixelSpacing || 1;
const ySpacing = imagePlaneModule.rowPixelSpacing || 1;
const zSpacing = 1;
const xVoxels = imagePlaneModule.columns;
const yVoxels = imagePlaneModule.rows;
const zVoxels = 1;
return {
bitsAllocated: 8,
numberOfComponents: 3,
origin,
rows,
columns,
direction: [...rowCosineVec, ...colCosineVec, ...scanAxisNormal],
dimensions: [xVoxels, yVoxels, zVoxels],
spacing: [xSpacing, ySpacing, zSpacing],
hasPixelSpacing: !!imagePlaneModule.columnPixelSpacing,
numVoxels: xVoxels * yVoxels * zVoxels,
imagePlaneModule,
};
}
export function loadVideoStreamMetadata(
imageId: string
): LoadedVideoStreamMetadata {
const renderedUrl = getRenderedVideoUrl(imageId);
if (!renderedUrl) {
throw new Error(
`Video Image ID ${imageId} does not have a rendered video view`
);
}
const generalSeries = metaData.get(MetadataModules.GENERAL_SERIES, imageId);
const cine = metaData.get(MetadataModules.CINE, imageId) || {};
const instance = metaData.get(MetadataModules.INSTANCE, imageId);
const frameTime = cine.frameTime ?? cine.FrameTime;
return {
renderedUrl,
modality: generalSeries?.Modality ?? generalSeries?.modality,
metadata: getVideoImageDataMetadata(imageId),
cineRate:
cine.cineRate ??
cine.recommendedDisplayFrameRate ??
(frameTime ? 1000 / Number(frameTime) : undefined),
numberOfFrames: cine.numberOfFrames ?? instance?.NumberOfFrames,
};
}
function getRenderedVideoUrl(imageId: string): string | undefined {
const imageUrlModule = metaData.get(MetadataModules.IMAGE_URL, imageId);
if (imageUrlModule?.rendered) {
return imageUrlModule.rendered;
}
const transferSyntax = metaData.get(MetadataModules.TRANSFER_SYNTAX, imageId);
const isVideo =
transferSyntax?.isVideo ||
isVideoTransferSyntax(transferSyntax?.transferSyntaxUID);
if (!isVideo) {
return;
}
const imageUrl = imageId.startsWith('wadors:')
? imageId.substring(7)
: imageId;
if (!imageUrl.includes('/frames/')) {
return;
}
return imageUrl
.replace('/frames/', '/rendered/')
.replace(/\/rendered\/\d+($|[?#])/, '/rendered$1');
}
export function normalizeVideoPlaybackInfo(args: {
durationSeconds: number;
cineRate?: number;
numberOfFrames?: number;
}): {
fps: number;
numberOfFrames: number;
frameRange: [number, number];
} {
const durationSeconds = Math.max(args.durationSeconds || 0, 0.001);
let numberOfFrames = args.numberOfFrames;
let fps = args.cineRate;
if (!numberOfFrames || numberOfFrames === 1) {
numberOfFrames = Math.max(1, Math.round(durationSeconds * (fps || 30)));
}
if (!fps) {
fps = Math.max(1, Math.round(numberOfFrames / durationSeconds));
}
return {
fps,
numberOfFrames,
frameRange: [1, numberOfFrames],
};
}
export function frameNumberToTimeSeconds(
frameNumber: number,
fps: number
): number {
return Math.max(0, frameNumber - 1) / Math.max(1, fps);
}
export function timeSecondsToFrameNumber(
timeSeconds: number,
fps: number
): number {
return 1 + Math.round(Math.max(0, timeSeconds) * Math.max(1, fps));
}
|