diff --git a/src/store/segmentGroups.ts b/src/store/segmentGroups.ts index 20914dbab..fb9929d98 100644 --- a/src/store/segmentGroups.ts +++ b/src/store/segmentGroups.ts @@ -47,6 +47,18 @@ const writeImage = async (format: string, image: vtkImageData) => { } // copyImage so writeImage does not detach live data when passing to worker const itkImage = copyImage(vtkITKHelper.convertVtkToItkImage(image)); + + // Transpose the direction matrix to fix bug in @itk-wasm/image-io.writeImage + // Remove when @itk-wasm/image-io version is above 0.5.0 https://github.com/InsightSoftwareConsortium/itk-wasm/commit/ad9ca85eedc47c9d3444cf36859569c529886bde + const oldDirection = [...itkImage.direction]; + const { dimension } = itkImage.imageType; + for (let idx = 0; idx < dimension; ++idx) { + for (let idy = 0; idy < dimension; ++idy) { + itkImage.direction[idx + idy * dimension] = + oldDirection[idy + idx * dimension]; + } + } + const result = await writeImageItk(null, itkImage, `image.${format}`); return result.serializedImage.data; };