00001 // SpectralImage.cpp: Implementierung der Klasse SpectralImage. 00002 // 00004 #include <stddef.h> 00005 #include "SpectralImage.h" 00006 00007 namespace ns_vu1112112 { 00008 using namespace ns_vu1112112; 00009 00010 00012 // Konstruktion/Destruktion 00014 00015 SpectralImage::SpectralImage() 00016 { 00017 spix = NULL; 00018 mask = NULL; 00019 maxx = maxy = 0; 00020 init(0,0); 00021 } 00022 00023 SpectralImage::~SpectralImage() 00024 { 00025 if(spix) delete [] spix; 00026 spix = NULL; 00027 if(mask) delete [] mask; 00028 mask = NULL; 00029 } 00030 00031 bool SpectralImage::init(int sizx, int sizy) 00032 { 00033 if(maxx != sizx || maxy != sizy) { 00034 maxx=sizx; 00035 maxy=sizy; 00036 if(spix) delete [] spix; 00037 if(mask) delete [] mask; 00038 if(sizx && sizy) 00039 { 00040 spix = new ColourType[maxx*maxy]; 00041 if(!spix) return false; 00042 mask = new bool[maxx*maxy]; 00043 if(!mask) return false; 00044 } 00045 } 00046 if(sizx && sizy) 00047 { 00048 int i; 00049 for(i=0; i<maxx*maxy; i++) 00050 spix[i] = 0.0f; 00051 set_mask(false); 00052 } else { spix = NULL; mask = NULL; } 00053 return true; 00054 } 00055 00056 void SpectralImage::set_mask(bool value) 00057 { 00058 int i; 00059 for(i=0; i<maxx*maxy; i++) 00060 mask[i] = 0; 00061 } 00062 00063 void SpectralImage::create_black_mask() 00064 { 00065 int i; 00066 for(i=0; i<maxx*maxy; i++) 00067 mask[i] = spix[i].maxComponent()>0.00001f; 00068 } 00069 00070 bool SpectralImage::get_rgb(byte *buf, int size) const 00071 { 00072 int sizeXY = maxx*maxy; 00073 if(spix == NULL || buf == NULL || size<3*sizeXY) return false; 00074 vuColourXYZa xyza; 00075 vuColourRGBa rgba; 00076 const ColourType *spec = spix; 00077 //xyza.setNormalSpectrum(vuColour31a(ambient)); //HERE-- fixed scaling 00078 int i; 00079 for(i=0;i<sizeXY;i++, spec++) 00080 { 00081 if(mask[i]) { 00082 #if defined USE_RGBA 00083 ColourType col(*spec); 00084 col*=light; 00085 // col.clampTo1(); 00086 // rgba = col; 00087 //* 00088 xyza.From(col); 00089 // xyza.normalize(); // normalization darkens too much... 00090 rgba.from(xyza); 00091 rgba.clampTo1(); 00092 //*/ 00093 #elif defined USE_SPECTRUM9A 00094 (vuColour9a(spix[x+maxx*y]*light)).to(rgba); 00095 rgba.clampTo01(); 00096 #else //if 31a or 7a 00097 //xyza.from(spix[x+maxx*y]*light); 00098 //xyza.normalize(); // Do I do this here?? 00099 //rgba.from(xyza); 00100 rgba.from(*spec*light); 00101 rgba.clampTo01(); 00102 #endif 00103 *(buf++)=(unsigned char)(255.f*rgba[0]); 00104 *(buf++)=(unsigned char)(255.f*rgba[1]); 00105 *(buf++)=(unsigned char)(255.f*rgba[2]); 00106 } else { 00107 *(buf++)= 0; *(buf++)= 0; *(buf++)= 0; 00108 } 00109 } 00110 return true; 00111 } 00112 00113 void SpectralImage::set_light(ColourType light) 00114 { 00115 this->light = light; 00116 } 00117 00118 00119 void SpectralImage::get_extents(int &sizx, int &sizy) 00120 { 00121 sizx=maxx; 00122 sizy=maxy; 00123 } 00124 00125 const ColourType& SpectralImage::get_light() const 00126 { 00127 return light; 00128 } 00129 00130 } // end of namespace