QtDcmConvert.cpp
Go to the documentation of this file.
1 /*
2  QtDcm is a C++ Qt based library for communication and conversion of Dicom images.
3  Copyright (C) 2011 Alexandre Abadie <Alexandre.Abadie@univ-rennes1.fr>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #include "QtDcmConvert.h"
20 
21 #include <QtDcmManager.h>
22 #include <QtDcmPreferences.h>
23 
24 #include <itkImage.h>
25 #include <itkGDCMImageIO.h>
26 #include <itkGDCMSeriesFileNames.h>
27 #include <itkImageSeriesReader.h>
28 #include <itkMetaDataDictionary.h>
29 #include <itkObjectFactoryBase.h>
30 #include <itkMetaDataObject.h>
31 #include <itkImageFileWriter.h>
32 
34 {
35 
36 public:
37  QString tempDirectory;
38  QString serieUID;
39  QString inputDirectory;
40  QString outputDirectory;
41  QString outputFilename;
42 };
43 
44 QtDcmConvert::QtDcmConvert ( QObject * parent )
45  : QObject(parent),
46  d ( new QtDcmConvert::Private )
47 {
48  d->inputDirectory = "";
49  d->outputFilename = "";
50 }
51 
53 {
54  delete d;
55  d = NULL;
56 }
57 
59 {
60  if (QtDcmPreferences::instance()->useDcm2nii()) {
61  const QString program = QtDcmPreferences::instance()->dcm2niiPath();
62  QStringList arguments;
63  arguments << "-x" << "N";
64  arguments << "-r" << "N";
65  arguments << "-g" << "N";
66  arguments << "-o" << d->outputDirectory << d->inputDirectory;
67 
68  QProcess process(this);
69  process.setStandardOutputFile(d->tempDirectory + QDir::separator() + "logs" + QDir::separator() + d->serieUID + ".txt");
70  process.start(program, arguments);
71  process.waitForFinished();
72  }
73  else {
74  typedef signed short PixelType;
75  const unsigned int Dimension = 3;
76  typedef itk::Image< PixelType, Dimension > ImageType;
77  typedef itk::ImageSeriesReader< ImageType > ReaderType;
78  typedef itk::ImageFileWriter<ImageType> WriterType;
79  typedef itk::GDCMImageIO ImageIOType;
80  typedef itk::GDCMSeriesFileNames NamesGeneratorType;
81  typedef std::vector< std::string > FileNamesContainer;
82  typedef std::vector< std::string > SeriesIdContainer;
83 
84 // ImageType::Pointer image = 0;
85 
86  ReaderType::Pointer reader = ReaderType::New();
87  ImageIOType::Pointer dicomIO = ImageIOType::New();
88 
89  NamesGeneratorType::Pointer inputNames = NamesGeneratorType::New();
90  inputNames->SetUseSeriesDetails ( true );
91  inputNames->AddSeriesRestriction ( "0008|0021" );
92  inputNames->AddSeriesRestriction ( "0020,0037" );
93  inputNames->LoadSequencesOn();
94  inputNames->LoadPrivateTagsOn();
95  inputNames->SetInputDirectory ( d->inputDirectory.toStdString() );
96  try {
97  const SeriesIdContainer & seriesUID = inputNames->GetSeriesUIDs();
98  if (seriesUID.empty()) { // Prevent crash
99  qCritical() << "Series uid list is empty";
100  return;
101  }
102  std::string seriesIdentifier = seriesUID.begin()->c_str();
103  FileNamesContainer filenames = inputNames->GetFileNames ( seriesIdentifier );
104 
105  dicomIO->SetFileName ( filenames.begin()->c_str() );
106  try {
107  dicomIO->ReadImageInformation();
108  }
109  catch ( itk::ExceptionObject &e ) {
110  qCritical() << e.GetDescription();
111  return;
112  }
113 
114  reader->UseStreamingOn();
115  reader->SetFileNames ( filenames );
116  reader->SetImageIO ( dicomIO );
117 
118  try {
119  reader->Update();
120  }
121  catch ( itk::ExceptionObject &excp ) {
122  qCritical() << excp.GetDescription();
123  return;
124  }
125 
126 // IteratorType itOut;
127 //
128 // image = reader->GetOutput();
129 //
130 // RegionType region;
131 // region.SetSize ( 0, image->GetLargestPossibleRegion().GetSize() [0] );
132 // region.SetSize ( 1, image->GetLargestPossibleRegion().GetSize() [1] );
133 // region.SetSize ( 2, image->GetLargestPossibleRegion().GetSize() [2] );
134 // image->SetRegions ( region );
135 // image->Allocate();
136 // SpacingType spacing;
137 // spacing[0] = image->GetSpacing() [0];
138 // spacing[1] = image->GetSpacing() [1];
139 // spacing[2] = image->GetSpacing() [2];
140 // spacing[3] = 1;
141 // image->SetSpacing ( spacing );
142 // PointType origin;
143 // origin[0] = image->GetOrigin() [0];
144 // origin[1] = image->GetOrigin() [1];
145 // origin[2] = image->GetOrigin() [2];
146 // origin[3] = 0;
147 // image->SetOrigin ( origin );
148 // DirectionType direction;
149 // for ( unsigned int i=0; i<4; i++ )
150 // for ( unsigned int j=0; j<4; j++ )
151 // {
152 // if ( ( i < 3 ) && ( j < 3 ) )
153 // direction[i][j] = image->GetDirection() [i][j];
154 // else
155 // direction[i][j] = ( i == j ) ? 1 : 0;
156 // }
157 // image->SetDirection ( direction );
158 // itOut = IteratorType ( image, region );
159 //
160 // image->SetMetaDataDictionary ( dicomIO->GetMetaDataDictionary() );
161 //
162 //
163 // itk::ImageRegionIterator<ImageType> itIn ( image, image->GetLargestPossibleRegion() );
164 // while ( !itIn.IsAtEnd() )
165 // {
166 // itOut.Set ( itIn.Get() );
167 // ++itIn;
168 // ++itOut;
169 // }
170 
171 
172  WriterType::Pointer writer = WriterType::New();
173 
174  QString completeFilename = d->outputDirectory + QDir::separator() + d->outputFilename;
175 
176  writer->SetFileName ( completeFilename.toStdString() );
177  writer->SetInput ( reader->GetOutput() );
178 // writer->SetInput ( image );
179 
180  try {
181  writer->Update();
182  }
183  catch ( itk::ExceptionObject &ex ) {
184  qCritical() << ex.GetDescription();
185  return;
186  }
187  }
188  catch ( itk::ExceptionObject &ex ) {
189  qCritical() << ex.GetDescription();
190  return;
191  }
192  }
193 }
194 
195 void QtDcmConvert::setInputDirectory ( const QString & dir )
196 {
197  d->inputDirectory = dir;
198 }
199 
200 void QtDcmConvert::setOutputDirectory ( const QString & dir )
201 {
202  d->outputDirectory = dir;
203 }
204 
205 void QtDcmConvert::setOutputFilename ( const QString & fname )
206 {
207  d->outputFilename = fname;
208 }
209 
210 void QtDcmConvert::setSerieUID(const QString & uid)
211 {
212  d->serieUID = uid;
213 }
214 
215 void QtDcmConvert::setTempDirectory(const QString & dir)
216 {
217  d->tempDirectory = dir;
218 }
219 
static QtDcmPreferences * instance()
void setSerieUID(const QString &uid)
QtDcmConvert(QObject *parent=0)
void setInputDirectory(const QString &dir)
QString dcm2niiPath() const
void setOutputFilename(const QString &fname)
void setOutputDirectory(const QString &dir)
void setTempDirectory(const QString &dir)