GDCM 3.0.24
gdcmSurfaceHelper.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: GDCM (Grassroots DICOM). A DICOM library
4
5 Copyright (c) 2006-2017 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
22namespace gdcm
23{
24
30{
31public:
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
91private:
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
102template <typename T, typename U>
103unsigned 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.0f / (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
120template <typename T, typename U>
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.0f / (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.01f));
140 if(tmp[1] >= -128 && tmp[1] <= 0)
141 {
142 CIELab[1] = (unsigned short)(((float)(0x8080)/128.0f)*tmp[1] + ((float)0x8080));
143 }
144 else if(tmp[1] <= 127 && tmp[1] > 0)
145 {
146 CIELab[1] = (unsigned short)(((float)(0xFFFF - 0x8080)/127.0f)*tmp[1] + (float)(0x8080));
147 }
148 if(tmp[2] >= -128 && tmp[2] <= 0)
149 {
150 CIELab[2] = (unsigned short)(((float)0x8080/128.0f)*tmp[2] + ((float)0x8080));
151 }
152 else if(tmp[2] <= 127 && tmp[2] > 0)
153 {
154 CIELab[2] = (unsigned short)(((float)(0xFFFF - 0x8080)/127.0f)*tmp[2] + (float)(0x8080));
155 }
156
157 return CIELab;
158}
159
160template <typename T, typename U>
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.0f*CIELab[0] /(float)(0xFFFF);
172 if(CIELab[1] <= 0x8080)
173 {
174 tmp[1] = (float)(((CIELab[1] - 0x8080) * 128.0f)/(float)0x8080);
175 }
176 else
177 {
178 tmp[1] = (float)((CIELab[1]-0x8080)*127.0f / (float)(0xFFFF - 0x8080));
179 }
180 if(CIELab[2] <= 0x8080)
181 {
182 tmp[2] = (float)(((CIELab[2] - 0x8080) * 128.0f)/(float)0x8080);
183 }
184 else
185 {
186 tmp[2] = (float)((CIELab[2]-0x8080)*127.0f / (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
199template <typename U>
201 const U rangeMax/* = 255*/)
202{
203 return RecommendedDisplayCIELabToRGB<float>(CIELab, rangeMax);
204}
205
206} // end namespace gdcm
207
208#endif // GDCMSURFACEHELPER_H
SurfaceHelper.
Definition gdcmSurfaceHelper.h:30
static unsigned short RGBToRecommendedDisplayGrayscale(const std::vector< T > &RGB, const U rangeMax=255)
Convert a RGB color into DICOM grayscale (ready to write).
Definition gdcmSurfaceHelper.h:103
static std::vector< T > RecommendedDisplayCIELabToRGB(const ColorArray &CIELab, const U rangeMax=255)
Convert a DICOM CIE-Lab (after reading) color into RGB.
Definition gdcmSurfaceHelper.h:161
static ColorArray RGBToRecommendedDisplayCIELab(const std::vector< T > &RGB, const U rangeMax=255)
Convert a RGB color into DICOM CIE-Lab (ready to write).
Definition gdcmSurfaceHelper.h:121
std::vector< unsigned short > ColorArray
Definition gdcmSurfaceHelper.h:33
#define GDCM_EXPORT
Definition gdcmWin32.h:34
Definition gdcmASN1.h:21