Dirac Video Codec

# 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.21 2008/03/14 08:17:36 asuraparaju Exp $ $Name: Dirac_1_0_0 $
    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 #include <cstring>
    00052
    00053 namespace dirac
    00054 {
    00056
    00060     class Range
    00061     {
    00062     public:
    00064
    00067         Range(int s, int e): m_fst(s), m_lst(e){}
    00068
    00070         int First() const {return m_fst;}
    00071
    00073         int Last() const {return m_lst;}
    00074
    00075     private:
    00076         int m_fst ,m_lst;
    00077     };
    00078
    00080     //One-Dimensional Array type//
    00082
    00084
    00089     template <class T> class OneDArray
    00090     {
    00091     public:
    00093
    00096         OneDArray();
    00097
    00099
    00102         OneDArray(const int len);
    00103
    00105
    00110         OneDArray(const Range& r);
    00111
    00113
    00116         ~OneDArray()
    00117         {
    00118             FreePtr();
    00119         }
    00120
    00122
    00125         OneDArray(const OneDArray<T>& cpy);
    00126
    00128
    00131         OneDArray<T>& operator=(const OneDArray<T>& rhs);
    00132
    00134         void Resize(int l);
    00135
    00137         T& operator[](const int pos){return m_ptr[pos-m_first];}
    00138
    00140         const T& operator[](const int pos) const {return m_ptr[pos-m_first];}
    00141
    00143         int Length() const {return m_length;}
    00144
    00146         int First() const {return m_first;}
    00147
    00149         int Last() const {return m_last;}
    00150
    00151     private:
    00152         void Init(const int len);
    00153
    00154         void Init(const Range& r);
    00155
    00156         void FreePtr();
    00157
    00158         int m_first, m_last;
    00159         int m_length;
    00160         T* m_ptr;
    00161     };
    00162
    00163     //public member functions//
    00165
    00166     template <class T>
    00167     OneDArray<T>::OneDArray()
    00168     {
    00169         Init(0);
    00170     }
    00171
    00172     template <class T>
    00173     OneDArray<T>::OneDArray(const int len)
    00174     {
    00175         Init(len);
    00176     }
    00177
    00178     template <class T>
    00179     OneDArray<T>::OneDArray(const Range& r)
    00180     {
    00181         Init(r);
    00182     }
    00183
    00184     template <class T>
    00185     OneDArray<T>::OneDArray(const OneDArray<T>& cpy)
    00186     {
    00187         m_first = cpy.m_first;
    00188         m_last = cpy.m_last;
    00189         m_length = m_last - m_first + 1;
    00190
    00191         if (m_first==0)
    00192             Init(m_length);
    00193         else
    00194             Init(Range(m_first , m_last));
    00195
    00196         memcpy( m_ptr , cpy.m_ptr , m_length * sizeof( T ) );
    00197     }
    00198
    00199     template <class T>
    00200     OneDArray<T>& OneDArray<T>::operator=(const OneDArray<T>& rhs)
    00201     {
    00202         if (&rhs != this)
    00203         {
    00204             FreePtr();
    00205             m_first = rhs.m_first;
    00206             m_last = rhs.m_last;
    00207             m_length = rhs.m_length;
    00208
    00209             if (m_first == 0)
    00210                 Init(m_length);
    00211             else
    00212                 Init(Range(m_first , m_last));
    00213
    00214             memcpy( m_ptr , rhs.m_ptr , m_length * sizeof( T ) );
    00215
    00216         }
    00217         return *this;
    00218     }
    00219
    00220     template <class T>
    00221     void OneDArray<T>::Resize(int l)
    00222     {
    00223         if (l != m_length)
    00224         {
    00225             FreePtr();
    00226             Init(l);
    00227         }
    00228     }
    00229
    00230     //private member functions//
    00232
    00233     template <class T>
    00234     void OneDArray<T>::Init(const int len)
    00235     {
    00236         Range r(0 , len-1);
    00237
    00238         Init(r);
    00239
    00240     }
    00241
    00242     template <class T>
    00243     void OneDArray<T>::Init(const Range& r)
    00244     {
    00245
    00246         m_first = r.First();
    00247         m_last = r.Last();
    00248         m_length = m_last - m_first + 1;
    00249
    00250         if ( m_length>0 )
    00251         {
    00252             m_ptr = new T[ m_length ];
    00253         }
    00254         else
    00255         {
    00256             m_length = 0;
    00257             m_first = 0;
    00258             m_last = -1;
    00259             m_ptr = NULL;
    00260         }
    00261     }
    00262
    00263     template <class T>
    00264     void OneDArray<T>::FreePtr()
    00265     {
    00266         if ( m_length>0 )
    00267             delete[] m_ptr;
    00268     }
    00269
    00270
    00272     //Two-Dimensional Array type//
    00274
    00276
    00284     template <class T> class TwoDArray
    00285     {
    00286         typedef T* element_type;
    00287
    00288     public:
    00289
    00291
    00294         TwoDArray(){ Init(0,0); }
    00295
    00297
    00300         TwoDArray( const int height , const int width ){Init(height , width);}
    00301
    00303
    00307         TwoDArray( const int height , const int width , T val);
    00308
    00310
    00313         virtual ~TwoDArray(){
    00314             FreeData();
    00315         }
    00316
    00318
    00321         TwoDArray(const TwoDArray<T>& Cpy);
    00322
    00324
    00327         TwoDArray<T>& operator=(const TwoDArray<T>& rhs);
    00328
    00330
    00336         bool CopyContents(TwoDArray<T>& out) const;
    00337
    00339
    00342         void Fill(T val);
    00343
    00345         void Resize(const int height, const int width);
    00346
    00348
    00352         inline element_type& operator[](const int pos){return m_array_of_rows[pos];}
    00353
    00355
    00359         inline const element_type& operator[](const int pos) const {return m_array_of_rows[pos];}
    00360
    00362         int LengthX() const { return m_length_x; }
    00363
    00365         int LengthY() const { return m_length_y; }
    00366
    00368         int FirstX() const { return m_first_x; }
    00369
    00371         int FirstY() const { return m_first_y; }
    00372
    00374         int LastX() const { return m_last_x; }
    00375
    00377         int LastY() const { return m_last_y; }
    00378
    00379     private:
    00381         void Init(const int height,const int width);
    00382
    00384         void FreeData();
    00385
    00386         int m_first_x;
    00387         int m_first_y;
    00388
    00389         int m_last_x;
    00390         int m_last_y;
    00391
    00392         int m_length_x;
    00393         int m_length_y;
    00394
    00395         element_type* m_array_of_rows;
    00396     };
    00397
    00398     //public member functions//
    00400
    00401     template <class T>
    00402     TwoDArray<T>::TwoDArray( const int height , const int width , const T val)
    00403     {
    00404         Init( height , width );
    00405         std::fill_n( m_array_of_rows[0], m_length_x*m_length_y, val);
    00406     }
    00407
    00408     template <class T>
    00409     TwoDArray<T>::TwoDArray(const TwoDArray<T>& Cpy)
    00410     {
    00411         m_first_x = Cpy.m_first_x;
    00412         m_first_y = Cpy.m_first_y;
    00413         m_last_x = Cpy.m_last_x;
    00414         m_last_y = Cpy.m_last_y;
    00415
    00416         m_length_x = m_last_x - m_first_x + 1;
    00417         m_length_y = m_last_y - m_first_y + 1;
    00418
    00419         if (m_first_x == 0 && m_first_y == 0)
    00420             Init(m_length_y , m_length_x);
    00421         else{
    00422                 //based 2D arrays not yet supported
    00423         }
    00424
    00425         memcpy( m_array_of_rows[0] , (Cpy.m_array_of_rows)[0] , m_length_x * m_length_y * sizeof( T ) );
    00426
    00427     }
    00428
    00429     template <class T>
    00430     TwoDArray<T>& TwoDArray<T>::operator=(const TwoDArray<T>& rhs)
    00431     {
    00432         if (&rhs != this)
    00433         {
    00434             FreeData();
    00435
    00436             m_first_x = rhs.m_first_x;
    00437             m_first_y = rhs.m_first_y;
    00438
    00439             m_last_x = rhs.m_last_x;
    00440             m_last_y = rhs.m_last_y;
    00441
    00442             m_length_x = m_last_x - m_first_x + 1;
    00443             m_length_y = m_last_y - m_first_y + 1;
    00444
    00445             if (m_first_x == 0 && m_first_y == 0)
    00446                 Init(m_length_y , m_length_x);
    00447             else
    00448             {
    00449                     //based 2D arrays not yet supported
    00450             }
    00451
    00452             memcpy( m_array_of_rows[0], (rhs.m_array_of_rows)[0], m_length_x * m_length_y * sizeof( T ) );
    00453
    00454         }
    00455
    00456         return *this;
    00457
    00458     }
    00459
    00460     template <class T>
    00461     bool TwoDArray<T>::CopyContents(TwoDArray<T>& out) const
    00462     {
    00463         if (&out != this)
    00464         {
    00465             int rows = std::min (m_length_y, out.m_length_y);
    00466             int cols = std::min (m_length_x, out.m_length_x);
    00467             for (int j = 0; j < rows; ++j)
    00468             {
    00469                 memcpy( out.m_array_of_rows[j], m_array_of_rows[j], cols * sizeof( T )) ;
    00470                 for (int i = cols; i <out.m_length_x; ++i)
    00471                     out.m_array_of_rows[j][i] = out.m_array_of_rows[j][cols-1];
    00472             }
    00473             for (int j = rows; j < out.m_length_y; ++j)
    00474             {
    00475                 memcpy( out.m_array_of_rows[j], out.m_array_of_rows[rows-1], out.m_length_x * sizeof( T )) ;
    00476             }
    00477         }
    00478         return true;
    00479     }
    00480
    00481     template <class T>
    00482     void TwoDArray<T>::Fill( T val)
    00483     {
    00484         if (m_length_x && m_length_y)
    00485             std::fill_n( m_array_of_rows[0], m_length_x*m_length_y, val);
    00486     }
    00487
    00488     template <class T>
    00489     void TwoDArray<T>::Resize(const int height, const int width)
    00490     {
    00491         if (height != m_length_y || width != m_length_x)
    00492         {
    00493             FreeData();
    00494             Init(height , width);
    00495         }
    00496     }
    00497
    00498     //private member functions//
    00500
    00501     template <class T>
    00502     void TwoDArray<T>::Init(const int height , const int width)
    00503     {
    00504         m_length_x = width;
    00505         m_length_y = height;
    00506         m_first_x = 0;
    00507         m_first_y = 0;
    00508
    00509         m_last_x = m_length_x-1;
    00510         m_last_y = m_length_y-1;
    00511
    00512         if (m_length_y>0)
    00513         {
    00514             // allocate the array containing ptrs to all the rows
    00515             m_array_of_rows = new element_type[ m_length_y ];
    00516
    00517             if ( m_length_x>0 )
    00518             {
    00519                 // Allocate the whole thing as a single big block
    00520                 m_array_of_rows[0] = new T[ m_length_x * m_length_y ];
    00521
    00522                 // Point the pointers
    00523                 for (int j=1 ; j<m_length_y ; ++j)
    00524                     m_array_of_rows[j] = m_array_of_rows[0] + j * m_length_x;
    00525             }
    00526             else
    00527             {
    00528                 m_length_x = 0;
    00529                 m_first_x = 0;
    00530                 m_last_x = -1;
    00531             }
    00532         }
    00533         else
    00534         {
    00535             m_length_x = 0;
    00536             m_length_y = 0;
    00537             m_first_x = 0;
    00538             m_first_y = 0;
    00539             m_last_x = -1;
    00540             m_last_y = -1;
    00541             m_array_of_rows = NULL;
    00542         }
    00543     }
    00544
    00545     template <class T>
    00546     void TwoDArray<T>::FreeData()
    00547     {
    00548         if (m_length_y>0)
    00549         {
    00550             if (m_length_x>0)
    00551             {
    00552                 delete[] m_array_of_rows[0];
    00553             }
    00554
    00555             m_length_y = m_length_x = 0;
    00556             // deallocate the array of rows
    00557             delete[] m_array_of_rows;
    00558         }
    00559     }
    00560
    00561     // Related functions
    00562
    00564     template <class T >
    00565     std::ostream & operator<< (std::ostream & stream, TwoDArray<T> & array)
    00566     {
    00567         for (int j=0 ; j<array.LengthY() ; ++j)
    00568         {
    00569             for (int i=0 ; i<array.LengthX() ; ++i)
    00570             {
    00571                 stream << array[j][i] << " ";
    00572             }// i
    00573             stream << std::endl;
    00574         }// j
    00575
    00576         return stream;
    00577     }
    00578
    00580     template <class T >
    00581     std::istream & operator>> (std::istream & stream, TwoDArray<T> & array)
    00582     {
    00583         for (int j=0 ; j<array.LengthY() ; ++j)
    00584         {
    00585             for (int i=0 ; i<array.LengthX() ; ++i)
    00586             {
    00587                 stream >> array[j][i];
    00588             }// i
    00589         }// j
    00590
    00591         return stream;
    00592     }
    00593
    00594 } //namespace dirac
    00595 #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.