GDCM
2.0.18
|
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 GDCMIMAGECHANGEPHOTOMETRICINTERPRETATION_H 00015 #define GDCMIMAGECHANGEPHOTOMETRICINTERPRETATION_H 00016 00017 #include "gdcmImageToImageFilter.h" 00018 #include "gdcmPhotometricInterpretation.h" 00019 00020 namespace gdcm 00021 { 00022 00023 class DataElement; 00028 class GDCM_EXPORT ImageChangePhotometricInterpretation : public ImageToImageFilter 00029 { 00030 public: 00031 ImageChangePhotometricInterpretation():PI() {} 00032 ~ImageChangePhotometricInterpretation() {} 00033 00035 void SetPhotometricInterpretation(PhotometricInterpretation const &pi) { PI = pi; } 00036 const PhotometricInterpretation &GetPhotometricInterpretation() const { return PI; } 00037 00039 bool Change(); 00040 00042 template <typename T> 00043 static void RGB2YBR(T ybr[3], const T rgb[3]); 00044 template <typename T> 00045 static void YBR2RGB(T rgb[3], const T ybr[3]); 00046 00047 protected: 00048 bool ChangeMonochrome(); 00049 00050 private: 00051 PhotometricInterpretation PI; 00052 }; 00053 00054 00055 // http://en.wikipedia.org/wiki/YCbCr 00056 template <typename T> 00057 void ImageChangePhotometricInterpretation::RGB2YBR(T ybr[3], const T rgb[3]) 00058 { 00059 #if 1 00060 ybr[0] = 65.738 * rgb[0] + 129.057 * rgb[1] + 25.064 * rgb[2] + 16; 00061 ybr[1] = -37.945 * rgb[0] + -74.494 * rgb[1] + 112.439 * rgb[2] + 128; 00062 ybr[2] = 112.439 * rgb[0] + -94.154 * rgb[1] + -18.285 * rgb[2] + 128; 00063 #else 00064 00065 const double R = rgb[0]; 00066 const double G = rgb[1]; 00067 const double B = rgb[2]; 00068 const double Y = .2990 * R + .5870 * G + .1140 * B; 00069 const double CB = -.168736 * R - .331264 * G + .5000 * B + 128; 00070 const double CR = .5000 * R - .418688 * G - .081312 * B + 128; 00071 //assert( Y >= 0 && Y <= 255 ); 00072 //assert( CB >= 0 && CB <= 255 ); 00073 //assert( CR >= 0 && CR <= 255 ); 00074 ybr[0] = Y /*+ 0.5*/; 00075 ybr[1] = CB /*+ 0.5*/; 00076 ybr[2] = CR /*+ 0.5*/; 00077 #endif 00078 } 00079 00080 template <typename T> 00081 void ImageChangePhotometricInterpretation::YBR2RGB(T rgb[3], const T ybr[3]) 00082 { 00083 00084 #if 1 00085 rgb[0] = 298.082 * ((int)ybr[0]-16) + 0. * ((int)ybr[1]-128) + 408.583 * ((int)ybr[2]-128) - 1. / 256; 00086 rgb[1] = 298.082 * ((int)ybr[0]-16) + -100.291 * ((int)ybr[1]-128) + -208.12 * ((int)ybr[2]-128) - 1. / 256; 00087 rgb[2] = 298.082 * ((int)ybr[0]-16) + 516.411 * ((int)ybr[1]-128) + 0. * ((int)ybr[2]-128) - 1. / 256; 00088 00089 #else 00090 const double Y = ybr[0]; 00091 const double Cb = ybr[1]; 00092 const double Cr = ybr[2]; 00093 //const double R = 1.0000e+00 * Y - 3.6820e-05 * CB + 1.4020e+00 * CR; 00094 //const double G = 1.0000e+00 * Y - 3.4411e-01 * CB - 7.1410e-01 * CR; 00095 //const double B = 1.0000e+00 * Y + 1.7720e+00 * CB - 1.3458e-04 * CR; 00096 const double r = Y + 1.402 * (Cr-128); 00097 const double g = Y - 0.344136 * (Cb-128) - 0.714136 * (Cr-128); 00098 const double b = Y + 1.772 * (Cb-128); 00099 double R = r < 0 ? 0 : r; 00100 R = R > 255 ? 255 : R; 00101 double G = g < 0 ? 0 : g; 00102 G = G > 255 ? 255 : G; 00103 double B = b < 0 ? 0 : b; 00104 B = B > 255 ? 255 : B; 00105 assert( R >= 0 && R <= 255 ); 00106 assert( G >= 0 && G <= 255 ); 00107 assert( B >= 0 && B <= 255 ); 00108 rgb[0] = ((R < 0 ? 0 : R) > 255 ? 255 : R); 00109 rgb[1] = G; 00110 rgb[2] = B; 00111 #endif 00112 00113 } 00114 00115 } // end namespace gdcm 00116 00117 #endif //GDCMIMAGECHANGEPHOTOMETRICINTERPRETATION_H