Hauptseite   Klassenhierarchie   Auflistung der Dateien  

channel.cpp

00001 /**********************************************************************************
00002 * Copyright (c) 2003, Christoph Rueegg <opendev@cdrnet.ch> and Matthias Bader     *
00003 * All rights reserved.                                                            *
00004 *                                                                                 *
00005 * Project Website: http://www.cdrnet.net/projects/painter/                        *
00006 *                                                                                 *
00007 * Redistribution and use in source and binary forms, with or without modification,*
00008 * are permitted provided that the following conditions are met:                   *
00009 *                                                                                 *
00010 * 1. Redistributions of source code must retain the above copyright notice,       *
00011 * this list of conditions and the following disclaimer.                           *
00012 *                                                                                 *
00013 * 2. Redistributions in binary form must reproduce the above copyright notice,    *
00014 * this list of conditions and the following disclaimer in the documentation       *
00015 * and/or other materials provided with the distribution.                          *
00016 *                                                                                 *
00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"     *
00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE       *
00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE      *
00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE        *
00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR             *
00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF            *
00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS        *
00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN         *
00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)         *
00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF          *
00027 * THE POSSIBILITY OF SUCH DAMAGE.                                                 *
00028 **********************************************************************************/
00029 
00030 #ifndef __CHANNEL_GNX__
00031 #define __CHANNEL_GNX__
00032 
00033 //NOTE: no separated headerfiles = generics workaround (missing support for token 'extern')
00034 
00035 #include <stdlib.h>
00036 #include <iostream>
00037 using namespace std;
00038 
00039 template<class T>
00040 class Channel
00041 {
00042 private:
00043   T *data;
00044   int id;
00045 protected:
00046   int width;
00047   int height;
00048 public:
00049   Channel(const int height, const int width);
00050   Channel();
00051   virtual ~Channel();
00052   virtual int GetId();
00053   virtual void SetId(const int id);
00054   virtual inline T Get(const int x, const int y);
00055   virtual inline void Set(const int x, const int y, const T val);
00056   virtual inline int GetWidth();
00057   virtual inline int GetHeight();
00058   virtual Channel<T>* EmptyClone();
00059   virtual T** GetRawData();
00060   virtual void ResetChannel();
00061   virtual void Normalize(int* x, int* y);
00062   virtual void Normalize(int* topleftx, int* toplefty, int* bottomrightx, int* bottomrighty);
00063 };
00064 
00065 
00066 template<class T>
00067 Channel<T>::Channel(const int height, const int width)
00068 {
00069   data = new T[width*height];
00070   this->height=height;
00071   this->width=width;
00072   id = 0;
00073 }
00074 
00075 template<class T>
00076 Channel<T>::Channel()
00077 {
00078   data = 0;
00079   id = 0;
00080 }
00081 
00082 template<class T>
00083 Channel<T>::~Channel()
00084 {
00085   if(data != 0) //avoid undefined behavior (=crash) on mixed single/field pointers
00086     delete [] data;
00087 }
00088 
00089 template<class T>
00090 void Channel<T>::SetId(const int id)
00091 {
00092   this->id = id;
00093 }
00094 
00095 template<class T>
00096 int Channel<T>::GetId()
00097 {
00098   return(id);
00099 }
00100 
00101 template<class T>
00102 inline T Channel<T>::Get(const int x, const int y)
00103 {
00104   //if(x<0||y<0||x>=width||y>=height)
00105   //{
00106   //  cerr << "ILLEGALGET[" << x << "|" << y << "]";
00107   //  return 0;
00108   //}
00109   return(data[x+y*width]);
00110 }
00111 
00112 template<class T>
00113 inline void Channel<T>::Set(const int x, const int y, const T val)
00114 {
00115   //if(x<0||y<0||x>=width||y>=height)
00116   //{
00117   //  cerr << "ILLEGALSET[" << x << "|" << y << "]";
00118   //  return;
00119   //}
00120   data[x+y*width] = val;
00121 }
00122 
00123 
00124 template<class T>
00125 inline int Channel<T>::GetWidth()
00126 {
00127   return(width);
00128 }
00129 
00130 template<class T>
00131 inline int Channel<T>::GetHeight()
00132 {
00133   return(height);
00134 }
00135 
00136 template<class T>
00137 T** Channel<T>::GetRawData()
00138 {
00139   return(&data);
00140 }
00141 
00142 template<class T>
00143 Channel<T>* Channel<T>::EmptyClone()
00144 {
00145   Channel<T> *copy = new Channel<T>(height,width);
00146   return(copy);
00147 }
00148 
00149 template<class T>
00150 void Channel<T>::ResetChannel()
00151 {
00152 }
00153 
00154 template<class T>
00155 void Channel<T>::Normalize(int* x, int* y)
00156 
00157 {
00158 
00159   if(*x<0)
00160     *x = 0;
00161   if(*y<0)
00162     *y = 0;
00163   if(*x >= width)
00164     *x = width-1;
00165   if(*y >= height)
00166     *y = height-1;
00167 }
00168 
00169 template<class T>
00170 
00171 void Channel<T>::Normalize(int* topleftx, int* toplefty, int* bottomrightx, int* bottomrighty)
00172 {
00173   int temp;
00174   Normalize(topleftx,toplefty);
00175   Normalize(bottomrightx,bottomrighty);
00176   if(*topleftx > *bottomrightx)
00177   {
00178     temp = *topleftx;
00179     *topleftx = *bottomrightx;
00180     *bottomrightx = temp;
00181   }
00182   if(*toplefty > *bottomrighty)
00183   {
00184     temp = *toplefty;
00185     *toplefty = *bottomrighty;
00186     *bottomrighty = temp;
00187   }
00188 }
00189 
00190 #endif

Erzeugt am Fri Jan 31 15:27:35 2003 für OOP Miniprojekt von doxygen1.3-rc2