]> Creatis software - gdcm.git/blob - Example/exExtractDicomTags.cxx
Fix mistypings
[gdcm.git] / Example / exExtractDicomTags.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exExtractDicomTags.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/04/03 17:00:24 $
7   Version:   $Revision: 1.6 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmDocument.h"
21 #include "gdcmSeqEntry.h"
22 #include "gdcmSQItem.h"
23 #include "gdcmDebug.h"
24 #include "gdcmUtil.h"
25 #include "gdcmOrientation.h"
26
27 #include "gdcmArgMgr.h"
28 #include <iostream>
29
30 //#include <fstream>
31
32 #ifndef _WIN32
33 #include <unistd.h> //for access, unlink
34 #else
35 #include <io.h> //for _access
36 #endif
37
38
39 int main(int argc, char *argv[])
40 {
41
42    START_USAGE(usage)
43    " \n exExtractDicomTags :\n",
44    "  ",
45    "         ",
46    "",
47    " usage: exExtractDicomTags filein=inputDicomFileName ", 
48    "                      [debug]  ", 
49    "        debug    : user wants to run the program in 'debug mode'        ",
50    FINISH_USAGE
51
52
53    // ----- Initialize Arguments Manager ------
54    
55    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
56
57    if (am->ArgMgrDefined("usage")) 
58    {
59       am->ArgMgrUsage(usage); // Display 'usage'
60       delete am;
61       return 0;
62    }
63    char *fileName = am->ArgMgrWantString("filein",usage);
64    if ( fileName == NULL )
65    {
66       delete am;
67       return 0;
68    }
69
70    if (am->ArgMgrDefined("debug"))
71       GDCM_NAME_SPACE::Debug::DebugOn();
72  
73    // if unused Params we give up
74    if ( am->ArgMgrPrintUnusedLabels() )
75    { 
76       am->ArgMgrUsage(usage);
77       delete am;
78       return 0;
79    }
80
81    delete am;  // we don't need Argument Manager any longer
82
83    // ----------- End Arguments Manager ---------
84
85
86    int loadMode = 0x0; // load everything
87    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
88    f->SetLoadMode( loadMode );
89    f->SetFileName( fileName );
90    bool res = f->Load();  
91
92    if ( !res )
93    {
94       f->Delete();
95       return 0;
96    }
97
98    // 
99    if (!f->IsReadable())
100    {
101       std::cout << "NOT a Dicom File : " << fileName <<std::endl;
102       f->Delete();
103       return 1;
104    }
105
106    std::string MediaStSOPinstUID;      
107    std::string TransferSyntax;
108    std::string StudyDate;
109    std::string StudyTime;
110    std::string Modality;
111    std::string PatientName;
112    std::string PatientID;
113    std::string PatientSex;
114
115    //std::string MediaStSOPinstUID;
116    std::string SOPInstanceUID; // == image ID
117    std::string StudyInstanceUID;
118    std::string SeriesInstanceUID;
119    std::string AcquistionDate;
120    std::string AcquisitionTime;
121    std::string AcquisitionDateTime;
122    std::string InstitutionName;
123    std::string InstitutionalDepartmentName; // always empty
124    std::string ProtocolName;
125
126
127 // ------------> Region (Organ) : *no* DICOM field is expected 
128 //                        to hold information
129
130 // Get informations on the file : 
131 //  Modality, Transfer Syntax, Study Date, Study Time
132 // Patient Name, Media Storage SOP Instance UID, etc
133
134    MediaStSOPinstUID   = f->GetEntryString(0x0002,0x0002);
135    TransferSyntax      = f->GetEntryString(0x0002,0x0010);
136    StudyDate           = f->GetEntryString(0x0008,0x0020);
137    StudyTime           = f->GetEntryString(0x0008,0x0030);
138    PatientName         = f->GetEntryString(0x0010,0x0010);
139    PatientID           = f->GetEntryString(0x0010,0x0020);  //patientid
140    PatientSex          = f->GetEntryString(0x0010,0x0040);  //sex
141    SOPInstanceUID      = f->GetEntryString(0x0008,0x0018);  //imageid = SOPinsUID
142    StudyInstanceUID    = f->GetEntryString(0x0020,0x000d);  //STUInsUID                                              [Study Instance UID] [1.2.840.113680.1.103.56887.1017329008.714317]
143    SeriesInstanceUID   = f->GetEntryString(0x0020,0x000e);  //SerInsUID
144    AcquistionDate      = f->GetEntryString(0x0008,0x0022);
145    AcquisitionTime     = f->GetEntryString(0x0008,0x0032);
146    AcquisitionDateTime = f->GetEntryString(0x0008,0x002a);
147
148
149    Modality            = f->GetEntryString(0x0008,0x0060);  //modality
150    InstitutionName     = f->GetEntryString(0x0008,0x0080);  //hospital
151    // always empty :-(
152    InstitutionalDepartmentName     
153                      = f->GetEntryString(0x0008,0x1040);  //departement
154
155    // --> I'll have to ask people working on PACS which one they use. JPRx
156    // Radiologist :
157    // 0008|0090  [Referring Physician's Name]
158    // 0008|1050  [Performing Physician's Name]
159    // 0008|1060  [Name of Physician(s) Reading Study] 
160    // 0008|1048  [Physician(s) of Record] 
161    // 0032|1032  [Requesting Physician]
162    
163    std::string ReferringPhysiciansName          = f->GetEntryString(0x0008,0x0090);   
164    std::string PerformingPhysiciansName         = f->GetEntryString(0x0008,0x1050);
165    std::string NameofPhysiciansReadingStudy     = f->GetEntryString(0x0008,0x1060);   
166    std::string PhysiciansofRecord               = f->GetEntryString(0x0008,0x1048);
167    std::string RequestingPhysician              = f->GetEntryString(0x0032,0x1032);   
168         
169
170    ProtocolName = f->GetEntryString(0x0018,0x1030); 
171   
172    // --> Big trouble with nz (number of planes) and nt (number of 'times')
173    // --> that belong to LibIDO, not to DICOM.
174    // --> DICOM has 'Number of Frames' (0028|0008), 
175    //     that's more or less number of 'times'
176    // Volumes are generaly stored in a 'Serie' 
177    //  (hope so ... a single Serie may be xti-slice xti-times)
178
179
180    std::string Rows;
181    std::string Columns;
182    std::string Planes;
183
184    std::string SamplesPerPixel;
185    std::string BitsAllocated;
186    std::string BitsStored;
187    std::string HighBit;
188    std::string PixelRepresentation;
189    std::string PixelType;
190    
191    SamplesPerPixel     = f->GetEntryString(0x0028,0x0002);  // 3 -> RGB
192    Rows                = f->GetEntryString(0x0028,0x0010);  //ny
193    Columns             = f->GetEntryString(0x0028,0x0011);  //nx
194    Planes              = f->GetEntryString(0x0028,0x0012);  //nz
195
196    BitsAllocated       = f->GetEntryString(0x0028,0x0100);
197    BitsStored          = f->GetEntryString(0x0028,0x0101);
198    HighBit             = f->GetEntryString(0x0028,0x0102);
199    PixelRepresentation = f->GetEntryString(0x0028,0x0103);
200
201    PixelType           = f->GetPixelType();                //type 
202 /*
203    int iRows            = (uint32_t) atoi( Rows.c_str() );    //ny
204    int iColumns         = (uint32_t) atoi( Columns.c_str() ); //nz
205    int iPlanes          = (uint32_t) atoi( Planes.c_str() );  //nz
206    int iSamplesPerPixel = (uint32_t) atoi( SamplesPerPixel.c_str() );
207    int iBitsAllocated   = (uint32_t) atoi( BitsAllocated.c_str() );
208 */
209
210
211
212    float sx =  f->GetXSpacing();
213    float sy =  f->GetYSpacing();
214    float sz =  f->GetZSpacing(); // meaningless for DICOM 
215                                  // (cannot be extracted from a single image)
216
217    std::cout << "Rows = ["            << Rows                    << "]" << std::endl;
218    std::cout << "Columns = ["         << Columns                 << "]" << std::endl;
219    std::cout << "Planes = ["          << Planes                  << "]" << std::endl;
220    std::cout << "SamplesPerPixel = [" << SamplesPerPixel         << "]" << std::endl;
221    std::cout << "BitsAllocated = ["   << BitsAllocated           << "]" << std::endl;
222    std::cout << "BitsStored = ["      << BitsStored              << "]" << std::endl;
223    std::cout << "HighBit = ["         << HighBit                 << "]" << std::endl;
224    std::cout << "PixelRepresentation = [" << PixelRepresentation << "]" << std::endl;
225    std::cout << "PixelType = ["       << PixelType               << "]" << std::endl;
226
227    std::cout << "TransferSyntax = [" << TransferSyntax << "]" << std::endl;
228    std::cout << "StudyDate = ["      << StudyDate      << "]" << std::endl;
229    std::cout << "StudyTime = ["      << StudyTime      << "]" << std::endl;
230    std::cout << "Modality = ["       << Modality       << "]" << std::endl;
231    std::cout << "PatientName = ["    << PatientName    << "]" << std::endl;
232    std::cout << "PatientID = ["      << PatientID      << "]" << std::endl;
233    std::cout << "PatientSex = ["     << PatientSex     << "]" << std::endl;
234    std::cout << std::endl;  
235    std::cout << "SOPInstanceUID = ["              << SOPInstanceUID << "]" 
236           << std::endl; 
237    std::cout << "StudyInstanceUID = ["            << StudyInstanceUID 
238           << "]" << std::endl;
239    std::cout << "SeriesInstanceUID = ["           << SeriesInstanceUID 
240           << "]" << std::endl;
241    std::cout << "AcquistionDate = ["              << AcquistionDate 
242           << "]" << std::endl;
243    std::cout << "AcquisitionTime = ["             << AcquisitionTime 
244           << "]" << std::endl;
245    std::cout << "AcquisitionDateTime = ["         << AcquisitionDateTime << "]" 
246              << std::endl;
247    
248    std::cout << std::endl;
249        
250    std::cout << "ReferringPhysiciansName = ["             << ReferringPhysiciansName 
251           << "]" << std::endl;
252    std::cout << "PerformingPhysiciansName = ["            << PerformingPhysiciansName 
253           << "]" << std::endl;  
254    std::cout << "NameofPhysiciansReadingStudy = ["        << NameofPhysiciansReadingStudy 
255           << "]" << std::endl;  
256    std::cout << "PhysiciansofRecord = ["                  << PhysiciansofRecord 
257           << "]" << std::endl;  
258    std::cout << "RequestingPhysician = ["                 << RequestingPhysician 
259           << "]" << std::endl; 
260    
261    std::cout << std::endl;
262    
263    std::cout << "InstitutionalDepartmentName = [" 
264                                         << InstitutionalDepartmentName << "]"
265                                << std::endl;
266        
267    std::cout << "InstitutionName = ["             << InstitutionName 
268           << "]" << std::endl;       
269        
270    std::cout << "ProtocolName = ["                << ProtocolName << "]" << std::endl;
271
272    std::cout << "GetXSpacing = ["            << sx << "]" << std::endl;
273    std::cout << "GetYSpacing = ["            << sy << "]" << std::endl;
274    std::cout << "GetZSpacing = ["            << sz << "]" << std::endl;
275
276
277       // Let's get and print some usefull fields about 'Orientation'
278       // ------------------------------------------------------------
279
280       std::string strPatientPosition = 
281                                       f->GetEntryString(0x0018,0x5100);
282       if ( strPatientPosition != GDCM_NAME_SPACE::GDCM_UNFOUND 
283         && strPatientPosition != "" )  
284             std::cout << "PatientPosition (0x0010,0x5100)= [" 
285                       << strPatientPosition << "]" << std::endl;
286  
287       std::string strViewPosition = 
288                                       f->GetEntryString(0x0018,0x5101);
289       if ( strViewPosition != GDCM_NAME_SPACE::GDCM_UNFOUND 
290         && strViewPosition != "" )  
291             std::cout << "View Position (0x0018,0x5101)= [" 
292                       << strViewPosition << "]" << std::endl;
293       
294      std::string strPatientOrientation = 
295                                       f->GetEntryString(0x0020,0x0020);
296       if ( strPatientOrientation != GDCM_NAME_SPACE::GDCM_UNFOUND
297         && strPatientOrientation != "")  
298          std::cout << "PatientOrientation (0x0020,0x0020)= [" 
299                    << strPatientOrientation << "]" << std::endl;
300
301       std::string strImageOrientationPatient = 
302                                       f->GetEntryString(0x0020,0x0037);  
303       if ( strImageOrientationPatient != GDCM_NAME_SPACE::GDCM_UNFOUND
304         && strImageOrientationPatient != "" )  
305          std::cout << "ImageOrientationPatient (0x0020,0x0037)= [" 
306                    << strImageOrientationPatient << "]" << std::endl;
307
308       std::string strImageOrientationRET = 
309                                       f->GetEntryString(0x0020,0x0035);
310       if ( strImageOrientationRET != GDCM_NAME_SPACE::GDCM_UNFOUND
311         && strImageOrientationRET != "" )  
312          std::cout << "ImageOrientationRET (0x0020,0x0035)= [" 
313                    << strImageOrientationRET << "]" << std::endl;
314
315       std::string strImagePositionPatient = 
316                                       f->GetEntryString(0x0020,0x0032);  
317       if ( strImagePositionPatient != GDCM_NAME_SPACE::GDCM_UNFOUND
318         && strImagePositionPatient != "" )  
319          std::cout << "ImagePositionPatient (0x0020,0x0032)= [" 
320                    << strImagePositionPatient << "]" << std::endl;
321
322       std::string strImagePositionPatientRET = 
323                                       f->GetEntryString(0x0020,0x0030);
324       if ( strImagePositionPatientRET != GDCM_NAME_SPACE::GDCM_UNFOUND
325         && strImagePositionPatientRET != "" )  
326          std::cout << "ImagePositionPatientRET (0x0020,0x0030)= [" 
327                    << strImagePositionPatientRET << "]" << std::endl;
328   
329      float iop[6];
330      /*bool riop = */f->GetImageOrientationPatient(iop);  
331      float ipp[3];
332      /*bool ripp = */f->GetImagePositionPatient(ipp);
333
334      std::cout << "Image Position (0x0020,0x0032|0x0030) : "
335                << ipp[0] << " , " << ipp[1] << " , "<< ipp[2]
336                << std::endl;
337      std::cout << "Image Orientation (0x0020,0x0037|0x0035) : "
338                << iop[0] << " , " << iop[1] << " , "<< iop[2] << " , "
339                << iop[3] << " , " << iop[4] << " , "<< iop[5]
340                << std::endl; 
341
342
343       // Let's compute 'user friendly' results about 'Orientation'
344       // ---------------------------------------------------------
345  
346       GDCM_NAME_SPACE::Orientation *o = GDCM_NAME_SPACE::Orientation::New();
347
348       if ( strImageOrientationPatient != GDCM_NAME_SPACE::GDCM_UNFOUND ||
349            strImageOrientationRET     != GDCM_NAME_SPACE::GDCM_UNFOUND )
350       {
351   
352          GDCM_NAME_SPACE::OrientationType orient = o->GetOrientationType( f );
353  
354          std::cout << "TypeOrientation = " << orient << " (-> " 
355                    << o->GetOrientationTypeString(orient) << " )" << std::endl;
356       }
357
358       std::string ori = o->GetOrientation ( f );
359       if (ori != "\\" )
360          std::cout << "Orientation [" << ori << "]" << std::endl;
361       o->Delete();
362    f->Delete();
363    return 0;
364 }