14 #ifndef GDCMSURFACEHELPER_H
15 #define GDCMSURFACEHELPER_H
42 template <
typename T,
typename U>
43 static unsigned short RGBToRecommendedDisplayGrayscale(
const std::vector<T> & RGB,
44 const U rangeMax = 255);
56 template <
typename T,
typename U>
57 static ColorArray RGBToRecommendedDisplayCIELab(
const std::vector<T> & RGB,
58 const U rangeMax = 255);
70 template <
typename T,
typename U>
71 static std::vector<T> RecommendedDisplayCIELabToRGB(
const ColorArray & CIELab,
72 const U rangeMax = 255);
84 static std::vector<float> RecommendedDisplayCIELabToRGB(
const ColorArray & CIELab,
85 const U rangeMax = 255);
89 static std::vector< float > RGBToXYZ(
const std::vector<float> & RGB);
91 static std::vector< float > XYZToRGB(
const std::vector<float> & XYZ);
93 static std::vector< float > XYZToCIELab(
const std::vector<float> & XYZ);
95 static std::vector< float > CIELabToXYZ(
const std::vector<float> & CIELab);
98 template <
typename T,
typename U>
102 assert(RGB.size() > 2);
104 unsigned short Grayscale = 0;
106 const float inverseRangeMax = 1. / (float) rangeMax;
109 Grayscale = (
unsigned short) ((0.2989 * RGB[0] + 0.5870 * RGB[1] + 0.1140 * RGB[2])
116 template <
typename T,
typename U>
120 assert(RGB.size() > 2);
123 std::vector<float> tmp(3);
126 const float inverseRangeMax = 1. / (float) rangeMax;
127 tmp[0] = (float) (RGB[0] * inverseRangeMax);
128 tmp[1] = (float) (RGB[1] * inverseRangeMax);
129 tmp[2] = (float) (RGB[2] * inverseRangeMax);
131 tmp = SurfaceHelper::XYZToCIELab( SurfaceHelper::RGBToXYZ( tmp ) );
135 CIELab[0] = (
unsigned short) ( 0xFFFF * (tmp[0]*0.01));
136 if(tmp[1] >= -128 && tmp[1] <= 0)
138 CIELab[1] = (
unsigned short)(((
float)(0x8080)/128.0)*tmp[1] + ((float)0x8080));
140 else if(tmp[1] <= 127 && tmp[1] > 0)
142 CIELab[1] = (
unsigned short)(((
float)(0xFFFF - 0x8080)/127.0)*tmp[1] + (float)(0x8080));
144 if(tmp[2] >= -128 && tmp[2] <= 0)
146 CIELab[2] = (
unsigned short)(((
float)0x8080/128.0)*tmp[2] + ((float)0x8080));
148 else if(tmp[2] <= 127 && tmp[2] > 0)
150 CIELab[2] = (
unsigned short)(((
float)(0xFFFF - 0x8080)/127.0)*tmp[2] + (float)(0x8080));
156 template <
typename T,
typename U>
160 assert(CIELab.size() > 2);
162 std::vector<T> RGB(3);
163 std::vector<float> tmp(3);
167 tmp[0] = 100.0*CIELab[0] /(float)(0xFFFF);
168 if(CIELab[1] >= 0x0000 && CIELab[1] <= 0x8080)
170 tmp[1] = (float)(((CIELab[1] - 0x8080) * 128.0)/(float)0x8080);
172 else if(CIELab[1] <= 0xFFFF && CIELab[1] > 0x8080)
174 tmp[1] = (float)((CIELab[1]-0x8080)*127.0 / (float)(0xFFFF - 0x8080));
176 if(CIELab[2] >= 0x0000 && CIELab[2] <= 0x8080)
178 tmp[2] = (float)(((CIELab[2] - 0x8080) * 128.0)/(float)0x8080);
180 else if(CIELab[2] <= 0xFFFF && CIELab[2] > 0x8080)
182 tmp[2] = (float)((CIELab[2]-0x8080)*127.0 / (float)(0XFFFF - 0x8080));
185 tmp = SurfaceHelper::XYZToRGB( SurfaceHelper::CIELabToXYZ( tmp ) );
188 RGB[0] = (T) (tmp[0] * rangeMax);
189 RGB[1] = (T) (tmp[1] * rangeMax);
190 RGB[2] = (T) (tmp[2] * rangeMax);
195 template <
typename U>
199 return RecommendedDisplayCIELabToRGB<float>(CIELab, rangeMax);
204 #endif // GDCMSURFACEHELPER_H