Dirac - A Video Codec

Created by the British Broadcasting Corporation.


arrays.h

Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002 *
00003 * $Id: arrays.h,v 1.20 2007/09/03 11:30:29 asuraparaju Exp $ $Name: Dirac_0_9_1 $
00004 *
00005 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006 *
00007 * The contents of this file are subject to the Mozilla Public License
00008 * Version 1.1 (the "License"); you may not use this file except in compliance
00009 * with the License. You may obtain a copy of the License at
00010 * http://www.mozilla.org/MPL/
00011 *
00012 * Software distributed under the License is distributed on an "AS IS" basis,
00013 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
00014 * the specific language governing rights and limitations under the License.
00015 *
00016 * The Original Code is BBC Research and Development code.
00017 *
00018 * The Initial Developer of the Original Code is the British Broadcasting
00019 * Corporation.
00020 * Portions created by the Initial Developer are Copyright (C) 2004.
00021 * All Rights Reserved.
00022 *
00023 * Contributor(s): Thomas Davies (Original Author), 
00024 *                 Peter Meerwald (pmeerw@users.sourceforge.net)
00025 *                 Mike Ferenduros (mike_ferenzduros@users.sourceforge.net)
00026 *                 Anuradha Suraparaju
00027 *
00028 * Alternatively, the contents of this file may be used under the terms of
00029 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
00030 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
00031 * the GPL or the LGPL are applicable instead of those above. If you wish to
00032 * allow use of your version of this file only under the terms of the either
00033 * the GPL or LGPL and not to allow others to use your version of this file
00034 * under the MPL, indicate your decision by deleting the provisions above
00035 * and replace them with the notice and other provisions required by the GPL
00036 * or LGPL. If you do not delete the provisions above, a recipient may use
00037 * your version of this file under the terms of any one of the MPL, the GPL
00038 * or the LGPL.
00039 * ***** END LICENSE BLOCK ***** */
00040 
00041 #ifndef _ARRAYS_H_
00042 #define _ARRAYS_H_
00043 
00044 //basic array types used for pictures etc
00045 
00046 #include <memory>
00047 #include <cstddef>
00048 #include <stdexcept>
00049 #include <iostream>
00050 #include <algorithm>
00051 
00052 namespace dirac
00053 {
00055 
00059     class Range
00060     {
00061     public:
00063 
00066         Range(int s, int e): m_fst(s), m_lst(e){}
00067 
00069         const int First() const {return m_fst;}
00070 
00072         const int Last() const {return m_lst;}
00073 
00074     private:
00075         int m_fst ,m_lst;
00076     };
00077 
00079     //One-Dimensional Array type//
00081 
00083 
00088     template <class T> class OneDArray
00089     {
00090     public:
00092 
00095         OneDArray();
00096 
00098 
00101         OneDArray(const int len);
00102 
00104 
00109         OneDArray(const Range& r);
00110 
00112 
00115         ~OneDArray()
00116         {
00117             FreePtr();
00118         }
00119 
00121 
00124         OneDArray(const OneDArray<T>& cpy);
00125 
00127 
00130         OneDArray<T>& operator=(const OneDArray<T>& rhs);    
00131 
00133         void Resize(int l);
00134 
00136         T& operator[](const int pos){return m_ptr[pos-m_first];}
00137 
00139         const T& operator[](const int pos) const {return m_ptr[pos-m_first];}
00140 
00142         int Length() const {return m_length;}
00143 
00145         int First() const {return m_first;}
00146 
00148         int Last() const {return m_last;}
00149 
00150     private:
00151         void Init(const int len);
00152 
00153         void Init(const Range& r);
00154 
00155         void FreePtr();    
00156 
00157         int m_first, m_last;
00158         int m_length;
00159         T* m_ptr;
00160     };
00161 
00162     //public member functions//
00164 
00165     template <class T>
00166     OneDArray<T>::OneDArray()
00167     {
00168         Init(0);
00169     }
00170 
00171     template <class T>
00172     OneDArray<T>::OneDArray(const int len)
00173     {
00174         Init(len);
00175     }
00176 
00177     template <class T>
00178     OneDArray<T>::OneDArray(const Range& r)
00179     {
00180         Init(r);
00181     }
00182 
00183     template <class T>
00184     OneDArray<T>::OneDArray(const OneDArray<T>& cpy)
00185     {
00186         m_first = cpy.m_first;
00187         m_last = cpy.m_last;
00188         m_length = m_last - m_first + 1;
00189 
00190         if (m_first==0)
00191             Init(m_length);
00192         else
00193             Init(Range(m_first , m_last));
00194 
00195         memcpy( m_ptr , cpy.m_ptr , m_length * sizeof( T ) );
00196     }
00197 
00198     template <class T>
00199     OneDArray<T>& OneDArray<T>::operator=(const OneDArray<T>& rhs)
00200     {
00201         if (&rhs != this)
00202         {
00203             FreePtr();
00204             m_first = rhs.m_first;
00205             m_last = rhs.m_last;
00206             m_length = rhs.m_length;
00207 
00208             if (m_first == 0)
00209                 Init(m_length);
00210             else
00211                 Init(Range(m_first , m_last));
00212 
00213             memcpy( m_ptr , rhs.m_ptr , m_length * sizeof( T ) );
00214 
00215         }
00216         return *this;
00217     }
00218 
00219     template <class T> 
00220     void OneDArray<T>::Resize(int l)
00221     {
00222         if (l != m_length)
00223         {
00224             FreePtr();
00225             Init(l);
00226         }
00227     }
00228 
00229     //private member functions//
00231 
00232     template <class T>
00233     void OneDArray<T>::Init(const int len)
00234     {
00235         Range r(0 , len-1);
00236 
00237         Init(r);
00238 
00239     }        
00240 
00241     template <class T>
00242     void OneDArray<T>::Init(const Range& r)
00243     {
00244 
00245         m_first = r.First();
00246         m_last = r.Last();
00247         m_length = m_last - m_first + 1; 
00248 
00249         if ( m_length>0 ) 
00250         {
00251             m_ptr = new T[ m_length ];
00252         }
00253         else 
00254         {
00255             m_length = 0;
00256             m_first = 0;
00257             m_last = -1;
00258             m_ptr = NULL;
00259         }
00260     }
00261 
00262     template <class T>
00263     void OneDArray<T>::FreePtr()
00264     {
00265         if ( m_length>0 )
00266             delete[] m_ptr;
00267     }
00268 
00269 
00271     //Two-Dimensional Array type//
00273 
00275 
00283     template <class T> class TwoDArray
00284     {
00285         typedef T* element_type;
00286 
00287     public:
00288 
00290 
00293         TwoDArray(){ Init(0,0); }
00294 
00296 
00299         TwoDArray( const int height , const int width ){Init(height , width);}
00300 
00302 
00306         TwoDArray( const int height , const int width , T val);
00307 
00309 
00312         virtual ~TwoDArray(){
00313             FreeData();    
00314         }
00315 
00317 
00320         TwoDArray(const TwoDArray<T>& Cpy);
00321 
00323 
00326         TwoDArray<T>& operator=(const TwoDArray<T>& rhs);
00327 
00329 
00335         bool CopyContents(TwoDArray<T>& out) const;
00336 
00338 
00341         void Fill(T val);
00342 
00344         void Resize(const int height, const int width);    
00345 
00347 
00351         inline element_type& operator[](const int pos){return m_array_of_rows[pos];}
00352 
00354 
00358         inline const element_type& operator[](const int pos) const {return m_array_of_rows[pos];}
00359 
00361         const int LengthX() const { return m_length_x; }
00362 
00364         const int LengthY() const { return m_length_y; }
00365 
00367         const int FirstX() const { return m_first_x; } 
00368 
00370         const int FirstY() const { return m_first_y; } 
00371 
00373         const int LastX() const { return m_last_x; } 
00374 
00376         const int LastY() const { return m_last_y; }
00377 
00378     private:
00380         void Init(const int height,const int width);
00381 
00383         void FreeData();    
00384 
00385         int m_first_x;
00386         int m_first_y;
00387 
00388         int m_last_x;
00389         int m_last_y;
00390 
00391         int m_length_x;
00392         int m_length_y;
00393 
00394         element_type* m_array_of_rows;
00395     };
00396 
00397     //public member functions//
00399 
00400     template <class T>
00401     TwoDArray<T>::TwoDArray( const int height , const int width , const T val)
00402     {
00403         Init( height , width );  
00404         std::fill_n( m_array_of_rows[0], m_length_x*m_length_y, val);
00405     }  
00406 
00407     template <class T>
00408     TwoDArray<T>::TwoDArray(const TwoDArray<T>& Cpy)
00409     {
00410         m_first_x = Cpy.m_first_x;
00411         m_first_y = Cpy.m_first_y;        
00412         m_last_x = Cpy.m_last_x;
00413         m_last_y = Cpy.m_last_y;
00414 
00415         m_length_x = m_last_x - m_first_x + 1;
00416         m_length_y = m_last_y - m_first_y + 1;        
00417 
00418         if (m_first_x == 0 && m_first_y == 0)        
00419             Init(m_length_y , m_length_x);
00420         else{
00421                 //based 2D arrays not yet supported    
00422         }
00423 
00424         memcpy( m_array_of_rows[0] , (Cpy.m_array_of_rows)[0] , m_length_x * m_length_y * sizeof( T ) );
00425 
00426     }
00427 
00428     template <class T>
00429     TwoDArray<T>& TwoDArray<T>::operator=(const TwoDArray<T>& rhs)
00430     {
00431         if (&rhs != this)
00432         {
00433             FreeData();
00434 
00435             m_first_x = rhs.m_first_x;
00436             m_first_y = rhs.m_first_y;            
00437 
00438             m_last_x = rhs.m_last_x;
00439             m_last_y = rhs.m_last_y;
00440 
00441             m_length_x = m_last_x - m_first_x + 1;
00442             m_length_y = m_last_y - m_first_y + 1;        
00443 
00444             if (m_first_x == 0 && m_first_y == 0)
00445                 Init(m_length_y , m_length_x);
00446             else
00447             {
00448                     //based 2D arrays not yet supported
00449             }
00450 
00451             memcpy( m_array_of_rows[0], (rhs.m_array_of_rows)[0], m_length_x * m_length_y * sizeof( T ) );
00452 
00453         }
00454 
00455         return *this;
00456 
00457     }
00458     
00459     template <class T>
00460     bool TwoDArray<T>::CopyContents(TwoDArray<T>& out) const
00461     {
00462         if (&out != this)
00463         {
00464             int rows = std::min (m_length_y, out.m_length_y);
00465             int cols = std::min (m_length_x, out.m_length_x);
00466             for (int j = 0; j < rows; ++j)
00467             {
00468                 memcpy( out.m_array_of_rows[j], m_array_of_rows[j], cols * sizeof( T )) ;
00469                 for (int i = cols; i <out.m_length_x; ++i)
00470                     out.m_array_of_rows[j][i] = out.m_array_of_rows[j][cols-1];
00471             }
00472             for (int j = rows; j < out.m_length_y; ++j)
00473             {
00474                 memcpy( out.m_array_of_rows[j], out.m_array_of_rows[rows-1], out.m_length_x * sizeof( T )) ;
00475             }
00476         }
00477         return true;
00478     }
00479     
00480     template <class T>
00481     void TwoDArray<T>::Fill( T val)
00482     {
00483         if (m_length_x && m_length_y)
00484             std::fill_n( m_array_of_rows[0], m_length_x*m_length_y, val);
00485     }  
00486 
00487     template <class T>
00488     void TwoDArray<T>::Resize(const int height, const int width)
00489     {
00490         if (height != m_length_y || width != m_length_x)
00491         {
00492             FreeData();
00493             Init(height , width);
00494         }
00495     }
00496 
00497     //private member functions//
00499 
00500     template <class T>
00501     void TwoDArray<T>::Init(const int height , const int width)
00502     {
00503         m_length_x = width; 
00504         m_length_y = height;
00505         m_first_x = 0;
00506         m_first_y = 0;
00507 
00508         m_last_x = m_length_x-1;
00509         m_last_y = m_length_y-1;
00510 
00511         if (m_length_y>0)
00512         {
00513             // allocate the array containing ptrs to all the rows
00514             m_array_of_rows = new element_type[ m_length_y ];
00515 
00516             if ( m_length_x>0 )
00517             {
00518                 // Allocate the whole thing as a single big block
00519                 m_array_of_rows[0] = new T[ m_length_x * m_length_y ];
00520 
00521                 // Point the pointers
00522                 for (int j=1 ; j<m_length_y ; ++j)
00523                     m_array_of_rows[j] = m_array_of_rows[0] + j * m_length_x;
00524             }
00525             else
00526             {
00527                 m_length_x = 0;
00528                 m_first_x = 0;
00529                 m_last_x = -1;
00530             }
00531         }
00532         else 
00533         {
00534             m_length_x = 0;
00535             m_length_y = 0;
00536             m_first_x = 0;
00537             m_first_y = 0;
00538             m_last_x = -1;
00539             m_last_y = -1;
00540             m_array_of_rows = NULL;
00541         }
00542     }
00543 
00544     template <class T>
00545     void TwoDArray<T>::FreeData()
00546     {
00547         if (m_length_y>0)
00548         {
00549             if (m_length_x>0) 
00550             {
00551                 delete[] m_array_of_rows[0];
00552             }
00553 
00554             m_length_y = m_length_x = 0;
00555             // deallocate the array of rows
00556             delete[] m_array_of_rows;
00557         }    
00558     }
00559 
00560     // Related functions
00561 
00563     template <class T >
00564     std::ostream & operator<< (std::ostream & stream, TwoDArray<T> & array)
00565     {
00566         for (int j=0 ; j<array.LengthY() ; ++j)
00567         {
00568             for (int i=0 ; i<array.LengthX() ; ++i)
00569             {
00570                 stream << array[j][i] << " ";
00571             }// i
00572             stream << std::endl;
00573         }// j
00574 
00575         return stream;
00576     }
00577 
00579     template <class T >
00580     std::istream & operator>> (std::istream & stream, TwoDArray<T> & array)
00581     {
00582         for (int j=0 ; j<array.LengthY() ; ++j)
00583         {
00584             for (int i=0 ; i<array.LengthX() ; ++i)
00585             {
00586                 stream >> array[j][i];
00587             }// i
00588         }// j
00589 
00590         return stream;
00591     }
00592 
00593 } //namespace dirac
00594 #endif

© 2004 British Broadcasting Corporation. Dirac code licensed under the Mozilla Public License (MPL) Version 1.1.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.