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.15 2005/11/08 11:41:58 asuraparaju Exp $ $Name: Dirac_0_5_4 $
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 {
00054     typedef short ValueType;
00055     // For clipping purposes, set the maximum value at 1020 = 4*255
00056     // (we have two accuracy bits throughout)
00057     const int PIXEL_VALUE_MAX = 1020;
00058     const int PIXEL_VALUE_MIN = 0;
00059 
00060     typedef int CalcValueType;
00061 
00063 
00067     class Range
00068     {
00069     public:
00071 
00074         Range(int s, int e): m_fst(s), m_lst(e){}
00075 
00077         const int First() const {return m_fst;}
00078 
00080         const int Last() const {return m_lst;}
00081 
00082     private:
00083         int m_fst ,m_lst;
00084     };
00085 
00087     //One-Dimensional Array type//
00089 
00091 
00096     template <class T> class OneDArray
00097     {
00098     public:
00100 
00103         OneDArray();
00104 
00106 
00109         OneDArray(const int len);
00110 
00112 
00117         OneDArray(const Range& r);
00118 
00120 
00123         ~OneDArray()
00124         {
00125             FreePtr();
00126         }
00127 
00129 
00132         OneDArray(const OneDArray<T>& cpy);
00133 
00135 
00138         OneDArray<T>& operator=(const OneDArray<T>& rhs);    
00139 
00141         void Resize(int l);
00142 
00144         T& operator[](const int pos){return m_ptr[pos-m_first];}
00145 
00147         const T& operator[](const int pos) const {return m_ptr[pos-m_first];}
00148 
00150         int Length() const {return m_length;}
00151 
00153         int First() const {return m_first;}
00154 
00156         int Last() const {return m_last;}
00157 
00158     private:
00159         void Init(const int len);
00160 
00161         void Init(const Range& r);
00162 
00163         void FreePtr();    
00164 
00165         int m_first, m_last;
00166         int m_length;
00167         T* m_ptr;
00168     };
00169 
00170     //public member functions//
00172 
00173     template <class T>
00174     OneDArray<T>::OneDArray()
00175     {
00176         Init(0);
00177     }
00178 
00179     template <class T>
00180     OneDArray<T>::OneDArray(const int len)
00181     {
00182         Init(len);
00183     }
00184 
00185     template <class T>
00186     OneDArray<T>::OneDArray(const Range& r)
00187     {
00188         Init(r);
00189     }
00190 
00191     template <class T>
00192     OneDArray<T>::OneDArray(const OneDArray<T>& cpy)
00193     {
00194         m_first = cpy.m_first;
00195         m_last = cpy.m_last;
00196         m_length = m_last - m_first + 1;
00197 
00198         if (m_first==0)
00199             Init(m_length);
00200         else
00201             Init(Range(m_first , m_last));
00202 
00203         memcpy( m_ptr , cpy.m_ptr , m_length * sizeof( T ) );
00204     }
00205 
00206     template <class T>
00207     OneDArray<T>& OneDArray<T>::operator=(const OneDArray<T>& rhs)
00208     {
00209         if (&rhs != this)
00210         {
00211             FreePtr();
00212             m_first = rhs.m_first;
00213             m_last = rhs.m_last;
00214             m_length = rhs.m_length;
00215 
00216             if (m_first == 0)
00217                 Init(m_length);
00218             else
00219                 Init(Range(m_first , m_last));
00220 
00221             memcpy( m_ptr , rhs.m_ptr , m_length * sizeof( T ) );
00222 
00223         }
00224         return *this;
00225     }
00226 
00227     template <class T> 
00228     void OneDArray<T>::Resize(int l)
00229     {
00230         if (l != m_length)
00231         {
00232             FreePtr();
00233             Init(l);
00234         }
00235     }
00236 
00237     //private member functions//
00239 
00240     template <class T>
00241     void OneDArray<T>::Init(const int len)
00242     {
00243         Range r(0 , len-1);
00244 
00245         Init(r);
00246 
00247     }        
00248 
00249     template <class T>
00250     void OneDArray<T>::Init(const Range& r)
00251     {
00252 
00253         m_first = r.First();
00254         m_last = r.Last();
00255         m_length = m_last - m_first + 1; 
00256 
00257         if ( m_length>0 ) 
00258         {
00259             m_ptr = new T[ m_length ];
00260         }
00261         else 
00262         {
00263             m_length = 0;
00264             m_first = 0;
00265             m_last = -1;
00266         }
00267     }
00268 
00269     template <class T>
00270     void OneDArray<T>::FreePtr()
00271     {
00272         if ( m_length>0 )
00273             delete[] m_ptr;
00274     }
00275 
00276 
00278     //Two-Dimensional Array type//
00280 
00282 
00290     template <class T> class TwoDArray
00291     {
00292         typedef T* element_type;
00293 
00294     public:
00295 
00297 
00300         TwoDArray(){ Init(0,0); }
00301 
00303 
00306         TwoDArray( const int height , const int width ){Init(height , width);}
00307 
00309 
00313         TwoDArray( const int height , const int width , T val);
00314 
00316 
00319         virtual ~TwoDArray(){
00320             FreeData();    
00321         }
00322 
00324 
00327         TwoDArray(const TwoDArray<T>& Cpy);
00328 
00330 
00333         TwoDArray<T>& operator=(const TwoDArray<T>& rhs);
00334 
00336         void Resize(const int height, const int width);    
00337 
00339 
00343         inline element_type& operator[](const int pos){return m_array_of_rows[pos];}
00344 
00346 
00350         inline const element_type& operator[](const int pos) const {return m_array_of_rows[pos];}
00351 
00353         const int LengthX() const { return m_length_x; }
00354 
00356         const int LengthY() const { return m_length_y; }
00357 
00359         const int FirstX() const { return m_first_x; } 
00360 
00362         const int FirstY() const { return m_first_y; } 
00363 
00365         const int LastX() const { return m_last_x; } 
00366 
00368         const int LastY() const { return m_last_y; }
00369 
00370     private:
00372         void Init(const int height,const int width);
00373 
00375         void FreeData();    
00376 
00377         int m_first_x;
00378         int m_first_y;
00379 
00380         int m_last_x;
00381         int m_last_y;
00382 
00383         int m_length_x;
00384         int m_length_y;
00385 
00386         element_type* m_array_of_rows;
00387     };
00388 
00389     //public member functions//
00391 
00392     template <class T>
00393     TwoDArray<T>::TwoDArray( const int height , const int width , const T val)
00394     {
00395         Init( height , width );  
00396         std::fill_n( m_array_of_rows[0], m_length_x*m_length_y, val);
00397     }  
00398 
00399     template <class T>
00400     TwoDArray<T>::TwoDArray(const TwoDArray<T>& Cpy)
00401     {
00402         m_first_x = Cpy.m_first_x;
00403         m_first_y = Cpy.m_first_y;        
00404         m_last_x = Cpy.m_last_x;
00405         m_last_y = Cpy.m_last_y;
00406 
00407         m_length_x = m_last_x - m_first_x + 1;
00408         m_length_y = m_last_y - m_first_y + 1;        
00409 
00410         if (m_first_x == 0 && m_first_y == 0)        
00411             Init(m_length_y , m_length_x);
00412         else{
00413                 //based 2D arrays not yet supported    
00414         }
00415 
00416         memcpy( m_array_of_rows[0] , (Cpy.m_array_of_rows)[0] , m_length_x * m_length_y * sizeof( T ) );
00417 
00418     }
00419 
00420     template <class T>
00421     TwoDArray<T>& TwoDArray<T>::operator=(const TwoDArray<T>& rhs)
00422     {
00423         if (&rhs != this)
00424         {
00425             FreeData();
00426 
00427             m_first_x = rhs.m_first_x;
00428             m_first_y = rhs.m_first_y;            
00429 
00430             m_last_x = rhs.m_last_x;
00431             m_last_y = rhs.m_last_y;
00432 
00433             m_length_x = m_last_x - m_first_x + 1;
00434             m_length_y = m_last_y - m_first_y + 1;        
00435 
00436             if (m_first_x == 0 && m_first_y == 0)
00437                 Init(m_length_y , m_length_x);
00438             else
00439             {
00440                     //based 2D arrays not yet supported
00441             }
00442 
00443             memcpy( m_array_of_rows[0], (rhs.m_array_of_rows)[0], m_length_x * m_length_y * sizeof( T ) );
00444 
00445         }
00446 
00447         return *this;
00448 
00449     }
00450 
00451     template <class T>
00452     void TwoDArray<T>::Resize(const int height, const int width)
00453     {
00454         if (height != m_length_y || width != m_length_x)
00455         {
00456             FreeData();
00457             Init(height , width);
00458         }
00459     }
00460 
00461     //private member functions//
00463 
00464     template <class T>
00465     void TwoDArray<T>::Init(const int height , const int width)
00466     {
00467         m_length_x = width; 
00468         m_length_y = height;
00469         m_first_x = 0;
00470         m_first_y = 0;
00471 
00472         m_last_x = m_length_x-1;
00473         m_last_y = m_length_y-1;
00474 
00475         if (m_length_y>0)
00476         {
00477             // allocate the array containing ptrs to all the rows
00478             m_array_of_rows = new element_type[ m_length_y ];
00479 
00480             if ( m_length_x>0 )
00481             {
00482                 // Allocate the whole thing as a single big block
00483                 m_array_of_rows[0] = new T[ m_length_x * m_length_y ];
00484 
00485                 // Point the pointers
00486                 for (int j=1 ; j<m_length_y ; ++j)
00487                     m_array_of_rows[j] = m_array_of_rows[0] + j * m_length_x;
00488             }
00489             else
00490             {
00491                 m_length_x = 0;
00492                 m_first_x = 0;
00493                 m_last_x = -1;
00494             }
00495         }
00496         else 
00497         {
00498             m_length_x = 0;
00499             m_length_y = 0;
00500             m_first_x = 0;
00501             m_first_y = 0;
00502             m_last_x = -1;
00503             m_last_y = -1;
00504         }
00505     }
00506 
00507     template <class T>
00508     void TwoDArray<T>::FreeData()
00509     {
00510         if (m_length_y>0)
00511         {
00512             if (m_length_x>0) 
00513             {
00514                 delete[] m_array_of_rows[0];
00515             }
00516 
00517             m_length_y = m_length_x = 0;
00518             // deallocate the array of rows
00519             delete[] m_array_of_rows;
00520         }    
00521     }
00522 
00523     // Related functions
00524 
00526     template <class T >
00527     std::ostream & operator<< (std::ostream & stream, TwoDArray<T> & array)
00528     {
00529         for (int j=0 ; j<array.LengthY() ; ++j)
00530         {
00531             for (int i=0 ; i<array.LengthX() ; ++i)
00532             {
00533                 stream << array[j][i] << " ";
00534             }// i
00535             stream << std::endl;
00536         }// j
00537 
00538         return stream;
00539     }
00540 
00542     template <class T >
00543     std::istream & operator>> (std::istream & stream, TwoDArray<T> & array)
00544     {
00545         for (int j=0 ; j<array.LengthY() ; ++j)
00546         {
00547             for (int i=0 ; i<array.LengthX() ; ++i)
00548             {
00549                 stream >> array[j][i];
00550             }// i
00551         }// j
00552 
00553         return stream;
00554     }
00555 
00556 } //namespace dirac
00557 #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.