GDCM 3.2.1

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#include <limits>
20
21 namespace gdcm
22{
23
24 class DataElement;
29 class GDCM_EXPORT ImageChangePhotometricInterpretation : public ImageToImageFilter
30{
31 public:
32 ImageChangePhotometricInterpretation():PI() {}
33 ~ImageChangePhotometricInterpretation() = default;
34
36 void SetPhotometricInterpretation(PhotometricInterpretation const &pi) { PI = pi; }
37 const PhotometricInterpretation &GetPhotometricInterpretation() const { return PI; }
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
49 protected:
50 bool ChangeMonochrome();
51 bool ChangeYBR2RGB();
52 bool ChangeRGB2YBR();
53
54 private:
55 PhotometricInterpretation PI;
56};
57
58 template <typename T>
59 static inline int Round(T x)
60{
61 return (int)(x+0.5);
62}
63
64 template <typename T>
65 static inline T Clamp(int v)
66{
67 gdcm_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
72 template <typename T>
73 void 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 gdcm_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
92 template <typename T>
93 void 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 gdcm_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
gdcm::DataElement
Class to represent a Data Element either Implicit or Explicit.
Definition gdcmDataElement.h:59
gdcm::ImageChangePhotometricInterpretation::RGB2YBR
static void RGB2YBR(T ybr[3], const T rgb[3], unsigned short storedbits=8)
Definition gdcmImageChangePhotometricInterpretation.h:73
gdcm::ImageChangePhotometricInterpretation::YBR2RGB
static void YBR2RGB(T rgb[3], const T ybr[3], unsigned short storedbits=8)
Definition gdcmImageChangePhotometricInterpretation.h:93
gdcm::ImageChangePhotometricInterpretation::GetPhotometricInterpretation
const PhotometricInterpretation & GetPhotometricInterpretation() const
Definition gdcmImageChangePhotometricInterpretation.h:37
gdcm::ImageChangePhotometricInterpretation::Change
bool Change()
Change.
gdcm::ImageChangePhotometricInterpretation::ChangeRGB2YBR
bool ChangeRGB2YBR()
gdcm::ImageChangePhotometricInterpretation::ChangeYBR2RGB
bool ChangeYBR2RGB()
gdcm::ImageChangePhotometricInterpretation::~ImageChangePhotometricInterpretation
~ImageChangePhotometricInterpretation()=default
gdcm::ImageChangePhotometricInterpretation::ChangeMonochrome
bool ChangeMonochrome()
gdcm::ImageChangePhotometricInterpretation::ImageChangePhotometricInterpretation
ImageChangePhotometricInterpretation()
Definition gdcmImageChangePhotometricInterpretation.h:32
gdcm::ImageChangePhotometricInterpretation::SetPhotometricInterpretation
void SetPhotometricInterpretation(PhotometricInterpretation const &pi)
Set/Get requested PhotometricInterpretation.
Definition gdcmImageChangePhotometricInterpretation.h:36
gdcm::ImageToImageFilter::ImageToImageFilter
ImageToImageFilter()
gdcm::PhotometricInterpretation
Class to represent an PhotometricInterpretation.
Definition gdcmPhotometricInterpretation.h:29
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcmImageToImageFilter.h
gdcmPhotometricInterpretation.h
GDCM_EXPORT
#define GDCM_EXPORT
Definition gdcmWin32.h:34
gdcm
Definition gdcmASN1.h:21
gdcm::Clamp
static T Clamp(int v)
Definition gdcmImageChangePhotometricInterpretation.h:65
gdcm::Round
static int Round(T x)
Definition gdcmImageChangePhotometricInterpretation.h:59