]> Creatis software - gdcm.git/blob - Example/exExtractDicomTags.cxx
Rewrite an *anonymized* Dicomdir !
[gdcm.git] / Example / exExtractDicomTags.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exExtractDicomTags.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:05 $
7   Version:   $Revision: 1.5 $
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
26 #include "gdcmArgMgr.h"
27 #include <iostream>
28
29 //#include <fstream>
30
31 #ifndef _WIN32
32 #include <unistd.h> //for access, unlink
33 #else
34 #include <io.h> //for _access
35 #endif
36
37
38 int main(int argc, char *argv[])
39 {
40
41    START_USAGE(usage)
42    " \n exExtractDicomTags :\n",
43    "  ",
44    "         ",
45    "",
46    " usage: exExtractDicomTags filein=inputDicomFileName ", 
47    "                      [debug]  ", 
48    "        debug    : user wants to run the program in 'debug mode'        ",
49    FINISH_USAGE
50
51
52    // ----- Initialize Arguments Manager ------
53    
54    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
55   
56    if (am->ArgMgrDefined("usage")) 
57    {
58       am->ArgMgrUsage(usage); // Display 'usage'
59       delete am;
60       return 0;
61    }
62    char *fileName = am->ArgMgrWantString("filein",usage);
63    if ( fileName == NULL )
64    {
65       delete am;
66       return 0;
67    }
68
69    if (am->ArgMgrDefined("debug"))
70       GDCM_NAME_SPACE::Debug::DebugOn();
71  
72    // if unused Params we give up
73    if ( am->ArgMgrPrintUnusedLabels() )
74    { 
75       am->ArgMgrUsage(usage);
76       delete am;
77       return 0;
78    }
79
80    delete am;  // we don't need Argument Manager any longer
81
82    // ----------- End Arguments Manager ---------
83
84
85    int loadMode = 0x0; // load everything
86    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
87    f->SetLoadMode( loadMode );
88    f->SetFileName( fileName );
89    bool res = f->Load();  
90
91    if ( !res )
92    {
93       f->Delete();
94       return 0;
95    }
96
97    // 
98    if (!f->IsReadable())
99    {
100       std::cout << "NOT a Dicom File : " << fileName <<std::endl;
101       f->Delete();
102       return 1;
103    }
104
105    std::string MediaStSOPinstUID;      
106    std::string TransferSyntax;
107    std::string StudyDate;
108    std::string StudyTime;
109    std::string Modality;
110    std::string PatientName;
111    std::string PatientID;
112    std::string PatientSex;
113
114    //std::string MediaStSOPinstUID;
115    std::string SOPInstanceUID; // == image ID
116    std::string StudyInstanceUID;
117    std::string SeriesInstanceUID;
118    std::string AcquistionDate;
119    std::string AcquisitionTime;
120    std::string AcquisitionDateTime;
121    std::string InstitutionName;
122    std::string InstitutionalDepartmentName; // always empty
123    std::string ProtocolName;
124
125
126 // ------------> Region (Organ) : *no* DICOM field is expected 
127 //                        to hold information
128
129 // Get informations on the file : 
130 //  Modality, Transfer Syntax, Study Date, Study Time
131 // Patient Name, Media Storage SOP Instance UID, etc
132
133    MediaStSOPinstUID   = f->GetEntryString(0x0002,0x0002);
134    TransferSyntax      = f->GetEntryString(0x0002,0x0010);
135    StudyDate           = f->GetEntryString(0x0008,0x0020);
136    StudyTime           = f->GetEntryString(0x0008,0x0030);
137    PatientName         = f->GetEntryString(0x0010,0x0010);
138    PatientID           = f->GetEntryString(0x0010,0x0020);  //patientid
139    PatientSex          = f->GetEntryString(0x0010,0x0040);  //sex
140    SOPInstanceUID      = f->GetEntryString(0x0008,0x0018);  //imageid = SOPinsUID
141    StudyInstanceUID    = f->GetEntryString(0x0020,0x000d);  //STUInsUID                                              [Study Instance UID] [1.2.840.113680.1.103.56887.1017329008.714317]
142    SeriesInstanceUID   = f->GetEntryString(0x0020,0x000e);  //SerInsUID
143    AcquistionDate      = f->GetEntryString(0x0008,0x0022);
144    AcquisitionTime     = f->GetEntryString(0x0008,0x0032);
145    AcquisitionDateTime = f->GetEntryString(0x0008,0x002a);
146
147
148    Modality            = f->GetEntryString(0x0008,0x0060);  //modality
149    InstitutionName     = f->GetEntryString(0x0008,0x0080);  //hospital
150    // always empty :-(
151    InstitutionalDepartmentName     
152                      = f->GetEntryString(0x0008,0x1040);  //departement
153
154    // Radiologist :
155    // 0008|0090  [Referring Physician's Name]
156    // 0008|1050  [Performing Physician's Name]
157    // 0008|1060  [Name of Physician(s) Reading Study] 
158    // 0008|1048  [Physician(s) of Record] 
159    // 0032|1032  [Requesting Physician] 
160
161    // --> I'll have to ask people working on PACS which one they use. JPRx
162
163    ProtocolName = f->GetEntryString(0x0018,0x1030); 
164   
165    // --> Big trouble with nz (number of planes) and nt (number of 'times')
166    // --> that belong to LibIDO, not to DICOM.
167    // --> DICOM has 'Number of Frames' (0028|0008), 
168    //     that's more or less number of 'times'
169    // Volumes are generaly stored in a 'Serie' 
170    //  (hope so ... a single Serie may be xti-slice xti-times)
171
172
173    std::string Rows;
174    std::string Columns;
175    std::string Planes;
176
177    std::string SamplesPerPixel;
178    std::string BitsAllocated;
179    std::string BitsStored;
180    std::string HighBit;
181    std::string PixelRepresentation;
182    std::string PixelType;
183    
184    SamplesPerPixel     = f->GetEntryString(0x0028,0x0002);  // 3 -> RGB
185    Rows                = f->GetEntryString(0x0028,0x0010);  //ny
186    Columns             = f->GetEntryString(0x0028,0x0011);  //nx
187    Planes              = f->GetEntryString(0x0028,0x0012);  //nz
188
189    BitsAllocated       = f->GetEntryString(0x0028,0x0100);
190    BitsStored          = f->GetEntryString(0x0028,0x0101);
191    HighBit             = f->GetEntryString(0x0028,0x0102);
192    PixelRepresentation = f->GetEntryString(0x0028,0x0103);
193
194    PixelType           = f->GetPixelType();                //type 
195 /*
196    int iRows            = (uint32_t) atoi( Rows.c_str() );    //ny
197    int iColumns         = (uint32_t) atoi( Columns.c_str() ); //nz
198    int iPlanes          = (uint32_t) atoi( Planes.c_str() );  //nz
199    int iSamplesPerPixel = (uint32_t) atoi( SamplesPerPixel.c_str() );
200    int iBitsAllocated   = (uint32_t) atoi( BitsAllocated.c_str() );
201 */
202
203   
204
205    float sx =  f->GetXSpacing();
206    float sy =  f->GetYSpacing();
207    float sz =  f->GetZSpacing(); // meaningless for DICOM 
208                                  // (cannot be extracted from a single image)
209
210    std::cout << "Rows = ["            << Rows << "]" << std::endl;
211    std::cout << "Columns = ["         << Columns << "]" << std::endl;
212    std::cout << "Planes = ["          << Planes << "]" << std::endl;
213    std::cout << "SamplesPerPixel = [" << SamplesPerPixel << "]" << std::endl;
214    std::cout << "BitsAllocated = ["   << BitsAllocated << "]" << std::endl;
215    std::cout << "BitsStored = ["      << BitsStored << "]" << std::endl;
216    std::cout << "HighBit = ["         << HighBit << "]" << std::endl;
217    std::cout << "PixelRepresentation = [" << PixelRepresentation << "]" << std::endl;
218    std::cout << "PixelType = ["       << PixelType << "]" << std::endl;
219
220    std::cout << "TransferSyntax = [" << TransferSyntax << "]" << std::endl;
221    std::cout << "StudyDate = ["      << StudyDate << "]" << std::endl;
222    std::cout << "StudyTime = ["      << StudyTime << "]" << std::endl;
223    std::cout << "Modality = ["       << Modality << "]" << std::endl;
224    std::cout << "PatientName = ["    << PatientName << "]" << std::endl;
225    std::cout << "PatientID = ["      << PatientID << "]" << std::endl;
226    std::cout << "PatientSex = ["     << PatientSex << "]" << std::endl;
227    std::cout << "SOPInstanceUID = ["              << SOPInstanceUID << "]" 
228           << std::endl; 
229    std::cout << "StudyInstanceUID = ["            << StudyInstanceUID 
230           << "]" << std::endl;
231    std::cout << "SeriesInstanceUID = ["           << SeriesInstanceUID 
232           << "]" << std::endl;
233    std::cout << "AcquistionDate = ["              << AcquistionDate 
234           << "]" << std::endl;
235    std::cout << "AcquisitionTime = ["             << AcquisitionTime 
236           << "]" << std::endl;
237    std::cout << "AcquisitionDateTime = ["         << AcquisitionDateTime << "]" 
238              << std::endl;
239    std::cout << "InstitutionName = ["             << InstitutionName 
240           << "]" << std::endl;
241    std::cout << "InstitutionalDepartmentName = [" 
242                                         << InstitutionalDepartmentName << "]"
243                                << std::endl;
244    std::cout << "ProtocolName = ["                << ProtocolName << "]" << std::endl;
245
246    std::cout << "GetXSpacing = ["            << sx << "]" << std::endl;
247    std::cout << "GetYSpacing = ["            << sy << "]" << std::endl;
248    std::cout << "GetZSpacing = ["            << sz << "]" << std::endl;
249
250    f->Delete();
251    return 0;
252 }