Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DataSegmentBase.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   : ICLGeom/src/ICLCore/DataSegmentBase.h                  **
00010 ** Module : ICLGeom                                                **
00011 ** Authors: Christof Elbrechter, Patrick Nobou                     **
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 <ICLUtils/BasicTypes.h>
00035 #include <ICLUtils/Exception.h>
00036 #include <algorithm>
00037 
00038 namespace icl{
00039   //forward declare PointCloudObjectBase to friend later
00040   namespace geom {
00041   class PointCloudObjectBase;
00042   }
00043   namespace core{
00044     
00046     template<class T,int N> struct DataSegment;
00049 
00050 
00099     struct ICLGeom_API DataSegmentBase{
00101       friend class geom::PointCloudObjectBase;
00102 
00103     protected:
00105       static inline int getSizeOf(core::depth d){
00106         static const int lens[] = { 1, 2, 4, 4, 8 };
00107         return lens[(int)d];
00108       }
00109   
00111       icl8u *data;
00112       
00114 
00120       size_t stride;   
00121       
00123       size_t numElements;
00124       
00126       icl32s organizedWidth; // -1 if unorganized 
00127   
00129       core::depth dataDepth;
00130       
00132       size_t elemDim;     // number of elements (in Type units)
00133   
00134     public:
00136       inline bool isOrganized() const {
00137         return organizedWidth > 0;
00138       }
00139       
00141       inline utils::Size getSize() const {
00142         if(!isOrganized()) return utils::Size::null;
00143         else return utils::Size(organizedWidth, numElements/organizedWidth);
00144       }
00145       
00147       inline int getStride() const { 
00148         return stride;
00149       }
00150       
00152 
00153       inline int getDim() const{
00154         return numElements;
00155       }    
00156       
00158       inline core::depth getDepth() const{
00159         return dataDepth;
00160       }
00161       
00163       inline int getElemDim() const{
00164         return elemDim;
00165       }
00166       
00168 
00169       icl8u *getDataPointer() { return data; }
00170 
00172 
00173       const icl8u *getDataPointer() const { return data; }
00174   
00176       inline DataSegmentBase(void *data=0, size_t stride=0, 
00177                              size_t numElements=0, icl32s organizedWidth=-1, 
00178                              core::depth dataDepth=core::depth8u, size_t elemDim=0):
00179         data((icl8u*)data),stride(stride),numElements(numElements),
00180         organizedWidth(organizedWidth),dataDepth(dataDepth),elemDim(elemDim){}
00181       
00183 
00190       template<class T, int N>
00191       const DataSegment<T,N> &as() const;
00192       
00194       class ICLGeom_API Bytes{
00195         icl8u *data; 
00196         int len;     
00197 
00198         Bytes(icl8u *data=0, int len=0):data(data),len(len){}
00200         Bytes(const Bytes &other){} 
00201         friend struct DataSegmentBase; 
00202         public:
00203         
00205         inline int getDim() const{ return len; }
00206         
00208         icl8u &operator[](int idx) { return data[idx]; }
00209   
00211         const icl8u operator[](int idx) const { return data[idx]; }
00212         
00214 
00215         inline Bytes &operator=(const Bytes &other) throw (utils::ICLException){
00216           if(len != other.len){
00217             throw utils::ICLException("unable to assign DataSegmentBase::Bytes: lengths differ!");
00218           }
00219           std::copy(other.data,other.data+len,data);
00220           return *this;
00221         }
00222         
00224         icl8u *begin() { return data; }
00225   
00227         icl8u *end() { return data+len; }
00228   
00230         const icl8u *begin() const { return data; }
00231   
00233         const icl8u *end() const { return data+len; }
00234   
00235       };
00236       
00238       inline Bytes operator[](int idx) {
00239         return Bytes(data+idx*stride*getSizeOf(dataDepth), elemDim*getSizeOf(dataDepth));
00240       }
00241   
00243       inline const Bytes operator[](int idx) const{
00244         return const_cast<DataSegmentBase*>(this)->operator[](idx); 
00245       }
00246   
00248       inline Bytes operator()(int x, int y) {
00249        return operator[](x + organizedWidth * y );
00250       }
00251       
00253       inline const Bytes operator()(int x, int y) const{
00254         return operator[](x + organizedWidth * y );
00255       }
00256     };
00257   
00258   } // namespace core
00259 }
00260 
00261 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines