GDCM  2.2.0
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 
26 {
27 public:
28 
29  typedef std::vector< unsigned short > ColorArray;
30 
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);
83  template <typename U>
84  static std::vector<float> RecommendedDisplayCIELabToRGB(const ColorArray & CIELab,
85  const U rangeMax = 255);
86 
87 private:
88 
89  static std::vector< float > RGBToXYZ(const std::vector<float> & RGB);
90 
91  static std::vector< float > XYZToRGB(const std::vector<float> & XYZ);
92 
93  static std::vector< float > XYZToCIELab(const std::vector<float> & XYZ);
94 
95  static std::vector< float > CIELabToXYZ(const std::vector<float> & CIELab);
96 };
97 
98 template <typename T, typename U>
99 unsigned short SurfaceHelper::RGBToRecommendedDisplayGrayscale(const std::vector<T> & RGB,
100  const U rangeMax/* = 255*/)
101 {
102  assert(RGB.size() > 2);
103 
104  unsigned short Grayscale = 0;
105 
106  const float inverseRangeMax = 1. / (float) rangeMax;
107 
108  // 0xFFFF "=" 255 "=" white
109  Grayscale = (unsigned short) ((0.2989 * RGB[0] + 0.5870 * RGB[1] + 0.1140 * RGB[2])
110  * inverseRangeMax // Convert to range 0-1
111  * 0xFFFF); // Convert to range 0x0000-0xFFFF
112 
113  return Grayscale;
114 }
115 
116 template <typename T, typename U>
118  const U rangeMax/* = 255*/)
119 {
120  assert(RGB.size() > 2);
121 
122  ColorArray CIELab(3);
123  std::vector<float> tmp(3);
124 
125  // Convert to range 0-1
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);
130 
131  tmp = SurfaceHelper::XYZToCIELab( SurfaceHelper::RGBToXYZ( tmp ) );
132 
133  // Convert to range 0x0000-0xFFFF
134  // 0xFFFF "=" 127, 0x8080 "=" 0, 0x0000 "=" -128
135  CIELab[0] = (unsigned short) ( 0xFFFF * (tmp[0]*0.01));
136  if(tmp[1] >= -128 && tmp[1] <= 0)
137  {
138  CIELab[1] = (unsigned short)(((float)(0x8080)/128.0)*tmp[1] + ((float)0x8080));
139  }
140  else if(tmp[1] <= 127 && tmp[1] > 0)
141  {
142  CIELab[1] = (unsigned short)(((float)(0xFFFF - 0x8080)/127.0)*tmp[1] + (float)(0x8080));
143  }
144  if(tmp[2] >= -128 && tmp[2] <= 0)
145  {
146  CIELab[2] = (unsigned short)(((float)0x8080/128.0)*tmp[2] + ((float)0x8080));
147  }
148  else if(tmp[2] <= 127 && tmp[2] > 0)
149  {
150  CIELab[2] = (unsigned short)(((float)(0xFFFF - 0x8080)/127.0)*tmp[2] + (float)(0x8080));
151  }
152 
153  return CIELab;
154 }
155 
156 template <typename T, typename U>
158  const U rangeMax/* = 255*/)
159 {
160  assert(CIELab.size() > 2);
161 
162  std::vector<T> RGB(3);
163  std::vector<float> tmp(3);
164 
165  // Convert to range 0-1
166 
167  tmp[0] = 100.0*CIELab[0] /(float)(0xFFFF);
168  if(CIELab[1] >= 0x0000 && CIELab[1] <= 0x8080)
169  {
170  tmp[1] = (float)(((CIELab[1] - 0x8080) * 128.0)/(float)0x8080);
171  }
172  else if(CIELab[1] <= 0xFFFF && CIELab[1] > 0x8080)
173  {
174  tmp[1] = (float)((CIELab[1]-0x8080)*127.0 / (float)(0xFFFF - 0x8080));
175  }
176  if(CIELab[2] >= 0x0000 && CIELab[2] <= 0x8080)
177  {
178  tmp[2] = (float)(((CIELab[2] - 0x8080) * 128.0)/(float)0x8080);
179  }
180  else if(CIELab[2] <= 0xFFFF && CIELab[2] > 0x8080)
181  {
182  tmp[2] = (float)((CIELab[2]-0x8080)*127.0 / (float)(0XFFFF - 0x8080));
183  }
184 
185  tmp = SurfaceHelper::XYZToRGB( SurfaceHelper::CIELabToXYZ( tmp ) );
186 
187  // Convert to range 0-rangeMax
188  RGB[0] = (T) (tmp[0] * rangeMax);
189  RGB[1] = (T) (tmp[1] * rangeMax);
190  RGB[2] = (T) (tmp[2] * rangeMax);
191 
192  return RGB;
193 }
194 
195 template <typename U>
196 std::vector<float> SurfaceHelper::RecommendedDisplayCIELabToRGB(const ColorArray & CIELab,
197  const U rangeMax/* = 255*/)
198 {
199  return RecommendedDisplayCIELabToRGB<float>(CIELab, rangeMax);
200 }
201 
202 } // end namespace gdcm
203 
204 #endif // GDCMSURFACEHELPER_H

Generated on Wed Jun 13 2012 20:40:37 for GDCM by doxygen 1.8.1
SourceForge.net Logo