BaseProperty::Pointer mitk::TransferFunctionPropertySerializer::Deserialize(TiXmlElement* element)
{
  if (!element) 
    return NULL;
  
  TransferFunction::Pointer tf = TransferFunction::New();

  // deserialize scalar opacity function
  TiXmlElement* scalarOpacityPointlist = element->FirstChildElement("ScalarOpacity");
  if (scalarOpacityPointlist == NULL)
    return NULL;
    
  tf->ClearScalarOpacityPoints();  
    
  for( TiXmlElement* pointElement = scalarOpacityPointlist->FirstChildElement("point"); pointElement != NULL; pointElement = pointElement->NextSiblingElement("point"))
  {
    double x;
    double y;
    if (pointElement->QueryDoubleAttribute("x", &x) == TIXML_WRONG_TYPE)
      return NULL; // TODO: can we do a better error handling?
    if (pointElement->QueryDoubleAttribute("y", &y) == TIXML_WRONG_TYPE)
      return NULL; // TODO: can we do a better error handling?
    tf->AddScalarOpacityPoint(x, y);
  }

  TiXmlElement* gradientOpacityPointlist = element->FirstChildElement("GradientOpacity");
  if (gradientOpacityPointlist == NULL)
    return NULL;
  
  tf->ClearGradientOpacityPoints();
  
  for( TiXmlElement* pointElement = gradientOpacityPointlist->FirstChildElement("point"); pointElement != NULL; pointElement = pointElement->NextSiblingElement("point"))
  {
    double x;
    double y;
    if (pointElement->QueryDoubleAttribute("x", &x) == TIXML_WRONG_TYPE)
      return NULL; // TODO: can we do a better error handling?
    if (pointElement->QueryDoubleAttribute("y", &y) == TIXML_WRONG_TYPE)
      return NULL; // TODO: can we do a better error handling?
    tf->AddGradientOpacityPoint(x, y);
  }

  TiXmlElement* rgbPointlist = element->FirstChildElement("Color");
  if (rgbPointlist == NULL)
    return NULL;
  vtkColorTransferFunction* ctf = tf->GetColorTransferFunction();
  if (ctf == NULL)
    return NULL;
  
  ctf->RemoveAllPoints();
  
  for( TiXmlElement* pointElement = rgbPointlist->FirstChildElement("point"); pointElement != NULL; pointElement = pointElement->NextSiblingElement("point"))
  {
    double x;
    double r,g,b, midpoint, sharpness;
    if (pointElement->QueryDoubleAttribute("x", &x) == TIXML_WRONG_TYPE)
      return NULL; // TODO: can we do a better error handling?
    if (pointElement->QueryDoubleAttribute("r", &r) == TIXML_WRONG_TYPE)
      return NULL; // TODO: can we do a better error handling?
    if (pointElement->QueryDoubleAttribute("g", &g) == TIXML_WRONG_TYPE)
      return NULL; // TODO: can we do a better error handling?
    if (pointElement->QueryDoubleAttribute("b", &b) == TIXML_WRONG_TYPE)
      return NULL; // TODO: can we do a better error handling?
    if (pointElement->QueryDoubleAttribute("midpoint", &midpoint) == TIXML_WRONG_TYPE)
      return NULL; // TODO: can we do a better error handling?
    if (pointElement->QueryDoubleAttribute("sharpness", &sharpness) == TIXML_WRONG_TYPE)
      return NULL; // TODO: can we do a better error handling?
    ctf->AddRGBPoint(x, r, g, b, midpoint, sharpness);
  }
  return TransferFunctionProperty::New(tf).GetPointer();
}
BaseProperty::Pointer mitk::TransferFunctionPropertySerializer::Deserialize(TiXmlElement* element)
{
  if (!element)
    return nullptr;

  mitk::LocaleSwitch localeSwitch("C");

  TransferFunction::Pointer tf = TransferFunction::New();

  // deserialize scalar opacity function
  TiXmlElement* scalarOpacityPointlist = element->FirstChildElement("ScalarOpacity");
  if (scalarOpacityPointlist == nullptr)
  {
    return nullptr;
  }

  tf->ClearScalarOpacityPoints();

  try
  {
    for( TiXmlElement* pointElement = scalarOpacityPointlist->FirstChildElement("point");
         pointElement != nullptr;
         pointElement = pointElement->NextSiblingElement("point"))
    {
      std::string x;
      std::string y;
      if (pointElement->QueryStringAttribute("x", &x) != TIXML_SUCCESS) return nullptr;
      if (pointElement->QueryStringAttribute("y", &y) != TIXML_SUCCESS) return nullptr;
      tf->AddScalarOpacityPoint(boost::lexical_cast<double>(x), boost::lexical_cast<double>(y));
    }

    TiXmlElement* gradientOpacityPointlist = element->FirstChildElement("GradientOpacity");
    if (gradientOpacityPointlist == nullptr)
    {
      return nullptr;
    }

    tf->ClearGradientOpacityPoints();

    for( TiXmlElement* pointElement = gradientOpacityPointlist->FirstChildElement("point");
         pointElement != nullptr;
         pointElement = pointElement->NextSiblingElement("point"))
    {
      std::string x;
      std::string y;
      if (pointElement->QueryStringAttribute("x", &x) != TIXML_SUCCESS) return nullptr;
      if (pointElement->QueryStringAttribute("y", &y) != TIXML_SUCCESS) return nullptr;
      tf->AddGradientOpacityPoint(boost::lexical_cast<double>(x), boost::lexical_cast<double>(y));
    }

    TiXmlElement* rgbPointlist = element->FirstChildElement("Color");
    if (rgbPointlist == nullptr)
    {
      return nullptr;
    }
    vtkColorTransferFunction* ctf = tf->GetColorTransferFunction();
    if (ctf == nullptr)
    {
      return nullptr;
    }

    ctf->RemoveAllPoints();

    for( TiXmlElement* pointElement = rgbPointlist->FirstChildElement("point");
         pointElement != nullptr;
         pointElement = pointElement->NextSiblingElement("point"))
    {
      std::string x;
      std::string r,g,b, midpoint, sharpness;
      if (pointElement->QueryStringAttribute("x", &x) != TIXML_SUCCESS) return nullptr;
      if (pointElement->QueryStringAttribute("r", &r) != TIXML_SUCCESS) return nullptr;
      if (pointElement->QueryStringAttribute("g", &g) != TIXML_SUCCESS) return nullptr;
      if (pointElement->QueryStringAttribute("b", &b) != TIXML_SUCCESS) return nullptr;
      if (pointElement->QueryStringAttribute("midpoint", &midpoint) != TIXML_SUCCESS) return nullptr;
      if (pointElement->QueryStringAttribute("sharpness", &sharpness) != TIXML_SUCCESS) return nullptr;
      ctf->AddRGBPoint(boost::lexical_cast<double>(x),
                       boost::lexical_cast<double>(r), boost::lexical_cast<double>(g), boost::lexical_cast<double>(b),
                       boost::lexical_cast<double>(midpoint),
                       boost::lexical_cast<double>(sharpness));
    }
  }
  catch ( boost::bad_lexical_cast& e )
  {
    MITK_ERROR << "Could not parse string as number: " << e.what();

    return nullptr;
  }

  return TransferFunctionProperty::New(tf).GetPointer();
}