PaletteOp.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef DRAIN_PALETTEOP_H
32 #define DRAIN_PALETTEOP_H
33 
34 #include <map>
35 #include "drain/util/Bank.h"
36 #include "drain/image/Palette.h"
37 
38 #include "ImageOp.h"
39 #include "CopyOp.h"
40 
41 
42 
43 namespace drain
44 {
45 
46 namespace image
47 {
48 
50 
54 class PaletteOp : public ImageOp
55 {
56 public:
57 
58  PaletteOp() : ImageOp(__FUNCTION__,"Applies colour palette to an image"), palettePtr(&myPalette) {
59  this->parameters.link("scale", scale = 1.0);
60  this->parameters.link("offset", offset = 0.0);
61  specialCodes.title = "Special codes";
62  };
63 
64  PaletteOp(const Palette & palette) : ImageOp(__FUNCTION__,"Applies colour palette to an image"), palettePtr(&myPalette) {
65  this->parameters.link("scale", scale = 1.0);
66  this->parameters.link("offset", offset = 0.0);
67  specialCodes.title = "Special codes";
68  setPalette(palette);
69  };
70 
71 
72  PaletteOp(const std::string & filename = "") : ImageOp(__FUNCTION__,"Applies colour palette to an image"), palettePtr(&myPalette) {
73  this->parameters.link("scale", scale = 1.0);
74  this->parameters.link("offset", offset = 0.0);
75  specialCodes.title = "Special codes";
76  myPalette.load(filename);
77  };
78 
79  virtual ~PaletteOp(){};
80 
81 
82  //void processOLD(const ImageFrame &src,Image &dst) const ;
83 
84  void traverseChannels(const ImageTray<const Channel> & src, ImageTray<Channel> & dst) const;
85 
86  virtual
87  void help(std::ostream & ostr = std::cout) const;
88 
90 
93  //void setPalette(const ImageFrame &palette) const; //,unsigned int maxColors = 256);
94  void setPalette(const Palette &palette); //,unsigned int maxColors = 256);
95 
96  inline
97  const Palette & getPalette() const {
98  return *palettePtr;
99  }
100 
102 
109  static
110  Palette & getPalette(const std::string & key);
111 
113  static
114  Palette & loadPalette(const std::string & key);
115 
117  // static
118  // Palette & getGrayPalette(const std::string & key);
119 
120  // TODO: Refinement. Or better in actual op exec, with lookup? Or: retrieve a lookup...
121  // const Palette & getPalette(const std::string & key) const;
122 
123 
125  // TODO T 256, T2 32768
126  // void setGrayPalette(unsigned int iChannels=3, unsigned int aChannels=0, float brightness=0.0,float contrast=1.0) const;
127 
128 
130  double scale;
131 
133  double offset;
134 
136 
142  void registerSpecialCode(const std::string & code, double d);
143 
144  // protect:
146  Palette specialCodes;
147 
148  /*
149  static inline
150  std::map<std::string,drain::image::Palette> & getPaletteMap(){
151  return paletteMap;
152  };
153  */
154  //typedef std::map<std::string,drain::image::Palette> palette_map_t;
155  typedef PaletteMap palette_map_t;
156 
158  static
159  palette_map_t & getPaletteMap();
160  /*
161  static
162  std::map<std::string,drain::image::Palette> paletteMap;
163  */
164 protected:
165 
166 
167  //void initialize() const {Image & dst};
168 
169  void getDstConf(const ImageConf &src, ImageConf &dst) const;
170  //void makeCompatible(const ImageFrame &src, Image &dst) const;
171 
172  //mutable Image paletteImage;
173  Palette myPalette;
174  const Palette *palettePtr;
175 
176 };
177 
178 
179 
180 }
181 
182 }
183 
184 #endif /*PALETTEOP_H_*/
185 
186 // Drain
Struct for image (excluding data)
Definition: ImageConf.h:333
Base class for image processing functions.
Definition: ImageOp.h:49
ImageOp(const std::string &name=__FUNCTION__, const std::string &description="")
Definition: ImageOp.h:156
Container applicable for Channels and Images, with alpha support.
Definition: ImageTray.h:267
Colorizes an image of 1 channel to an image of N channels by using a palette image as a lookup table.
Definition: PaletteOp.h:55
Palette specialCodes
Intensity mappings set by user, originally with std::string keys in Palette.
Definition: PaletteOp.h:146
double offset
Prescale intensities with scale*i + offset.
Definition: PaletteOp.h:133
void setPalette(const Palette &palette)
Creates an internal palette by taking the colors on the diagonal from lower left to upper right corne...
Definition: PaletteOp.cpp:78
static palette_map_t & getPaletteMap()
Returns a static palette map which is initially empty.
Definition: PaletteOp.cpp:165
double scale
Uses the original coloured palette to create a gray-level palette.
Definition: PaletteOp.h:130
virtual void help(std::ostream &ostr=std::cout) const
Prints name, description and parameters using BeanLike::toOStr(). Virtual, so derived classes may ext...
Definition: PaletteOp.cpp:252
static Palette & loadPalette(const std::string &key)
NEW Load palette to Palette::paletteBank by key (like "DBZH") from TXT or JSON file.
Definition: PaletteOp.cpp:100
void getDstConf(const ImageConf &src, ImageConf &dst) const
Given source image, determine respective dest image configuration.
Definition: PaletteOp.cpp:217
void registerSpecialCode(const std::string &code, double d)
Connect special code (label) to intensity.
Definition: PaletteOp.cpp:174
Definition: DataSelector.cpp:1277