cgv
color_storage.h
1 #pragma once
2 
3 #include <vector>
4 #include "color.h"
5 
6 #include "lib_begin.h"
7 
8 namespace cgv {
9  namespace media {
11  enum ColorType {
12  CT_RGB8,
13  CT_RGBA8,
14  CT_RGB,
15  CT_RGBA
16  };
17 
19  template <typename T> struct color_storage_traits {};
20  template <> struct color_storage_traits<color<float, RGB> > { static const ColorType color_type = CT_RGB; };
21  template <> struct color_storage_traits<color<float, RGB, OPACITY> > { static const ColorType color_type = CT_RGBA; };
22  template <> struct color_storage_traits<color<unsigned char, RGB> > { static const ColorType color_type = CT_RGB8; };
23  template <> struct color_storage_traits<color<unsigned char, RGB, OPACITY> > { static const ColorType color_type = CT_RGBA8; };
24 
27  {
28  public:
34  };
35 
38  {
39  protected:
40  // type of color stored in storage
41  ColorType color_type;
42  public:
44  abst_color_storage(ColorType _color_type);
46  virtual ~abst_color_storage();
48  ColorType get_color_type() const;
50  size_t get_color_size() const;
52  virtual abst_color_storage* clone() const = 0;
54  virtual size_t get_nr_colors() const = 0;
56  virtual void resize(size_t nr_colors) = 0;
58  virtual const void* get_data_ptr() const = 0;
60  virtual const void* get_data_vector_ptr() const = 0;
62  virtual void set_color(size_t i, const void* col_ptr) = 0;
64  virtual void set_color(size_t i, const rgb& col) = 0;
66  virtual void set_color(size_t i, const rgba& col) = 0;
68  virtual void set_color(size_t i, const rgb8& col) = 0;
70  virtual void set_color(size_t i, const rgba8& col) = 0;
72  virtual void put_color(size_t i, void* col_ptr) const = 0;
74  virtual void put_color(size_t i, rgb& col) const = 0;
76  virtual void put_color(size_t i, rgba& col) const = 0;
78  virtual void put_color(size_t i, rgb8& col) const = 0;
80  virtual void put_color(size_t i, rgba8& col) const = 0;
81  };
82 
84  template <typename C>
86  {
87  public:
88  std::vector<C> colors;
93  colors(csm.colors) { }
95  template <typename C1>
97  for (const auto& col : csm.colors)
98  colors.push_back(col);
99  }
102  switch (acsm.get_color_type()) {
103  case CT_RGB: new (this) color_storage<C>(static_cast<const color_storage<rgb>&>(acsm)); break;
104  case CT_RGBA: new (this) color_storage<C>(static_cast<const color_storage<rgba>&>(acsm)); break;
105  case CT_RGB8: new (this) color_storage<C>(static_cast<const color_storage<rgb8>&>(acsm)); break;
106  case CT_RGBA8: new (this) color_storage<C>(static_cast<const color_storage<rgba8>&>(acsm)); break;
107  }
108  }
111  return new color_storage(*this);
112  }
113  // implementation of vector access passes interface to std::vector class
114  size_t get_nr_colors() const { return colors.size(); }
115  void resize(size_t nr_colors) { colors.resize(nr_colors); }
116  const void* get_data_ptr() const { return &colors.front(); }
117  const void* get_data_vector_ptr() const { return &colors; }
118  // implementation of color access uses type conversion operators implemented for color class
119  void set_color(size_t i, const void* col_ptr) { colors[i] = *reinterpret_cast<const C*>(col_ptr); }
120  void set_color(size_t i, const rgb& col) { colors[i] = col; }
121  void set_color(size_t i, const rgba& col) { colors[i] = col; }
122  void set_color(size_t i, const rgb8& col) { colors[i] = col; }
123  void set_color(size_t i, const rgba8& col) { colors[i] = col; }
124  void put_color(size_t i, void* col_ptr) const { *reinterpret_cast<C*>(col_ptr) = colors[i]; }
125  void put_color(size_t i, rgb& col) const { col = colors[i]; }
126  void put_color(size_t i, rgba& col) const { col = colors[i]; }
127  void put_color(size_t i, rgb8& col) const { col = colors[i]; }
128  void put_color(size_t i, rgba8& col) const { col = colors[i]; }
129  };
130  }
131 }
132 
133 #include <cgv/config/lib_end.h>
cgv::media::color_storage::set_color
void set_color(size_t i, const rgba &col)
set i-th color from color of type rgba
Definition: color_storage.h:121
cgv::media::abst_color_storage::set_color
virtual void set_color(size_t i, const rgba &col)=0
set i-th color from color of type rgba
cgv::media::color_storage::put_color
void put_color(size_t i, void *col_ptr) const
set color of type stored in storage to i-th color
Definition: color_storage.h:124
cgv::media::color_storage::color_storage
color_storage(const color_storage< C > &csm)
construct from color storage of same type
Definition: color_storage.h:92
cgv::media::color_storage::get_data_vector_ptr
const void * get_data_vector_ptr() const
return a void pointer to the color data vector
Definition: color_storage.h:117
cgv::media::abst_color_storage::set_color
virtual void set_color(size_t i, const rgb8 &col)=0
set i-th color from color of type rgb8
cgv::media::abst_color_storage::get_color_type
ColorType get_color_type() const
return color type of color storage
Definition: color_storage.cxx:15
cgv::media::color_storage_types::rgb
color< float, RGB > rgb
define supported color types
Definition: color_storage.h:30
cgv::media::color_storage_traits
traits struct used to derive color type (need to be declared on namespace level for specialization)
Definition: color_storage.h:19
cgv::media::abst_color_storage::get_nr_colors
virtual size_t get_nr_colors() const =0
return number colors stored in color storage
cgv::media::color_storage::clone
abst_color_storage * clone() const
clone the color storage
Definition: color_storage.h:110
cgv::media::color_storage::put_color
void put_color(size_t i, rgb8 &col) const
set color of type rgb8 to i-th color
Definition: color_storage.h:127
cgv::media::color_storage::put_color
void put_color(size_t i, rgba8 &col) const
set color of type rgba8 to i-th color
Definition: color_storage.h:128
cgv::media::abst_color_storage::set_color
virtual void set_color(size_t i, const rgba8 &col)=0
set i-th color from color of type rgba8
cgv::media::abst_color_storage::set_color
virtual void set_color(size_t i, const void *col_ptr)=0
set i-th color to color of type stored in storage
cgv::media::color_storage::set_color
void set_color(size_t i, const void *col_ptr)
set i-th color to color of type stored in storage
Definition: color_storage.h:119
cgv::media::abst_color_storage::put_color
virtual void put_color(size_t i, rgba8 &col) const =0
set color of type rgba8 to i-th color
cgv::media::abst_color_storage::get_color_size
size_t get_color_size() const
return size of a single color in byte
Definition: color_storage.cxx:21
cgv::media::abst_color_storage::abst_color_storage
abst_color_storage(ColorType _color_type)
construct from color type enumerate
Definition: color_storage.cxx:6
cgv::media::color
Definition: color.h:51
cgv::media::abst_color_storage::put_color
virtual void put_color(size_t i, rgba &col) const =0
set color of type rgba to i-th color
cgv::media::abst_color_storage::get_data_vector_ptr
virtual const void * get_data_vector_ptr() const =0
return a void pointer to the color data vector
cgv::media::abst_color_storage::get_data_ptr
virtual const void * get_data_ptr() const =0
return a void pointer to the color data
cgv::media::abst_color_storage::put_color
virtual void put_color(size_t i, void *col_ptr) const =0
set color of type stored in storage to i-th color
cgv::media::color_storage::get_data_ptr
const void * get_data_ptr() const
return a void pointer to the color data
Definition: color_storage.h:116
cgv::media::abst_color_storage::clone
virtual abst_color_storage * clone() const =0
clone the color storage
cgv::media::color_storage::put_color
void put_color(size_t i, rgba &col) const
set color of type rgba to i-th color
Definition: color_storage.h:126
cgv::media::abst_color_storage::~abst_color_storage
virtual ~abst_color_storage()
virtual destructor ensures that std::vector defined in template class color_storage is destructed too
Definition: color_storage.cxx:11
cgv::media::color_storage_types
declaration of types for color storage
Definition: color_storage.h:27
cgv::media::abst_color_storage::set_color
virtual void set_color(size_t i, const rgb &col)=0
set i-th color from color of type rgb
cgv::media::color_storage::get_nr_colors
size_t get_nr_colors() const
return number colors stored in color storage
Definition: color_storage.h:114
cgv::media::color_storage::color_storage
color_storage(const color_storage< C1 > &csm)
construct from color storage of different type
Definition: color_storage.h:96
cgv::media::abst_color_storage
interface for color storage of different internal types
Definition: color_storage.h:38
cgv::media::color_storage::set_color
void set_color(size_t i, const rgb8 &col)
set i-th color from color of type rgb8
Definition: color_storage.h:122
cgv::media::color_storage::color_storage
color_storage()
construct empty color storage
Definition: color_storage.h:90
cgv::media::color_storage::color_storage
color_storage(const abst_color_storage &acsm)
construct from abstract color storage of unknown type
Definition: color_storage.h:101
cgv::media::abst_color_storage::put_color
virtual void put_color(size_t i, rgb8 &col) const =0
set color of type rgb8 to i-th color
cgv::media::abst_color_storage::put_color
virtual void put_color(size_t i, rgb &col) const =0
set color of type rgb to i-th color
cgv::media::color_storage
template implementation of color storage model
Definition: color_storage.h:86
cgv::media::color_storage::set_color
void set_color(size_t i, const rgb &col)
set i-th color from color of type rgb
Definition: color_storage.h:120
cgv::media::color_storage::resize
void resize(size_t nr_colors)
resize to the given number of colors
Definition: color_storage.h:115
cgv::media::color_storage::set_color
void set_color(size_t i, const rgba8 &col)
set i-th color from color of type rgba8
Definition: color_storage.h:123
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::media::abst_color_storage::resize
virtual void resize(size_t nr_colors)=0
resize to the given number of colors
cgv::media::color_storage::put_color
void put_color(size_t i, rgb &col) const
set color of type rgb to i-th color
Definition: color_storage.h:125