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.GPL  **
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 <ICLCore/Types.h>
00034 #include <ICLUtils/SmartArray.h>
00035 #include <ICLUtils/Exception.h>
00036 #include <ICLUtils/Macros.h>
00037 #include <ICLMath/FixedMatrix.h>
00038 #include <ICLUtils/ClippedCast.h>
00039 
00040 namespace icl{
00041   namespace core{
00043 
00058     template<class T>
00059     class PixelRef{
00060       
00062       std::vector<T*> m_data;
00063       
00064       public:
00065       
00067       inline PixelRef(){}
00068       
00070       inline bool isNull() const { return !m_data->size(); }
00071   
00073 
00074       inline PixelRef(int x, int y, int width, std::vector<utils::SmartArray<T> > &data):
00075       m_data(data.size()){
00076         int offs = x+width*y;
00077         for(unsigned int i=0;i<data.size();++i){
00078           this->m_data[i] = data[i].get()+offs;
00079         }
00080       }
00081       
00083       inline PixelRef(const PixelRef &other):m_data(other.m_data){}
00084   
00086 
00091       inline PixelRef &operator=(const PixelRef &other)throw (utils::ICLException){
00092         ICLASSERT_THROW(other.m_data.size() == m_data.size(),utils::ICLException("incompatible channel count"));
00093         for(unsigned int i=0;i<m_data.size();++i){
00094           *m_data[i] = *other.m_data[i];
00095         }
00096         return *this;
00097       }
00098       
00100       inline PixelRef &operator=(const std::vector<T> &vec)throw (utils::ICLException){
00101         ICLASSERT_THROW(vec.size() == m_data.size(),utils::ICLException("incompatible channel count"));
00102         for(unsigned int i=0;i<m_data.size();++i){
00103           *m_data[i] = vec[i];
00104         }
00105         return *this;
00106       }
00107   
00109 
00117       template<class MT,unsigned int COLS,unsigned int ROWS>
00118       inline PixelRef &operator=(const math::FixedMatrix<MT,COLS,ROWS> &mat)throw (utils::ICLException){
00119         ICLASSERT_THROW((m_data.size() == math::FixedMatrix<MT,COLS,ROWS>::DIM), utils::ICLException("channel count and matrix dim are incompatible"));
00120         for(unsigned int i=0;i<m_data.size();++i){
00121           *m_data[i] = utils::clipped_cast<MT,T>(mat[i]);
00122         }
00123         return *this;
00124       }
00125       
00127       inline std::vector<T> asVec() const{
00128         std::vector<T> v(m_data.size());
00129         for(unsigned int i=0;i<m_data.size();++i){
00130           v[i] = *m_data[i];
00131         }
00132         return v;
00133       }
00134       
00136       inline void set(const T &v0) { *m_data[0] = v0; }
00137   
00139       inline void set(const T &v0, const T&v1) { set(v0); *m_data[1] = v1; }
00140   
00142       inline void set(const T &v0, const T&v1, const T&v2) { set(v0,v1); *m_data[2] = v2; }
00143   
00145       inline void set(const T &v0, const T&v1, const T&v2, const T &v3) { set(v0,v1,v2); *m_data[3] = v3; }
00146       
00148 
00149       template<class ForwardIterator>
00150       inline void setFromRange(ForwardIterator begin, ForwardIterator end) throw (utils::ICLException){
00151         for(unsigned int i=0;i<m_data.size();++i,++begin){
00152           if(begin == end) throw utils::ICLException("Range is longer then channel count");
00153           *m_data[i] = *begin;
00154         }
00155       }
00156       
00158       T &operator[](unsigned int channel) throw (utils::ICLException){
00159         ICLASSERT_THROW(channel < m_data.size(),utils::ICLException("invalid channel index"));
00160         return *m_data[channel];
00161       }
00162       
00164       const T &operator[](unsigned int channel) const throw (utils::ICLException){
00165         ICLASSERT_THROW(channel < m_data.size(),utils::ICLException("invalid channel index"));
00166         return *m_data[channel];
00167       }
00168       
00170       int getChannels() const {
00171         return (int)m_data.size();
00172       }
00173     };
00174   } // namespace core
00175 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines