forked from Kitware/VolView
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SegmentGroupControls): add save button
- Loading branch information
Showing
5 changed files
with
180 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<template> | ||
<v-card> | ||
<v-card-title class="d-flex flex-row align-center"> | ||
Save Segment Group | ||
</v-card-title> | ||
<v-card-text> | ||
<v-form v-model="valid" @submit.prevent="saveSegmentGroup"> | ||
<v-text-field | ||
v-model="fileName" | ||
hint="Filename that will appear in downloads." | ||
label="Filename" | ||
:rules="[validFileName]" | ||
required | ||
id="filename" | ||
/> | ||
|
||
<v-radio-group v-model="fileFormat" label="Format"> | ||
<v-radio v-for="ext in EXTENSIONS" :key="ext" :value="ext"> | ||
<template #label> | ||
<div class="d-flex flex-row align-center w-100" :title="ext"> | ||
<span class="group-name">{{ ext }}</span> | ||
</div> | ||
</template> | ||
</v-radio> | ||
</v-radio-group> | ||
</v-form> | ||
</v-card-text> | ||
<v-card-actions> | ||
<v-spacer /> | ||
<v-btn | ||
:loading="saving" | ||
color="secondary" | ||
@click="saveSegmentGroup" | ||
:disabled="!valid" | ||
> | ||
<v-icon class="mr-2">mdi-content-save</v-icon> | ||
<span data-testid="save-confirm-button">Save</span> | ||
</v-btn> | ||
</v-card-actions> | ||
</v-card> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, onMounted, ref } from 'vue'; | ||
import { saveAs } from 'file-saver'; | ||
import { useSegmentGroupStore } from '@/src/store/segmentGroups'; | ||
import { writeImage } from '@/src/io/readWriteImage'; | ||
const EXTENSIONS = [ | ||
'dcm', | ||
'nrrd', | ||
'hdf5', | ||
'nii', | ||
'nii.gz', | ||
'tif', | ||
'mha', | ||
'vtk', | ||
'iwi.cbor', | ||
]; | ||
export default defineComponent({ | ||
props: { | ||
close: { | ||
type: Function, | ||
required: true, | ||
}, | ||
id: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
setup(props) { | ||
const fileName = ref(''); | ||
const valid = ref(true); | ||
const saving = ref(false); | ||
const fileFormat = ref('dcm'); | ||
const segmentGroupStore = useSegmentGroupStore(); | ||
async function saveSegmentGroup() { | ||
if (fileName.value.trim().length === 0) { | ||
return; | ||
} | ||
saving.value = true; | ||
try { | ||
const image = segmentGroupStore.dataIndex[props.id]; | ||
const serialized = await writeImage(fileFormat.value, image); | ||
saveAs(new Blob([serialized]), `${fileName.value}.${fileFormat.value}`); | ||
props.close(); | ||
} finally { | ||
saving.value = false; | ||
} | ||
} | ||
onMounted(() => { | ||
// trigger form validation check so can immediately save with default value | ||
fileName.value = segmentGroupStore.metadataByID[props.id].name; | ||
}); | ||
function validFileName(name: string) { | ||
return name.trim().length > 0 || 'Required'; | ||
} | ||
return { | ||
saving, | ||
saveSegmentGroup, | ||
fileName, | ||
validFileName, | ||
valid, | ||
fileFormat, | ||
EXTENSIONS, | ||
}; | ||
}, | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import vtkITKHelper from '@kitware/vtk.js/Common/DataModel/ITKHelper'; | ||
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData'; | ||
import { copyImage } from 'itk-wasm'; | ||
import { | ||
readImage as readImageItk, | ||
writeImage as writeImageItk, | ||
} from '@itk-wasm/image-io'; | ||
import { vtiReader, vtiWriter } from '@/src/io/vtk/async'; | ||
|
||
export const readImage = async (file: File) => { | ||
if (file.name.endsWith('.vti')) | ||
return (await vtiReader(file)) as vtkImageData; | ||
|
||
const { image } = await readImageItk(null, file); | ||
return vtkITKHelper.convertItkToVtkImage(image); | ||
}; | ||
|
||
export const writeImage = async (format: string, image: vtkImageData) => { | ||
if (format === 'vti') { | ||
return vtiWriter(image); | ||
} | ||
// 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}`); | ||
result.webWorker?.terminate(); | ||
return result.serializedImage.data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters