@@ -206,11 +206,16 @@ export default class DicomChunkImage
206206 } ) ;
207207 this . onChunksUpdated ( ) ;
208208
209+ if ( this . getModality ( ) !== 'SEG' ) {
210+ this . reallocateImage ( ) ;
211+ }
212+
209213 this . registerChunkListeners ( ) ;
210214 this . processNewChunks ( newChunks ) ;
211215
216+ // Update data range with already loaded chunks after reallocating image
212217 if ( this . getModality ( ) !== 'SEG' ) {
213- this . reallocateImage ( ) ;
218+ this . updateDataRangeFromChunks ( ) ;
214219 }
215220 }
216221
@@ -235,18 +240,22 @@ export default class DicomChunkImage
235240 }
236241
237242 private processNewChunks ( chunks : Chunk [ ] ) {
238- chunks
239- . filter ( ( chunk ) => chunk . state === ChunkState . Loaded )
240- . forEach ( ( _ , idx ) => {
241- this . onChunkHasData ( idx ) ;
243+ chunks . forEach ( ( chunk , idx ) => {
244+ if ( chunk . state !== ChunkState . Loaded ) return ;
245+
246+ this . onChunkHasData ( idx ) . catch ( ( err ) => {
247+ this . onChunkErrored ( idx , err ) ;
242248 } ) ;
249+ } ) ;
243250 }
244251
245252 private registerChunkListeners ( ) {
246253 this . chunkListeners = [
247254 ...this . chunks . map ( ( chunk , index ) => {
248255 const stopDoneData = chunk . addEventListener ( 'doneData' , ( ) => {
249- this . onChunkHasData ( index ) ;
256+ this . onChunkHasData ( index ) . catch ( ( err ) => {
257+ this . onChunkErrored ( index , err ) ;
258+ } ) ;
250259 } ) ;
251260
252261 const stopError = chunk . addEventListener ( 'error' , ( err ) => {
@@ -270,13 +279,17 @@ export default class DicomChunkImage
270279 private reallocateImage ( ) {
271280 this . vtkImageData . value . delete ( ) ;
272281 this . vtkImageData . value = allocateImageFromChunks ( this . chunks ) ;
282+ }
273283
274- // recalculate image data's data range, since allocateImageFromChunks doesn't know anything about it
284+ private updateDataRangeFromChunks ( ) {
275285 const scalars = this . vtkImageData . value . getPointData ( ) . getScalars ( ) ;
276- this . dataRangeFromChunks ( ) . forEach ( ( [ min , max ] , compIdx ) => {
277- scalars . setRange ( { min, max } , compIdx ) ;
278- } ) ;
279- scalars . modified ( ) ; // so image-stats will trigger update of range
286+ const ranges = this . dataRangeFromChunks ( ) ;
287+ if ( ranges . length > 0 ) {
288+ ranges . forEach ( ( [ min , max ] , compIdx ) => {
289+ scalars . setRange ( { min, max } , compIdx ) ;
290+ } ) ;
291+ scalars . modified ( ) ; // so image-stats will trigger update of range
292+ }
280293 }
281294
282295 private dataRangeFromChunks ( ) {
@@ -309,7 +322,9 @@ export default class DicomChunkImage
309322
310323 private async onSegChunkHasData ( chunkIndex : number ) {
311324 if ( this . chunks . length !== 1 || chunkIndex !== 0 )
312- throw new Error ( 'cannot handle multiple seg files' ) ;
325+ throw new Error (
326+ `Cannot handle multiple SEG files. Expected 1 chunk at index 0, got ${ this . chunks . length } chunks with current index ${ chunkIndex } `
327+ ) ;
313328
314329 const [ chunk ] = this . chunks ;
315330 const results = await buildSegmentGroups (
@@ -327,15 +342,27 @@ export default class DicomChunkImage
327342
328343 private async onRegularChunkHasData ( chunkIndex : number ) {
329344 const chunk = this . chunks [ chunkIndex ] ;
330- if ( ! chunk . dataBlob ) throw new Error ( 'Chunk does not have data' ) ;
345+ if ( ! chunk . dataBlob )
346+ throw new Error ( `Chunk ${ chunkIndex } does not have data` ) ;
347+
348+ const chunkId = chunk . metadata ? getChunkId ( chunk ) : `index-${ chunkIndex } ` ;
331349 const result = await readImage (
332350 new File ( [ chunk . dataBlob ] , `file-${ chunkIndex } .dcm` ) ,
333351 {
334352 webWorker : getWorker ( ) ,
335353 }
336354 ) ;
337355
338- if ( ! result . image . data ) throw new Error ( 'No data read from chunk' ) ;
356+ if ( ! result . image . data )
357+ throw new Error ( `No data read from chunk ${ chunkId } ` ) ;
358+
359+ if ( result . image . size [ 2 ] > 1 && this . chunks . length > 1 ) {
360+ // we're trying to load multiple chunks where individual chunks have multiple frames
361+ throw new Error (
362+ `Loading a single volume from multiple DICOM files where individual files contain multiple frames is not supported. ` +
363+ `File ${ chunkId } (chunk ${ chunkIndex } ) contains ${ result . image . size [ 2 ] } frames.`
364+ ) ;
365+ }
339366
340367 const scalars = this . vtkImageData . value . getPointData ( ) . getScalars ( ) ;
341368 const pixelData = scalars . getData ( ) as TypedArray ;
0 commit comments