GDCM
2.2.0
|
00001 /*========================================================================= 00002 00003 Program: GDCM (Grassroots DICOM). A DICOM library 00004 00005 Copyright (c) 2006-2011 Mathieu Malaterre 00006 All rights reserved. 00007 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details. 00008 00009 This software is distributed WITHOUT ANY WARRANTY; without even 00010 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00011 PURPOSE. See the above copyright notice for more information. 00012 00013 =========================================================================*/ 00014 #ifndef GDCMSURFACEHELPER_H 00015 #define GDCMSURFACEHELPER_H 00016 00017 #include "gdcmTypes.h" // for GDCM_EXPORT 00018 00019 #include <vector> 00020 #include <iostream> 00021 00022 namespace gdcm 00023 { 00024 00025 class GDCM_EXPORT SurfaceHelper 00026 { 00027 public: 00028 00029 typedef std::vector< unsigned short > ColorArray; 00030 00042 template <typename T, typename U> 00043 static unsigned short RGBToRecommendedDisplayGrayscale(const std::vector<T> & RGB, 00044 const U rangeMax = 255); 00056 template <typename T, typename U> 00057 static ColorArray RGBToRecommendedDisplayCIELab(const std::vector<T> & RGB, 00058 const U rangeMax = 255); 00070 template <typename T, typename U> 00071 static std::vector<T> RecommendedDisplayCIELabToRGB(const ColorArray & CIELab, 00072 const U rangeMax = 255); 00083 template <typename U> 00084 static std::vector<float> RecommendedDisplayCIELabToRGB(const ColorArray & CIELab, 00085 const U rangeMax = 255); 00086 00087 private: 00088 00089 static std::vector< float > RGBToXYZ(const std::vector<float> & RGB); 00090 00091 static std::vector< float > XYZToRGB(const std::vector<float> & XYZ); 00092 00093 static std::vector< float > XYZToCIELab(const std::vector<float> & XYZ); 00094 00095 static std::vector< float > CIELabToXYZ(const std::vector<float> & CIELab); 00096 }; 00097 00098 template <typename T, typename U> 00099 unsigned short SurfaceHelper::RGBToRecommendedDisplayGrayscale(const std::vector<T> & RGB, 00100 const U rangeMax/* = 255*/) 00101 { 00102 assert(RGB.size() > 2); 00103 00104 unsigned short Grayscale = 0; 00105 00106 const float inverseRangeMax = 1. / (float) rangeMax; 00107 00108 // 0xFFFF "=" 255 "=" white 00109 Grayscale = (unsigned short) ((0.2989 * RGB[0] + 0.5870 * RGB[1] + 0.1140 * RGB[2]) 00110 * inverseRangeMax // Convert to range 0-1 00111 * 0xFFFF); // Convert to range 0x0000-0xFFFF 00112 00113 return Grayscale; 00114 } 00115 00116 template <typename T, typename U> 00117 SurfaceHelper::ColorArray SurfaceHelper::RGBToRecommendedDisplayCIELab(const std::vector<T> & RGB, 00118 const U rangeMax/* = 255*/) 00119 { 00120 assert(RGB.size() > 2); 00121 00122 ColorArray CIELab(3); 00123 std::vector<float> tmp(3); 00124 00125 // Convert to range 0-1 00126 const float inverseRangeMax = 1. / (float) rangeMax; 00127 tmp[0] = (float) (RGB[0] * inverseRangeMax); 00128 tmp[1] = (float) (RGB[1] * inverseRangeMax); 00129 tmp[2] = (float) (RGB[2] * inverseRangeMax); 00130 00131 tmp = SurfaceHelper::XYZToCIELab( SurfaceHelper::RGBToXYZ( tmp ) ); 00132 00133 // Convert to range 0x0000-0xFFFF 00134 // 0xFFFF "=" 127, 0x8080 "=" 0, 0x0000 "=" -128 00135 CIELab[0] = (unsigned short) ( 0xFFFF * (tmp[0]*0.01)); 00136 if(tmp[1] >= -128 && tmp[1] <= 0) 00137 { 00138 CIELab[1] = (unsigned short)(((float)(0x8080)/128.0)*tmp[1] + ((float)0x8080)); 00139 } 00140 else if(tmp[1] <= 127 && tmp[1] > 0) 00141 { 00142 CIELab[1] = (unsigned short)(((float)(0xFFFF - 0x8080)/127.0)*tmp[1] + (float)(0x8080)); 00143 } 00144 if(tmp[2] >= -128 && tmp[2] <= 0) 00145 { 00146 CIELab[2] = (unsigned short)(((float)0x8080/128.0)*tmp[2] + ((float)0x8080)); 00147 } 00148 else if(tmp[2] <= 127 && tmp[2] > 0) 00149 { 00150 CIELab[2] = (unsigned short)(((float)(0xFFFF - 0x8080)/127.0)*tmp[2] + (float)(0x8080)); 00151 } 00152 00153 return CIELab; 00154 } 00155 00156 template <typename T, typename U> 00157 std::vector<T> SurfaceHelper::RecommendedDisplayCIELabToRGB(const ColorArray & CIELab, 00158 const U rangeMax/* = 255*/) 00159 { 00160 assert(CIELab.size() > 2); 00161 00162 std::vector<T> RGB(3); 00163 std::vector<float> tmp(3); 00164 00165 // Convert to range 0-1 00166 00167 tmp[0] = 100.0*CIELab[0] /(float)(0xFFFF); 00168 if(CIELab[1] >= 0x0000 && CIELab[1] <= 0x8080) 00169 { 00170 tmp[1] = (float)(((CIELab[1] - 0x8080) * 128.0)/(float)0x8080); 00171 } 00172 else if(CIELab[1] <= 0xFFFF && CIELab[1] > 0x8080) 00173 { 00174 tmp[1] = (float)((CIELab[1]-0x8080)*127.0 / (float)(0xFFFF - 0x8080)); 00175 } 00176 if(CIELab[2] >= 0x0000 && CIELab[2] <= 0x8080) 00177 { 00178 tmp[2] = (float)(((CIELab[2] - 0x8080) * 128.0)/(float)0x8080); 00179 } 00180 else if(CIELab[2] <= 0xFFFF && CIELab[2] > 0x8080) 00181 { 00182 tmp[2] = (float)((CIELab[2]-0x8080)*127.0 / (float)(0XFFFF - 0x8080)); 00183 } 00184 00185 tmp = SurfaceHelper::XYZToRGB( SurfaceHelper::CIELabToXYZ( tmp ) ); 00186 00187 // Convert to range 0-rangeMax 00188 RGB[0] = (T) (tmp[0] * rangeMax); 00189 RGB[1] = (T) (tmp[1] * rangeMax); 00190 RGB[2] = (T) (tmp[2] * rangeMax); 00191 00192 return RGB; 00193 } 00194 00195 template <typename U> 00196 std::vector<float> SurfaceHelper::RecommendedDisplayCIELabToRGB(const ColorArray & CIELab, 00197 const U rangeMax/* = 255*/) 00198 { 00199 return RecommendedDisplayCIELabToRGB<float>(CIELab, rangeMax); 00200 } 00201 00202 } // end namespace gdcm 00203 00204 #endif // GDCMSURFACEHELPER_H