MIRA
Colormap.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_COLORMAP_H_
49 #define _MIRA_COLORMAP_H_
50 
51 #include <factory/Factory.h>
52 #include <image/Color.h>
53 
54 namespace mira {
55 
57 
63 {
65 public:
66 
72  virtual std::size_t size() const = 0;
73 
77  virtual Color::RGB get(std::size_t idx) const = 0;
78 
82  Color::RGB operator()(std::size_t idx) {return get(idx);}
83 
87  Color::RGB operator[](std::size_t idx) {return get(idx);}
88 
89 public:
90 
96  class iterator
97  {
98  public:
100  typedef int difference_type;
101 
102  typedef const Color::RGB* pointer;
103  typedef const Color::RGB& reference;
104  typedef std::bidirectional_iterator_tag iterator_category;
105 
106  protected:
107  friend class Colormap;
108  iterator(const Colormap* colormap, std::size_t position) :
109  mColormap(colormap), mPosition(position) {}
110 
111  public:
112  iterator() : mColormap(NULL), mPosition(0) {}
113  iterator(const iterator& other) :
114  mColormap(other.mColormap), mPosition(other.mPosition) {}
115 
116  public:
117 
119  iterator& operator++() { // pre-increment
120  ++mPosition;
121  return *this;
122  }
123 
125  iterator operator++(int) { // post-increment
126  iterator old = *this;
127  mPosition++;
128  return old;
129  }
130 
132  iterator& operator--() { // pre-decrement
133  --mPosition;
134  return *this;
135  }
136 
138  iterator operator--(int) { // post-decrement
139  iterator old = *this;
140  mPosition--;
141  return old;
142  }
143 
144  bool operator==(const iterator& other) {
145  return mColormap==other.mColormap && mPosition==other.mPosition;
146  }
147 
148  bool operator!=(const iterator& other) {
149  return !operator==(other);
150  }
151 
152  public:
153 
156  assert(mColormap!=NULL);
157  mCache = mColormap->get(mPosition);
158  return mCache;
159  }
160 
162  pointer operator->() const {
163  assert(mColormap!=NULL);
164  mCache = mColormap->get(mPosition);
165  return &mCache;
166  }
167 
168  private:
169  const Colormap* mColormap;
170  std::size_t mPosition;
172  mutable Color::RGB mCache;
173  };
174 
176 
177 public:
178 
180  iterator begin() {return iterator(this,0);}
181 
183  const_iterator begin() const {return const_iterator(this,0);}
184 
186  iterator end() {return iterator(this,size());}
187 
189  const_iterator end() const {return const_iterator(this,size());}
190 };
191 
193 
196 {
198 };
199 
201 
218 {
220 public:
221  ContinuousColormap(std::size_t size=256) : mSize(size) {}
222 
223 public:
224 
226  void resize(std::size_t pSize) {mSize=pSize;}
227 
229  virtual std::size_t size() const {return mSize;}
230 
232  virtual Color::RGB get(std::size_t idx) const {
233  return getf((float)idx / (float)size());}
234 
235 public:
236 
241  virtual Color::RGB getf(float f) const = 0;
242 
243 private:
244  std::size_t mSize;
245 };
246 
252 {
253 public:
254 
255  GradientColormapBase(std::size_t size) : ContinuousColormap(size) {}
256 
257  virtual Color::RGB getf(float f) const;
258 
259 protected:
260 
265  void setColorAt(float position, const Color::RGB& color);
266 
267  void setColorAt(float position, int r, int g, int b) {
268  setColorAt(position, Color::RGB(r/255.0f,g/255.0f,b/255.0f));
269  }
270 
271 
273  void clear();
274 
275 private:
276  std::map<float, Color::RGB> mColorKeys;
277 };
278 
286 {
287 public:
288 
289  GradientColormap(std::size_t size=256) : GradientColormapBase(size) {}
290 
295  void setColorAt(float position, const Color::RGB& color) {
296  GradientColormapBase::setColorAt(position, color);
297  }
298 
300  void clear() {
302  }
303 };
304 
306 
312 {
313 public:
314 
315  FormulaeColormapBase(std::size_t size) : ContinuousColormap(size),
316  mRFormula(7),mGFormula(5),mBFormula(15) {}
317 
318  virtual Color::RGB getf(float f) const;
319 
320 protected:
321 
326  void setFormulae(int rformula, int gformula, int bformula);
327 
328 private:
329  int mRFormula, mGFormula, mBFormula;
330 };
331 
360 {
361 public:
362 
363  FormulaeColormap(std::size_t size=256) : FormulaeColormapBase(size) {}
364 
365  FormulaeColormap(int rformula, int gformula, int bformula, std::size_t size=256) : FormulaeColormapBase(size) {
366  setFormulae(rformula, gformula, bformula);
367  }
368 
372  void setFormulae(int rformula, int gformula, int bformula) {
373  FormulaeColormapBase::setFormulae(rformula, gformula, bformula);
374  }
375 };
376 
378 
386 {
388 public:
389 
395  TabularColormap(const uint8* pData, std::size_t pSize) :
396  mData(pData), mSize(pSize) {}
397 
398  virtual std::size_t size() const {return mSize;}
399 
400  virtual Color::RGB get(std::size_t idx) const {
401  return Color::RGB((float)mData[idx*3]/255.0f,
402  (float)mData[idx*3+1]/255.0f,
403  (float)mData[idx*3+2]/255.0f);
404  }
405 
406 private:
407  const uint8* mData;
408  std::size_t mSize;
409 };
410 
412 
418 {
420 public:
421 
422  GrayscaleColormap(std::size_t size=256) : ContinuousColormap(size) {}
423 
424  static Color::RGB sGetf(float f)
425  {
426  if(f<=0.0f)
427  return Color::RGB(0,0,0);
428  if(f<=1.0f)
429  return Color::RGB(f,f,f);
430 
431  return Color::RGB(1.0f,1.0f,1.0f);
432  }
433 
434  virtual Color::RGB getf(float f) const {
435  return GrayscaleColormap::sGetf(f);
436  }
437 
438 };
439 
441 
447 {
449 public:
450 
451  HSVColormap(std::size_t size=256) : ContinuousColormap(size) {}
452 
453  static Color::RGB sGetf(float f)
454  {
455  /*
456  0.0000 - 1 0 0
457  0.1667 - 1 1 0
458  0.3333 - 0 1 0
459  0.5000 - 0 1 1
460  0.6667 - 0 0 1
461  0.8333 - 1 0 1
462  1.0000 - 1 0 0
463  */
464 
465  if(f<=0.0f)
466  return Color::RGB(1,0,0);
467  if(f<=0.1667f)
468  return Color::RGB(1,f*6,0);
469  if(f<=0.3333f)
470  return Color::RGB(1-(f-0.1667f)*6,1,0);
471  if(f<=0.5000f)
472  return Color::RGB(0,1,(f-0.3333f)*6);
473  if(f<=0.6667f)
474  return Color::RGB(0,1-(f-0.5000f)*6,1);
475  if(f<=0.8333f)
476  return Color::RGB((f-0.6667f)*6,0,1);
477  if(f<=1.0000f)
478  return Color::RGB(1,0,1-(f-0.8333f)*6);
479 
480  return Color::RGB(1,0,0);
481  }
482 
483  virtual Color::RGB getf(float f) const
484  {
485  return HSVColormap::sGetf(f);
486  }
487 };
488 
490 
496 {
498 public:
499 
500  JetColormap(std::size_t size=256) : ContinuousColormap(size) {}
501 
502  static Color::RGB sGetf(float f)
503  {
504  /*
505  0.000 - 0 0 0.5
506  0.125 - 0 0 1.0
507  0.375 - 0 1.0 1.0
508  0.625 - 1.0 1.0 0.0
509  0.875 - 1.0 0.0 0.0
510  1.000 - 0.5 0.0 0.0
511  */
512 
513  if(f<=0.0f)
514  return Color::RGB(0,0,0.5f);
515  if(f<=0.125f)
516  return Color::RGB(0,0,0.5f+f*4.0f);
517  if(f<=0.375f)
518  return Color::RGB(0,(f-0.125f)*4.0f,1.0f);
519  if(f<=0.625f) {
520  float alpha = (f-0.375f)*4.0f;
521  return Color::RGB(alpha,1.0f,1.0f-alpha);
522  }
523  if(f<=0.875f)
524  return Color::RGB(1.0f,1.0f-(f-0.625f)*4.0f,0.0f);
525  if(f<=1.0f)
526  return Color::RGB(1.0f-(f-0.875f)*4,0.0f,0.0f);
527 
528  return Color::RGB(0.5f,0,0);
529  }
530 
531  virtual Color::RGB getf(float f) const
532  {
533  return JetColormap::sGetf(f);
534  }
535 };
536 
538 
545 public:
546 
547  RedBlueColormap(std::size_t size = 256) :
548  ContinuousColormap(size) {
549  }
550 
551  static Color::RGB sGetf(float f)
552  {
553  if (f < 0.0f)
554  return Color::RGB(0.0f, 0.0f, 1.0f);
555  if (f < 1.0f)
556  return Color::RGB(f, 0.0f, 1.0f - f);
557  return Color::RGB(1.0f, 0.0f, 0.0f);
558  }
559 
560  virtual Color::RGB getf(float f) const {
561  return RedBlueColormap::sGetf(f);
562  }
563 };
564 
566 
573 public:
574  Pm3dColormap(std::size_t size=256);
575 };
576 
578 
584 {
586 public:
588 private:
589  const static uint8 colormap[3*10];
590 };
591 
593 
599 {
601 public:
603 private:
604  const static uint8 colormap[3*6];
605 };
606 
608 }
609 
611 
612 #endif
iterator begin()
Gives an iterator to the beginning of the colormap.
Definition: Colormap.h:180
GradientColormap(std::size_t size=256)
Definition: Colormap.h:289
static Color::RGB sGetf(float f)
Definition: Colormap.h:424
base class for discrete color colormaps
Definition: Colormap.h:195
virtual std::size_t size() const
Returns the set size.
Definition: Colormap.h:229
A continuous HSV colormap.
Definition: Colormap.h:446
A continuous Red-Blue colormap.
Definition: Colormap.h:543
GrayscaleColormap(std::size_t size=256)
Definition: Colormap.h:422
iterator & operator--()
go to previous color
Definition: Colormap.h:132
static Color::RGB sGetf(float f)
Definition: Colormap.h:453
const_iterator end() const
Gives an const iterator to the end of the colormap.
Definition: Colormap.h:189
void setColorAt(float position, const Color::RGB &color)
Sets a color key at the given position with the given color.
Definition: Colormap.h:295
Internal base class for linear gradient color maps.
Definition: Colormap.h:251
pointer operator->() const
Dereference to the color, the iterator is pointing to.
Definition: Colormap.h:162
static Color::RGB sGetf(float f)
Definition: Colormap.h:502
iterator const_iterator
Definition: Colormap.h:175
FormulaeColormapBase(std::size_t size)
Definition: Colormap.h:315
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
const_iterator begin() const
Gives a const iterator to the beginning of the colormap.
Definition: Colormap.h:183
const Color::RGB & reference
Definition: Colormap.h:103
Internal base class for formulae based color maps.
Definition: Colormap.h:311
void setColorAt(float position, const Color::RGB &color)
Sets a color key at the given position with the given color.
iterator end()
Gives an iterator to the end of the colormap.
Definition: Colormap.h:186
Base class for continuous color colormaps.
Definition: Colormap.h:217
Base class for tabular color colormaps.
Definition: Colormap.h:385
The default pm3d gnuplot palette (black-blue-red-yellow)
Definition: Colormap.h:571
virtual Color::RGB getf(float f) const
Accesses the continuous colors.
Definition: Colormap.h:560
int difference_type
Definition: Colormap.h:100
A discrete colormap with 6 complementary colors.
Definition: Colormap.h:598
GradientColormapBase(std::size_t size)
Definition: Colormap.h:255
#define MIRA_ABSTRACT_OBJECT(classIdentifier)
Use this MACRO instead of MIRA_OBJECT to declare the class as abstract.
Definition: FactoryMacros.h:235
void clear()
Removes all color keys.
virtual Color::RGB getf(float f) const
Accesses the continuous colors.
Definition: Colormap.h:434
std::bidirectional_iterator_tag iterator_category
Definition: Colormap.h:104
void setFormulae(int rformula, int gformula, int bformula)
Sets a color key at the given position with the given color.
$Header file containing base classes to enable class creation using a class factory$ ...
const Color::RGB * pointer
Definition: Colormap.h:102
ContinuousColormap(std::size_t size=256)
Definition: Colormap.h:221
HSVColormap(std::size_t size=256)
Definition: Colormap.h:451
Color::RGB operator()(std::size_t idx)
Returns the color in the colormap at the given index.
Definition: Colormap.h:82
The object class acts as a generic base class for classes which should be used with the classFactory...
Definition: Object.h:144
Color::RGB value_type
Definition: Colormap.h:99
virtual Color::RGB getf(float f) const
Accesses the continuous colors.
Definition: Colormap.h:483
void resize(std::size_t pSize)
Sets a certain size of this colormap.
Definition: Colormap.h:226
bool operator==(const ImgIteratorBase &a, const ImgIteratorBase &b)
Definition: ImgIterator.h:225
JetColormap(std::size_t size=256)
Definition: Colormap.h:500
bool operator==(const iterator &other)
Definition: Colormap.h:144
RedBlueColormap(std::size_t size=256)
Definition: Colormap.h:547
#define MIRA_OBJECT(classIdentifier)
Use this MACRO if you like the factory to automatically extract the class name from the given identif...
Definition: FactoryMacros.h:179
FormulaeColormap(std::size_t size=256)
Definition: Colormap.h:363
void setColorAt(float position, int r, int g, int b)
Definition: Colormap.h:267
reference operator*() const
Dereference to the color, the iterator is pointing to.
Definition: Colormap.h:155
iterator()
Definition: Colormap.h:112
This file contains color classes for the Img class.
#define MIRA_NO_PUBLIC_DEFAULT_CONSTRUCTOR(CLASS)
Use this macro if your class does not have a public default constructor and should be managed by the ...
Definition: FactoryMacros.h:270
A discrete colormap with 10 complementary colors.
Definition: Colormap.h:583
Color::RGB operator[](std::size_t idx)
Returns the color in the colormap at the given index.
Definition: Colormap.h:87
void clear()
Removes all color keys.
Definition: Colormap.h:300
Complementary10Colormap()
Definition: Colormap.h:587
class MIRA_BASE_EXPORT RGB
Definition: Color.h:65
A continuous Jet colormap.
Definition: Colormap.h:495
virtual std::size_t size() const
Returns the number of elements in the colormap.
Definition: Colormap.h:398
iterator & operator++()
advance the iterator
Definition: Colormap.h:119
Complementary6Colormap()
Definition: Colormap.h:602
bool operator!=(const iterator &other)
Definition: Colormap.h:148
TabularColormap(const uint8 *pData, std::size_t pSize)
Creates a new TabularColormap using the specified color table that is specified as first parameter...
Definition: Colormap.h:395
A continuous grayscale colormap.
Definition: Colormap.h:417
The different color spaces.
Definition: Color.h:104
static Color::RGB sGetf(float f)
Definition: Colormap.h:551
iterator(const iterator &other)
Definition: Colormap.h:113
A class for creating formula based color maps.
Definition: Colormap.h:359
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
Iterator that can iterate over the whole color colormap similar to STL iterators on containers...
Definition: Colormap.h:96
iterator operator++(int)
advance the iterator
Definition: Colormap.h:125
Base class for color colormaps.
Definition: Colormap.h:62
iterator(const Colormap *colormap, std::size_t position)
Definition: Colormap.h:108
A class for creating continuous color maps based on linear gradients between predefined points...
Definition: Colormap.h:285
FormulaeColormap(int rformula, int gformula, int bformula, std::size_t size=256)
Definition: Colormap.h:365
void setFormulae(int rformula, int gformula, int bformula)
Sets the formulae for the color components.
Definition: Colormap.h:372
virtual Color::RGB getf(float f) const
Accesses the continuous colors.
Definition: Colormap.h:531
iterator operator--(int)
go to previous color
Definition: Colormap.h:138