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