Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
PixelRef.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLCore/src/ICLCore/PixelRef.h                         **
00010 ** Module : ICLCore                                                **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/CompatMacros.h>
00034 #include <ICLCore/Types.h>
00035 #include <ICLUtils/SmartArray.h>
00036 #include <ICLUtils/Exception.h>
00037 #include <ICLUtils/Macros.h>
00038 #include <ICLMath/FixedMatrix.h>
00039 #include <ICLUtils/ClippedCast.h>
00040 
00041 namespace icl{
00042   namespace core{
00044 
00059     template<class T>
00060     class PixelRef{
00061       
00063       std::vector<T*> m_data;
00064       
00065       public:
00066       
00068       inline PixelRef(){}
00069       
00071       inline bool isNull() const { return !m_data->size(); }
00072   
00074 
00075       inline PixelRef(int x, int y, int width, std::vector<utils::SmartArray<T> > &data):
00076       m_data(data.size()){
00077         int offs = x+width*y;
00078         for(unsigned int i=0;i<data.size();++i){
00079           this->m_data[i] = data[i].get()+offs;
00080         }
00081       }
00082       
00084       inline PixelRef(const PixelRef &other):m_data(other.m_data){}
00085   
00087 
00092       inline PixelRef &operator=(const PixelRef &other)throw (utils::ICLException){
00093         ICLASSERT_THROW(other.m_data.size() == m_data.size(),utils::ICLException("incompatible channel count"));
00094         for(unsigned int i=0;i<m_data.size();++i){
00095           *m_data[i] = *other.m_data[i];
00096         }
00097         return *this;
00098       }
00099       
00101       inline PixelRef &operator=(const std::vector<T> &vec)throw (utils::ICLException){
00102         ICLASSERT_THROW(vec.size() == m_data.size(),utils::ICLException("incompatible channel count"));
00103         for(unsigned int i=0;i<m_data.size();++i){
00104           *m_data[i] = vec[i];
00105         }
00106         return *this;
00107       }
00108   
00110 
00118       template<class MT,unsigned int COLS,unsigned int ROWS>
00119       inline PixelRef &operator=(const math::FixedMatrix<MT,COLS,ROWS> &mat)throw (utils::ICLException){
00120         ICLASSERT_THROW((m_data.size() == math::FixedMatrix<MT,COLS,ROWS>::DIM), utils::ICLException("channel count and matrix dim are incompatible"));
00121         for(unsigned int i=0;i<m_data.size();++i){
00122           *m_data[i] = utils::clipped_cast<MT,T>(mat[i]);
00123         }
00124         return *this;
00125       }
00126       
00128       inline std::vector<T> asVec() const{
00129         std::vector<T> v(m_data.size());
00130         for(unsigned int i=0;i<m_data.size();++i){
00131           v[i] = *m_data[i];
00132         }
00133         return v;
00134       }
00135       
00137       inline void set(const T &v0) { *m_data[0] = v0; }
00138   
00140       inline void set(const T &v0, const T&v1) { set(v0); *m_data[1] = v1; }
00141   
00143       inline void set(const T &v0, const T&v1, const T&v2) { set(v0,v1); *m_data[2] = v2; }
00144   
00146       inline void set(const T &v0, const T&v1, const T&v2, const T &v3) { set(v0,v1,v2); *m_data[3] = v3; }
00147       
00149 
00150       template<class ForwardIterator>
00151       inline void setFromRange(ForwardIterator begin, ForwardIterator end) throw (utils::ICLException){
00152         for(unsigned int i=0;i<m_data.size();++i,++begin){
00153           if(begin == end) throw utils::ICLException("Range is longer then channel count");
00154           *m_data[i] = *begin;
00155         }
00156       }
00157       
00159       T &operator[](unsigned int channel) throw (utils::ICLException){
00160         ICLASSERT_THROW(channel < m_data.size(),utils::ICLException("invalid channel index"));
00161         return *m_data[channel];
00162       }
00163       
00165       const T &operator[](unsigned int channel) const throw (utils::ICLException){
00166         ICLASSERT_THROW(channel < m_data.size(),utils::ICLException("invalid channel index"));
00167         return *m_data[channel];
00168       }
00169       
00171       int getChannels() const {
00172         return (int)m_data.size();
00173       }
00174     };
00175   } // namespace core
00176 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines