Serialize.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "Serialize.h"
  2. #include "tinyxml2.h"
  3. #include "XmlException.h"
  4. #include <sstream>
  5. using alm::ImageView;
  6. using alm::XmlException;
  7. using tinyxml2::XMLElement;
  8. using tinyxml2::XMLDocument;
  9. using tinyxml2::XMLError;
  10. using tinyxml2::XMLNode;
  11. using tinyxml2::XML_SUCCESS;
  12. alm::Gradient alm::deserializeGradient(XMLElement* elem)
  13. {
  14. if (elem == nullptr)
  15. throw alm::XmlException{ "invalid root node" };
  16. bool repeat = false;
  17. bool hasMax = false;
  18. float maxVal = 0.0f;
  19. repeat = elem->BoolAttribute("repeat", false);
  20. XMLError e = elem->QueryFloatAttribute("max", &maxVal);
  21. if (e == XML_SUCCESS)
  22. hasMax = true;
  23. std::vector<std::pair<RGBColor, float>> points;
  24. XMLElement* colorNode = elem->FirstChildElement("color");
  25. while(colorNode != nullptr) {
  26. int r = colorNode->IntAttribute("r");
  27. int g = colorNode->IntAttribute("g");
  28. int b = colorNode->IntAttribute("b");
  29. float p = colorNode->FloatAttribute("p");
  30. uint8_t cr = uint8_t(std::clamp(r, 0, 255));
  31. uint8_t cg = uint8_t(std::clamp(g, 0, 255));
  32. uint8_t cb = uint8_t(std::clamp(b, 0, 255));
  33. points.push_back(std::make_pair(RGBColor{ cr, cg, cb }, p));
  34. colorNode = colorNode->NextSiblingElement("color");
  35. }
  36. if (hasMax)
  37. return Gradient{ std::move(points), maxVal, repeat };
  38. else
  39. return Gradient{ std::move(points), repeat };
  40. }
  41. alm::Gradient alm::loadGradient(const std::string& xml)
  42. {
  43. XMLDocument xmlDoc;
  44. XMLError err = xmlDoc.Parse(xml.c_str());
  45. if (err != XML_SUCCESS)
  46. throw alm::XmlException{ "error parsing gradient xml" };
  47. return deserializeGradient(xmlDoc.RootElement());
  48. }
  49. std::string alm::saveGradient(const Gradient& g)
  50. {
  51. std::stringstream buf;
  52. std::string version = "1.0.0";
  53. buf << "<gradient max=\"" << g.getMax() << "\" repeat=\"" << (g.isRepeat() ? "true" : "false")
  54. << "\" version=\"" << version << "\" >" << std::endl;
  55. for (const auto&[color, val] : g.getPoints()) {
  56. buf << " <color " <<
  57. "r=\"" << int(color.r) <<
  58. "\" g=\"" << int(color.g) <<
  59. "\" b=\"" << int(color.b) <<
  60. "\" p=\"" << val << "\" />" << std::endl;
  61. }
  62. buf << "</gradient>" << std::endl;
  63. return buf.str();
  64. }
  65. ImageView deserializeImageView(tinyxml2::XMLElement* xml);
  66. std::unique_ptr<tinyxml2::XMLElement> serializeImageView(const ImageView& iv);