Hauptseite   Klassenhierarchie   Auflistung der Dateien  

hybridchannel.cpp

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

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