GDCM 2.2.1

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

gdcmImageChangePhotometricInterpretation.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 GDCMIMAGECHANGEPHOTOMETRICINTERPRETATION_H
15 #define GDCMIMAGECHANGEPHOTOMETRICINTERPRETATION_H
16
17 #include "gdcmImageToImageFilter.h"
18 #include "gdcmPhotometricInterpretation.h"
19
20 namespace gdcm
21 {
22
23 class DataElement;
28 class GDCM_EXPORT ImageChangePhotometricInterpretation : public ImageToImageFilter
29 {
30 public:
31 ImageChangePhotometricInterpretation():PI() {}
32 ~ImageChangePhotometricInterpretation() {}
33
35 void SetPhotometricInterpretation(PhotometricInterpretation const &pi) { PI = pi; }
36 const PhotometricInterpretation &GetPhotometricInterpretation() const { return PI; }
37
39 bool Change();
40
42 template <typename T>
43 static void RGB2YBR(T ybr[3], const T rgb[3]);
44 template <typename T>
45 static void YBR2RGB(T rgb[3], const T ybr[3]);
46
47 protected:
48 bool ChangeMonochrome();
49
50 private:
51 PhotometricInterpretation PI;
52 };
53
54
55 // http://en.wikipedia.org/wiki/YCbCr
56 template <typename T>
57 void ImageChangePhotometricInterpretation::RGB2YBR(T ybr[3], const T rgb[3])
58 {
59 #if 1
60 ybr[0] = 65.738 * rgb[0] + 129.057 * rgb[1] + 25.064 * rgb[2] + 16;
61 ybr[1] = -37.945 * rgb[0] + -74.494 * rgb[1] + 112.439 * rgb[2] + 128;
62 ybr[2] = 112.439 * rgb[0] + -94.154 * rgb[1] + -18.285 * rgb[2] + 128;
63 #else
64
65 const double R = rgb[0];
66 const double G = rgb[1];
67 const double B = rgb[2];
68 const double Y = .2990 * R + .5870 * G + .1140 * B;
69 const double CB = -.168736 * R - .331264 * G + .5000 * B + 128;
70 const double CR = .5000 * R - .418688 * G - .081312 * B + 128;
71 //assert( Y >= 0 && Y <= 255 );
72 //assert( CB >= 0 && CB <= 255 );
73 //assert( CR >= 0 && CR <= 255 );
74 ybr[0] = Y /*+ 0.5*/;
75 ybr[1] = CB /*+ 0.5*/;
76 ybr[2] = CR /*+ 0.5*/;
77 #endif
78 }
79
80 template <typename T>
81 void ImageChangePhotometricInterpretation::YBR2RGB(T rgb[3], const T ybr[3])
82 {
83
84 #if 1
85 rgb[0] = 298.082 * ((int)ybr[0]-16) + 0. * ((int)ybr[1]-128) + 408.583 * ((int)ybr[2]-128) - 1. / 256;
86 rgb[1] = 298.082 * ((int)ybr[0]-16) + -100.291 * ((int)ybr[1]-128) + -208.12 * ((int)ybr[2]-128) - 1. / 256;
87 rgb[2] = 298.082 * ((int)ybr[0]-16) + 516.411 * ((int)ybr[1]-128) + 0. * ((int)ybr[2]-128) - 1. / 256;
88
89 #else
90 const double Y = ybr[0];
91 const double Cb = ybr[1];
92 const double Cr = ybr[2];
93 //const double R = 1.0000e+00 * Y - 3.6820e-05 * CB + 1.4020e+00 * CR;
94 //const double G = 1.0000e+00 * Y - 3.4411e-01 * CB - 7.1410e-01 * CR;
95 //const double B = 1.0000e+00 * Y + 1.7720e+00 * CB - 1.3458e-04 * CR;
96 const double r = Y + 1.402 * (Cr-128);
97 const double g = Y - 0.344136 * (Cb-128) - 0.714136 * (Cr-128);
98 const double b = Y + 1.772 * (Cb-128);
99 double R = r < 0 ? 0 : r;
100 R = R > 255 ? 255 : R;
101 double G = g < 0 ? 0 : g;
102 G = G > 255 ? 255 : G;
103 double B = b < 0 ? 0 : b;
104 B = B > 255 ? 255 : B;
105 assert( R >= 0 && R <= 255 );
106 assert( G >= 0 && G <= 255 );
107 assert( B >= 0 && B <= 255 );
108 rgb[0] = ((R < 0 ? 0 : R) > 255 ? 255 : R);
109 rgb[1] = G;
110 rgb[2] = B;
111 #endif
112
113 }
114
115 } // end namespace gdcm
116
117 #endif //GDCMIMAGECHANGEPHOTOMETRICINTERPRETATION_H