00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "color.h"
00025 #include "spectrum.h"
00026 #include "spectrumwavelengths.h"
00027 #include "regular.h"
00028 #include "memory.h"
00029
00030 using namespace lux;
00031
00032 XYZColor SWCSpectrum::ToXYZ(const TsPack *tspack) const {
00033 SpectrumWavelengths *sw = tspack->swl;
00034 float xyz[3];
00035 xyz[0] = xyz[1] = xyz[2] = 0.;
00036 if (sw->single) {
00037 const int j = sw->single_w;
00038 xyz[0] = sw->cie_X[j] * c[j];
00039 xyz[1] = sw->cie_Y[j] * c[j];
00040 xyz[2] = sw->cie_Z[j] * c[j];
00041 } else {
00042 for (unsigned int j = 0; j < WAVELENGTH_SAMPLES; ++j) {
00043 xyz[0] += sw->cie_X[j] * c[j];
00044 xyz[1] += sw->cie_Y[j] * c[j];
00045 xyz[2] += sw->cie_Z[j] * c[j];
00046 }
00047 }
00048
00049 return XYZColor(xyz);
00050 }
00051
00052 Scalar SWCSpectrum::Y(const TsPack *tspack) const {
00053 SpectrumWavelengths *sw = tspack->swl;
00054 Scalar y = 0.f;
00055
00056 if (sw->single) {
00057 const int j = sw->single_w;
00058 y = sw->cie_Y[j] * c[j];
00059 } else {
00060 for (unsigned int j = 0; j < WAVELENGTH_SAMPLES; ++j) {
00061 y += sw->cie_Y[j] * c[j];
00062 }
00063 }
00064
00065 return y;
00066 }
00067 Scalar SWCSpectrum::Filter(const TsPack *tspack) const
00068 {
00069 SpectrumWavelengths *sw = tspack->swl;
00070 Scalar result = 0.f;
00071 if (sw->single) {
00072 result = c[sw->single_w];
00073 } else {
00074 for (int i = 0; i < WAVELENGTH_SAMPLES; ++i)
00075 result += c[i];
00076 result *= inv_WAVELENGTH_SAMPLES;
00077 }
00078 return result;
00079 }
00080
00081 SWCSpectrum::SWCSpectrum(const TsPack *tspack, const SPD *s) {
00082 SpectrumWavelengths *sw = tspack->swl;
00083 for (unsigned int j = 0; j < WAVELENGTH_SAMPLES; ++j) {
00084 c[j] = s->sample(sw->w[j]);
00085 }
00086 }
00087
00088 SWCSpectrum::SWCSpectrum(const TsPack *tspack, const RGBColor &s) {
00089 SpectrumWavelengths *sw = tspack->swl;
00090 const float r = s.c[0];
00091 const float g = s.c[1];
00092 const float b = s.c[2];
00093
00094 for (unsigned int j = 0; j < WAVELENGTH_SAMPLES; ++j)
00095 c[j] = 0.;
00096
00097 if (r <= g && r <= b)
00098 {
00099 AddWeighted(r, sw->spect_w);
00100
00101 if (g <= b)
00102 {
00103 AddWeighted(g - r, sw->spect_c);
00104 AddWeighted(b - g, sw->spect_b);
00105 }
00106 else
00107 {
00108 AddWeighted(b - r, sw->spect_c);
00109 AddWeighted(g - b, sw->spect_g);
00110 }
00111 }
00112 else if (g <= r && g <= b)
00113 {
00114 AddWeighted(g, sw->spect_w);
00115
00116 if (r <= b)
00117 {
00118 AddWeighted(r - g, sw->spect_m);
00119 AddWeighted(b - r, sw->spect_b);
00120 }
00121 else
00122 {
00123 AddWeighted(b - g, sw->spect_m);
00124 AddWeighted(r - b, sw->spect_r);
00125 }
00126 }
00127 else
00128 {
00129 AddWeighted(b, sw->spect_w);
00130
00131 if (r <= g)
00132 {
00133 AddWeighted(r - b, sw->spect_y);
00134 AddWeighted(g - r, sw->spect_g);
00135 }
00136 else
00137 {
00138 AddWeighted(g - b, sw->spect_y);
00139 AddWeighted(r - g, sw->spect_r);
00140 }
00141 }
00142 }