Biomedical Image Analysis Library
The Biomedical Image Analysis Library is a poweful tool for developers, physicians, researchers, engineers, and so on.
dicomdir.cpp
Go to the documentation of this file.
1 #include "dicomdir.h"
2 #ifdef LIBGDCM
3 #include <gdcmAttribute.h>
4 #include <gdcmReader.h>
5 #include <gdcmStreamImageReader.h>
6 #include <gdcmStringFilter.h>
7 #endif
8 
9 /* ImageInfo */
10 QDir ImageInfo::getDir( ) const {
11  return( dir );
12 }
13 
14 void ImageInfo::setDir( const QDir &value ) {
15  dir = value;
16 }
17 
19  QString imgpath = QDir::fromNativeSeparators( referencedFileID );
20  for( int pos = 0; pos < imgpath.size( ); ++pos ) {
21  if( ( ( imgpath[ pos ] == '\\' ) || ( imgpath[ pos ] == '/' ) ) && ( imgpath[ pos ] != DIR_SEPARATOR ) ) {
22  imgpath[ pos ] = DIR_SEPARATOR;
23  }
24  }
25  QStringList list = imgpath.split( DIR_SEPARATOR );
26  QFileInfo info;
27  for( QString str : list ) {
28  info.setFile( dir.absoluteFilePath( str ) );
29  for( int i = 0; i < 2 && !info.exists( ); ++i ) {
30  if( i == 0 ) {
31  str = str.toUpper( );
32  }
33  else {
34  str = str.toLower( );
35  }
36  info.setFile( dir.absoluteFilePath( str ) );
37  }
38  dir.cd( str );
39  }
40  return( info.absoluteFilePath( ) );
41 }
42 
44 }
45 
47  return( referencedFileID );
48 }
49 
50 void ImageInfo::setReferencedFileID( const QString &value ) {
51  referencedFileID = value;
52 }
53 
54 QString ImageInfo::getUid( ) const {
55  return( uid );
56 }
57 
58 void ImageInfo::setUid( const QString &value ) {
59  uid = value;
60 }
61 
62 QString Series::getUid( ) const {
63  return( uid );
64 }
65 
66 void Series::setUid( const QString &value ) {
67  uid = value;
68 }
69 
70 QString Series::getDescr( ) const {
71  return( descr );
72 }
73 
74 void Series::setDescr( const QString &value ) {
75  descr = value;
76 }
77 
78 QStringList Series::getImages( ) {
79  QStringList files;
80  for( auto itr = images.begin( ); itr != images.end( ); ++itr ) {
81  files.push_back( itr->getFileName( ) );
82  }
83  return( files );
84 }
85 
86 void Series::addImage( const ImageInfo &imageInfo ) {
87  images.push_back( imageInfo );
88 }
89 
90 QString Series::getModality( ) const {
91  return( modality );
92 }
93 
94 void Series::setModality( const QString &value ) {
95  modality = value;
96 }
97 /* Study */
98 QString Study::getUid( ) const {
99  return( uid );
100 }
101 
102 void Study::setUid( const QString &value ) {
103  uid = value;
104 }
105 
106 QString Study::getDescr( ) const {
107  return( descr );
108 }
109 
110 void Study::setDescr( const QString &value ) {
111  descr = value;
112 }
113 
114 void Study::addSeries( const Series &s ) {
115  series.push_back( s );
116 }
117 
118 QString Study::getDate( ) const {
119  return( date );
120 }
121 
122 void Study::setDate( const QString &value ) {
123  date = value;
124 }
125 
126 QStringList Study::getImages( ) {
127  QStringList files;
128  for( auto itr = series.begin( ); itr != series.end( ); ++itr ) {
129  QStringList sFiles = itr->getImages( );
130  files << sFiles;
131  }
132  return( files );
133 }
134 /* Patient */
135 QString Patient::getName( ) const {
136  return( name );
137 }
138 
139 void Patient::setName( const QString &value ) {
140  name = value;
141 }
142 
143 QString Patient::getId( ) const {
144  return( id );
145 }
146 
147 void Patient::setId( const QString &value ) {
148  id = value;
149 }
150 
151 void Patient::addStudy( const Study &s ) {
152  study.push_back( s );
153 }
154 
155 QStringList Patient::getImages( ) {
156  QStringList files;
157  for( auto itr = study.begin( ); itr != study.end( ); ++itr ) {
158  QStringList sFiles = itr->getImages( );
159  files << sFiles;
160  }
161  return( files );
162 }
163 /* Dicomdir */
164 #ifdef LIBGDCM
165 QString str( const gdcm::Value &val ) {
166  std::stringstream strm;
167  strm << val;
168  return( QString::fromStdString( strm.str( ) ) );
169 }
170 
171 QString str( gdcm::SmartPointer< gdcm::Value > val ) {
172  std::stringstream strm;
173  strm << *val;
174  return( QString::fromStdString( strm.str( ) ) );
175 }
176 #endif
178 }
179 
180 bool DicomDir::open( const QString &filename ) {
181  this->filename = filename;
182  QFileInfo fileInfo( filename );
183  title = fileInfo.baseName( );
184  /* TODO Verify if file exists ( The function Bial::File::Exists do not exists yes ) */
185  QDir dir = fileInfo.dir( );
186 #ifndef LIBGDCM
187  BIAL_WARNING( "GDCM library not found and/or reprocessor variable LIBGDCM not set!" );
188  return( false );
189 #else
190  gdcm::Reader reader;
191  COMMENT( "Opening dicomdir file: " << filename.toStdString( ), 1 );
192  reader.SetFileName( filename.toUtf8( ) );
193  if( !reader.Read( ) ) {
194  BIAL_WARNING( "Cannot read file!" );
195  return( false );
196  }
197  const gdcm::File &file = reader.GetFile( );
198  const gdcm::FileMetaInformation header = file.GetHeader( );
199  const gdcm::DataSet &ds = file.GetDataSet( );
200  gdcm::MediaStorage ms;
201  ms.SetFromFile( file );
202  if( gdcm::MediaStorage::GetMSType( ms.GetString( ) ) != gdcm::MediaStorage::MediaStorageDirectoryStorage ) {
203  BIAL_WARNING( "This file is not a DICOMDIR ( Media storage type: " << ms << " )" )
204  return( false );
205  }
206  if( header.FindDataElement( gdcm::Tag( 0x0002, 0x0002 ) ) ) {
207  if( str( header.GetDataElement( gdcm::Tag( 0x0002, 0x0002 ) ).GetValue( ) ) != "1.2.840.10008.1.3.10" ) {
208  BIAL_WARNING( "This file is not a DICOMDIR" );
209  }
210  }
211  else {
212  BIAL_WARNING( "Media Storage SOP Class not present" );
213  return( false );
214  }
215  for( auto it = ds.GetDES( ).begin( ); it != ds.GetDES( ).end( ); ++it ) {
216  const gdcm::DataElement &de = *it;
217  if( de.GetTag( ) == gdcm::Tag( 0x004, 0x1220 ) ) { /* verifies if it is a valid ... */
218  gdcm::SmartPointer< gdcm::SequenceOfItems > sq = de.GetValueAsSQ( );
219  COMMENT( "Iterating through DicomDir itens", 1 );
220  for( size_t itemNr = 1; itemNr < sq->GetNumberOfItems( ); ++itemNr ) {
221  gdcm::Item &item = sq->GetItem( itemNr );
222  if( item.FindDataElement( gdcm::Tag( 0x0004, 0x1430 ) ) ) {
223  gdcm::SmartPointer< gdcm::Value > val = item.GetDataElement( gdcm::Tag( 0x0004, 0x1430 ) ).GetValue( );
224  while( ( str( val ) == "PATIENT " ) || ( str( val ) == "PATIENT" ) ) {
225  COMMENT( "Creating Patient", 2 );
226  Patient p;
227  if( item.FindDataElement( gdcm::Tag( 0x0010, 0x0010 ) ) ) {
228  val = item.GetDataElement( gdcm::Tag( 0x0010, 0x0010 ) ).GetValue( );
229  p.setName( str( val ) );
230  }
231  if( item.FindDataElement( gdcm::Tag( 0x0010, 0x0020 ) ) ) {
232  val = item.GetDataElement( gdcm::Tag( 0x0010, 0x0020 ) ).GetValue( );
233  p.setId( str( val ) );
234  }
235  /* Next */
236  item = sq->GetItem( ++itemNr );
237  if( item.FindDataElement( gdcm::Tag( 0x0004, 0x1430 ) ) ) {
238  val = item.GetDataElement( gdcm::Tag( 0x0004, 0x1430 ) ).GetValue( );
239  }
240  while( ( str( val ) == "STUDY " ) || ( str( val ) == "STUDY" ) ) {
241  COMMENT( "Creating Study", 2 )
242  Study study;
243  /* std::cout << str( val ) << std::endl; */
244  if( item.FindDataElement( gdcm::Tag( 0x0020, 0x000d ) ) ) {
245  val = item.GetDataElement( gdcm::Tag( 0x0020, 0x000d ) ).GetValue( );
246  /* std::cout << " UID = " << *val << std::endl; */
247  study.setUid( str( val ) );
248  }
249  if( item.FindDataElement( gdcm::Tag( 0x0008, 0x0020 ) ) ) {
250  val = item.GetDataElement( gdcm::Tag( 0x0008, 0x0020 ) ).GetValue( );
251  /* std::cout << " Date = " << *val << std::endl; */
252  study.setDate( str( val ) );
253  }
254  if( item.FindDataElement( gdcm::Tag( 0x0008, 0x1030 ) ) ) {
255  val = item.GetDataElement( gdcm::Tag( 0x0008, 0x1030 ) ).GetValue( );
256  /* std::cout << " Description = " << *val << std::endl; */
257  study.setDescr( str( val ) );
258  }
259  item = sq->GetItem( ++itemNr );
260  if( item.FindDataElement( gdcm::Tag( 0x0004, 0x1430 ) ) ) {
261  val = item.GetDataElement( gdcm::Tag( 0x0004, 0x1430 ) ).GetValue( );
262  }
263  while( str( val ) == "SERIES " || str( val ) == "SERIES" ) {
264  COMMENT( "Creating Series", 2 );
265  Series series;
266  /* std::cout << " " << str( val ) << std::endl; */
267  if( item.FindDataElement( gdcm::Tag( 0x0020, 0x000e ) ) ) {
268  val = item.GetDataElement( gdcm::Tag( 0x0020, 0x000e ) ).GetValue( );
269  /* std::cout << " UID = " << *val << std::endl; */
270  series.setUid( str( val ) );
271  }
272  if( item.FindDataElement( gdcm::Tag( 0x0008, 0x0060 ) ) ) {
273  val = item.GetDataElement( gdcm::Tag( 0x0008, 0x0060 ) ).GetValue( );
274  /* std::cout << " Modality = " << *val << std::endl; */
275  series.setModality( str( val ) );
276  }
277  if( item.FindDataElement( gdcm::Tag( 0x0008, 0x103e ) ) ) {
278  val = item.GetDataElement( gdcm::Tag( 0x0008, 0x103e ) ).GetValue( );
279  /* std::cout << " Description = " << *val << std::endl; */
280  series.setDescr( str( val ) );
281  }
282  item = sq->GetItem( ++itemNr );
283  if( item.FindDataElement( gdcm::Tag( 0x0004, 0x1430 ) ) ) {
284  val = item.GetDataElement( gdcm::Tag( 0x0004, 0x1430 ) ).GetValue( );
285  }
286  while( str( val ) == "IMAGE " || str( val ) == "IMAGE" ) {
287  COMMENT( "Creating ImageInfo", 2 );
288  ImageInfo imageInfo;
289  imageInfo.setDir( dir );
290  /*
291  * std::cout << " " << item << std::endl;
292  * std::cout << " " << *val << std::endl;
293  */
294  if( item.FindDataElement( gdcm::Tag( 0x0004, 0x1511 ) ) ) {
295  val = item.GetDataElement( gdcm::Tag( 0x0004, 0x1511 ) ).GetValue( );
296  /* std::cout << " UID = " << *val << std::endl; */
297  imageInfo.setUid( str( val ) );
298  }
299  if( item.FindDataElement( gdcm::Tag( 0x0004, 0x1500 ) ) ) {
300  val = item.GetDataElement( gdcm::Tag( 0x0004, 0x1500 ) ).GetValue( );
301  /* std::cout << " ReferencedFileID = " << *val << std::endl; */
302  imageInfo.setReferencedFileID( str( val ) );
303  }
304  if( itemNr < sq->GetNumberOfItems( ) ) {
305  item = sq->GetItem( ++itemNr );
306  if( item.FindDataElement( gdcm::Tag( 0x0004, 0x1430 ) ) ) {
307  val = item.GetDataElement( gdcm::Tag( 0x0004, 0x1430 ) ).GetValue( );
308  }
309  }
310  else {
311  break;
312  }
313  series.addImage( imageInfo );
314  }
315  study.addSeries( series );
316  }
317  p.addStudy( study );
318  }
319  patient.push_back( p );
320  }
321  }
322  }
323  }
324  }
325 #endif
326  return( true );
327 }
328 
329 QStringList DicomDir::getImages( ) {
330  QStringList files;
331  for( auto itr = patient.begin( ); itr != patient.end( ); ++itr ) {
332  QStringList pFiles = itr->getImages( );
333  files << pFiles;
334  }
335  return( files );
336 }
bool open(const QString &filename)
Definition: dicomdir.cpp:180
QString getFileName()
Definition: dicomdir.cpp:18
QDir getDir() const
Definition: dicomdir.cpp:10
QStringList getImages()
Definition: dicomdir.cpp:126
void setDir(const QDir &value)
Definition: dicomdir.cpp:14
QString getDate() const
Definition: dicomdir.cpp:118
#define DIR_SEPARATOR
Definition: Common.hpp:340
void addSeries(const Series &s)
Definition: dicomdir.cpp:114
QString getUid() const
Definition: dicomdir.cpp:54
QStringList getImages()
Definition: dicomdir.cpp:155
void setDate(const QString &value)
Definition: dicomdir.cpp:122
void setUid(const QString &value)
Definition: dicomdir.cpp:102
QString getId() const
Definition: dicomdir.cpp:143
QString getDescr() const
Definition: dicomdir.cpp:106
QString getDescr() const
Definition: dicomdir.cpp:70
void setId(const QString &value)
Definition: dicomdir.cpp:147
void setDescr(const QString &value)
Definition: dicomdir.cpp:110
D Value(D num, size_t bit)
Returns bit value.
void setDescr(const QString &value)
Definition: dicomdir.cpp:74
QStringList getImages()
Definition: dicomdir.cpp:78
void addStudy(const Study &s)
Definition: dicomdir.cpp:151
void setUid(const QString &value)
Definition: dicomdir.cpp:66
void addImage(const ImageInfo &imageInfo)
Definition: dicomdir.cpp:86
QString getUid() const
Definition: dicomdir.cpp:98
void setUid(const QString &value)
Definition: dicomdir.cpp:58
Definition: dicomdir.h:56
QString getReferencedFileID() const
Definition: dicomdir.cpp:46
void setReferencedFileID(const QString &value)
Definition: dicomdir.cpp:50
QStringList getImages()
Definition: dicomdir.cpp:329
void setName(const QString &value)
Definition: dicomdir.cpp:139
QString getUid() const
Definition: dicomdir.cpp:62
QString getName() const
Definition: dicomdir.cpp:135
QString getModality() const
Definition: dicomdir.cpp:90
Definition: gzappend.c:170
#define BIAL_WARNING(exp)
Use BIAL_WARNING to print a message to the output stream warn the user that something bad may have ha...
Definition: Common.hpp:158
void setModality(const QString &value)
Definition: dicomdir.cpp:94