OGS
XmlLutReader.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <QFile>
7#include <QtXml/QDomDocument>
8
9#include "BaseLib/Logging.h"
10
12
13
14namespace FileIO
15{
16
21{
22public:
23 static bool readFromFile(const QString &fileName, DataHolderLib::ColorLookupTable &lut)
24 {
25 QFile file(fileName);
26 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
27 {
28 ERR("XmlLutReader::readFromFile(): Can't open xml-file {:s}.",
29 fileName.toStdString());
30 return false;
31 }
32
33 QDomDocument doc("ColorMap");
34 doc.setContent(&file);
35 QDomElement docElement = doc.documentElement();
36 if (docElement.nodeName().compare("ColorMap"))
37 {
38 ERR("XmlLutReader::readFromFile(): Unexpected XML root.");
39 file.close();
40 return false;
41 }
42
43 if (docElement.hasAttribute("interpolation"))
44 {
45 if (docElement.attribute("interpolation").compare("Linear") == 0)
47 else if (docElement.attribute("interpolation").compare("Exponential") == 0)
49 else
51 }
52 else // default
54
55 QDomElement point = docElement.firstChildElement();
56 double range[2] = { point.attribute("x").toDouble(), point.attribute("x").toDouble() };
57
58 while (!point.isNull())
59 {
60 if ((point.nodeName().compare("Point") == 0 )
61 && point.hasAttribute("x")
62 && point.hasAttribute("r")
63 && point.hasAttribute("g")
64 && point.hasAttribute("b"))
65 {
66 double value = point.attribute("x").toDouble();
67 unsigned char r = static_cast<int>(255 * point.attribute("r").toDouble());
68 unsigned char g = static_cast<int>(255 * point.attribute("g").toDouble());
69 unsigned char b = static_cast<int>(255 * point.attribute("b").toDouble());
70 unsigned char o = static_cast<int>(255 * (point.hasAttribute("o") ? point.attribute("o").toDouble() : 1));
71
72 if (value < range[0])
73 range[0] = value;
74 if (value > range[1])
75 range[1] = value;
76
78 lut.setColor(value, color);
79 }
80 point = point.nextSiblingElement();
81 }
82
83 lut.setTableRange(range[0], range[1]);
84
85 file.close();
86 return true;
87 };
88
89
90};
91
92} // namespace FileIO
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
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)
static bool readFromFile(const QString &fileName, DataHolderLib::ColorLookupTable &lut)
Color createColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
Definition Color.cpp:10
std::array< unsigned char, 4 > Color
Definition Color.h:12