00001 #ifndef _SPALETTE_H_
00002 #define _SPALETTE_H_
00003
00004 #include <iostream.h>
00005
00006 #include "vuColour31a.h"
00007 #include "vuColour7a.h"
00008 #include "vuColourRGBa.h"
00009 #include "vuDVector.h"
00010 #include "vuSimpleTypes.h"
00011
00012 #include "SOptimizer.h"
00013
00014 #define SPAL_RANGE 100000.0f // +/- max value for a bound
00015
00016
00017
00018
00019
00020
00021
00028 class SPalette
00029 {
00030 protected:
00031
00034 class SDesignColour
00035 {
00036 public:
00038 SDesignColour() : design(false), freecolour(-1) {};
00040 SDesignColour(const vuColourRGBa& _rgbw, bool _design = false)
00041 : design(_design), freecolour(-1), rgbw(_rgbw) {};
00043 SDesignColour(SDesignColour& rhs)
00044 {
00045 design = rhs.design;
00046 rgbw = rhs.rgbw;
00047 freecolour = rhs.freecolour;
00048 };
00049
00050 SVector getCol() const {return SVector(3,rgbw.getData());};
00051
00052 float getWeight() const {return rgbw[3];};
00053
00054
00055 vuColourRGBa& getRGBW() { return rgbw;};
00056
00057 bool design;
00058
00059 int freecolour;
00060
00061 vuColourRGBa rgbw;
00062 };
00063
00065 class SFreeColour
00066 {
00067 public:
00069 SFreeColour(dword _id=0,
00070 const vuColourRGBa& _lb= vuColourRGBa(0.0f),
00071 const vuColourRGBa& _ub= vuColourRGBa(1.0f))
00072 : ub(_ub), lb(_lb), useBounds(true), ID(_id), used(0) {};
00074 SFreeColour(SFreeColour& rhs)
00075 {
00076 ID = rhs.ID;
00077 ub = rhs.ub;
00078 lb = rhs.lb;
00079 used = 0;
00080 useBounds = rhs.useBounds;
00081 };
00083 vuColourRGBa ub, lb;
00084 bool useBounds;
00085 dword ID;
00086 dword used;
00087 dword position;
00088 };
00089
00090
00092 class SSpectrum
00093 {
00094 public:
00096 SSpectrum(vuColour31a _spec=vuColour31a(0.0f), dword _id=0)
00097 : ID(_id), spec(_spec), design(false), used(0),
00098 ub(SPAL_RANGE), lb(0.0f), useBounds(true)
00099 { setName("unnamed");};
00101 SSpectrum(SSpectrum& rhs)
00102 {
00103 ID = rhs.ID;
00104 spec = rhs.spec;
00105 design = rhs.design;
00106 used = 0;
00107 ub = rhs.ub;
00108 lb = rhs.lb;
00109 useBounds = rhs.useBounds;
00110 setName(rhs.name);
00111 };
00113 void setName(const char* _name)
00114 {
00115 if(name && strlen(_name)<127)
00116 strcpy(name,_name);
00117 else name[0]=0;
00118 }
00119 friend ostream& operator<< (ostream& os, const SSpectrum& s)
00120 {
00121 os << s.name << endl;
00122 if(s.name[0]==0) os << "unnamed" << endl;
00123 else s.spec.write(os) << endl;
00124 return os;
00125 }
00126 friend istream& operator>> (istream& is, SSpectrum& s)
00127 {
00128 is >> s.name;
00129 s.spec.read(is);
00130 return is;
00131 }
00132
00133 dword ID;
00134 vuColour31a spec;
00135 bool design;
00136 dword used;
00137 vuColour31a ub,lb;
00138 bool useBounds;
00139 char name[128];
00140 };
00141
00142
00143
00144
00145 public:
00147 SPalette();
00149 ~SPalette();
00150
00153 bool load(const char* filename);
00156 bool save(const char* filename);
00159 bool loadSpectrum(int rid, int lid, const char* filename);
00162 bool saveSpectrum(int rid, int lid, const char* filename);
00163
00165 void reset();
00166
00170 bool createSpectrum();
00171
00172
00174 void setSmoothnessWeight(float weight);
00176 void setErrorMinWeight(float weight);
00181 void useV7(bool useit=true) { m_UseV7 = useit;};
00182
00184 void matchDesignColours();
00185
00186
00188 dword getNLights() const { return m_Lights.getLength();};
00191 int addLight(const vuColour31a& light);
00194 void removeLight(word index);
00195
00196
00198 dword getNRefls() const
00199 { return m_Refls.getLength();};
00202 int addReflectance(const vuColour31a& refl);
00205 void removeReflectance(word index);
00206
00207
00209
00211 vuColour31a& getSpec(int rid, int lid)
00212 { return getSpecRec(rid,lid).spec;};
00214
00216 bool& useSpecBounds(int rid, int lid)
00217 { return getSpecRec(rid,lid).useBounds;};
00219
00221 vuColour31a& getSpecUB(int rid, int lid)
00222 {return getSpecRec(rid,lid).ub;};
00224
00226 vuColour31a& getSpecLB(int rid, int lid)
00227 {return getSpecRec(rid,lid).lb;};
00229
00231 void setSpecName(int rid, int lid, const char* name)
00232 { getSpecRec(rid,lid).setName(name);};
00234
00236 const char* getSpecName(int rid, int lid)
00237 { return getSpecRec(rid,lid).name;};
00239
00241 bool& getSpecDesignState(int rid, int lid)
00242 { return getSpecRec(rid,lid).design;};
00243
00244
00246 vuColourRGBa getRLColour(dword rid, dword lid, bool useV7=false);
00249 void setDesignRGBW(dword rid, dword lid, const vuColourRGBa& col);
00251 vuColourRGBa& getDesignRGBW(dword rid, dword lid);
00254 bool& getDesignState(dword rid, dword lid)
00255 { return m_DesCol[rid][lid].design;}
00256
00257
00261 void attachFreeColour(dword rid, dword lid, int fcid);
00263 void detachFreeColour(dword rid, dword lid)
00264 {attachFreeColour(rid,lid,-1);};
00266 int getFreeColourID(int rid, int lid) {
00267 if(rid>=0 && lid>=0) return m_DesCol[rid][lid].freecolour;
00268 return -1;
00269 };
00270
00272 dword getNFreeColour() { return m_FreeCol.getLength();};
00275 void setFreeColour(dword fcid,
00276 const vuColourRGBa& lb= vuColourRGBa(0.0f),
00277 const vuColourRGBa& ub= vuColourRGBa(1.0f));
00279 vuColourRGBa& getFreeColUB(dword fcid)
00280 { return m_FreeCol[fcid].ub;}
00282 vuColourRGBa& getFreeColLB(dword fcid)
00283 { return m_FreeCol[fcid].lb;}
00285 bool& getFreeColBoundState(dword fcid)
00286 { return m_FreeCol[fcid].useBounds;}
00287
00288
00289
00290
00291 private:
00293 SSpectrum& getSpecRec(int rid, int lid) {
00294 if(rid>=0) return m_Refls[rid];
00295 else if(lid>=0) return m_Lights[lid];
00296 else return m_Refls[0];
00297 }
00300 bool checkSetup();
00302 SMatrix makeErrorMat(const vuColour31a& diag);
00304 SMatrix makeSmoothingMat();
00307 SMatrix makeCXFMat(const vuColour31a& diag)
00308 { return makeCXFMat(diag, m_UseV7);}
00313 SMatrix makeCXFMat(const vuColour31a& diag, bool useV7);
00314
00315
00316
00317
00318 private:
00321 vuDVector<SSpectrum> m_Lights;
00325 vuDVector<SSpectrum> m_Refls;
00326
00331 vuDVector< vuDVector<SDesignColour> > m_DesCol;
00332
00334 vuDVector< SFreeColour > m_FreeCol;
00335
00337 bool m_UseV7;
00338
00342 float m_WSmoothness;
00344 bool m_UseSmoothness;
00345
00350 float m_WErrorMin;
00352 bool m_UseErrorMin;
00353
00355 SOptimizer m_Optimizer;
00356
00357 SMatrix m_C31t7;
00358
00359 dword m_NDesCol;
00360 dword m_NDesSpec;
00361 SMatrix m_CXF31toRGB;
00362 SMatrix m_CXF7toRGB;
00363 SMatrix m_CXF31to7;
00364 };
00365
00366 #endif
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376