# gdcmImageChangePhotometricInterpretation.h

Go to the documentation of this file.

    00001 /*=========================================================================
    00002
    00003   Program: GDCM (Grassroots DICOM). A DICOM library
    00004   Module:  $URL$
    00005
    00006   Copyright (c) 2006-2010 Mathieu Malaterre
    00007   All rights reserved.
    00008   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
    00009
    00010      This software is distributed WITHOUT ANY WARRANTY; without even
    00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    00012      PURPOSE.  See the above copyright notice for more information.
    00013
    00014 =========================================================================*/
    00015 #ifndef GDCMIMAGECHANGEPHOTOMETRICINTERPRETATION_H
    00016 #define GDCMIMAGECHANGEPHOTOMETRICINTERPRETATION_H
    00017
    00018 #include "gdcmImageToImageFilter.h"
    00019 #include "gdcmPhotometricInterpretation.h"
    00020
    00021 namespace gdcm
    00022 {
    00023
    00024 class DataElement;
    00029 class GDCM_EXPORT ImageChangePhotometricInterpretation : public ImageToImageFilter
    00030 {
    00031 public:
    00032   ImageChangePhotometricInterpretation():PI() {}
    00033   ~ImageChangePhotometricInterpretation() {}
    00034
    00036   void SetPhotometricInterpretation(PhotometricInterpretation const &pi) { PI = pi; }
    00037   const PhotometricInterpretation &GetPhotometricInterpretation() const { return PI; }
    00038
    00040   bool Change();
    00041
    00043   template <typename T>
    00044   static void RGB2YBR(T ybr[3], const T rgb[3]);
    00045   template <typename T>
    00046   static void YBR2RGB(T rgb[3], const T ybr[3]);
    00047
    00048 protected:
    00049   bool ChangeMonochrome();
    00050
    00051 private:
    00052   PhotometricInterpretation PI;
    00053 };
    00054
    00055
    00056 // http://en.wikipedia.org/wiki/YCbCr
    00057 template <typename T>
    00058 void ImageChangePhotometricInterpretation::RGB2YBR(T ybr[3], const T rgb[3])
    00059 {
    00060 #if 1
    00061   ybr[0] =   65.738 * rgb[0] +    129.057 * rgb[1] +    25.064 * rgb[2] + 16;
    00062   ybr[1] =  -37.945 * rgb[0] +    -74.494 * rgb[1] +   112.439 * rgb[2] + 128;
    00063   ybr[2] =  112.439 * rgb[0] +    -94.154 * rgb[1] +   -18.285 * rgb[2] + 128;
    00064 #else
    00065
    00066   const double R = rgb[0];
    00067   const double G = rgb[1];
    00068   const double B = rgb[2];
    00069   const double Y  =  .2990 * R + .5870 * G + .1140 * B;
    00070   const double CB = -.168736 * R - .331264 * G + .5000 * B + 128;
    00071   const double CR =  .5000 * R - .418688 * G - .081312 * B + 128;
    00072   //assert( Y >= 0  && Y <= 255 );
    00073   //assert( CB >= 0 && CB <= 255 );
    00074   //assert( CR >= 0 && CR <= 255 );
    00075   ybr[0] = Y  /*+ 0.5*/;
    00076   ybr[1] = CB /*+ 0.5*/;
    00077   ybr[2] = CR /*+ 0.5*/;
    00078 #endif
    00079 }
    00080
    00081 template <typename T>
    00082 void ImageChangePhotometricInterpretation::YBR2RGB(T rgb[3], const T ybr[3])
    00083 {
    00084
    00085 #if 1
    00086  rgb[0] = 298.082 * ((int)ybr[0]-16) +     0.    * ((int)ybr[1]-128) +   408.583 * ((int)ybr[2]-128) - 1. / 256;
    00087  rgb[1] = 298.082 * ((int)ybr[0]-16) +  -100.291 * ((int)ybr[1]-128) +  -208.12  * ((int)ybr[2]-128) - 1. / 256;
    00088  rgb[2] = 298.082 * ((int)ybr[0]-16) +   516.411 * ((int)ybr[1]-128) +     0.    * ((int)ybr[2]-128) - 1. / 256;
    00089
    00090 #else
    00091   const double Y  = ybr[0];
    00092   const double Cb = ybr[1];
    00093   const double Cr = ybr[2];
    00094   //const double R =  1.0000e+00 * Y - 3.6820e-05 * CB + 1.4020e+00 * CR;
    00095   //const double G =  1.0000e+00 * Y - 3.4411e-01 * CB - 7.1410e-01 * CR;
    00096   //const double B =  1.0000e+00 * Y + 1.7720e+00 * CB - 1.3458e-04 * CR;
    00097   const double r = Y                    + 1.402   * (Cr-128);
    00098   const double g = Y - 0.344136 * (Cb-128) - 0.714136 * (Cr-128);
    00099   const double b = Y + 1.772   * (Cb-128);
    00100   double R = r < 0 ? 0 : r;
    00101   R = R > 255 ? 255 : R;
    00102   double G = g < 0 ? 0 : g;
    00103   G = G > 255 ? 255 : G;
    00104   double B = b < 0 ? 0 : b;
    00105   B = B > 255 ? 255 : B;
    00106   assert( R >= 0 && R <= 255 );
    00107   assert( G >= 0 && G <= 255 );
    00108   assert( B >= 0 && B <= 255 );
    00109   rgb[0] = ((R < 0 ? 0 : R) > 255 ? 255 : R);
    00110   rgb[1] = G;
    00111   rgb[2] = B;
    00112 #endif
    00113
    00114 }
    00115
    00116 } // end namespace gdcm
    00117
    00118 #endif //GDCMIMAGECHANGEPHOTOMETRICINTERPRETATION_H