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.13 2005/04/13 15:36:29 asuraparaju Exp $ $Name: Dirac_0_5_2 $
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 *
00027 * Alternatively, the contents of this file may be used under the terms of
00028 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
00029 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
00030 * the GPL or the LGPL are applicable instead of those above. If you wish to
00031 * allow use of your version of this file only under the terms of the either
00032 * the GPL or LGPL and not to allow others to use your version of this file
00033 * under the MPL, indicate your decision by deleting the provisions above
00034 * and replace them with the notice and other provisions required by the GPL
00035 * or LGPL. If you do not delete the provisions above, a recipient may use
00036 * your version of this file under the terms of any one of the MPL, the GPL
00037 * or the LGPL.
00038 * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef _ARRAYS_H_
00041 #define _ARRAYS_H_
00042 
00043 //basic array types used for pictures etc
00044 
00045 #include <memory>
00046 #include <cstddef>
00047 #include <stdexcept>
00048 #include <iostream>
00049 #include <algorithm>
00050 
00051 namespace dirac
00052 {
00053     typedef short ValueType;
00054     // For clipping purposes, set the maximum value at 1020 = 4*255
00055     // (we have two accuracy bits throughout)
00056     const int PIXEL_VALUE_MAX = 1020;
00057     const int PIXEL_VALUE_MIN = 0;
00058 
00059     typedef int CalcValueType;
00060 
00062 
00066     class Range
00067     {
00068     public:
00070 
00073         Range(int s, int e): m_fst(s), m_lst(e){}
00074 
00076         const int First() const {return m_fst;}
00077 
00079         const int Last() const {return m_lst;}
00080 
00081     private:
00082         int m_fst ,m_lst;
00083     };
00084 
00086     //One-Dimensional Array type//
00088 
00090 
00095     template <class T> class OneDArray
00096     {
00097     public:
00099 
00102         OneDArray();
00103 
00105 
00108         OneDArray(const int len);
00109 
00111 
00116         OneDArray(const Range& r);
00117 
00119 
00122         ~OneDArray()
00123         {
00124             FreePtr();
00125         }
00126 
00128 
00131         OneDArray(const OneDArray<T>& cpy);
00132 
00134 
00137         OneDArray<T>& operator=(const OneDArray<T>& rhs);    
00138 
00140         void Resize(int l);
00141 
00143         T& operator[](const int pos){return m_ptr[pos-m_first];}
00144 
00146         const T& operator[](const int pos) const {return m_ptr[pos-m_first];}
00147 
00149         int Length() const {return m_length;}
00150 
00152         int First() const {return m_first;}
00153 
00155         int Last() const {return m_last;}
00156 
00157     private:
00158         void Init(const int len);
00159 
00160         void Init(const Range& r);
00161 
00162         void FreePtr();    
00163 
00164         int m_first, m_last;
00165         int m_length;
00166         T* m_ptr;
00167     };
00168 
00169     //public member functions//
00171 
00172     template <class T>
00173     OneDArray<T>::OneDArray()
00174     {
00175         Init(0);
00176     }
00177 
00178     template <class T>
00179     OneDArray<T>::OneDArray(const int len)
00180     {
00181         Init(len);
00182     }
00183 
00184     template <class T>
00185     OneDArray<T>::OneDArray(const Range& r)
00186     {
00187         Init(r);
00188     }
00189 
00190     template <class T>
00191     OneDArray<T>::OneDArray(const OneDArray<T>& cpy)
00192     {
00193         m_first = cpy.m_first;
00194         m_last = cpy.m_last;
00195         m_length = m_last - m_first + 1;
00196 
00197         if (m_first==0)
00198             Init(m_length);
00199         else
00200             Init(Range(m_first , m_last));
00201 
00202         memcpy( m_ptr , cpy.m_ptr , m_length * sizeof( T ) );
00203     }
00204 
00205     template <class T>
00206     OneDArray<T>& OneDArray<T>::operator=(const OneDArray<T>& rhs)
00207     {
00208         if (&rhs != this)
00209         {
00210             FreePtr();
00211             m_first = rhs.m_first;
00212             m_last = rhs.m_last;
00213             m_length = rhs.m_length;
00214 
00215             if (m_first == 0)
00216                 Init(m_length);
00217             else
00218                 Init(Range(m_first , m_last));
00219 
00220             memcpy( m_ptr , rhs.m_ptr , m_length * sizeof( T ) );
00221 
00222         }
00223         return *this;
00224     }
00225 
00226     template <class T> 
00227     void OneDArray<T>::Resize(int l)
00228     {    
00229         FreePtr();
00230         Init(l);
00231     }
00232 
00233     //private member functions//
00235 
00236     template <class T>
00237     void OneDArray<T>::Init(const int len)
00238     {
00239         Range r(0 , len-1);
00240 
00241         Init(r);
00242 
00243     }        
00244 
00245     template <class T>
00246     void OneDArray<T>::Init(const Range& r)
00247     {
00248 
00249         m_first = r.First();
00250         m_last = r.Last();
00251         m_length = m_last - m_first + 1; 
00252 
00253         if ( m_length>0 ) 
00254         {
00255             m_ptr = new T[ m_length ];
00256         }
00257         else 
00258         {
00259             m_length = 0;
00260             m_first = 0;
00261             m_last = -1;
00262         }
00263     }
00264 
00265     template <class T>
00266     void OneDArray<T>::FreePtr()
00267     {
00268         if ( m_length>0 )
00269             delete[] m_ptr;
00270     }
00271 
00272 
00274     //Two-Dimensional Array type//
00276 
00278 
00286     template <class T> class TwoDArray
00287     {
00288         typedef T* element_type;
00289 
00290     public:
00291 
00293 
00296         TwoDArray(){ Init(0,0); }
00297 
00299 
00302         TwoDArray( const int height , const int width ){Init(height , width);}
00303 
00305 
00309         TwoDArray( const int height , const int width , T val);
00310 
00312 
00315         virtual ~TwoDArray(){
00316             FreeData();    
00317         }
00318 
00320 
00323         TwoDArray(const TwoDArray<T>& Cpy);
00324 
00326 
00329         TwoDArray<T>& operator=(const TwoDArray<T>& rhs);
00330 
00332         void Resize(const int height, const int width);    
00333 
00335 
00339         inline element_type& operator[](const int pos){return m_array_of_rows[pos];}
00340 
00342 
00346         inline const element_type& operator[](const int pos) const {return m_array_of_rows[pos];}
00347 
00349         const int LengthX() const { return m_length_x; }
00350 
00352         const int LengthY() const { return m_length_y; }
00353 
00355         const int FirstX() const { return m_first_x; } 
00356 
00358         const int FirstY() const { return m_first_y; } 
00359 
00361         const int LastX() const { return m_last_x; } 
00362 
00364         const int LastY() const { return m_last_y; }
00365 
00366     private:
00368         void Init(const int height,const int width);
00369 
00371         void FreeData();    
00372 
00373         int m_first_x;
00374         int m_first_y;
00375 
00376         int m_last_x;
00377         int m_last_y;
00378 
00379         int m_length_x;
00380         int m_length_y;
00381 
00382         element_type* m_array_of_rows;
00383     };
00384 
00385     //public member functions//
00387 
00388     template <class T>
00389     TwoDArray<T>::TwoDArray( const int height , const int width , const T val)
00390     {
00391         Init( height , width );  
00392         std::fill_n( m_array_of_rows[0], m_length_x*m_length_y, val);
00393     }  
00394 
00395     template <class T>
00396     TwoDArray<T>::TwoDArray(const TwoDArray<T>& Cpy)
00397     {
00398         m_first_x = Cpy.m_first_x;
00399         m_first_y = Cpy.m_first_y;        
00400         m_last_x = Cpy.m_last_x;
00401         m_last_y = Cpy.m_last_y;
00402 
00403         m_length_x = m_last_x - m_first_x + 1;
00404         m_length_y = m_last_y - m_first_y + 1;        
00405 
00406         if (m_first_x == 0 && m_first_y == 0)        
00407             Init(m_length_y , m_length_x);
00408         else{
00409                 //based 2D arrays not yet supported    
00410         }
00411 
00412         memcpy( m_array_of_rows[0] , (Cpy.m_array_of_rows)[0] , m_length_x * m_length_y * sizeof( T ) );
00413 
00414     }
00415 
00416     template <class T>
00417     TwoDArray<T>& TwoDArray<T>::operator=(const TwoDArray<T>& rhs)
00418     {
00419         if (&rhs != this)
00420         {
00421             FreeData();
00422 
00423             m_first_x = rhs.m_first_x;
00424             m_first_y = rhs.m_first_y;            
00425 
00426             m_last_x = rhs.m_last_x;
00427             m_last_y = rhs.m_last_y;
00428 
00429             m_length_x = m_last_x - m_first_x + 1;
00430             m_length_y = m_last_y - m_first_y + 1;        
00431 
00432             if (m_first_x == 0 && m_first_y == 0)
00433                 Init(m_length_y , m_length_x);
00434             else
00435             {
00436                     //based 2D arrays not yet supported
00437             }
00438 
00439             memcpy( m_array_of_rows[0], (rhs.m_array_of_rows)[0], m_length_x * m_length_y * sizeof( T ) );
00440 
00441         }
00442 
00443         return *this;
00444 
00445     }
00446 
00447     template <class T>
00448     void TwoDArray<T>::Resize(const int height, const int width)
00449     {
00450         FreeData();
00451         Init(height , width);
00452     }
00453 
00454     //private member functions//
00456 
00457     template <class T>
00458     void TwoDArray<T>::Init(const int height , const int width)
00459     {
00460         m_length_x = width; 
00461         m_length_y = height;
00462         m_first_x = 0;
00463         m_first_y = 0;
00464 
00465         m_last_x = m_length_x-1;
00466         m_last_y = m_length_y-1;
00467 
00468         if (m_length_y>0)
00469         {
00470             // allocate the array containing ptrs to all the rows
00471             m_array_of_rows = new element_type[ m_length_y ];
00472 
00473             if ( m_length_x>0 )
00474             {
00475                 // Allocate the whole thing as a single big block
00476                 m_array_of_rows[0] = new T[ m_length_x * m_length_y ];
00477 
00478                 // Point the pointers
00479                 for (int j=1 ; j<m_length_y ; ++j)
00480                     m_array_of_rows[j] = m_array_of_rows[0] + j * m_length_x;
00481             }
00482             else
00483             {
00484                 m_length_x = 0;
00485                 m_first_x = 0;
00486                 m_last_x = -1;
00487             }
00488         }
00489         else 
00490         {
00491             m_length_x = 0;
00492             m_length_y = 0;
00493             m_first_x = 0;
00494             m_first_y = 0;
00495             m_last_x = -1;
00496             m_last_y = -1;
00497         }
00498     }
00499 
00500     template <class T>
00501     void TwoDArray<T>::FreeData()
00502     {
00503         if (m_length_y>0)
00504         {
00505             if (m_length_x>0) 
00506             {
00507                 delete[] m_array_of_rows[0];
00508             }
00509 
00510             // deallocate the array of rows
00511             delete[] m_array_of_rows;
00512         }    
00513     }
00514 
00515     // Related functions
00516 
00518     template <class T >
00519     std::ostream & operator<< (std::ostream & stream, TwoDArray<T> & array)
00520     {
00521         for (int j=0 ; j<array.LengthY() ; ++j)
00522         {
00523             for (int i=0 ; i<array.LengthX() ; ++i)
00524             {
00525                 stream << array[j][i] << " ";
00526             }// i
00527             stream << std::endl;
00528         }// j
00529 
00530         return stream;
00531     }
00532 
00534     template <class T >
00535     std::istream & operator>> (std::istream & stream, TwoDArray<T> & array)
00536     {
00537         for (int j=0 ; j<array.LengthY() ; ++j)
00538         {
00539             for (int i=0 ; i<array.LengthX() ; ++i)
00540             {
00541                 stream >> array[j][i];
00542             }// i
00543         }// j
00544 
00545         return stream;
00546     }
00547 
00548 } //namespace dirac
00549 #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.