Loading...
Searching...
No Matches
Palette.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef DRAIN_PALETTE
32#define DRAIN_PALETTE
33
34#include "drain/UniTuple.h"
35#include "drain/util/BeanLike.h"
36#include "drain/util/JSON.h"
37#include "drain/util/Dictionary.h"
38
39#include "ImageFile.h"
40#include "Geometry.h"
41#include "TreeSVG.h"
42#include "TreeSLD.h"
43#include "Legend.h"
44#include "PaletteEntry.h"
45
46
47namespace drain
48{
49
50
51namespace image
52{
53
54
58class Palette : public ImageCodeMap<PaletteEntry> {
59
60public:
61
62 static
63 const SprinterLayout cppLayout2;
64
66 inline
67 Palette() : ImageCodeMap<PaletteEntry>(), refinement(0) {
68 };
69
71
75 Palette(std::initializer_list<std::pair<Variable, PaletteEntry> > inits);
76
92 const value_type & getEntryByCode(const std::string & code, bool lenient=true) const;
93
95 inline
96 const key_type getValueByCode(const std::string & code, bool lenient=true) const {
97 const value_type & entry = getEntryByCode(code, lenient);
98 return entry.first;
99 }
100
101 void reset();
102
104 void addEntry(double min, double red=0.0, double green=0.0, double blue=0.0, const std::string &id="", const std::string & label =""); // alpha?
105
107
110 void load(const std::string & filename, bool flexible = false);
111
113 void loadTXT(std::istream & ifstr);
114
116 void loadJSON(std::istream & ifstr);
117
118 void write(const std::string & filename) const;
119
120 void exportTXT(std::ostream & ostr, char separator='\t', char separator2=0) const;
121
122 //void exportJSON(drain::JSONtree::tree_t & json) const;
123 void exportJSON(drain::JSONtree & json) const;
124
125 // Export formatted
126 void exportFMT(std::ostream & ostr, const std::string & format) const;
127
129 void exportSVGLegend(TreeSVG & svg, bool up = true) const;
130
132 void exportSLD(TreeSLD & sld) const;
133
135
141
144 // void convertJSON(const drain::JSONtree::tree_t & json);
145
146 const ChannelGeometry & getChannels() const {
147 update();
148 return channels;
149 }
150
152 std::string title;
153
155 // std::string id; ?
156
158 typedef std::map<std::string,PaletteEntry> spec_t;
159 spec_t specialCodes; // To be read from palette
160
162 void refine(size_t n=0);
163
164 // typedef drain::Dictionary<key_t, std::string> dict_t;
165
167
169
172 size_t refinement;
173
174 // Metadata - for example, the file path of a loaded palette.
175 std::string comment;
176
177protected:
178
180 // dict_t dictionary; // deprecating!
181
182
183 void update() const;
184
186 void importJSON(const drain::JSONtree & json);
187
188 mutable
189 ChannelGeometry channels;
190
191
192};
193
194
195inline
196std::ostream & operator<<(std::ostream &ostr, const Palette & p){
197 p.exportTXT(ostr);
198 return ostr;
199}
200
201
202
203
204class PaletteMap : public std::map<std::string,drain::image::Palette> {
205
206public:
207
208 inline
209 Palette & get(const std::string & key){
210 if (aliases.hasKey(key)){
211 return (*this)[aliases.getValue(key)];
212 }
213 else {
214 return (*this)[key];
215 }
216 }
217
218 inline
219 void addAlias(const std::string & key, const std::string & aliasKey){
220 aliases.add(aliasKey,key);
221 }
222
223// protected:
224
226 // std::map<std::string,std::string> aliasMap;
227
228};
229
230} // image::
231
232template <>
233std::ostream & drain::Sprinter::toStream(std::ostream & ostr, const drain::image::Palette & map, const drain::SprinterLayout & layout);
234
235} // drain::
236
237
238#endif
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:63
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition Sprinter.h:420
Definition DataSelector.cpp:1277
Definition Sprinter.h:137