| 128 | |
| 129 | |
| 130 | bool LoadNewJSONPreset(const std::string& presetFilename, |
| 131 | mitk::MultiLabelSegmentation* inputImage) |
| 132 | { |
| 133 | if (nullptr == inputImage) |
| 134 | return false; |
| 135 | |
| 136 | std::ifstream input(presetFilename); |
| 137 | if (!input.is_open()) |
| 138 | { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | nlohmann::json fileContent; |
| 143 | try |
| 144 | { |
| 145 | input >> fileContent; |
| 146 | } |
| 147 | catch (const nlohmann::json::parse_error& e) |
| 148 | { |
| 149 | mitkThrow() << "Cannot read data due to parsing error. Parse error: " << e.what() << '\n'; |
| 150 | } |
| 151 | |
| 152 | //check version |
| 153 | int version = 0; |
| 154 | |
| 155 | if (!mitk::MultiLabelIOHelper::GetValueFromJson<int>(fileContent, "version", version)) |
| 156 | { |
| 157 | MITK_WARN << "Preset has unknown version. Assuming that it can be read. Result might be invalid."; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | const int MULTILABEL_SEGMENTATION_VERSION_VALUE = 4; |
| 162 | if (version > MULTILABEL_SEGMENTATION_VERSION_VALUE) |
| 163 | { |
| 164 | mitkThrow() << "The preset you are trying to load has an unsupported version. " |
| 165 | << "This software is too old to ensure correct loading. " |
| 166 | << "Please use a compatible version of MITK or save the data in another format. " |
| 167 | << "Data version: " << version |
| 168 | << "; supported versions up to: " << MULTILABEL_SEGMENTATION_VERSION_VALUE; |
| 169 | |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | auto groupInfos = mitk::MultiLabelIOHelper::DeserializeMultiLabelGroupsFromJSON(fileContent["groups"]); |
| 174 | |
| 175 | mitk::MultiLabelSegmentation::GroupIndexType groupIndex = 0; |
| 176 | for (const auto& groupInfo : groupInfos) |
| 177 | { |
| 178 | auto cleanedLabels = mitk::MultiLabelIOHelper::CloneLabelsWithoutMetaProperties(groupInfo.labels); |
| 179 | |
| 180 | if (inputImage->ExistGroup(groupIndex)) |
| 181 | { //group exists so update name and labels |
| 182 | inputImage->SetGroupName(groupIndex, groupInfo.name); |
| 183 | |
| 184 | for (auto label : cleanedLabels) |
| 185 | { |
| 186 | if (inputImage->ExistLabel(label->GetValue(), groupIndex)) |
| 187 | { |
no test coverage detected