OGS
XmlLutReader.h
Go to the documentation of this file.
1 
15 #pragma once
16 
17 #include <QFile>
18 #include <QtXml/QDomDocument>
19 
20 #include "BaseLib/Logging.h"
21 
23 
24 
25 namespace FileIO
26 {
27 
32 {
33 public:
34  static bool readFromFile(const QString &fileName, DataHolderLib::ColorLookupTable &lut)
35  {
36  QFile file(fileName);
37  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
38  {
39  ERR("XmlLutReader::readFromFile(): Can't open xml-file {:s}.",
40  fileName.toStdString());
41  return false;
42  }
43 
44  QDomDocument doc("ColorMap");
45  doc.setContent(&file);
46  QDomElement docElement = doc.documentElement();
47  if (docElement.nodeName().compare("ColorMap"))
48  {
49  ERR("XmlLutReader::readFromFile(): Unexpected XML root.");
50  file.close();
51  return false;
52  }
53 
54  if (docElement.hasAttribute("interpolation"))
55  {
56  if (docElement.attribute("interpolation").compare("Linear") == 0)
58  else if (docElement.attribute("interpolation").compare("Exponential") == 0)
60  else
62  }
63  else // default
65 
66  QDomElement point = docElement.firstChildElement();
67  double range[2] = { point.attribute("x").toDouble(), point.attribute("x").toDouble() };
68 
69  while (!point.isNull())
70  {
71  if ((point.nodeName().compare("Point") == 0 )
72  && point.hasAttribute("x")
73  && point.hasAttribute("r")
74  && point.hasAttribute("g")
75  && point.hasAttribute("b"))
76  {
77  double value = point.attribute("x").toDouble();
78  unsigned char r = static_cast<int>(255 * point.attribute("r").toDouble());
79  unsigned char g = static_cast<int>(255 * point.attribute("g").toDouble());
80  unsigned char b = static_cast<int>(255 * point.attribute("b").toDouble());
81  unsigned char o = static_cast<int>(255 * (point.hasAttribute("o") ? point.attribute("o").toDouble() : 1));
82 
83  if (value < range[0])
84  range[0] = value;
85  if (value > range[1])
86  range[1] = value;
87 
89  lut.setColor(value, color);
90  }
91  point = point.nextSiblingElement();
92  }
93 
94  lut.setTableRange(range[0], range[1]);
95 
96  file.close();
97  return true;
98  };
99 
100 
101 };
102 
103 } // namespace FileIO
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
void setColor(double id, DataHolderLib::Color const &color)
void setInterpolationType(LUTType type)
void setTableRange(double min, double max)
Reader for vtk-Lookup-Tables (in XML / ParaView Format)
Definition: XmlLutReader.h:32
static bool readFromFile(const QString &fileName, DataHolderLib::ColorLookupTable &lut)
Definition: XmlLutReader.h:34
std::array< unsigned char, 4 > Color
Definition: Color.h:24
Color createColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
Definition: Color.cpp:21
static const double r