GDCM 3.0.24
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
19#include <limits>
20
21namespace gdcm
22{
23
24class DataElement;
30{
31public:
34
38
40 bool Change();
41
44 template <typename T>
45 static void RGB2YBR(T ybr[3], const T rgb[3], unsigned short storedbits = 8);
46 template <typename T>
47 static void YBR2RGB(T rgb[3], const T ybr[3], unsigned short storedbits = 8);
48
49protected:
53
54private:
56};
57
58template <typename T>
59static inline int Round(T x)
60{
61 return (int)(x+0.5);
62}
63
64template <typename T>
65static inline T Clamp(int v)
66{
67 assert( std::numeric_limits<T>::min() == 0 );
68 return v < 0 ? 0 : (v > std::numeric_limits<T>::max() ? std::numeric_limits<T>::max() : v);
69}
70
71
72template <typename T>
73void ImageChangePhotometricInterpretation::RGB2YBR(T ybr[3], const T rgb[3], unsigned short storedbits)
74{
75 // Implementation details, since the equations from:
76 // http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.7.6.3.html#sect_C.7.6.3.1.2
77 // are rounded to the 4th decimal precision, prefer the exact equation from the original document at:
78 // CCIR Recommendation 601-2, also found in T.871 (Section ยง7, page 4)
79 const double R = rgb[0];
80 const double G = rgb[1];
81 const double B = rgb[2];
82 assert( storedbits <= sizeof(T) * 8 );
83 const int halffullscale = 1 << (storedbits - 1);
84 const int Y = Round( 0.299 * R + 0.587 * G + 0.114 * B );
85 const int CB = Round((-0.299 * R - 0.587 * G + 0.886 * B)/1.772 + halffullscale);
86 const int CR = Round(( 0.701 * R - 0.587 * G - 0.114 * B)/1.402 + halffullscale);
87 ybr[0] = Clamp<T>(Y );
88 ybr[1] = Clamp<T>(CB);
89 ybr[2] = Clamp<T>(CR);
90}
91
92template <typename T>
93void ImageChangePhotometricInterpretation::YBR2RGB(T rgb[3], const T ybr[3], unsigned short storedbits)
94{
95 const double Y = ybr[0];
96 const double Cb = ybr[1];
97 const double Cr = ybr[2];
98 assert( storedbits <= sizeof(T) * 8 );
99 const int halffullscale = 1 << (storedbits - 1);
100 const int R = Round(Y + 1.402 * (Cr-halffullscale) );
101 const int G = Round(Y -( 0.114 * 1.772 * (Cb-halffullscale) + 0.299 * 1.402 * (Cr-halffullscale))/0.587);
102 const int B = Round(Y + 1.772 * (Cb-halffullscale) );
103 rgb[0] = Clamp<T>(R);
104 rgb[1] = Clamp<T>(G);
105 rgb[2] = Clamp<T>(B);
106}
107
108} // end namespace gdcm
109
110#endif //GDCMIMAGECHANGEPHOTOMETRICINTERPRETATION_H
ImageChangePhotometricInterpretation class.
Definition gdcmImageChangePhotometricInterpretation.h:30
static void RGB2YBR(T ybr[3], const T rgb[3], unsigned short storedbits=8)
Definition gdcmImageChangePhotometricInterpretation.h:73
static void YBR2RGB(T rgb[3], const T ybr[3], unsigned short storedbits=8)
Definition gdcmImageChangePhotometricInterpretation.h:93
const PhotometricInterpretation & GetPhotometricInterpretation() const
Definition gdcmImageChangePhotometricInterpretation.h:37
ImageChangePhotometricInterpretation()
Definition gdcmImageChangePhotometricInterpretation.h:32
void SetPhotometricInterpretation(PhotometricInterpretation const &pi)
Set/Get requested PhotometricInterpretation.
Definition gdcmImageChangePhotometricInterpretation.h:36
ImageToImageFilter class.
Definition gdcmImageToImageFilter.h:28
Class to represent an PhotometricInterpretation.
Definition gdcmPhotometricInterpretation.h:29
#define GDCM_EXPORT
Definition gdcmWin32.h:34
Definition gdcmASN1.h:21
static T Clamp(int v)
Definition gdcmImageChangePhotometricInterpretation.h:65
static int Round(T x)
Definition gdcmImageChangePhotometricInterpretation.h:59