File: igstkDICOMImageReaderTest.cxx

package info (click to toggle)
igstk 4.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 38,980 kB
  • sloc: cpp: 86,267; xml: 96; makefile: 75; python: 38
file content (228 lines) | stat: -rwxr-xr-x 7,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*=========================================================================

  Program:   Image Guided Surgery Software Toolkit
  Module:    $RCSfile: igstkDICOMImageReaderTest.cxx,v $
  Language:  C++
  Date:      $Date: 2010-12-14 16:31:33 $
  Version:   $Revision: 1.12 $

  Copyright (c) ISC  Insight Software Consortium.  All rights reserved.
  See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#include "igstkDICOMImageReader.h"
#include "igstkImageSpatialObject.h"
#include "igstkCTImageReader.h"

#include "igstkLogger.h"
#include "itkCommand.h"
#include "itkStdStreamLogOutput.h"

namespace igstkDICOMImageReaderTestNamespace
{
  
class DICOMImageModalityInformationCallback: public itk::Command
{
public:
  typedef DICOMImageModalityInformationCallback   Self;
  typedef itk::SmartPointer<Self>                 Pointer;
  typedef itk::Command                            Superclass;
  itkNewMacro(Self);

  typedef igstk::ImageSpatialObject<short,3> ImageSpatialObjectType;
  
 
  typedef igstk::DICOMModalityEvent DICOMModalityEventType;
  
  void Execute(const itk::Object * itkNotUsed(caller), const itk::EventObject & itkNotUsed(event))
    {
    }

  void Execute(itk::Object * itkNotUsed(caller), const itk::EventObject & event)
    {
    if( DICOMModalityEventType().CheckEvent( &event ) )
      {
      const DICOMModalityEventType * modalityEvent = 
                   dynamic_cast< const DICOMModalityEventType *>( &event );
      std::cout << "Modality= " << modalityEvent->Get() << std::endl;
      }   
    }

protected:
  DICOMImageModalityInformationCallback()   { };

private:
};

class DICOMImagePatientNameInformationCallback: public itk::Command
{
public:
  typedef DICOMImagePatientNameInformationCallback  Self;
  typedef itk::SmartPointer<Self>                   Pointer;
  typedef itk::Command                              Superclass;
  itkNewMacro(Self);

  typedef igstk::ImageSpatialObject<short,3> ImageSpatialObjectType;
 
  typedef igstk::DICOMPatientNameEvent DICOMPatientNameEventType;
  
  void Execute(const itk::Object * itkNotUsed(caller), const itk::EventObject & itkNotUsed(event))
    {
    }
  void Execute(itk::Object * itkNotUsed(caller), const itk::EventObject & event)
    {
    const DICOMPatientNameEventType * patientNameEvent = 
          dynamic_cast<const DICOMPatientNameEventType*> (&event);
    std::cerr << "PatientName= " << patientNameEvent->Get() << std::endl;  
    }

protected:
  DICOMImagePatientNameInformationCallback()   { };

private:
};


class myDicomTestReader : 
  public igstk::DICOMImageReader< igstk::ImageSpatialObject< short, 3 > >
{
public:

  typedef igstk::DICOMImageReader< 
                  igstk::ImageSpatialObject< short, 3 > > DICOMReaderSuperclass;

  /** Macro with standard traits declarations. */
  igstkStandardClassTraitsMacro( myDicomTestReader, DICOMReaderSuperclass )

  void TestMe()
    {
    m_CheckTheNullImage = true;
    const ImageType * imageThatShallNotBeNamed = GetITKImage();
    if( imageThatShallNotBeNamed != NULL )
      {
      std::cout << "Test of private abstract method Passed" << std::endl;
      }
    m_CheckTheNullImage = false;
    }

protected:
   myDicomTestReader():m_StateMachine(this),m_CheckTheNullImage(false) {}
   ~myDicomTestReader() {}
private:
  typedef Superclass::ImageType ImageType;
  virtual const ImageType * GetITKImage() const 
    { 
    if(m_CheckTheNullImage)
      {
      return NULL;
      }
    else
      { 
      return this->Superclass::GetITKImage();
      }
    }

  myDicomTestReader(const Self&);         //purposely not implemented
  void operator=(const Self&);        //purposely not implemented

  bool m_CheckTheNullImage;
};


} // end of igstkDICOMImageReaderTestNamespace namespace


int igstkDICOMImageReaderTest( int argc, char* argv[] )
{

  igstk::RealTimeClock::Initialize();

  if(argc < 2)
    {
    std::cerr<<"Usage: "<<argv[0]<<"  DICOMSeries "<<std::endl;
    return EXIT_FAILURE;
    }
  
  typedef igstk::Object::LoggerType     LoggerType;
  typedef itk::StdStreamLogOutput       LogOutputType;
  
  // logger object created for logging mouse activities
  LoggerType::Pointer   logger = LoggerType::New();
  LogOutputType::Pointer logOutput = LogOutputType::New();
  logOutput->SetStream( std::cout );
  logger->AddLogOutput( logOutput );
  logger->SetPriorityLevel( itk::Logger::DEBUG );


  //typedef igstk::CTImageReader         ReaderType;
  typedef igstkDICOMImageReaderTestNamespace::myDicomTestReader   ReaderType;


  ReaderType::Pointer   reader = ReaderType::New();
  reader->SetLogger( logger );

  if( reader->FileSuccessfullyRead() )
    {
    std::cerr << "FileSuccessfullyRead() is malfunctioning. It returned true ";
    std::cerr << "when the image has not even been read" << std::endl;
    return EXIT_FAILURE;
    }
      
  /* Read in a DICOM series */
  std::cout << "Reading the DICOM series : " << argv[1] <<std::endl;
  ReaderType::DirectoryNameType directoryName = argv[1];
  reader->RequestSetDirectory( directoryName );
  reader->RequestReadImage();
  
  /* Add observer to listen to modality info */
  typedef 
    igstkDICOMImageReaderTestNamespace::DICOMImageModalityInformationCallback  
                                                          ModalityCallbackType;

  ModalityCallbackType::Pointer dimcb = ModalityCallbackType::New();
  reader->AddObserver( igstk::DICOMModalityEvent(), dimcb );
  reader->RequestGetModalityInformation(); 
 
  /* Add observer to listen to patient name  info */
  typedef 
  igstkDICOMImageReaderTestNamespace::DICOMImagePatientNameInformationCallback
                                                           PatientCallbackType;

  PatientCallbackType::Pointer dipncb = PatientCallbackType::New();
  reader->AddObserver( igstk::DICOMPatientNameEvent(), dipncb );
  reader->RequestGetPatientNameInformation(); 

  reader->Print( std::cout );

  /* Exercising the Unsafe GetPatientName() method */
  if( reader->FileSuccessfullyRead() )
    {
    ReaderType::DICOMInformationType  patientName = reader->GetPatientName();
    ReaderType::DICOMInformationType  patientID   = reader->GetPatientID();
    ReaderType::DICOMInformationType  modality    = reader->GetModality();
    
    std::cout << "Patient Name = " << patientName << std::endl;
    std::cout << "Patient ID = "   << patientID << std::endl;
    std::cout << "Modality = "     << modality << std::endl;
    }
  else
    {
    std::cout << "The file was not successfully read" << std::endl;
    return EXIT_FAILURE;
    }

  /* Testing the return of null pointer in the GetITKImage method */
  reader->TestMe();

  
  return EXIT_SUCCESS;
}