GDCM 2.2.1

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

gdcmSurfaceHelper.h
Go to the documentation of this file.
1 /*=========================================================================
2
3 Program: GDCM (Grassroots DICOM). A DICOM library
4
5 Copyright (c) 2006-2011 Mathieu Malaterre
6 All rights reserved.
7 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13 =========================================================================*/
14 #ifndef GDCMSURFACEHELPER_H
15 #define GDCMSURFACEHELPER_H
16
17 #include "gdcmTypes.h" // for GDCM_EXPORT
18
19 #include <vector>
20 #include <iostream>
21
22 namespace gdcm
23 {
24
29 class GDCM_EXPORT SurfaceHelper
30 {
31 public:
32
33 typedef std::vector< unsigned short > ColorArray;
34
46 template <typename T, typename U>
47 static unsigned short RGBToRecommendedDisplayGrayscale(const std::vector<T> & RGB,
48 const U rangeMax = 255);
60 template <typename T, typename U>
61 static ColorArray RGBToRecommendedDisplayCIELab(const std::vector<T> & RGB,
62 const U rangeMax = 255);
74 template <typename T, typename U>
75 static std::vector<T> RecommendedDisplayCIELabToRGB(const ColorArray & CIELab,
76 const U rangeMax = 255);
87 template <typename U>
88 static std::vector<float> RecommendedDisplayCIELabToRGB(const ColorArray & CIELab,
89 const U rangeMax = 255);
90
91 private:
92
93 static std::vector< float > RGBToXYZ(const std::vector<float> & RGB);
94
95 static std::vector< float > XYZToRGB(const std::vector<float> & XYZ);
96
97 static std::vector< float > XYZToCIELab(const std::vector<float> & XYZ);
98
99 static std::vector< float > CIELabToXYZ(const std::vector<float> & CIELab);
100 };
101
102 template <typename T, typename U>
103 unsigned short SurfaceHelper::RGBToRecommendedDisplayGrayscale(const std::vector<T> & RGB,
104 const U rangeMax/* = 255*/)
105 {
106 assert(RGB.size() > 2);
107
108 unsigned short Grayscale = 0;
109
110 const float inverseRangeMax = 1. / (float) rangeMax;
111
112 // 0xFFFF "=" 255 "=" white
113 Grayscale = (unsigned short) ((0.2989 * RGB[0] + 0.5870 * RGB[1] + 0.1140 * RGB[2])
114 * inverseRangeMax // Convert to range 0-1
115 * 0xFFFF); // Convert to range 0x0000-0xFFFF
116
117 return Grayscale;
118 }
119
120 template <typename T, typename U>
121 SurfaceHelper::ColorArray SurfaceHelper::RGBToRecommendedDisplayCIELab(const std::vector<T> & RGB,
122 const U rangeMax/* = 255*/)
123 {
124 assert(RGB.size() > 2);
125
126 ColorArray CIELab(3);
127 std::vector<float> tmp(3);
128
129 // Convert to range 0-1
130 const float inverseRangeMax = 1. / (float) rangeMax;
131 tmp[0] = (float) (RGB[0] * inverseRangeMax);
132 tmp[1] = (float) (RGB[1] * inverseRangeMax);
133 tmp[2] = (float) (RGB[2] * inverseRangeMax);
134
135 tmp = SurfaceHelper::XYZToCIELab( SurfaceHelper::RGBToXYZ( tmp ) );
136
137 // Convert to range 0x0000-0xFFFF
138 // 0xFFFF "=" 127, 0x8080 "=" 0, 0x0000 "=" -128
139 CIELab[0] = (unsigned short) ( 0xFFFF * (tmp[0]*0.01));
140 if(tmp[1] >= -128 && tmp[1] <= 0)
141 {
142 CIELab[1] = (unsigned short)(((float)(0x8080)/128.0)*tmp[1] + ((float)0x8080));
143 }
144 else if(tmp[1] <= 127 && tmp[1] > 0)
145 {
146 CIELab[1] = (unsigned short)(((float)(0xFFFF - 0x8080)/127.0)*tmp[1] + (float)(0x8080));
147 }
148 if(tmp[2] >= -128 && tmp[2] <= 0)
149 {
150 CIELab[2] = (unsigned short)(((float)0x8080/128.0)*tmp[2] + ((float)0x8080));
151 }
152 else if(tmp[2] <= 127 && tmp[2] > 0)
153 {
154 CIELab[2] = (unsigned short)(((float)(0xFFFF - 0x8080)/127.0)*tmp[2] + (float)(0x8080));
155 }
156
157 return CIELab;
158 }
159
160 template <typename T, typename U>
161 std::vector<T> SurfaceHelper::RecommendedDisplayCIELabToRGB(const ColorArray & CIELab,
162 const U rangeMax/* = 255*/)
163 {
164 assert(CIELab.size() > 2);
165
166 std::vector<T> RGB(3);
167 std::vector<float> tmp(3);
168
169 // Convert to range 0-1
170
171 tmp[0] = 100.0*CIELab[0] /(float)(0xFFFF);
172 if(CIELab[1] >= 0x0000 && CIELab[1] <= 0x8080)
173 {
174 tmp[1] = (float)(((CIELab[1] - 0x8080) * 128.0)/(float)0x8080);
175 }
176 else if(CIELab[1] <= 0xFFFF && CIELab[1] > 0x8080)
177 {
178 tmp[1] = (float)((CIELab[1]-0x8080)*127.0 / (float)(0xFFFF - 0x8080));
179 }
180 if(CIELab[2] >= 0x0000 && CIELab[2] <= 0x8080)
181 {
182 tmp[2] = (float)(((CIELab[2] - 0x8080) * 128.0)/(float)0x8080);
183 }
184 else if(CIELab[2] <= 0xFFFF && CIELab[2] > 0x8080)
185 {
186 tmp[2] = (float)((CIELab[2]-0x8080)*127.0 / (float)(0XFFFF - 0x8080));
187 }
188
189 tmp = SurfaceHelper::XYZToRGB( SurfaceHelper::CIELabToXYZ( tmp ) );
190
191 // Convert to range 0-rangeMax
192 RGB[0] = (T) (tmp[0] * rangeMax);
193 RGB[1] = (T) (tmp[1] * rangeMax);
194 RGB[2] = (T) (tmp[2] * rangeMax);
195
196 return RGB;
197 }
198
199 template <typename U>
200 std::vector<float> SurfaceHelper::RecommendedDisplayCIELabToRGB(const ColorArray & CIELab,
201 const U rangeMax/* = 255*/)
202 {
203 return RecommendedDisplayCIELabToRGB<float>(CIELab, rangeMax);
204 }
205
206 } // end namespace gdcm
207
208 #endif // GDCMSURFACEHELPER_H