Image Component Library (ICL)
PixelRef.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLCore/src/ICLCore/PixelRef.h **
10 ** Module : ICLCore **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLCore/Types.h>
35 #include <ICLUtils/SmartArray.h>
36 #include <ICLUtils/Exception.h>
37 #include <ICLUtils/Macros.h>
38 #include <ICLMath/FixedMatrix.h>
39 #include <ICLUtils/ClippedCast.h>
40 
41 namespace icl{
42  namespace core{
44 
59  template<class T>
60  class PixelRef{
61 
63  std::vector<T*> m_data;
64 
65  public:
66 
68  inline PixelRef(){}
69 
71  inline bool isNull() const { return !m_data->size(); }
72 
74 
75  inline PixelRef(int x, int y, int width, std::vector<utils::SmartArray<T> > &data):
76  m_data(data.size()){
77  int offs = x+width*y;
78  for(unsigned int i=0;i<data.size();++i){
79  this->m_data[i] = data[i].get()+offs;
80  }
81  }
82 
84  inline PixelRef(const PixelRef &other):m_data(other.m_data){}
85 
87 
92  inline PixelRef &operator=(const PixelRef &other){
93  ICLASSERT_THROW(other.m_data.size() == m_data.size(),utils::ICLException("incompatible channel count"));
94  for(unsigned int i=0;i<m_data.size();++i){
95  *m_data[i] = *other.m_data[i];
96  }
97  return *this;
98  }
99 
101  inline PixelRef &operator=(const std::vector<T> &vec){
102  ICLASSERT_THROW(vec.size() == m_data.size(),utils::ICLException("incompatible channel count"));
103  for(unsigned int i=0;i<m_data.size();++i){
104  *m_data[i] = vec[i];
105  }
106  return *this;
107  }
108 
110 
118  template<class MT,unsigned int COLS,unsigned int ROWS>
120  ICLASSERT_THROW((m_data.size() == math::FixedMatrix<MT,COLS,ROWS>::DIM), utils::ICLException("channel count and matrix dim are incompatible"));
121  for(unsigned int i=0;i<m_data.size();++i){
122  *m_data[i] = utils::clipped_cast<MT,T>(mat[i]);
123  }
124  return *this;
125  }
126 
128  inline std::vector<T> asVec() const{
129  std::vector<T> v(m_data.size());
130  for(unsigned int i=0;i<m_data.size();++i){
131  v[i] = *m_data[i];
132  }
133  return v;
134  }
135 
137  inline void set(const T &v0) { *m_data[0] = v0; }
138 
140  inline void set(const T &v0, const T&v1) { set(v0); *m_data[1] = v1; }
141 
143  inline void set(const T &v0, const T&v1, const T&v2) { set(v0,v1); *m_data[2] = v2; }
144 
146  inline void set(const T &v0, const T&v1, const T&v2, const T &v3) { set(v0,v1,v2); *m_data[3] = v3; }
147 
149 
150  template<class ForwardIterator>
151  inline void setFromRange(ForwardIterator begin, ForwardIterator end) {
152  for(unsigned int i=0;i<m_data.size();++i,++begin){
153  if(begin == end) throw utils::ICLException("Range is longer then channel count");
154  *m_data[i] = *begin;
155  }
156  }
157 
159  T &operator[](unsigned int channel) {
160  ICLASSERT_THROW(channel < m_data.size(),utils::ICLException("invalid channel index"));
161  return *m_data[channel];
162  }
163 
165  const T &operator[](unsigned int channel) const {
166  ICLASSERT_THROW(channel < m_data.size(),utils::ICLException("invalid channel index"));
167  return *m_data[channel];
168  }
169 
171  int getChannels() const {
172  return (int)m_data.size();
173  }
174  };
175  } // namespace core
176 }
void set(const T &v0, const T &v1, const T &v2)
sets up the first three indices (unsafe)
Definition: PixelRef.h:143
Pixel-Type class for copying image pixles to image pixels.
Definition: PixelRef.h:60
Powerful and highly flexible matrix class implementation.
Definition: FixedMatrix.h:172
undocument this line if you encounter any issues!
Definition: Any.h:37
void setFromRange(ForwardIterator begin, ForwardIterator end)
assigns a ranges contents to the pixel data
Definition: PixelRef.h:151
std::vector< T * > m_data
Internal data.
Definition: PixelRef.h:63
bool isNull() const
returs whether this instance is null (created with the empty constructor)
Definition: PixelRef.h:71
const T & operator[](unsigned int channel) const
references a single element (const) (safe)
Definition: PixelRef.h:165
PixelRef & operator=(const PixelRef &other)
assignment operator which copies the values (most common)
Definition: PixelRef.h:92
ICLQt_API ImgROI data(ImgQ &r)
creates full ROI ROI-struct
PixelRef()
Empty constructor, create a null pixel ref with 0 length.
Definition: PixelRef.h:68
T & operator[](unsigned int channel)
references a single element (safe)
Definition: PixelRef.h:159
Specialization of the SmartPtrBase class for Arrays.
Definition: SmartArray.h:46
void set(const T &v0)
sets up the first index (unsafe)
Definition: PixelRef.h:137
void set(const T &v0, const T &v1)
sets up the first two indices (unsafe)
Definition: PixelRef.h:140
Base class for Exception handling in the ICL.
Definition: Exception.h:42
PixelRef & operator=(const std::vector< T > &vec)
assigns reference pixel values from vector data
Definition: PixelRef.h:101
PixelRef(int x, int y, int width, std::vector< utils::SmartArray< T > > &data)
single constructor to create a pixelref instance
Definition: PixelRef.h:75
ICLQt_API ImgQ channel(const ImgQ &image, int channel)
picks a specific image channel
int getChannels() const
returns the channel count
Definition: PixelRef.h:171
void set(const T &v0, const T &v1, const T &v2, const T &v3)
sets up the first four indices (unsafe)
Definition: PixelRef.h:146
PixelRef & operator=(const math::FixedMatrix< MT, COLS, ROWS > &mat)
assigns reference pixel values from FixedMatrix data
Definition: PixelRef.h:119
#define ICLASSERT_THROW(X, OBJ)
Definition: Macros.h:155
std::vector< T > asVec() const
copies image data into a std::vector
Definition: PixelRef.h:128
PixelRef(const PixelRef &other)
PixelRef copy constructor (copies the reference, not the values)
Definition: PixelRef.h:84