26 if (presetFilename.find(
".lsetp") == std::string::npos)
28 presetFilename.append(
".lsetp");
31 TiXmlDocument *presetXmlDoc =
new TiXmlDocument();
33 TiXmlDeclaration *decl =
new TiXmlDeclaration(
"1.0",
"",
"");
34 presetXmlDoc->LinkEndChild(decl);
36 TiXmlElement *presetElement =
new TiXmlElement(
"LabelSetImagePreset");
37 presetElement->SetAttribute(
"layers", inputImage->GetNumberOfLayers());
39 presetXmlDoc->LinkEndChild(presetElement);
41 for (
unsigned int layerIdx = 0; layerIdx < inputImage->GetNumberOfLayers(); layerIdx++)
43 TiXmlElement *layerElement =
new TiXmlElement(
"Layer");
44 layerElement->SetAttribute(
"index", layerIdx);
45 layerElement->SetAttribute(
"labels", inputImage->GetNumberOfLabels(layerIdx));
47 presetElement->LinkEndChild(layerElement);
49 for (
unsigned int labelIdx = 0; labelIdx < inputImage->GetNumberOfLabels(layerIdx); labelIdx++)
52 layerElement->LinkEndChild(labelAsXml);
56 bool wasSaved = presetXmlDoc->SaveFile(presetFilename);
65 if (presetFilename.find(
".lsetp") == std::string::npos)
67 presetFilename.append(
".lsetp");
70 std::unique_ptr<TiXmlDocument> presetXmlDoc(
new TiXmlDocument());
72 bool ok = presetXmlDoc->LoadFile(presetFilename);
76 TiXmlElement *presetElem = presetXmlDoc->FirstChildElement(
"LabelSetImagePreset");
84 presetElem->QueryIntAttribute(
"layers", &numberOfLayers);
86 for (
int i = 0; i < numberOfLayers; i++)
88 TiXmlElement *layerElem = presetElem->FirstChildElement(
"Layer");
90 layerElem->QueryIntAttribute(
"labels", &numberOfLabels);
92 if (inputImage->GetLabelSet(i) == NULL)
93 inputImage->AddLayer();
95 TiXmlElement *labelElement = layerElem->FirstChildElement(
"Label");
96 if (labelElement == NULL)
98 for (
int j = 0; j < numberOfLabels; j++)
102 if (label->GetValue() == 0)
103 inputImage->SetExteriorLabel(label);
105 inputImage->GetLabelSet()->AddLabel(label);
107 labelElement = labelElement->NextSiblingElement(
"Label");
108 if (labelElement == NULL)
116 TiXmlElement *labelElem =
new TiXmlElement(
"Label");
120 for (PropertyList::PropertyMap::const_iterator iter = propmap->begin(); iter != propmap->end(); ++iter)
122 std::string key = iter->first;
124 TiXmlElement *element = PropertyToXmlElem(key, property);
126 labelElem->LinkEndChild(element);
134 TiXmlElement *propElem = labelElem->FirstChildElement(
"property");
143 label->SetProperty(name, prop);
144 propElem = propElem->NextSiblingElement(
"property");
147 return label.GetPointer();
152 TiXmlElement *keyelement =
new TiXmlElement(
"property");
153 keyelement->SetAttribute(
"key", key);
154 keyelement->SetAttribute(
"type", property->GetNameOfClass());
157 std::string serializername(property->GetNameOfClass());
158 serializername +=
"Serializer";
160 std::list<itk::LightObject::Pointer> allSerializers =
161 itk::ObjectFactoryBase::CreateAllInstance(serializername.c_str());
162 if (allSerializers.size() < 1)
163 MITK_ERROR <<
"No serializer found for " << property->GetNameOfClass() <<
". Skipping object";
165 if (allSerializers.size() > 1)
166 MITK_WARN <<
"Multiple serializers found for " << property->GetNameOfClass() <<
"Using arbitrarily the first one.";
168 for (std::list<itk::LightObject::Pointer>::iterator iter = allSerializers.begin(); iter != allSerializers.end();
173 serializer->SetProperty(property);
176 TiXmlElement *valueelement = serializer->Serialize();
178 keyelement->LinkEndChild(valueelement);
180 catch (std::exception &e)
182 MITK_ERROR <<
"Serializer " << serializer->GetNameOfClass() <<
" failed: " << e.what();
195 elem->QueryStringAttribute(
"type", &type);
196 elem->QueryStringAttribute(
"key", &key);
199 std::string serializername(type);
200 serializername +=
"Serializer";
202 std::list<itk::LightObject::Pointer> allSerializers =
203 itk::ObjectFactoryBase::CreateAllInstance(serializername.c_str());
204 if (allSerializers.size() < 1)
205 MITK_ERROR <<
"No serializer found for " << type <<
". Skipping object";
207 if (allSerializers.size() > 1)
208 MITK_WARN <<
"Multiple deserializers found for " << type <<
"Using arbitrarily the first one.";
210 for (std::list<itk::LightObject::Pointer>::iterator iter = allSerializers.begin(); iter != allSerializers.end();
217 prop = serializer->Deserialize(elem->FirstChildElement());
219 catch (std::exception &e)
221 MITK_ERROR <<
"Deserializer " << serializer->GetNameOfClass() <<
" failed: " << e.what();
static itk::SmartPointer< mitk::Label > LoadLabelFromTiXmlDocument(TiXmlElement *labelElem)
Creates a mitk::Label from a TiXmlElement.
static TiXmlElement * PropertyToXmlElem(const std::string &key, const BaseProperty *property)
Since a mitk::Label is basically a mitk::PropertyList this function coverts the label's properties in...
A data structure describing a label.
static bool PropertyFromXmlElem(std::string &key, itk::SmartPointer< mitk::BaseProperty > &prop, TiXmlElement *elem)
Since a mitk::Label is basically a mitk::PropertyList this function coverts a XML element into a prop...
static void LoadLabelSetImagePreset(std::string &presetFilename, itk::SmartPointer< mitk::LabelSetImage > &inputImage)
Loads an existing preset for a mitk::LabelSetImage from presetFilename and applies it to inputImage...
Abstract base class for properties.
std::map< std::string, BaseProperty::Pointer > PropertyMap
Base class for objects that serialize BaseProperty types.
static TiXmlElement * GetLabelAsTiXmlElement(Label *label)
Creates a TiXmlElement from a mitk::Label.
static bool SaveLabelSetImagePreset(std::string &presetFilename, itk::SmartPointer< mitk::LabelSetImage > &inputImage)
Saves the mitk::LabelSet configuration of inputImage to presetFilename. The preset is stored as "*...
const PropertyMap * GetMap() const