Skip to content

Commit

Permalink
Fetching chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHax committed Aug 18, 2024
1 parent 4891ac2 commit 0c42192
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 28 deletions.
186 changes: 186 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.435.0",
"@itk-wasm/dicom": "6.0.1",
"@itk-wasm/htj2k": "^2.3.1",
"@itk-wasm/image-io": "1.1.1",
"@kitware/vtk.js": "30.9.0",
"@netlify/edge-functions": "^2.0.0",
Expand Down
60 changes: 50 additions & 10 deletions src/core/streaming/ahiChunkImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,44 @@ import {
} from '@/src/core/streaming/chunkImage';
import { ComputedRef, Ref, computed, ref } from 'vue';
import mitt, { Emitter } from 'mitt';
import {
decode,
encode,
setPipelinesBaseUrl,
getPipelinesBaseUrl,
setPipelineWorkerUrl,
getPipelineWorkerUrl,
} from '@itk-wasm/htj2k';

const nameToMetaKey = {
SOPInstanceUID: 'ID',
SOPInstanceUID: 'SOPInstanceUID',
ImagePositionPatient: 'ImagePositionPatient',
ImageOrientationPatient: 'ImageOrientationPatient',
PixelSpacing: 'PixelSpacing',
Rows: 'Rows',
Columns: 'Columns',
BitsStored: 'BitsStored',
PixelRepresentation: 'PixelRepresentation',
SamplesPerPixel: 'SamplesPerPixel',
RescaleIntercept: 'RescaleIntercept',
RescaleSlope: 'RescaleSlope',
NumberOfFrames: 'NumberOfFrames',
PatientID: 'PatientID',
PatientName: 'PatientName',
PatientBirthDate: 'PatientBirthDate',
PatientSex: 'PatientSex',
StudyID: 'StudyID',
StudyInstanceUID: 'StudyInstanceUID',
StudyDate: 'StudyDate',
StudyTime: 'StudyTime',
AccessionNumber: 'AccessionNumber',
StudyDescription: 'StudyDescription',
Modality: 'Modality',
SeriesInstanceUID: 'SeriesInstanceUID',
SeriesNumber: 'SeriesNumber',
SeriesDescription: 'SeriesDescription',
WindowLevel: 'WindowLevel',
WindowWidth: 'WindowWidth',
};

const { fastComputeRange } = vtkDataArray;
Expand Down Expand Up @@ -66,9 +101,11 @@ function itkImageToURI(itkImage: Image) {
}

async function dicomSliceToImageUri(blob: Blob) {
const file = new File([blob], 'file.dcm');
const itkImage = await readVolumeSlice(file, true);
return itkImageToURI(itkImage);
const array = await blob.arrayBuffer();
const uint8Array = new Uint8Array(array);
const result = await decode(uint8Array);
console.log(result);
return itkImageToURI(result.image);
}

export default class AhiChunkImage implements ChunkImage {
Expand Down Expand Up @@ -244,12 +281,14 @@ export default class AhiChunkImage implements ChunkImage {

const chunk = this.chunks[chunkIndex];
if (!chunk.dataBlob) throw new Error('Chunk does not have data');
const result = await readImage(
new File([chunk.dataBlob], `file-${chunkIndex}.dcm`),
{
webWorker: getWorker(),
}
);

// await chunk.dataBlob.arrayBuffer()
const array = await chunk.dataBlob.arrayBuffer();
const uint8Array = new Uint8Array(array);
// const result = await decode(uint8Array, {
// webWorker: getWorker(),
// });
const result = await decode(uint8Array);

if (!result.image.data) throw new Error('No data read from chunk');

Expand Down Expand Up @@ -300,6 +339,7 @@ export default class AhiChunkImage implements ChunkImage {
}

private updateDicomStore() {
console.log('updateDicomStore', this.chunks.length);
if (this.chunks.length === 0) return;

const firstChunk = this.chunks[0];
Expand Down
17 changes: 14 additions & 3 deletions src/io/import/awsAhi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,28 @@ const importAhiImageSet = async (uri: string) => {
const setResponse = await fetch(imageSetMetaUri);
const imageSetMeta = await setResponse.json();
console.log(imageSetMeta);
const patentTags = imageSetMeta.Patient.DICOM;
const studyTags = imageSetMeta.Study.DICOM;
const [id, firstSeries] = Object.entries(imageSetMeta.Study.Series)[0] as any;
const seriesTags = firstSeries.DICOM;
const frames = Object.values(firstSeries.Instances).flatMap((instance: any) =>
instance.ImageFrames.map((frame: any) => ({ ...instance, ...frame }))
instance.ImageFrames.map((frame: any) => ({
...patentTags,
...studyTags,
...seriesTags,
...instance.DICOM,
...frame,
}))
);

const chunks = frames.map((frame: any) => makeAhiChunk(uri, frame));
const chunks = frames.map((frame: any) =>
makeAhiChunk(imageSetMetaUri, frame)
);

const chunkStore = useChunkStore();
const image = new AhiChunkImage(firstSeries);
chunkStore.chunkImageById[id] = image;
image.addChunks(chunks);
await image.addChunks(chunks);
image.startLoad();

return id;
Expand Down
Loading

0 comments on commit 0c42192

Please sign in to comment.