00001 #include <stdio.h>
00002 #include <iostream.h>
00003
00004 #include "vuColour31a.h"
00005 #include "vuColour7a.h"
00006 #include "vuColour9a.h"
00007 #include "vuColourRGBa.h"
00008 #include "vuColourXYZa.h"
00009 #include "vuTFIntensity.h"
00010
00011
00012
00013
00014 vuTFIntensity::vuTFIntensity()
00015 {
00016 m_Table = 0;
00017 m_Light = 0;
00018 init(4,256);
00019 }
00020
00021
00022
00023
00024 vuTFIntensity::vuTFIntensity(dword ncomp, dword range)
00025 {
00026 m_Table = 0;
00027 m_Light = 0;
00028 init(ncomp,range);
00029 }
00030
00031
00032
00033
00034
00035 vuTFIntensity::vuTFIntensity(const vuTFIntensity& inst)
00036 {
00037 m_NComp = inst.m_NComp;
00038 m_Range = inst.m_Range;
00039 m_TableLength = inst.m_TableLength;
00040 m_Table = new float[m_TableLength];
00041 if(!m_Table) throw "Out of memory while creating transfer function table (class vuTFIntensity).\n";
00042 for (dword i=0;i<m_TableLength;++i)
00043 m_Table[i] = inst.m_Table[i];
00044 for (dword i=0;i<m_NComp;++i)
00045 m_Light[i] = inst.m_Light[i];
00046 }
00047
00048 vuTFIntensity::~vuTFIntensity()
00049 {
00050 cleanup();
00051 }
00052
00053
00054
00055
00056
00057 vuTFIntensity& vuTFIntensity::operator=(const vuTFIntensity& rhs)
00058 {
00059 if (this != &rhs)
00060 {
00061 if(m_Range == rhs.m_Range) {
00062 if(m_NComp == rhs.m_NComp) {
00063 for (dword i=0;i<m_TableLength;++i)
00064 m_Table[i] = rhs.m_Table[i];
00065 for(dword i=0;i<m_NComp;i++)
00066 m_Light[i] = rhs.m_Light[i];
00067 } else if(m_NComp == 4 && rhs.m_NComp > 4) {
00068 vuColourRGBa col;
00069 for (dword i=0, index=0;i<m_Range;i++) {
00070 rhs.getRGBa(i,col);
00071 for(dword j=0;j<m_NComp;j++)
00072 m_Table[index++] = col[j];
00073 }
00074 } else if(m_NComp == 8 && rhs.m_NComp == 32) {
00075 vuColour7a col;
00076 for (dword i=0, index=0;i<m_Range;i++) {
00077 col.from(vuColour31a(&rhs.m_Table[i*rhs.m_NComp]));
00078 for(dword j=0;j<m_NComp;j++)
00079 m_Table[index++] = col[j];
00080 }
00081 col.from(vuColour31a(rhs.m_Light));
00082 for(dword i=0;i<m_NComp;i++)
00083 m_Light[i] = col[i];
00084 } else
00085 cout << "Incompatible number of components in transfer function."<<endl;}
00086
00087 }
00088 return *this;
00089 }
00090
00091
00092
00093
00094
00095 bool vuTFIntensity::init(dword ncomp, dword range)
00096 {
00097 if(ncomp == 0 || range == 0) {
00098 m_Table = 0;
00099 m_Light = 0;
00100 m_NComp = 0;
00101 m_Range = 0;
00102 m_TableLength = 0;
00103 return true;
00104 }
00105 m_NComp = ncomp;
00106 m_Range = range;
00107 m_TableLength = m_NComp*m_Range;
00108
00109 m_Table = new float[m_TableLength];
00110 if(!m_Table) return false;
00111
00112
00113 m_Light = new float[m_NComp];
00114 if(!m_Light) return false;
00115
00116
00117 for (dword i=0;i<m_TableLength;i++)
00118 m_Table[i] = 0.0f;
00119 for (dword i=0;i<m_NComp;i++)
00120 m_Light[i] = 1.0f;
00121 return true;
00122 }
00123
00124
00125 void vuTFIntensity::cleanup()
00126 {
00127 if(m_Table) delete [] m_Table;
00128 m_Table = 0;
00129 if(m_Light) delete [] m_Light;
00130 m_Light = 0;
00131 }
00132
00133
00134 bool vuTFIntensity::resize(dword ncomp, dword range)
00135 {
00136 cleanup();
00137 return init(ncomp, range);
00138 }
00139
00140
00141 float vuTFIntensity::getOpacityAtPos(dword i)
00142 {
00143 return m_Table[i*m_NComp+(m_NComp-1)];
00144 }
00145
00146
00147 void vuTFIntensity::getRGBa(dword i, vuColourRGBa& rgba) const
00148 {
00149 if (m_NComp == 4) rgba = &m_Table[i*m_NComp];
00150 else if(m_NComp == 32) {
00151
00152
00153
00154 vuColour31a c31a(&m_Table[i*m_NComp]);
00155
00156 c31a *= vuColour31a(m_Light);
00157 rgba.from(c31a);
00158
00159 rgba.clampTo01();
00160 } else if(m_NComp == 8) {
00161
00162
00163
00164 vuColour7a c7a(&m_Table[i*m_NComp]);
00165
00166 c7a *= vuColour7a(m_Light);
00167 rgba.from(c7a);
00168
00169 rgba.clampTo01();
00170 } else if(m_NComp == 10) {
00171 vuColour9a c9a(&m_Table[i*m_NComp]);
00172
00173 c9a *= vuColour9a(m_Light);
00174 c9a.to(rgba);
00175
00176 rgba.clampTo01();
00177 }
00178 }
00179
00180 void vuTFIntensity::normalizeAlphaToOne()
00181 {
00182 dword i,c;
00183 float *fp = m_Table;
00184 for(i=0;i<m_Range;i++) {
00185 float alpha = fp[m_NComp-1];
00186 for(c=0;c<m_NComp-1;c++,fp++)
00187 *fp *= alpha;
00188 *(fp++) = 1.0f;
00189 }
00190 }
00191
00192 void vuTFIntensity::fromMap(const vuMap& map)
00193 {
00194 dword i,c;
00195 float *fp = m_Table;
00196 for(i=0;i<m_Range;i++) {
00197 for(c=0;c<m_NComp-1;c++,fp++)
00198 *fp = ((float)map[i])/255.0f;
00199 *(fp++) = 1.0f;
00200 }
00201 }