| 64 | } |
| 65 | |
| 66 | void MultiLabelSegmentationIO::Write() |
| 67 | { |
| 68 | ValidateOutputLocation(); |
| 69 | |
| 70 | auto input = dynamic_cast<const MultiLabelSegmentation *>(this->GetInput()); |
| 71 | |
| 72 | mitk::LocaleSwitch localeSwitch("C"); |
| 73 | |
| 74 | mitk::Image::Pointer inputVector = mitk::ConvertLabelSetImageToImage(input); |
| 75 | |
| 76 | // image write |
| 77 | if (inputVector.IsNull()) |
| 78 | { |
| 79 | mitkThrow() << "Cannot write non-image data"; |
| 80 | } |
| 81 | |
| 82 | itk::NrrdImageIO::Pointer nrrdImageIo = itk::NrrdImageIO::New(); |
| 83 | |
| 84 | ItkImageIO::PreparImageIOToWriteImage(nrrdImageIo, inputVector); |
| 85 | |
| 86 | LocalFile localFile(this); |
| 87 | const std::string path = localFile.GetFileName(); |
| 88 | |
| 89 | MITK_INFO << "Writing image: " << path << std::endl; |
| 90 | |
| 91 | try |
| 92 | { |
| 93 | itk::EncapsulateMetaData<std::string>( |
| 94 | nrrdImageIo->GetMetaDataDictionary(), std::string(MULTILABEL_SEGMENTATION_MODALITY_KEY), std::string(MULTILABEL_SEGMENTATION_MODALITY_VALUE)); |
| 95 | |
| 96 | //nrrd does only support string meta information. So we have to convert before. |
| 97 | itk::EncapsulateMetaData<std::string>( |
| 98 | nrrdImageIo->GetMetaDataDictionary(), std::string(MULTILABEL_SEGMENTATION_VERSION_KEY), std::to_string(MULTILABEL_SEGMENTATION_VERSION_VALUE)); |
| 99 | |
| 100 | auto json = MultiLabelIOHelper::SerializeMultLabelGroupsToJSON(input); |
| 101 | itk::EncapsulateMetaData<std::string>( |
| 102 | nrrdImageIo->GetMetaDataDictionary(), std::string(MULTILABEL_SEGMENTATION_LABELS_INFO_KEY), json.dump()); |
| 103 | // end label set specific meta data |
| 104 | |
| 105 | //nrrd does only support string meta information. So we have to convert before. |
| 106 | itk::EncapsulateMetaData<std::string>( |
| 107 | nrrdImageIo->GetMetaDataDictionary(), std::string(MULTILABEL_SEGMENTATION_UNLABELEDLABEL_LOCK_KEY), std::to_string(input->GetUnlabeledLabelLock())); |
| 108 | |
| 109 | // Handle properties |
| 110 | ItkImageIO::SavePropertyListAsMetaData(nrrdImageIo->GetMetaDataDictionary(), input->GetPropertyList(), this->GetMimeType()->GetName()); |
| 111 | |
| 112 | // Handle UID |
| 113 | itk::EncapsulateMetaData<std::string>(nrrdImageIo->GetMetaDataDictionary(), PROPERTY_KEY_UID, input->GetUID()); |
| 114 | |
| 115 | // use compression if available |
| 116 | nrrdImageIo->UseCompressionOn(); |
| 117 | nrrdImageIo->SetFileName(path); |
| 118 | |
| 119 | ImageReadAccessor imageAccess(inputVector); |
| 120 | nrrdImageIo->Write(imageAccess.GetData()); |
| 121 | } |
| 122 | catch (const std::exception &e) |
| 123 | { |
nothing calls this directly
no test coverage detected